|
@@ -1,11 +1,12 @@
|
1
|
|
-
|
2
|
1
|
package io.zipcoder.casino;
|
3
|
|
-import java.util.Scanner;
|
4
|
2
|
|
5
|
|
-public class Stud extends CardGame implements Gamble, Game {
|
6
|
|
- Scanner scanner = new Scanner(System.in);
|
7
|
|
- Console console;
|
8
|
|
- // private int roundCount = 0;
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+
|
|
5
|
+public class Stud extends CardGame implements Game {
|
|
6
|
+ Console console = new Console();
|
|
7
|
+ private boolean isDealt = false;
|
|
8
|
+ private boolean isCardFlipped = false;
|
|
9
|
+ private int countRound = 0;
|
9
|
10
|
|
10
|
11
|
public Stud(int ante) {
|
11
|
12
|
super(ante);
|
|
@@ -17,29 +18,30 @@ public class Stud extends CardGame implements Gamble, Game {
|
17
|
18
|
}
|
18
|
19
|
|
19
|
20
|
|
20
|
|
- public void fold(){
|
21
|
|
-
|
|
21
|
+ public boolean getIsDealt(){
|
|
22
|
+ return isDealt;
|
22
|
23
|
}
|
23
|
24
|
|
|
25
|
+
|
24
|
26
|
/**
|
25
|
27
|
* Determine what player wins by looping through player array and then
|
26
|
28
|
* passing each hand to the 'handValue' method
|
27
|
29
|
*/
|
28
|
|
- public CardPlayer determineWinner(){
|
29
|
|
- int max = 0;
|
30
|
|
- CardPlayer winner = null;
|
31
|
|
-
|
32
|
|
- for(int i = 0; i < getPlayers().size(); i ++){
|
33
|
|
- CardPlayer player = getPlayers().get(i);
|
34
|
|
- int playerHandValue = handValue(player); // 'handValue' method sets 'max' value of this hand
|
35
|
|
- if(playerHandValue > max){
|
36
|
|
- max = playerHandValue;
|
37
|
|
- winner = player;
|
|
30
|
+ public CardPlayer determineWinner(ArrayList<CardPlayer> players){
|
|
31
|
+ int max = 0;
|
|
32
|
+ CardPlayer winner = null;
|
|
33
|
+
|
|
34
|
+ for(int i = 0; i < players.size(); i ++){
|
|
35
|
+ int playerHandValue = handValue(players.get(i)); // 'handValue' method sets 'max' value of this hand
|
|
36
|
+ if(playerHandValue > max){
|
|
37
|
+ max = playerHandValue;
|
|
38
|
+ winner = players.get(i);
|
|
39
|
+ }
|
38
|
40
|
}
|
39
|
|
- }
|
40
|
|
- System.out.println("The winner is " + winner.getPlayer().getName());
|
41
|
|
- System.out.println(winner.getPlayer().getName() + "\'s hand was: " + winner.getHand().get(0).getName() + " - " + winner.getHand().get(1).getName() + " - " + winner.getHand().get(2).getName() );
|
42
|
|
- return winner;
|
|
41
|
+ System.out.println("The winner is " + winner.getPlayer().getName());
|
|
42
|
+ System.out.println(winner.getPlayer().getName() + "\'s hand was: " + winner.getHand().get(0).getName() +
|
|
43
|
+ " - " + winner.getHand().get(1).getName() + " - " + winner.getHand().get(2).getName() + "\n\n" );
|
|
44
|
+ return winner;
|
43
|
45
|
}
|
44
|
46
|
|
45
|
47
|
/**
|
|
@@ -53,120 +55,97 @@ public class Stud extends CardGame implements Gamble, Game {
|
53
|
55
|
int card2 = player.getHand().get(1).getCardValue();
|
54
|
56
|
int card3 = player.getHand().get(2).getCardValue();
|
55
|
57
|
|
56
|
|
- //Three of a Kind
|
57
|
|
- if (card1 == card2 && card1 == card3){
|
58
|
|
- handValue = card1 * 1000000;
|
59
|
|
- //Two pair
|
|
58
|
+ if (card1 == card2 && card1 == card3) { //Three of a Kind
|
|
59
|
+ handValue = threeOfAKindHandValue(card1);
|
60
|
60
|
}
|
61
|
|
- else if (card1 == card2){
|
|
61
|
+ else if (card1 == card2 || card1 == card3 || card2 == card3){
|
|
62
|
+ handValue = onePairHandValue(card1, card2, card3);
|
|
63
|
+ }
|
|
64
|
+ else {
|
|
65
|
+ handValue = highCardHandValue(card1, card2, card3);
|
|
66
|
+ }
|
|
67
|
+ return handValue;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ /**
|
|
71
|
+ * Helper method for handValue() if had is three-of-a-kind
|
|
72
|
+ * @param card1
|
|
73
|
+ * @return
|
|
74
|
+ */
|
|
75
|
+ public int threeOfAKindHandValue(int card1){
|
|
76
|
+ int handValue;
|
|
77
|
+ handValue = card1 * 1000000;
|
|
78
|
+ return handValue;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ /**
|
|
82
|
+ * Helper method for handValue() if had is one-pair
|
|
83
|
+ * @param card1
|
|
84
|
+ * @param card2
|
|
85
|
+ * @param card3
|
|
86
|
+ * @return
|
|
87
|
+ */
|
|
88
|
+ public int onePairHandValue(int card1, int card2, int card3){
|
|
89
|
+ int handValue = 0;
|
|
90
|
+
|
|
91
|
+ if (card1 == card2){
|
62
|
92
|
handValue = (card1 * 10000) + card3;
|
63
|
93
|
}
|
64
|
94
|
else if (card1 == card3){
|
65
|
95
|
handValue = (card1 * 10000) + card2;
|
66
|
96
|
}
|
67
|
|
- else if (card2 == card3){
|
|
97
|
+ else if (card2 == card3) {
|
68
|
98
|
handValue = (card2 * 10000) + card1;
|
69
|
|
- //High Card
|
70
|
|
- } else {
|
71
|
|
- // Card1 is Highest
|
72
|
|
- if (card1 > card2 && card1 > card3 && card2 > card3) {
|
73
|
|
- handValue = (card1 * 100) + (card2 * 10) + card3;
|
74
|
|
- }
|
75
|
|
- else if (card1 > card2 && card1 > card3 && card3 > card2) {
|
76
|
|
- handValue = (card1 * 100) + (card3 * 10) + card2;
|
77
|
|
- }
|
78
|
|
- // Card2 is Highest
|
79
|
|
- else if (card2 > card1 && card2 > card3 && card1 > card3) {
|
80
|
|
- handValue = (card2 * 100) + (card1 * 10) + card3;
|
81
|
|
- }
|
82
|
|
- else if (card2 > card1 && card2 > card3 && card3 > card1) {
|
83
|
|
- handValue = (card2 * 100) + (card3 * 10) + card1;
|
84
|
|
- }
|
85
|
|
- // Card3 is Highest
|
86
|
|
- else if (card3 > card1 && card3 > card2 && card1 > card3) {
|
87
|
|
- handValue = (card3 * 100) + (card1 * 10) + card3;
|
88
|
|
- }
|
89
|
|
- else if (card3 > card1 && card3 > card2 && card3 > card1) {
|
90
|
|
- handValue = (card3 * 100) + (card3 * 10) + card1;
|
91
|
|
- }
|
92
|
|
- else {
|
93
|
|
- handValue = 0;
|
94
|
|
- }
|
|
99
|
+ return handValue;
|
95
|
100
|
}
|
96
|
101
|
return handValue;
|
97
|
102
|
}
|
98
|
103
|
|
99
|
|
-
|
100
|
|
-
|
101
|
|
-
|
102
|
|
-
|
103
|
|
-
|
104
|
|
-
|
105
|
|
- /*
|
106
|
|
-
|
107
|
|
- create a bet method which asks the first player how much to bet
|
108
|
|
- loops through other plays to see how they would like to react to this.
|
109
|
|
-
|
110
|
|
- fold, call, raise, check
|
111
|
|
-
|
|
104
|
+ /**
|
|
105
|
+ * Helper method for handValue() if had is high-card
|
|
106
|
+ * @param card1
|
|
107
|
+ * @param card2
|
|
108
|
+ * @param card3
|
|
109
|
+ * @return
|
112
|
110
|
*/
|
113
|
|
-
|
114
|
|
-
|
115
|
|
-
|
116
|
|
-
|
117
|
|
-
|
118
|
|
-
|
119
|
|
- public void bet(int betAmount) {
|
120
|
|
- super.changeTablePot(betAmount);
|
121
|
|
- //player.changeBalance(betAmount * -1);
|
122
|
|
- }
|
123
|
|
-
|
124
|
|
-
|
125
|
|
-
|
126
|
|
-
|
127
|
|
-
|
128
|
|
-
|
129
|
|
-
|
130
|
|
-
|
131
|
|
-
|
132
|
|
-
|
133
|
|
-
|
134
|
|
-
|
135
|
|
-
|
136
|
|
-
|
137
|
|
-
|
138
|
|
-
|
139
|
|
- public void payout(){
|
140
|
|
- if(super.getWinner() != null){
|
141
|
|
- super.getWinner().changeBalance(super.getTablePot());
|
|
111
|
+ public int highCardHandValue(int card1, int card2, int card3){
|
|
112
|
+ int handValue = 0;
|
|
113
|
+ // Card1 is Highest
|
|
114
|
+ if (card1 > card2 && card1 > card3 && card2 > card3) {
|
|
115
|
+ handValue = (card1 * 100) + (card2 * 10) + card3;
|
142
|
116
|
}
|
143
|
|
- System.out.println(getWinner().getName() + " won: " + super.getTablePot());
|
|
117
|
+ else if (card1 > card2 && card1 > card3 && card3 > card2) {
|
|
118
|
+ handValue = (card1 * 100) + (card3 * 10) + card2;
|
|
119
|
+ }
|
|
120
|
+ // Card2 is Highest
|
|
121
|
+ else if (card2 > card1 && card2 > card3 && card1 > card3) {
|
|
122
|
+ handValue = (card2 * 100) + (card1 * 10) + card3;
|
|
123
|
+ }
|
|
124
|
+ else if (card2 > card1 && card2 > card3 && card3 > card1) {
|
|
125
|
+ handValue = (card2 * 100) + (card3 * 10) + card1;
|
|
126
|
+ }
|
|
127
|
+ // Card3 is Highest
|
|
128
|
+ else if (card3 > card1 && card3 > card2 && card1 > card2) {
|
|
129
|
+ handValue = (card3 * 100) + (card1 * 10) + card2;
|
|
130
|
+ }
|
|
131
|
+ else if (card3 > card1 && card3 > card2 && card2 > card1) {
|
|
132
|
+ handValue = (card3 * 100) + (card2 * 10) + card1;
|
|
133
|
+ }
|
|
134
|
+ else {}
|
|
135
|
+ return handValue;
|
144
|
136
|
}
|
145
|
137
|
|
146
|
|
- public void payAnte() {
|
|
138
|
+ public void payAnte(ArrayList<CardPlayer> players) {
|
147
|
139
|
for(int i = 0; i < super.getPlayers().size(); i ++)
|
148
|
140
|
{
|
149
|
|
- CardPlayer player = super.getPlayers().get(i);
|
150
|
|
- player.getPlayer().changeBalance(-super.getAnte());
|
|
141
|
+ players.get(i).getPlayer().changeBalance(-super.getAnte());
|
151
|
142
|
}
|
152
|
143
|
}
|
153
|
144
|
|
154
|
|
- public void Quit() {
|
155
|
|
- System.out.println("Play again? Y : or any key to quit.");
|
156
|
|
- String answer = scanner.next();
|
157
|
|
- if (answer.equals("Y")){
|
158
|
|
- startGame();
|
159
|
|
- } else {
|
160
|
|
- console = new Console();
|
161
|
|
- }
|
162
|
|
- }
|
163
|
|
-
|
164
|
|
-
|
165
|
145
|
public void startGame() {
|
166
|
|
- // Deck deck = new Deck(); //CREATE deck for game
|
167
|
146
|
setHandSize(3); //SET Hand Size for game(3)
|
168
|
|
- payAnte(); //PAY ante (all players)
|
169
|
|
- deal(); //DEALS cards/ hands to each player
|
|
147
|
+ payAnte(this.getPlayers()); //PAY ante (all players)
|
|
148
|
+ deal(this.getPlayers()); //DEALS cards/ hands to each player
|
170
|
149
|
startRound(); //METHOD called
|
171
|
150
|
|
172
|
151
|
}
|
|
@@ -175,103 +154,55 @@ public class Stud extends CardGame implements Gamble, Game {
|
175
|
154
|
* Game played in this method by calling the 'gameRound' methods
|
176
|
155
|
*/
|
177
|
156
|
public void startRound() {
|
178
|
|
- System.out.println("Welcome to Three Card Stud!");
|
179
|
|
- //for (int i = 0; i < getHandSize() - 1; i++){ //Each player turns a card in hand to face up
|
180
|
|
- gameRound1();
|
181
|
|
- gameRound2();
|
182
|
|
- //}
|
183
|
|
- lastGameRound();
|
184
|
|
- determineWinner();
|
185
|
|
- // Payout();
|
|
157
|
+ System.out.println("Welcome to Three Card Stud! Cards are dealt!");
|
|
158
|
+ flipCard();
|
|
159
|
+ gameRound(this.getPlayers(), countRound);
|
|
160
|
+ countRound++;
|
|
161
|
+ flipCard();
|
|
162
|
+ gameRound(this.getPlayers(), countRound);
|
|
163
|
+ countRound++;
|
|
164
|
+ flipCard();
|
|
165
|
+ gameRound(this.getPlayers(), countRound);
|
|
166
|
+ determineWinner(this.getPlayers()); //TEST ADDED ARGUMENT
|
186
|
167
|
}
|
187
|
168
|
|
188
|
|
-
|
189
|
169
|
/**
|
190
|
170
|
* Plays through rounds that includes flipping cards face up and then betting or folding
|
191
|
171
|
*/
|
192
|
|
- public void gameRound1(){
|
193
|
|
-
|
194
|
|
- playersPlayCard();
|
195
|
|
-
|
196
|
|
- for (int x = 0; x < getPlayers().size(); x++) { //Betting round or fold
|
197
|
|
- CardPlayer player = super.getPlayers().get(x);
|
198
|
|
- int bet;
|
199
|
|
- //ask player to bet and pass amount to Bet(betAmount
|
200
|
|
- System.out.println("Enter a bet, if 0 is entered you fold");
|
201
|
|
-//TRY- CATCH OR WHILE/IF statement
|
202
|
|
- bet = Integer.parseInt(scanner.next());
|
203
|
|
- if (bet == 0){
|
204
|
|
- System.out.println(player.getPlayer().getName() + " folds.");
|
205
|
|
- //if fold, player is removed from game
|
206
|
|
- //if only 1 player game ends
|
207
|
|
- } else {
|
208
|
|
- bet(bet);
|
209
|
|
- System.out.println(player.getPlayer().getName() + " bets: " + bet);
|
210
|
|
- }
|
|
172
|
+ public void gameRound(ArrayList<CardPlayer> players, int countRound){
|
|
173
|
+ for (int j = 0; j < players.size(); j++) {
|
|
174
|
+ playCard(players.get(j).getPlayer(), players.get(j).getHand().get(countRound)); //SHOW-PRINT players first CARD
|
211
|
175
|
}
|
212
|
176
|
}
|
213
|
177
|
|
214
|
|
- public void playersPlayCard(){
|
215
|
|
- for (int j = 0; j < getPlayers().size(); j++) {
|
216
|
|
- CardPlayer player = super.getPlayers().get(j); //GET a player
|
217
|
|
- playCard(player.getPlayer(), player.getHand().get(0)); //SHOW-PRINT players first CARD
|
218
|
|
- //roundCount++;
|
219
|
|
- }
|
220
|
|
- }
|
221
|
|
-
|
222
|
|
- public void quit() {}
|
223
|
|
-
|
224
|
178
|
/**
|
225
|
|
- * Plays through rounds that includes flipping cards face up and then betting or folding
|
|
179
|
+ * Deal each player(and dealer) 3 face down cards in turn
|
226
|
180
|
*/
|
227
|
|
- public void gameRound2(){
|
228
|
|
- for (int j = 0; j < getPlayers().size(); j++) {
|
229
|
|
- CardPlayer player = super.getPlayers().get(j); //GET a player
|
230
|
|
- playCard(player.getPlayer(), player.getHand().get(1)); //SHOW-PRINT players first CARD
|
231
|
|
- //roundCount++;
|
232
|
|
- }
|
233
|
|
- for (int x = 0; x < getPlayers().size(); x++) { //Betting round or fold
|
234
|
|
- CardPlayer player = super.getPlayers().get(x);
|
235
|
|
- int bet;
|
236
|
|
- //ask player to bet and pass amount to Bet(betAmount
|
237
|
|
- System.out.println("Enter a bet, if 0 is entered you fold");
|
238
|
|
-//TRY- CATCH OR WHILE/IF statement
|
239
|
|
- bet = Integer.parseInt(scanner.next());
|
240
|
|
- if (bet == 0){
|
241
|
|
- System.out.println(player.getPlayer().getName() + " folds.");
|
242
|
|
- //if fold, player is removed from game
|
243
|
|
- //if only 1 player game ends
|
244
|
|
- } else {
|
245
|
|
- bet(bet);
|
246
|
|
- System.out.println(player.getPlayer().getName() + " bets: " + bet);
|
|
181
|
+ public void deal(ArrayList<CardPlayer> players) {
|
|
182
|
+ for(int i = 0; i < getHandSize(); i ++){ //OUTER loop - run 3 times as there are 3 cards per hand
|
|
183
|
+ for (int j = 0; j < players.size(); j++) { //INNER loop through each player
|
|
184
|
+ players.get(j).getHand().add(getDeck().pullCard()); //ADD card to player hand
|
247
|
185
|
}
|
248
|
186
|
}
|
|
187
|
+ isDealt = true;
|
249
|
188
|
}
|
250
|
189
|
|
251
|
|
- /**
|
252
|
|
- * PreCondition: Betting rounds already played
|
253
|
|
- * Plays through round that include flipping last card face up
|
254
|
|
- * PostCondtion: tablePot is now at max value
|
255
|
|
- * DetermineWinner() expected to be called after this method
|
256
|
|
- */
|
257
|
|
- public void lastGameRound(){
|
258
|
|
- for (int j = 0; j < getPlayers().size(); j++) {
|
259
|
|
- CardPlayer player = super.getPlayers().get(j); //GET a player
|
260
|
|
- playCard(player.getPlayer(), player.getHand().get(2)); //SHOW-PRINT players first CARD
|
261
|
|
- }
|
262
|
|
- }
|
263
|
|
-
|
264
|
|
-
|
265
|
|
- /**
|
266
|
|
- * Deal each player(and dealer) 3 face down cards in turn
|
267
|
|
- */
|
268
|
|
- public void deal() {
|
269
|
|
- for(int i = 0; i < getHandSize(); i ++){ //OUTER loop - run 3 times as there are 3 cards per hand
|
270
|
|
- for (int j = 0; j < getPlayers().size(); j++) { //INNER loop through each player
|
271
|
|
- Card card = super.getDeck().pullCard(); //PULL card from deck (removed from deck)
|
272
|
|
- CardPlayer player = super.getPlayers().get(j); //GET a player
|
273
|
|
- player.getHand().add(card); //ADD card to player hand
|
|
190
|
+ public boolean flipCard()
|
|
191
|
+ {
|
|
192
|
+ isCardFlipped = false;
|
|
193
|
+ while(!isCardFlipped) {
|
|
194
|
+ String input = "";
|
|
195
|
+ input = console.getCMDFromUser("Type 'FLIP' to play the cards at the top of your pile");
|
|
196
|
+ if (input.equals("flip")) {
|
|
197
|
+ isCardFlipped = true;
|
|
198
|
+ } else {
|
|
199
|
+ System.out.println("Sorry, I don't understand that command.");
|
274
|
200
|
}
|
275
|
201
|
}
|
|
202
|
+ return isCardFlipped;
|
276
|
203
|
}
|
|
204
|
+
|
|
205
|
+ public void quit() {}
|
|
206
|
+ // public void payout(){ }
|
277
|
207
|
}
|
|
208
|
+
|