|
@@ -17,8 +17,6 @@ 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(){
|
24
|
22
|
readyPlayers();
|
|
@@ -39,54 +37,90 @@ public class BlackJack extends CardGame implements Gamble {
|
39
|
37
|
}
|
40
|
38
|
}
|
41
|
39
|
|
42
|
|
- public boolean play(BlackJackPlayer currentPlayer, long bet) {
|
43
|
|
- // this.bet = bet;
|
|
40
|
+ public void play(BlackJackPlayer currentPlayer, long bet) {
|
|
41
|
+ boolean continueGame = true;
|
|
42
|
+ boolean dealerWins = false;
|
44
|
43
|
|
45
|
|
- Console.println("The dealer is dealing cards to the players.");
|
|
44
|
+ placePlayerBets();
|
46
|
45
|
|
47
|
|
- for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
|
48
|
|
- placeBet();
|
49
|
|
- }
|
|
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
|
50
|
49
|
|
51
|
|
- dealerHand.add(deck.removeFirst());
|
52
|
|
- dealerHand.add(deck.removeFirst());
|
|
50
|
+ while (continueGame == true) {
|
|
51
|
+ dealerDraw();
|
|
52
|
+
|
|
53
|
+ Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
|
|
54
|
+
|
|
55
|
+ playersTurn();
|
|
56
|
+
|
|
57
|
+ continueGame = dealersTurn(); // IMPLEMENT: if dealer loses, all players should receive payout
|
|
58
|
+
|
|
59
|
+ evalWinner();
|
53
|
60
|
|
54
|
|
- Console.println("Dealer is showing a " + dealerHand.get(0).getCard()); // dealer reveals upcard
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ if (dealerWins == true) {
|
|
64
|
+ dealerCollectAll();
|
|
65
|
+ }
|
55
|
66
|
|
|
67
|
+ }
|
56
|
68
|
|
|
69
|
+ public boolean dealerCheck() {
|
57
|
70
|
if (getSum(dealerHand) == 21){
|
58
|
|
- Console.println("Dealer has +," + dealerHand.display());
|
59
|
|
- Console.println("You have " + currentPlayer.getHand().display());
|
|
71
|
+ Console.println("Dealer has " + dealerHand.display());
|
60
|
72
|
Console.println("Dealer has blackjack. Dealer wins.");
|
61
|
|
- for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
62
|
|
- evaluateBet(p.getP(), bet);
|
63
|
|
- }
|
|
73
|
+ return true;
|
64
|
74
|
}
|
65
|
|
- evalBlackjack();
|
|
75
|
+ return false;
|
|
76
|
+ }
|
66
|
77
|
|
67
|
|
- playersTurn();
|
|
78
|
+ public void dealerCollectAll() {
|
|
79
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
|
80
|
+ evaluateBet(p, -p.getBet());
|
|
81
|
+ }
|
|
82
|
+ }
|
68
|
83
|
|
|
84
|
+ public void payoutAll() {
|
|
85
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ // collects bet from all players
|
|
86
|
+ long winnings = p.getBet()*2;
|
|
87
|
+ evaluateBet(p, winnings);
|
|
88
|
+ }
|
|
89
|
+ }
|
69
|
90
|
|
70
|
|
- if (dealersTurn()) return false;
|
|
91
|
+ public void dealerCollect(BlackJackPlayer p) {
|
|
92
|
+ evaluateBet(p, -p.getBet());
|
|
93
|
+ }
|
71
|
94
|
|
72
|
|
- return evalWinner(currentPlayer, bet);
|
|
95
|
+ public void placePlayerBets() {
|
|
96
|
+ for (BlackJackPlayer p: this.blackJackPlayers){ //gets bet from each player
|
|
97
|
+ Console.println(p.getName());
|
|
98
|
+ placeBet(p);
|
|
99
|
+ }
|
|
100
|
+ }
|
73
|
101
|
|
|
102
|
+ public void dealerDraw() {
|
|
103
|
+ dealerHand.add(deck.removeFirst());
|
|
104
|
+ dealerHand.add(deck.removeFirst());
|
74
|
105
|
}
|
75
|
106
|
|
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;
|
|
107
|
+ public boolean evalWinner() {
|
|
108
|
+ for (BlackJackPlayer currentPlayer : blackJackPlayers) {
|
|
109
|
+ if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
|
|
110
|
+ Console.println("Dealer wins on a tie.");
|
|
111
|
+ evaluateBet(currentPlayer, -currentPlayer.getBet());
|
|
112
|
+ return false;
|
|
113
|
+ } else if (getSum(dealerHand) <= 21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
|
|
114
|
+ Console.println("Dealer wins.");
|
|
115
|
+ evaluateBet(currentPlayer, -currentPlayer.getBet());
|
|
116
|
+ return false;
|
|
117
|
+ } else {
|
|
118
|
+ Console.println("You win!");
|
|
119
|
+ evaluateBet(currentPlayer, currentPlayer.getBet());
|
|
120
|
+ return false;
|
|
121
|
+ }
|
89
|
122
|
}
|
|
123
|
+ return true;
|
90
|
124
|
}
|
91
|
125
|
|
92
|
126
|
public boolean dealersTurn() {
|
|
@@ -100,26 +134,30 @@ public class BlackJack extends CardGame implements Gamble {
|
100
|
134
|
|
101
|
135
|
if (getSum(dealerHand) > 21) {
|
102
|
136
|
Console.println("Dealer busted. You win.");
|
|
137
|
+ payoutAll();
|
103
|
138
|
return true;
|
104
|
139
|
}
|
|
140
|
+
|
105
|
141
|
return false;
|
106
|
142
|
}
|
107
|
143
|
|
108
|
144
|
public void playersTurn() {
|
|
145
|
+ BlackJackPlayer currentPlayer;
|
109
|
146
|
|
110
|
|
- for (BlackJackPlayer currentPlayer : this.blackJackPlayers) {
|
|
147
|
+ for (int i = 0; i < blackJackPlayers.size(); i++) {
|
|
148
|
+ currentPlayer = blackJackPlayers.get(i);
|
111
|
149
|
|
112
|
150
|
Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
|
113
|
|
-
|
114
|
151
|
Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
|
115
|
152
|
|
116
|
153
|
hitOrStand(currentPlayer);
|
117
|
154
|
|
118
|
|
- Console.println("You have: " + currentPlayer.getHand().display());
|
119
|
|
- Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
|
120
|
|
-
|
121
|
155
|
if (getSum(currentPlayer.getHand()) > 21){
|
|
156
|
+
|
122
|
157
|
Console.println("You busted. House wins.");
|
|
158
|
+ dealerCollect(currentPlayer);
|
|
159
|
+ blackJackPlayers.remove(currentPlayer);
|
|
160
|
+
|
123
|
161
|
}
|
124
|
162
|
}
|
125
|
163
|
}
|
|
@@ -139,17 +177,21 @@ public class BlackJack extends CardGame implements Gamble {
|
139
|
177
|
}
|
140
|
178
|
|
141
|
179
|
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.");
|
|
180
|
+ BlackJackPlayer p;
|
|
181
|
+ for (int i = 0; i < blackJackPlayers.size(); i++) {
|
|
182
|
+ p = blackJackPlayers.get(i);
|
|
183
|
+ if (getSum(p.getHand()) == 21) {
|
|
184
|
+ Console.println(p.getName() + ", you have " + p.getHand().display());
|
|
185
|
+ Console.println(p.getName() + ", you have blackjack. You win!");
|
|
186
|
+ evaluateBet(p, p.getBet());
|
|
187
|
+ blackJackPlayers.remove(i);
|
|
188
|
+ }
|
147
|
189
|
}
|
148
|
190
|
}
|
149
|
191
|
|
150
|
192
|
@Override
|
151
|
193
|
public void dealCards(int numberOfCards) {
|
152
|
|
-
|
|
194
|
+ Console.println("The dealer is dealing cards to the players.");
|
153
|
195
|
for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
|
154
|
196
|
for (int i = 0; i < numberOfCards; i++) {
|
155
|
197
|
Card card = deck.removeFirst();
|
|
@@ -167,11 +209,13 @@ public class BlackJack extends CardGame implements Gamble {
|
167
|
209
|
}
|
168
|
210
|
}
|
169
|
211
|
|
170
|
|
- public void placeBet() {
|
171
|
|
- this.bet = Console.getLongInput("Please enter your bet.");
|
|
212
|
+ @Override
|
|
213
|
+ public void placeBet(Player player) {
|
|
214
|
+ BlackJackPlayer blackJackPlayer = (BlackJackPlayer) player;
|
|
215
|
+ long bet = Console.getLongInput("Please enter your bet.");
|
|
216
|
+ blackJackPlayer.setBet(bet);
|
172
|
217
|
}
|
173
|
218
|
|
174
|
|
-
|
175
|
219
|
public void evaluateBet(Player player, long payout) {
|
176
|
220
|
player.setChipBalance(player.getChipBalance()+payout);
|
177
|
221
|
}
|
|
@@ -183,7 +227,6 @@ public class BlackJack extends CardGame implements Gamble {
|
183
|
227
|
return card;
|
184
|
228
|
}
|
185
|
229
|
|
186
|
|
-
|
187
|
230
|
public int getSum(CardHand cardHand) {
|
188
|
231
|
int cardSum = 0;
|
189
|
232
|
boolean ace = false;
|
|
@@ -199,7 +242,7 @@ public class BlackJack extends CardGame implements Gamble {
|
199
|
242
|
}
|
200
|
243
|
cardSum = cardSum + cardValue;
|
201
|
244
|
}
|
202
|
|
- if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
|
|
245
|
+ if (cardHand.size() == 2 && ace && cardSum + 10 == 21) {
|
203
|
246
|
cardSum = cardSum + 10;
|
204
|
247
|
}
|
205
|
248
|
return cardSum;
|