|
@@ -17,11 +17,11 @@ public class BlackJack extends CardGame implements Gamble {
|
17
|
17
|
private Deck deck = new Deck();
|
18
|
18
|
private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
|
19
|
19
|
CardHand dealerHand = new CardHand();
|
20
|
|
- long bet;
|
21
|
|
-
|
22
|
20
|
|
23
|
21
|
public BlackJack(){
|
|
22
|
+ announceGameChoice();
|
24
|
23
|
readyPlayers();
|
|
24
|
+ placePlayerBets();
|
25
|
25
|
dealCards(2);
|
26
|
26
|
runGame();
|
27
|
27
|
}
|
|
@@ -39,54 +39,91 @@ public class BlackJack extends CardGame implements Gamble {
|
39
|
39
|
}
|
40
|
40
|
}
|
41
|
41
|
|
42
|
|
- public boolean play(BlackJackPlayer currentPlayer, long bet) {
|
43
|
|
- // this.bet = bet;
|
|
42
|
+ boolean continueGame = true;
|
|
43
|
+ public void play(BlackJackPlayer currentPlayer, long bet) {
|
|
44
|
+ boolean dealerWins = false;
|
44
|
45
|
|
45
|
|
- Console.println("The dealer is dealing cards to the players.");
|
|
46
|
+ evalBlackjack(); // all players evaluate hand for blackjack. Winners are paid out and removed.
|
|
47
|
+ dealerWins = dealerCheck(); //TO ADD: if both player and dealer have blackjack, player keeps bet
|
|
48
|
+ if (dealerWins == true) continueGame = false; // all players lose
|
46
|
49
|
|
47
|
|
- for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
|
48
|
|
- placeBet(p);
|
49
|
|
- }
|
|
50
|
+ while (continueGame == true) {
|
|
51
|
+ dealerDraw();
|
50
|
52
|
|
51
|
|
- dealerHand.add(deck.removeFirst());
|
52
|
|
- dealerHand.add(deck.removeFirst());
|
|
53
|
+ Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
|
|
54
|
+
|
|
55
|
+ playersTurn();
|
|
56
|
+
|
|
57
|
+ if (blackJackPlayers.size() < 1) continueGame = false;
|
|
58
|
+
|
|
59
|
+ continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
|
|
60
|
+
|
|
61
|
+ evalWinner();
|
|
62
|
+
|
|
63
|
+ continueGame = false;
|
|
64
|
+ }
|
53
|
65
|
|
54
|
|
- Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
|
|
66
|
+ if (dealerWins == true) {
|
|
67
|
+ dealerCollectAll();
|
|
68
|
+ }
|
55
|
69
|
|
|
70
|
+ }
|
56
|
71
|
|
|
72
|
+ public boolean dealerCheck() {
|
57
|
73
|
if (getSum(dealerHand) == 21){
|
58
|
|
- Console.println("Dealer has +," + dealerHand.display());
|
59
|
|
- Console.println("You have " + currentPlayer.getHand().display());
|
|
74
|
+ Console.println("Dealer has " + dealerHand.display());
|
60
|
75
|
Console.println("Dealer has blackjack. Dealer wins.");
|
61
|
|
- for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
62
|
|
- evaluateBet(p.getP(), bet);
|
63
|
|
- }
|
|
76
|
+ return true;
|
64
|
77
|
}
|
65
|
|
- evalBlackjack();
|
|
78
|
+ return false;
|
|
79
|
+ }
|
66
|
80
|
|
67
|
|
- playersTurn();
|
|
81
|
+ public void dealerCollectAll() {
|
|
82
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
|
83
|
+ evaluateBet(p, -p.getBet());
|
|
84
|
+ }
|
|
85
|
+ }
|
68
|
86
|
|
|
87
|
+ public void payoutAll() {
|
|
88
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
|
89
|
+ long winnings = p.getBet()*2;
|
|
90
|
+ evaluateBet(p, winnings);
|
|
91
|
+ }
|
|
92
|
+ }
|
69
|
93
|
|
70
|
|
- if (dealersTurn()) return false;
|
|
94
|
+ public void dealerCollect(BlackJackPlayer p) {
|
|
95
|
+ evaluateBet(p, -p.getBet());
|
|
96
|
+ }
|
71
|
97
|
|
72
|
|
- return evalWinner(currentPlayer, bet);
|
|
98
|
+ public void placePlayerBets() {
|
|
99
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
|
|
100
|
+ Console.println(p.getName());
|
|
101
|
+ placeBet(p);
|
|
102
|
+ }
|
|
103
|
+ }
|
73
|
104
|
|
|
105
|
+ public void dealerDraw() {
|
|
106
|
+ dealerHand.add(deck.removeFirst());
|
|
107
|
+ dealerHand.add(deck.removeFirst());
|
74
|
108
|
}
|
75
|
109
|
|
76
|
|
- public boolean evalWinner(BlackJackPlayer currentPlayer, long bet) {
|
77
|
|
- if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
|
78
|
|
- Console.println("Dealer wins on a tie.");
|
79
|
|
- evaluateBet(currentPlayer.getP(), -bet);
|
80
|
|
- return false;
|
81
|
|
- } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
|
82
|
|
- Console.println("Dealer wins.");
|
83
|
|
- evaluateBet(currentPlayer.getP(), -bet);
|
84
|
|
- return false;
|
85
|
|
- } else {
|
86
|
|
- Console.println("You win!");
|
87
|
|
- evaluateBet(currentPlayer.getP(), bet);
|
88
|
|
- return false;
|
|
110
|
+ public boolean evalWinner() {
|
|
111
|
+ for (BlackJackPlayer currentPlayer : blackJackPlayers) {
|
|
112
|
+ if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
|
|
113
|
+ Console.println(String.format("Against %s's hand, dealer wins on a tie.", currentPlayer.getP().getName()));
|
|
114
|
+ evaluateBet(currentPlayer, -currentPlayer.getBet());
|
|
115
|
+ return false;
|
|
116
|
+ } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
|
|
117
|
+ Console.println(String.format("Against %s's hand, dealer wins."), currentPlayer.getP().getName());
|
|
118
|
+ evaluateBet(currentPlayer, -currentPlayer.getBet());
|
|
119
|
+ return false;
|
|
120
|
+ } else {
|
|
121
|
+ Console.println(String.format("%s, you win!", currentPlayer.getP().getName()));
|
|
122
|
+ evaluateBet(currentPlayer, currentPlayer.getBet());
|
|
123
|
+ return false;
|
|
124
|
+ }
|
89
|
125
|
}
|
|
126
|
+ return true;
|
90
|
127
|
}
|
91
|
128
|
|
92
|
129
|
public boolean dealersTurn() {
|
|
@@ -100,36 +137,50 @@ public class BlackJack extends CardGame implements Gamble {
|
100
|
137
|
|
101
|
138
|
if (getSum(dealerHand) > 21) {
|
102
|
139
|
Console.println("Dealer busted. You win.");
|
103
|
|
- return true;
|
|
140
|
+ payoutAll();
|
|
141
|
+ return false;
|
104
|
142
|
}
|
105
|
|
- return false;
|
|
143
|
+
|
|
144
|
+ return true;
|
106
|
145
|
}
|
107
|
146
|
|
108
|
147
|
public void playersTurn() {
|
|
148
|
+ BlackJackPlayer currentPlayer;
|
109
|
149
|
|
110
|
|
- for (BlackJackPlayer currentPlayer : this.blackJackPlayers) {
|
|
150
|
+ for (int i = 0; i < blackJackPlayers.size(); i++) {
|
|
151
|
+ currentPlayer = blackJackPlayers.get(i);
|
111
|
152
|
|
112
|
153
|
Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
|
113
|
|
-
|
114
|
|
- Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
|
|
154
|
+ displaySumPlayerHand(currentPlayer);
|
115
|
155
|
|
116
|
156
|
hitOrStand(currentPlayer);
|
|
157
|
+ }
|
|
158
|
+ }
|
117
|
159
|
|
118
|
|
- Console.println("You have: " + currentPlayer.getHand().display());
|
119
|
|
- Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
|
120
|
|
-
|
121
|
|
- if (getSum(currentPlayer.getHand()) > 21){
|
122
|
|
- Console.println("You busted. House wins.");
|
123
|
|
- }
|
|
160
|
+ private boolean playerBust(BlackJackPlayer currentPlayer) {
|
|
161
|
+ if (getSum(currentPlayer.getHand()) > 21){
|
|
162
|
+ Console.println(currentPlayer.getP().getName() + " busted. House wins.");
|
|
163
|
+ dealerCollect(currentPlayer);
|
|
164
|
+ blackJackPlayers.remove(currentPlayer);
|
|
165
|
+ return false;
|
124
|
166
|
}
|
|
167
|
+ return true;
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ private void displaySumPlayerHand(BlackJackPlayer currentPlayer) {
|
|
171
|
+ Console.println(currentPlayer.getP().getName() + ", you have: " + currentPlayer.getHand().display() + "\nThis hand is worth " + getSum(currentPlayer.getHand()));
|
125
|
172
|
}
|
126
|
173
|
|
127
|
174
|
public void hitOrStand(BlackJackPlayer currentPlayer) {
|
128
|
|
- String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
|
129
|
175
|
|
130
|
176
|
while (getSum(currentPlayer.getHand()) < 21) {
|
|
177
|
+ String hitOrStand = Console.getStringInput(currentPlayer.getP().getName() + ", do you want to Hit or Stand?\nEnter H for Hit or S for Stand");
|
|
178
|
+
|
131
|
179
|
if (hitOrStand.equalsIgnoreCase("H")) {
|
132
|
180
|
dealCards(currentPlayer, 1);
|
|
181
|
+ Console.println(currentPlayer.getP().getName() + ", you have been dealt %s.", currentPlayer.getHand().get(currentPlayer.getHand().size()-1).getCard());
|
|
182
|
+ displaySumPlayerHand(currentPlayer);
|
|
183
|
+ continueGame = playerBust(currentPlayer);
|
133
|
184
|
} else if (hitOrStand.equalsIgnoreCase("S")) {
|
134
|
185
|
break;
|
135
|
186
|
} else {
|
|
@@ -139,17 +190,21 @@ public class BlackJack extends CardGame implements Gamble {
|
139
|
190
|
}
|
140
|
191
|
|
141
|
192
|
public void evalBlackjack() {
|
142
|
|
- for (BlackJackPlayer p: this.blackJackPlayers)
|
143
|
|
- if (getSum(p.getHand()) == 21){
|
144
|
|
- Console.println("Dealer has " + dealerHand.display());
|
145
|
|
- Console.println("You have " + p.getHand().display());
|
146
|
|
- Console.println("You have blackjack. You win.");
|
|
193
|
+ BlackJackPlayer p;
|
|
194
|
+ for (int i = 0; i < blackJackPlayers.size(); i++) {
|
|
195
|
+ p = blackJackPlayers.get(i);
|
|
196
|
+ if (getSum(p.getHand()) == 21) {
|
|
197
|
+ Console.println(p.getP().getName() + ", you have " + p.getHand().display());
|
|
198
|
+ Console.println(p.getP().getName() + ", you have blackjack. You win!");
|
|
199
|
+ evaluateBet(p, p.getBet());
|
|
200
|
+ blackJackPlayers.remove(i);
|
|
201
|
+ }
|
147
|
202
|
}
|
148
|
203
|
}
|
149
|
204
|
|
150
|
205
|
@Override
|
151
|
206
|
public void dealCards(int numberOfCards) {
|
152
|
|
-
|
|
207
|
+ Console.println("The dealer is dealing cards to the players.");
|
153
|
208
|
for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
|
154
|
209
|
for (int i = 0; i < numberOfCards; i++) {
|
155
|
210
|
Card card = deck.removeFirst();
|
|
@@ -167,11 +222,13 @@ public class BlackJack extends CardGame implements Gamble {
|
167
|
222
|
}
|
168
|
223
|
}
|
169
|
224
|
|
|
225
|
+ @Override
|
170
|
226
|
public void placeBet(Player player) {
|
171
|
|
- this.bet = Console.getLongInput("Please enter your bet.");
|
|
227
|
+ BlackJackPlayer blackJackPlayer = (BlackJackPlayer) player;
|
|
228
|
+ long bet = Console.getLongInput(String.format("%s, please enter your bet.", ((BlackJackPlayer) player).getP().getName()));
|
|
229
|
+ blackJackPlayer.setBet(bet);
|
172
|
230
|
}
|
173
|
231
|
|
174
|
|
-
|
175
|
232
|
public void evaluateBet(Player player, long payout) {
|
176
|
233
|
player.setChipBalance(player.getChipBalance()+payout);
|
177
|
234
|
}
|
|
@@ -183,7 +240,6 @@ public class BlackJack extends CardGame implements Gamble {
|
183
|
240
|
return card;
|
184
|
241
|
}
|
185
|
242
|
|
186
|
|
-
|
187
|
243
|
public int getSum(CardHand cardHand) {
|
188
|
244
|
int cardSum = 0;
|
189
|
245
|
boolean ace = false;
|
|
@@ -199,7 +255,7 @@ public class BlackJack extends CardGame implements Gamble {
|
199
|
255
|
}
|
200
|
256
|
cardSum = cardSum + cardValue;
|
201
|
257
|
}
|
202
|
|
- if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
|
|
258
|
+ if (cardHand.size() == 2 && ace && cardSum + 10 == 21) {
|
203
|
259
|
cardSum = cardSum + 10;
|
204
|
260
|
}
|
205
|
261
|
return cardSum;
|
|
@@ -207,7 +263,7 @@ public class BlackJack extends CardGame implements Gamble {
|
207
|
263
|
|
208
|
264
|
public void revealCard(){
|
209
|
265
|
Card newCard = drawCard();
|
210
|
|
- Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());
|
|
266
|
+ Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit().getCardGraphic());
|
211
|
267
|
dealerHand.add(newCard);
|
212
|
268
|
}
|
213
|
269
|
|