forked from kiwi-coder/Rich-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerTest.java
More file actions
545 lines (443 loc) · 16 KB
/
PlayerTest.java
File metadata and controls
545 lines (443 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class PlayerTest {
private final Site DUMMY_SITE = null;
private static final int DUMMY_MONEY = 0;
private static final String DUMMY_NAME = "";
private Player player;
private Map map;
private final int MAP_SIZE = TestHelper.simpleMap().size();
private final int FIRST_SITE_INDEX = 0;
private final int LAST_SITE_INDEX = MAP_SIZE - 1;
@Before
public void setUp() {
map = TestHelper.simpleMap();
player = new Player("ATuBo", DUMMY_SITE, DUMMY_MONEY);
}
@Test
public void should_return_3_after_player_move_3_steps_from_0() {
player.setSite(map.getSite(0));
player.forward(3);
assertThat(player.getSite(), is(map.getSite(3)));
}
@Test
public void should_player_go_back_to_starting_site_from_last_site() {
player.setSite(map.getSite(LAST_SITE_INDEX));
player.forward(1);
assertThat(player.getSite(), is(map.getSite(FIRST_SITE_INDEX)));
}
@Test
public void should_return_A_for_player_name_is_ATuBo() {
assertThat(player.display(), is("A"));
}
@Test
public void should_player_buy_a_land_when_it_has_no_owner_and_he_has_enough_money() {
// Given
Property property = new Land();
Player player = new Player("Atubo", property, 5000);
property.setPlayer(player);
property.setPrice(200);
// When
player.buyProperty();
// Then
assertThat(player.getMoney(), is(4800));
assertThat(player.getSite().getType(), is("0"));
assertThat(((Property) player.getSite()).getOwner().getName(), is("Atubo"));
}
@Test
public void should_player_not_buy_a_land_when_he_has_no_enough_money() {
// Given
Property property = new Land();
Player player = new Player("Atubo", property, 180);
property.setPlayer(player);
property.setPrice(200);
// When
player.buyProperty();
// Then
assertThat(player.getMoney(), is(180));
assertThat(player.getSite().getType(), is("0"));
assertTrue(((Property) player.getSite()).getOwner() == null);
}
@Test
public void should_player_upgrade_his_land_to_cabin_when_he_has_enough_money() {
// Given
Property property = new Land();
Player player = new Player("Atubo", property, 5000);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(4800));
assertThat(player.getSite().getType(), is("1"));
}
@Test
public void should_not_player_upgrade_his_land_to_cabin_when_he_has_not_enough_money() {
// Given
Property property = new Land();
Player player = new Player("Atubo", property, 180);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(180));
assertThat(player.getSite().getType(), is("0"));
}
@Test
public void should_player_upgrade_its_cabin_to_house_when_he_has_enough_money() {
// Given
Property property = new Cabin();
Player player = new Player("Atubo", property, 5000);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(4800));
assertThat(player.getSite().getType(), is("2"));
}
@Test
public void should_not_player_upgrade_his_cabin_to_house_when_has_not_enough_money() {
// Given
Property property = new Cabin();
Player player = new Player("Atubo", property, 180);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(180));
assertThat(player.getSite().getType(), is("1"));
}
@Test
public void should_player_upgrade_its_house_to_skyscraper_when_he_has_enough_money() {
// Given
Property property = new House();
Player player = new Player("Atubo", property, 5000);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(4800));
assertThat(player.getSite().getType(), is("3"));
}
@Test
public void should_not_player_upgrade_his_house_to_skyscraper_when_has_not_enough_money() {
// Given
Property property = new House();
Player player = new Player("Atubo", property, 180);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(180));
assertThat(player.getSite().getType(), is("2"));
}
@Test
public void should_not_player_upgrade_its_skyscraper_when_he_has_enough_money() {
// Given
Property property = new Skyscraper();
Player player = new Player("Atubo", property, 5000);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(5000));
assertThat(player.getSite().getType(), is("3"));
}
@Test
public void should_not_player_upgrade_its_skyscraper_when_he_has_not_enough_money() {
// Given
Property property = new Skyscraper();
Player player = new Player("Atubo", property, 180);
property.setPlayer(player);
property.setPrice(200);
property.setOwner(player);
property.setMap(map);
// When
player.upgradeProperty();
// Then
assertThat(player.getMoney(), is(180));
assertThat(player.getSite().getType(), is("3"));
}
@Test
public void should_player_pay_when_he_is_on_other_one_s_land_and_has_enough_money() {
// Given
Property landofQianfuren = new Land();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 5000);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(200);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.getMoney(), is(4900));
assertThat(qianfuren.getMoney(), is(5100));
}
@Test
public void should_player_broke_when_he_is_on_other_one_s_land_and_has_not_enough_money() {
// Given
Property landofQianfuren = new Land();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 50);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(200);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.isBroke(), is(true));
assertThat(qianfuren.getMoney(), is(5050));
}
@Test
public void should_player_pay_when_he_is_on_other_one_s_cabin_and_has_enough_money() {
// Given
Property landofQianfuren = new Cabin();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 5000);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.getMoney(), is(4700));
assertThat(qianfuren.getMoney(), is(5300));
}
@Test
public void should_player_broke_when_he_is_on_other_one_s_cabin_and_has_not_enough_money() {
// Given
Property landofQianfuren = new Cabin();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 50);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.isBroke(), is(true));
assertThat(qianfuren.getMoney(), is(5050));
}
@Test
public void should_player_pay_when_he_is_on_other_one_s_house_and_has_enough_money() {
// Given
Property landofQianfuren = new House();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 5000);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.getMoney(), is(4400));
assertThat(qianfuren.getMoney(), is(5600));
}
@Test
public void should_player_broke_when_he_is_on_other_one_s_house_and_has_not_enough_money() {
// Given
Property landofQianfuren = new House();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 50);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.isBroke(), is(true));
assertThat(qianfuren.getMoney(), is(5050));
}
@Test
public void should_player_pay_when_he_is_on_other_one_s_skyscraper_and_has_enough_money() {
// Given
Property landofQianfuren = new Skyscraper();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 5000);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.getMoney(), is(3800));
assertThat(qianfuren.getMoney(), is(6200));
}
@Test
public void should_player_broke_when_he_is_on_other_one_s_skyscraper_and_has_not_enough_money() {
// Given
Property landofQianfuren = new Skyscraper();
Property randomProperty = new Land();
Player atubo = new Player("Atubo", landofQianfuren, 50);
Player qianfuren = new Player("Qianfuren", randomProperty, 5000);
landofQianfuren.setPlayer(atubo);
landofQianfuren.setPrice(300);
landofQianfuren.setOwner(qianfuren);
// When
atubo.payTollFee();
// Then
assertThat(atubo.isBroke(), is(true));
assertThat(qianfuren.getMoney(), is(5050));
}
@Test
public void test_player_selling_his_land(){
// Given
player.setMoney(5000);
Property property = new Land();
property.setPrice(300);
property.setOwner(player);
property.setMap(map);
// When
property = player.sellProperty(property);
// Then
assertTrue(property.getOwner() == null);
assertTrue(property instanceof Land);
assertThat(player.getMoney(), is(5600));
}
@Test
public void test_player_selling_his_cabin(){
// Given
player.setMoney(5000);
Property property = new Cabin();
property.setPrice(300);
property.setOwner(player);
property.setMap(map);
// When
property = player.sellProperty(property);
// Then
assertTrue(property.getOwner() == null);
assertTrue(property instanceof Land);
assertThat(player.getMoney(), is(6200));
}
@Test
public void test_player_selling_his_house(){
// Given
player.setMoney(5000);
Property property = new House();
property.setPrice(300);
property.setOwner(player);
property.setMap(map);
// When
property = player.sellProperty(property);
// Then
assertTrue(property.getOwner() == null);
assertTrue(property instanceof Land);
assertThat(player.getMoney(), is(6800));
}
@Test
public void test_player_selling_his_skyscraper(){
// Given
player.setMoney(5000);
Property property = new Skyscraper();
property.setPrice(300);
property.setOwner(player);
property.setMap(map);
// When
property = player.sellProperty(property);
// Then
assertTrue(property.getOwner() == null);
assertTrue(property instanceof Land);
assertThat(player.getMoney(), is(7400));
}
@Test
public void should_success_buy_player_when_player_has_enough_points() {
player.setPoints(500);
player.buyTool(new BlockTool());
assertThat(player.getToolsNumber(), is(1));
assertThat(player.getPoints(), is(450));
}
@Test(expected = PointsException.class)
public void should_throw_exception_when_player_not_has_enough_points() {
player.setPoints(0);
player.buyTool(new RobotTool());
}
@Test(expected = ToolException.class)
public void should_throw_exception_when_player_already_has_10_tools_and_cannot_buy_another() {
for (int i = 0; i < 10; i++) player.addTool(new BombTool());
player.setPoints(500);
player.buyTool(new BombTool());
}
@Test
public void should_success_sell_tool_when_player_has_enough_tool() {
player.addTool(new BombTool());
player.sellTool(new BombTool());
assertThat(player.getPoints(), is(50));
assertThat(player.getToolsNumber(), is(0));
}
@Test(expected = ToolException.class)
public void should_failed_sell_tool_when_player_do_not_have_that_kind_of_tool() {
player.sellTool(new BombTool());
}
@Test
public void should_return_3_when_count_bomb_tool() {
for (int i = 0; i < 3; i++) player.addTool(new BombTool());
player.addTool(new BlockTool());
player.addTool(new RobotTool());
assertThat(player.countTool(new BombTool()), is(3));
}
@Test
public void should_player_earn_2000_money_when_he_chose_money_at_gift_house(){
// Given
player.setMoney(5000);
// When
player.chooseMoneyAtGiftHouse();
// Then
assertThat(player.getMoney(), is(7000));
}
@Test
public void should_player_earn_200_point_when_he_chose_point_at_gift_house(){
// Given
player.setPoints(200);
// When
player.choosePointAtGiftHouse();
// Then
assertThat(player.getPoints(), is(400));
}
@Test
public void should_player_get_a_god_of_luck_carried_on_when_he_chose_it_at_gift_house(){
// Given
player.setGodOfLuck(null);
// When
player.chooseGodOfLuckAtGiftHouse();
// Then
assertTrue( player.hasGodOfLuck());
}
@Test
public void should_not_player_pay_toll_fee_when_he_has_a_god_of_luck(){
//TODO: 我感觉这个应该是更上层的逻辑, 应该写在 Game 里面.
}
@Test
public void should_god_of_luck_leave_the_player_after_five_rounds(){
// TODO: 我感觉这个应该是更上层的逻辑, 应该写在 Game 里面.
}
}