-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMonopoly.java
More file actions
937 lines (811 loc) · 25.5 KB
/
Monopoly.java
File metadata and controls
937 lines (811 loc) · 25.5 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/**
* ***************************************************************************
* <p>
* Author: Francis Ricci
* File: Monopoly.java
* <p>
* Source code file dependencies:
* Board.java
* Card.java
* Cards.java
* RandomDeck.java
* ProbDice.java
* Inactive.java
* Jail.java
* HumanPlayer.java
* Prob.java
* Property.java
* Railroad.java
* Shuffle.java
* Square.java
* Taxes.java
* Utility.java
* <p>
* Execution:
* monopoly.Monopoly
* <p>
* Purpose:
* Run a text-based Monopoly game emulator.
* <p>
* ***************************************************************************
*/
package monopoly;
import monopoly.Jail.JailType;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
class Monopoly {
private final boolean deterministic;
private final Dice dice; //two six-sided dice
private final Deck chance;
private final Deck community;
private State state;
private boolean chanceBoost = false;
private ValueEstimator valueEstimator;
private boolean lost = false;
private Monopoly() {
state = new State();
state.players = new LinkedList<>();
state.current = null;
state.state = DecisionState.NONE;
Input input = new Input();
System.out.println("Would you like to provide your own dice and card input?");
deterministic = input.inputBool();
if (deterministic) {
dice = new InputDice(input);
chance = new InputDeck();
community = new InputDeck();
} else {
dice = new ProbDice(); //two dice, six sided
chance = new RandomDeck();
community = new RandomDeck();
}
state.board = new Board(chance, community, deterministic); //create new board
initialize(input);
}
public static void main(String[] args) {
Monopoly monopoly = new Monopoly();
monopoly.run();
}
private void run() {
while (state.players.size() > 1) {
try {
state.current = state.players.remove();
turn();
if (!lost)
state.players.add(state.current);
lost = false;
} catch (NoSuchElementException e) {
System.out.println("Early Termination initiated.");
return;
} finally {
printState();
}
}
Player winner = state.players.remove();
System.out.println("----------------------------------------");
System.out.print("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
System.out.println();
System.out.println();
System.out.println();
System.out.println("THE WINNER IS " + winner.name() + "!!!");
System.out.println();
System.out.println();
System.out.println();
System.out.println("////////////////////////////////////////");
System.out.println("----------------------------------------");
}
private void initialize(Input input) {
System.out.println("How many total players?");
int N = input.inputInt();
while (N < 2 || N > 8) {
System.out.println("Must have between 2 and 8 players. Please try again.");
N = input.inputInt();
}
System.out.println("How many human players?");
int H = input.inputInt();
while (H < 1 || H > N) {
if (H < 1)
System.out.println("Must have at least one human player. Please try again.");
if (H > N)
System.out.println("Cannot have more human players than total players. Please try again.");
H = input.inputInt();
}
int[] order = new int[N];
for (int i = 0; i < H; i++) {
System.out.println("Player " + (i + 1) + " name?");
state.players.add(new HumanPlayer(input.inputString()));
}
for (int i = H; i < N; i++)
state.players.add(new CPUPlayer(i - H));
valueEstimator = new ValueEstimator(state.board, state.players, new ProbDice(), (Cards) state.board.square(7));
if (deterministic)
return;
boolean tie = true;
boolean[] ties = new boolean[N];
for (int i = 0; i < N; i++)
ties[i] = true;
int first = -1;
while (tie) {
for (int i = 0; i < N; i++) {
if (ties[i])
order[i] = dice.roll().val;
}
int maxRoll = 0;
for (int i = 0; i < N; i++) {
if (ties[i]) {
if (order[i] > maxRoll) {
maxRoll = order[i];
first = i;
}
}
}
tie = false;
for (int i = 0; i < N; i++)
ties[i] = false;
for (int i = 0; i < N; i++) {
if (order[i] == maxRoll && i != first) {
ties[i] = true;
tie = true;
}
}
}
for (int i = 0; i < first; i++)
state.players.add(state.players.remove());
printState();
}
private void turn() {
System.out.println("It's " + state.current.name() + "'s turn");
int double_count = 0;
while (true) {
if (state.current.inJail()) {
System.out.println("Would you like to get out of jail using cash or card?");
state.state = DecisionState.BUY_JAIL;
if (state.current.inputBool(state)) {
System.out.println("Select cash or card.");
state.state = DecisionState.CASH_CARD;
int choice = state.current.inputDecision(state, new String[]{"cash", "card"});
if (choice == 0) {
state.current.excMoney(-50);
state.current.leaveJail();
} else if (state.current.numJailFree() > 0) {
if (state.current.useJailFree())
chance.returnOutOfJail();
else
community.returnOutOfJail();
state.current.leaveJail();
} else
System.out.println("You don't have any cards.");
}
}
Dice.Roll roll = dice.roll();
if (roll.is_double)
double_count++;
if (state.current.inJail()) {
if (roll.is_double) {
state.current.leaveJail();
roll.is_double = false; //we don't re-roll if the double was used to escape jail
} else {
System.out.println("You did not roll a double.");
if (!state.current.stayJail())
leaveJail(state.current);
else
break;
}
}
if (double_count == 3) {
toJail(state.current);
break;
}
System.out.print("You rolled a " + roll.val);
if (roll.is_double)
System.out.print(" (double)");
Square[] square = state.board.getBoard();
System.out.println(" and landed on " + square[(state.current.position() + roll.val) % 40].name());
state.current.move(roll.val);
handleSquare(state.current, square[state.current.position()], roll.val);
if (!roll.is_double || state.current.inJail())
break;
}
boolean additional = true;
while (additional && !lost) {
System.out.println("Would you like to take any additional actions on this turn?");
System.out.println("Please select choice");
System.out.println("1) Buy/sell houses");
System.out.println("2) Mortgage/unmortgage properties");
System.out.println("3) Trade with another player");
System.out.println("4) Nothing");
state.state = DecisionState.ADDITIONAL;
int decision = state.current.inputInt(state);
switch (decision) {
case 1:
handleHouses(state.current);
break;
case 2:
handleMortgages(state.current);
break;
case 3:
handleTrade(state.current);
break;
case 4:
additional = false;
break;
default:
System.out.println("Please enter a valid decision.");
}
}
System.out.println();
}
private void handleSquare(Player player, Square sq, int roll) {
boolean owned = sq.isOwned();
boolean ownable = sq.isOwnable();
if (!owned && ownable)
unowned(player, sq);
else if (ownable && !sq.isMortgaged())
owned(player, sq, roll);
else if (sq instanceof Taxes)
payTax(player, (Taxes) sq, sq);
else if (sq instanceof Cards)
drawCard(player, (Cards) sq);
else if (sq instanceof Jail)
jailInteraction(player, (Jail) sq);
}
private void buyHouses(Player player) {
System.out.println("Expected Values:");
for (Square sq : player.properties()) {
Property prop;
if (sq instanceof Property)
prop = (Property) sq;
else
continue;
double val = valueEstimator.expectedValue(sq.position(), prop.rentDiff());
System.out.println(prop.name() + ": " + val);
}
do {
System.out.println("On which property would you like to purchase a house?");
Property prop = propertySelect(player);
if (prop.numHouses() == 5 || !prop.monopoly()) {
System.out.println("You cannot buy houses on " + prop.name());
System.out.println("Would you like to buy any more houses?");
continue;
}
if (player.getMoney() < prop.houseCost()) {
System.out.println("You cannot afford to buy houses on " + prop.name());
System.out.println("Would you like to buy any more houses?");
continue;
}
if (!prop.groupBuild()) {
System.out.println("You must build evenly. Select another property.");
System.out.println("Would you like to buy any more houses?");
continue;
}
prop.build(1);
player.excMoney(-1 * prop.houseCost());
System.out.println("You now own " + prop.numHouses() + " houses on " + prop.name());
System.out.println("Would you like to buy any more houses?");
} while (player.inputBool(state));
}
private int sellHouses(Player player) {
int value = 0;
do {
System.out.println("On which property would you like to sell a house?");
Property prop = propertySelect(player);
if (prop.numHouses() == 0) {
System.out.println("You cannot sell houses on " + prop.name());
System.out.println("Would you like to sell any more houses?");
continue;
}
if (!prop.groupSell()) {
System.out.println("You must build evenly. Select another property.");
System.out.println("Would you like to sell any more houses?");
continue;
}
prop.build(-1);
value += prop.houseCost() / 2;
System.out.println("You now own " + prop.numHouses() + " houses on " + prop.name());
System.out.println("Would you like to sell any more houses?");
} while (player.inputBool(state));
player.excMoney(value);
return value;
}
private void handleHouses(Player player) {
System.out.println("Would you like to buy houses?");
state.state = DecisionState.BUY_HOUSE;
if (player.inputBool(state))
buyHouses(player);
System.out.println("Would you like to sell houses?");
state.state = DecisionState.SELL_HOUSE;
if (player.inputBool(state))
sellHouses(player);
}
private void mortgage(Player player) {
do {
System.out.println("Which property would you like to mortgage?");
Square sq = squareSelect(player, false);
if (sq.isMortgaged()) {
System.out.println("This property is already mortgaged.");
System.out.println("Would you like to mortgage a different property?");
continue;
}
player.excMoney(sq.mortgage());
System.out.println("Would you like to mortgage any more properties?");
} while (player.inputBool(state));
}
private void unmortgage(Player player) {
do {
System.out.println("Which property would you like to unmortgage?");
Square sq = squareSelect(player, true);
player.excMoney(sq.mortgage());
System.out.println("Would you like to unmortgage any more properties?");
} while (player.inputBool(state));
}
private void handleMortgages(Player player) {
System.out.println("Would you like to mortgage properties?");
state.state = DecisionState.MORTGAGE;
if (player.inputBool(state)) {
mortgage(player);
}
System.out.println("Would you like to unmortgage properties?");
state.state = DecisionState.UNMORTGAGE;
if (player.inputBool(state)) {
unmortgage(player);
}
}
private void handleTrade(Player player) {
System.out.println("With which player would you like to trade?");
state.state = DecisionState.TRADE;
Player other = player.inputPlayer(state, player);
System.out.println("Would you like to exchange money?");
state.state = DecisionState.TRADE_MONEY;
if (player.inputBool(state)) {
System.out.println("Money exchange value? (Negative if you give them money)");
int val = player.inputInt(state);
player.excMoney(val);
other.excMoney(-1 * val);
}
System.out.println("Would you like to give them properties?");
state.state = DecisionState.GIVE_PROPS;
while (player.inputBool(state)) {
Square sq = squareSelect(player);
sq.purchase(other);
player.sellProp(sq);
other.addProperty(sq);
System.out.println("Any more properties to give?");
}
System.out.println("Would they like to give you properties?");
state.state = DecisionState.GET_PROPS;
while (player.inputBool(state)) {
Square sq = squareSelect(other);
sq.purchase(player);
other.sellProp(sq);
player.addProperty(sq);
System.out.println("Any more properties to give?");
}
}
private void leaveJail(Player player) {
int JAIL_COST = 50;
if (player.numJailFree() > 0) {
player.useJailFree();
System.out.println("You used a Get Out Of Jail Free Card!");
} else if (player.getMoney() >= JAIL_COST) {
player.excMoney(JAIL_COST * -1);
System.out.println("You paid $50 to get out of jail!");
} else {
int cost = JAIL_COST;
Player bank = new CPUPlayer(-1);
while (true) {
cost = additionalFunds(cost, player, bank);
if (cost == Integer.MIN_VALUE)
return;
if (cost <= 0) {
player.excMoney(cost * -1);
break;
}
}
}
}
private void unowned(Player player, Square square) {
int cost = square.cost();
if (totalVal(availableAssets(player)) + player.getMoney() < cost) {
System.out.println("You cannot afford to purchase " + square.name());
purchase(auction(player, square), square);
return;
}
boolean additional = false;
System.out.println("Would you like to purchase " + square.name() + " for " + cost + " (Yes/No)?");
state.state = DecisionState.PURCHASE;
if (player.getMoney() < cost) {
additional = true;
System.out.println("This transaction will require additional funds.");
}
if (player.inputBool(state)) {
if (!additional)
player.excMoney(-1 * cost);
else {
Player bank = new CPUPlayer(-1);
while (true) {
cost = additionalFunds(cost, player, bank);
if (cost == Integer.MIN_VALUE)
return;
if (cost <= 0) {
player.excMoney(cost * -1);
break;
}
}
}
purchase(player, square);
} else
purchase(auction(player, square), square);
}
private void purchase(Player player, Square square) {
if (player == null || square == null) return;
if (!square.isOwnable()) return;
player.addProperty(square);
square.purchase(player);
}
private Player auction(Player player, Square square) {
System.out.println("Auctioning off " + square.name() + ".");
int currentBid = -10;
final int BID_INCREMENT = 10;
Player winner = null;
while (true) {
int minBid = currentBid + BID_INCREMENT;
System.out.println("Would anyone like to place a bid? Minimum bid: $" + minBid);
state.state = DecisionState.AUCTION;
state.val = minBid;
if (!player.inputBool(state))
break;
System.out.println("Please enter player name"); //TODO has to be changed for CPU
winner = player.inputPlayer(state, player);
System.out.println(winner.name() + ", please enter your bid.");
int bid = player.inputInt(state);
if (bid < minBid) {
System.out.println("Bid is below minimum bid. Please try again.");
continue;
}
System.out.println("Bid accepted. Current highest bid - " + winner.name() + " for $" + bid);
currentBid = bid;
}
if (winner != null) {
winner.excMoney(-1 * currentBid);
System.out.println(winner.name() + " wins auction, for $" + currentBid);
} else
System.out.println("No player wins auction.");
return winner;
}
private void owned(Player player, Square square, int val) {
int cost = square.rent(val);
if (square instanceof Utility && chanceBoost)
cost = ((Utility) square).increasedRent();
else if (square instanceof Railroad && chanceBoost)
cost *= 2;
chanceBoost = false;
Player owner = square.owner();
if (player.name().equals(owner.name()))
return;
boolean additional = false;
System.out.println("You have landed on " + square.name() + " and owe " + cost + " in rent.");
if (player.getMoney() < cost) {
additional = true;
System.out.println("This transaction will require additional funds.");
}
if (!additional) {
player.excMoney(-1 * cost);
owner.excMoney(cost);
} else {
while (true) {
cost = additionalFunds(cost, player, owner);
if (cost == Integer.MIN_VALUE)
return;
if (cost <= 0) {
player.excMoney(cost * -1);
break;
}
}
}
}
private void payTax(Player player, Taxes tax, Square square) {
int cost;
if (square.position() == 4) {
System.out.println("Would you like to pay 10% or 200 (10%/200)?");
state.state = DecisionState.INCOME_TAX;
if (player.inputDecision(state, new String[]{"10%", "200"}) == 0)
cost = tax.tax(player.getAssets());
else
cost = tax.tax();
} else
cost = tax.tax();
boolean additional = false;
System.out.println("You have landed on " + square.name() + " and owe " + cost + " in rent.");
if (player.getMoney() < cost) {
additional = true;
System.out.println("This transaction will require additional funds.");
}
if (!additional)
player.excMoney(-1 * cost);
else {
Player bank = new CPUPlayer(-1);
while (true) {
cost = additionalFunds(cost, player, bank);
if (cost == Integer.MIN_VALUE)
return;
if (cost <= 0) {
player.excMoney(cost * -1);
break;
}
}
}
}
private void drawCard(Player player, Cards cards) {
int numString = 3;
Card card = cards.draw();
String[] string = new String[numString];
if (card.textA() != null)
string[0] = card.textA();
if (card.textB() != null)
string[1] = card.textB();
if (card.textC() != null)
string[2] = card.textC();
for (int i = 0; i < numString; i++) {
if (string[i] == null)
break;
System.out.println(string[i]);
}
int initialPos = player.position();
switch (card.action()) {
case BANK_MONEY:
player.excMoney(card.value());
break;
case PLAYER_MONEY:
allPlayers(-1 * card.eachPlayer(), player);
break;
case MOVE:
player.move(card.travel());
break;
case MOVE_TO:
player.moveTo(card.travelTo());
break;
case MOVE_NEAREST:
if (card.travelRail())
railMove(player);
else
utilMove(player);
break;
case STREET_REPAIRS:
streetRepairs(player, card.house(), card.hotel());
break;
case OUT_JAIL:
player.addJailFree(card.type() == Card.CardType.CHANCE);
break;
default:
break;
}
chanceBoost = card.increased();
if (initialPos == player.position())
return;
Square sq = state.board.square(player.position());
handleSquare(player, sq, 0);
}
private void railMove(Player player) {
int pos = player.position();
for (int i = pos; i < state.board.size(); i++) {
if (state.board.square(i) instanceof Railroad) {
player.moveTo(i);
return;
}
}
for (int i = 0; i < pos; i++) {
if (state.board.square(i) instanceof Railroad) {
player.moveTo(i);
return;
}
}
throw new RuntimeException("Problem finding railroad");
}
private void utilMove(Player player) {
int pos = player.position();
for (int i = pos; i < state.board.size(); i++) {
if (state.board.square(i) instanceof Utility) {
player.moveTo(i);
return;
}
}
for (int i = 0; i < pos; i++) {
if (state.board.square(i) instanceof Utility) {
player.moveTo(i);
return;
}
}
throw new RuntimeException("Problem finding utility");
}
private void streetRepairs(Player player, int house, int hotel) {
int val = 0;
for (Square sq : player.properties()) {
if (sq instanceof Property) {
Property prop = (Property) sq;
if (prop.numHouses() < 5)
val += house * prop.numHouses();
else
val += hotel;
}
}
boolean additional = false;
System.out.println("You owe " + val + " for street repairs.");
if (player.getMoney() < val) {
additional = true;
System.out.println("This transaction will require additional funds.");
}
if (!additional)
player.excMoney(-1 * val);
else {
while (true) {
val = additionalFunds(val, player, new CPUPlayer(-1));
if (val == Integer.MIN_VALUE)
return;
if (val <= 0) {
player.excMoney(val * -1);
break;
}
}
}
}
private void allPlayers(int value, Player player) {
player.excMoney(-1 * (state.players.size() - 1) * value);
state.players.stream().forEach(p -> p.excMoney(value));
}
private void jailInteraction(Player player, Jail jail) {
Jail.JailType type = jail.getType();
if (type == JailType.TO_JAIL)
intoJail(player);
}
private void toJail(Player player) {
System.out.println("Go to Jail!");
player.moveTo(40);
Square[] square = state.board.getBoard();
Jail jail = (Jail) square[40];
jailInteraction(player, jail);
}
private void intoJail(Player player) {
player.toJail();
}
private int additionalFunds(int cost, Player player, Player owner) {
Queue<Square> props = availableAssets(player);
int availableAssets = mortVal(props) + player.getMoney();
if (cost <= player.getMoney()) {
player.excMoney(-1 * cost);
owner.excMoney(cost);
return 0;
}
if (availableAssets < cost) {
lose(player, owner);
return Integer.MIN_VALUE;
} else {
System.out.println("You need additional funds!");
System.out.println("How will you obtain necessary funds (Mortgage/Sell Houses)?");
state.state = DecisionState.FUNDS;
int choice = player.inputDecision(state, new String[]{"Mortgage", "Sell Houses"});
if (choice == 0) {
System.out.println("Which property would you like to mortgage?");
System.out.println("Please enter number.");
Square sq = squareSelect(player, false);
cost -= sq.mortgage();
} else
cost -= sellHouses(player);
}
return cost;
}
private Property propertySelect(Player player) {
Queue<Square> props = new LinkedList<>();
for (Square sq : player.properties()) {
if (!(sq instanceof Property))
continue;
if (!sq.isMortgaged())
props.add(sq);
}
return (Property) propertySelect(props, player);
}
private Square squareSelect(Player player, boolean mort) {
Queue<Square> props = new LinkedList<>();
for (Square sq : player.properties())
if (sq.isMortgaged() == mort)
props.add(sq);
return propertySelect(props, player);
}
private Square squareSelect(Player player) {
return propertySelect(player.properties(), player);
}
private Square propertySelect(Iterable<Square> props, Player player) {
System.out.println("You own the following properties:");
int counter = 1;
for (Square sq : props)
System.out.println(counter++ + ") " + sq.name());
while (true) {
int propNum = player.inputInt(state);
int propState = 1;
for (Square sq : props) {
if (propState++ == propNum)
return sq;
}
System.out.println("Please select a valid property.");
}
}
private Queue<Square> availableAssets(Player player) {
Iterable<Square> props = player.properties();
Queue<Square> avail = new LinkedList<>();
for (Square sq : props)
if (!sq.isMortgaged())
avail.add(sq);
return avail;
}
private int totalVal(Queue<Square> props) {
int totalMoney = 0;
for (Square sq : props) {
totalMoney += sq.cost();
if (sq instanceof Property) {
Property prop = (Property) sq;
totalMoney += prop.numHouses() * prop.houseCost();
}
}
return totalMoney;
}
private int mortVal(Queue<Square> props) {
int totalMoney = 0;
for (Square sq : props) {
totalMoney += sq.mortgageCost();
if (sq instanceof Property) {
Property prop = (Property) sq;
totalMoney += prop.numHouses() * prop.houseCost() / 2;
}
}
return totalMoney;
}
private void lose(Player loser, Player winner) {
Iterable<Square> squares = loser.properties();
for (Square sq : squares)
winner.addProperty(sq);
winner.excMoney(loser.getMoney());
while (loser.numJailFree() > 0)
winner.addJailFree(loser.useJailFree());
lost = true;
System.out.println(loser.name() + " has LOST!");
}
private void printState() {
int counter = 1;
for (Player player : state.players) {
System.out.println("--------------------------------------------------");
System.out.println("Player " + counter++);
System.out.printf("%-10s%40s%n", "Name", player.name());
System.out.printf("%-10s%40s%n", "Money", player.getMoney());
System.out.printf("%-10s%40s%n", "Position", player.position());
System.out.printf("%-10s", "Properties");
Iterable<Square> owned = player.properties();
boolean first = true;
for (Square s : owned) {
if (first)
System.out.printf("%40s%n", s);
else
System.out.printf("%50s%n", s);
first = false;
}
if (first)
System.out.printf("%40s%n", "none");
if (player.inJail())
System.out.println("In jail");
if (player.numJailFree() > 0)
System.out.println(player.numJailFree() + " out of jail free cards");
System.out.println("--------------------------------------------------");
}
}
public enum DecisionState {
NONE, BUY_JAIL, CASH_CARD, BUY_HOUSE, SELL_HOUSE, MORTGAGE, UNMORTGAGE, TRADE,
TRADE_MONEY, GIVE_PROPS, GET_PROPS, PURCHASE, AUCTION, INCOME_TAX, FUNDS, ADDITIONAL
}
public class State {
public DecisionState state;
public Queue<Player> players;
public Board board; //game board
public Player current;
public int val = 0;
}
}