|
@@ -11,55 +11,50 @@ public class War extends CardGame implements Gamble, Game {
|
11
|
11
|
|
12
|
12
|
private ArrayList<Card> tableCards = new ArrayList<Card>();
|
13
|
13
|
private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
|
14
|
|
- private Scanner scanner = new Scanner(System.in);
|
15
|
|
- private boolean war = false;
|
16
|
|
-
|
17
|
|
- War(int ante) {
|
18
|
|
- super(ante);
|
19
|
|
- }
|
|
14
|
+ private Console console = new Console();
|
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
|
|
-
|
141
|
|
- public void startGame() {
|
142
|
|
- System.out.println("Welcome to war!");
|
|
117
|
+ Printer.welcomeTo("War");
|
143
|
118
|
super.chooseStatingPlayer();
|
144
|
119
|
payAnte();
|
145
|
120
|
deal();
|
|
@@ -148,59 +123,36 @@ public class War extends CardGame implements Gamble, Game {
|
148
|
123
|
|
149
|
124
|
public void startRound() {
|
150
|
125
|
while(super.getLoser() == null) {
|
151
|
|
-
|
152
|
|
- String input = getCommand();
|
153
|
|
-
|
154
|
|
- if (input.equals("play")) {
|
155
|
|
- //each player
|
156
|
|
- for (CardPlayer player : super.getPlayers()) {
|
157
|
|
- //plays a card, then
|
158
|
|
- playCard(true);
|
159
|
|
- //the turn updates to be the next players.
|
160
|
|
- super.chooseNextTurn();
|
161
|
|
- }
|
162
|
|
- //determine the winner once all players play a card
|
|
126
|
+ String input = console.getCMDFromUser("Type 'FLIP' to play the card at the top of your pile");
|
|
127
|
+ if (input.equals("flip")) {
|
|
128
|
+ eachPlayerPlayCard();
|
163
|
129
|
CardPlayer winner = determineWinner(super.getPlayers());
|
164
|
|
- System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
|
165
|
|
- //add all the table cards to the players discard
|
|
130
|
+ Printer.printMessage(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
|
166
|
131
|
winner.addDiscard(tableCards);
|
167
|
|
- //clear the table cards pile
|
168
|
132
|
tableCards = new ArrayList<Card>();
|
169
|
|
- //if the user does not type play
|
170
|
133
|
} else {
|
171
|
|
- //display a message
|
172
|
|
- System.out.println("Sorry, I don't understand that command.");
|
|
134
|
+ Printer.printMessage("Sorry, I don't understand that command.");
|
173
|
135
|
}
|
174
|
136
|
}
|
175
|
|
-
|
176
|
137
|
}
|
177
|
138
|
|
178
|
|
- public String getCommand(){
|
179
|
|
- System.out.println("Type play to play the top card from your pile.");
|
180
|
|
- String input = scanner.next();
|
181
|
|
- input = input.toLowerCase().trim();
|
182
|
|
- return input;
|
|
139
|
+ public void eachPlayerPlayCard(){
|
|
140
|
+ for (CardPlayer player : super.getPlayers()) {
|
|
141
|
+ playCard(true);
|
|
142
|
+ super.chooseNextTurn();
|
|
143
|
+ }
|
183
|
144
|
}
|
184
|
145
|
|
185
|
146
|
public void deal() {
|
186
|
|
- //while there are cards in the deck
|
187
|
147
|
while(super.getDeck().size() != 0){
|
188
|
|
- //for each player playing the game
|
189
|
148
|
for(int i = 0; i < super.getPlayers().size(); i ++)
|
190
|
149
|
{
|
191
|
|
- //grab the card from the top (last added) to the deck
|
192
|
150
|
Card card = super.getDeck().get(super.getDeck().size() - 1);
|
193
|
|
- //get the player whos hand we are adding the card to
|
194
|
151
|
CardPlayer player = super.getPlayers().get(i);
|
195
|
|
- //add the card to their hand
|
196
|
152
|
player.getHand().add(card);
|
197
|
|
- //remove the card from the deck
|
198
|
153
|
super.getDeck().remove(card);
|
199
|
154
|
}
|
200
|
155
|
}
|
201
|
|
-
|
202
|
|
- System.out.println(super.getPlayersTurn().getPlayer().getName() +
|
203
|
|
- "has: " + super.getPlayersTurn().getHand().size() + " cards.");
|
204
|
|
-
|
|
156
|
+ Printer.printMessage(super.getPlayersTurn().getPlayer().getName() + "has: " + super.getPlayersTurn().getHand().size() + " cards.");
|
205
|
157
|
}
|
206
|
|
-}
|
|
158
|
+}
|