|
@@ -12,54 +12,49 @@ public class War extends CardGame implements Gamble, Game {
|
12
|
12
|
private ArrayList<Card> tableCards = new ArrayList<Card>();
|
13
|
13
|
private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
|
14
|
14
|
private Console console = new Console();
|
15
|
|
- private boolean war = false;
|
16
|
|
-
|
17
|
|
- War(int ante) {
|
18
|
|
- super(ante);
|
19
|
|
- }
|
20
|
15
|
|
|
16
|
+ War(int ante) { super(ante); }
|
21
|
17
|
|
22
|
|
- /**
|
23
|
|
- * Specific to war methods
|
24
|
|
- */
|
25
|
18
|
public void playCard(boolean cardFace){
|
26
|
|
- //if the player has cards in their hand
|
27
|
19
|
if(super.getPlayersTurn().getHand().size() > 0) {
|
28
|
|
- //pull out a card to play
|
29
|
|
- Card card = super.getPlayersTurn().getHand().get(0);
|
30
|
|
- //play the card face up, on the table
|
31
|
|
- card.setVisibility(cardFace);
|
32
|
|
- tableCards.add(card);
|
33
|
|
- //store the last played card in the players wrapper class
|
34
|
|
- super.getPlayersTurn().setPlayedCard(card);
|
35
|
|
- //remove this card from their hand
|
36
|
|
- super.getPlayersTurn().getHand().remove(card);
|
37
|
|
- //print the card that was played
|
38
|
|
- if(cardFace == true) {
|
39
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played " + card.getName() + " and has " + super.getPlayersTurn().getHand().size() + " cards left.");
|
40
|
|
- } else {
|
41
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played a card face down.");
|
42
|
|
- }
|
43
|
|
- //if the player has not cards in their hand but has cards in their discard, pickup their discard and play a card
|
|
20
|
+ playCardInHand(cardFace);
|
44
|
21
|
} else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() > 0) {
|
45
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
|
46
|
|
- super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
|
47
|
|
- super.getPlayersTurn().setDiscard(new ArrayList<Card>());
|
48
|
|
- playCard(true);
|
49
|
|
- //if the person has no cards in their hand, and no cards in discard they lose.
|
|
22
|
+ playCardFromPile(cardFace);
|
50
|
23
|
} else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() == 0){
|
51
|
24
|
super.setLoser(super.getPlayersTurn().getPlayer());
|
52
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
|
|
25
|
+ Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
|
53
|
26
|
}
|
54
|
27
|
}
|
55
|
28
|
|
56
|
|
- public CardPlayer warMethod(){
|
57
|
|
- System.out.println("War!");
|
|
29
|
+ public void playCardInHand(boolean cardFace){
|
|
30
|
+ Card card = getCardFromHand(cardFace);
|
|
31
|
+ if(cardFace) {
|
|
32
|
+ Printer.printWarTurnResult(super.getPlayersTurn().getPlayer().getName(), card.getName(), super.getPlayersTurn().getHand().size());
|
|
33
|
+ } else {
|
|
34
|
+ Printer.playedFaceDown(super.getPlayersTurn().getPlayer().getName());
|
|
35
|
+ }
|
|
36
|
+ }
|
58
|
37
|
|
|
38
|
+ public Card getCardFromHand(boolean cardFace){
|
|
39
|
+ Card card = super.getPlayersTurn().getHand().get(0);
|
|
40
|
+ card.setVisibility(cardFace);
|
|
41
|
+ tableCards.add(card);
|
|
42
|
+ super.getPlayersTurn().setPlayedCard(card);
|
|
43
|
+ super.getPlayersTurn().getHand().remove(card);
|
|
44
|
+ return card;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public void playCardFromPile(boolean cardFace){
|
|
48
|
+ Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
|
|
49
|
+ super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
|
|
50
|
+ super.getPlayersTurn().setDiscard(new ArrayList<Card>());
|
|
51
|
+ playCard(cardFace);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public CardPlayer warMethod(){
|
|
55
|
+ Printer.printMessage("War!");
|
59
|
56
|
int max = 0;
|
60
|
57
|
CardPlayer winner = null;
|
61
|
|
-
|
62
|
|
- //each player plays 3 cards
|
63
|
58
|
for(int i = 0; i < warMembers.size(); i ++){
|
64
|
59
|
for(int m = 0; m < 2; m ++){
|
65
|
60
|
playCard(false);
|
|
@@ -67,36 +62,31 @@ public class War extends CardGame implements Gamble, Game {
|
67
|
62
|
playCard(true);
|
68
|
63
|
super.chooseNextTurn();
|
69
|
64
|
}
|
70
|
|
-
|
71
|
|
- //find the player with the highest value
|
72
|
65
|
winner = determineWinner(warMembers);
|
73
|
66
|
warMembers = new ArrayList<>();
|
74
|
67
|
return winner;
|
75
|
68
|
}
|
76
|
69
|
|
77
|
70
|
public CardPlayer determineWinner(ArrayList<CardPlayer> playerList){
|
78
|
|
-
|
79
|
71
|
int max = 0;
|
80
|
72
|
CardPlayer winner = null;
|
81
|
73
|
boolean war = false;
|
82
|
|
-
|
83
|
|
- //loop through and get the max card value
|
84
|
74
|
for(int i = 0; i < playerList.size(); i ++){
|
85
|
75
|
CardPlayer player = playerList.get(i);
|
86
|
|
- //if the players card is greater than the current max
|
87
|
76
|
if(player.getPlayedCard().getCardValue() > max)
|
88
|
77
|
{
|
89
|
|
- //set their value as max
|
90
|
78
|
max = player.getPlayedCard().getCardValue();
|
91
|
|
- //make them the winner
|
92
|
79
|
winner = player;
|
93
|
|
- //set war to false
|
94
|
80
|
war = false;
|
95
|
81
|
} else if (player.getPlayedCard().getCardValue() == max){
|
96
|
82
|
warMembers.add(player);
|
97
|
83
|
war = true;
|
98
|
84
|
}
|
99
|
85
|
}
|
|
86
|
+ return checkWar(war, winner);
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ public CardPlayer checkWar(boolean war, CardPlayer winner){
|
100
|
90
|
if(war)
|
101
|
91
|
{
|
102
|
92
|
warMembers.add(winner);
|
|
@@ -104,25 +94,16 @@ public class War extends CardGame implements Gamble, Game {
|
104
|
94
|
return winner;
|
105
|
95
|
} else if(!war)
|
106
|
96
|
{
|
107
|
|
- System.out.println("The winner is " + winner.getPlayer().getName());
|
|
97
|
+ Printer.printMessage("The winner is " + winner.getPlayer().getName());
|
108
|
98
|
return winner;
|
109
|
99
|
}
|
110
|
100
|
return null;
|
111
|
101
|
}
|
112
|
102
|
|
113
|
|
- /**
|
114
|
|
- * Below 3 Implemented from Gamble
|
115
|
|
- */
|
116
|
|
- public void bet(int betAmount) {
|
117
|
|
- super.changeTablePot(betAmount);
|
118
|
|
- }
|
119
|
|
-
|
|
103
|
+ public void bet(int betAmount) { super.changeTablePot(betAmount); }
|
120
|
104
|
|
121
|
105
|
public void payout() {
|
122
|
|
- if(super.getWinner() != null)
|
123
|
|
- {
|
124
|
|
- super.getWinner().changeBalance(super.getTablePot());
|
125
|
|
- }
|
|
106
|
+ if(super.getWinner() != null) { super.getWinner().changeBalance(super.getTablePot()); }
|
126
|
107
|
}
|
127
|
108
|
|
128
|
109
|
public void payAnte() {
|
|
@@ -133,13 +114,7 @@ public class War extends CardGame implements Gamble, Game {
|
133
|
114
|
}
|
134
|
115
|
}
|
135
|
116
|
|
136
|
|
- /**
|
137
|
|
- * Below 3 Implemented from Game
|
138
|
|
- */
|
139
|
|
-
|
140
|
|
- public void quit() {
|
141
|
|
-
|
142
|
|
- }
|
|
117
|
+ public void quit() { }
|
143
|
118
|
|
144
|
119
|
public void startGame() {
|
145
|
120
|
Printer.welcomeTo("War");
|
|
@@ -151,51 +126,37 @@ public class War extends CardGame implements Gamble, Game {
|
151
|
126
|
|
152
|
127
|
public void startRound() {
|
153
|
128
|
while(super.getLoser() == null) {
|
154
|
|
-
|
155
|
129
|
String input = console.getCMDFromUser("Type 'FLIP' to play the card at the top of your pile");
|
156
|
130
|
if (input.equals("flip")) {
|
157
|
|
- //each player
|
158
|
|
- for (CardPlayer player : super.getPlayers()) {
|
159
|
|
- //plays a card, then
|
160
|
|
- playCard(true);
|
161
|
|
- //the turn updates to be the next players.
|
162
|
|
- super.chooseNextTurn();
|
163
|
|
- }
|
164
|
|
- //determine the winner once all players play a card
|
|
131
|
+ eachPlayerPlayCard();
|
165
|
132
|
CardPlayer winner = determineWinner(super.getPlayers());
|
166
|
|
- System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
|
167
|
|
- //add all the table cards to the players discard
|
|
133
|
+ Printer.printMessage(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
|
168
|
134
|
winner.addDiscard(tableCards);
|
169
|
|
- //clear the table cards pile
|
170
|
135
|
tableCards = new ArrayList<Card>();
|
171
|
|
- //if the user does not type play
|
172
|
136
|
} else {
|
173
|
|
- //display a message
|
174
|
|
- System.out.println("Sorry, I don't understand that command.");
|
|
137
|
+ Printer.printMessage("Sorry, I don't understand that command.");
|
175
|
138
|
}
|
176
|
139
|
}
|
|
140
|
+ }
|
177
|
141
|
|
|
142
|
+ public void eachPlayerPlayCard(){
|
|
143
|
+ for (CardPlayer player : super.getPlayers()) {
|
|
144
|
+ playCard(true);
|
|
145
|
+ super.chooseNextTurn();
|
|
146
|
+ }
|
178
|
147
|
}
|
179
|
148
|
|
180
|
149
|
public void deal() {
|
181
|
|
- //while there are cards in the deck
|
182
|
150
|
while(super.getDeck().size() != 0){
|
183
|
|
- //for each player playing the game
|
184
|
151
|
for(int i = 0; i < super.getPlayers().size(); i ++)
|
185
|
152
|
{
|
186
|
|
- //grab the card from the top (last added) to the deck
|
187
|
153
|
Card card = super.getDeck().get(super.getDeck().size() - 1);
|
188
|
|
- //get the player whos hand we are adding the card to
|
189
|
154
|
CardPlayer player = super.getPlayers().get(i);
|
190
|
|
- //add the card to their hand
|
191
|
155
|
player.getHand().add(card);
|
192
|
|
- //remove the card from the deck
|
193
|
156
|
super.getDeck().remove(card);
|
194
|
157
|
}
|
195
|
158
|
}
|
196
|
|
-
|
197
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() +
|
198
|
|
- "has: " + super.getPlayersTurn().getHand().size() + " cards.");
|
|
159
|
+ Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + "has: " + super.getPlayersTurn().getHand().size() + " cards.");
|
199
|
160
|
|
200
|
161
|
}
|
201
|
162
|
}
|