|
@@ -1,7 +1,11 @@
|
1
|
1
|
package io.zipcoder.casino.Leviathan.Games;
|
|
2
|
+
|
2
|
3
|
import io.zipcoder.casino.Leviathan.Interfaces.*;
|
3
|
4
|
import io.zipcoder.casino.Leviathan.*;
|
4
|
|
-import io.zipcoder.casino.Leviathan.Games.GameUtilities.*;
|
|
5
|
+import io.zipcoder.casino.Leviathan.Games.GameUtilities.*;
|
|
6
|
+
|
|
7
|
+import java.util.ArrayList;
|
|
8
|
+import java.util.List;
|
5
|
9
|
|
6
|
10
|
public class BlackJack extends CardGame implements Gambling {
|
7
|
11
|
|
|
@@ -9,200 +13,262 @@ public class BlackJack extends CardGame implements Gambling {
|
9
|
13
|
int wageAmount;
|
10
|
14
|
int totalChips;
|
11
|
15
|
Player aPlayer;
|
|
16
|
+ boolean playAgain = true;
|
12
|
17
|
|
13
|
|
- public BlackJack(Player aPlayer){
|
|
18
|
+ public BlackJack(Player aPlayer) {
|
14
|
19
|
this.aPlayer = aPlayer;
|
15
|
20
|
}
|
16
|
21
|
|
17
|
22
|
int aPlayerScore = Game.playerScore;
|
18
|
23
|
int aHouseScore = Game.houseScore;
|
19
|
24
|
|
20
|
|
- //assuming this to be a random card rank in int
|
21
|
|
- //Card aCard = new Card(Rank.ACE,Suit.CLUBS);
|
22
|
|
- //Rank aGetRank = aCard.getRank();
|
23
|
|
-
|
24
|
25
|
Deck deck = new Deck();
|
25
|
26
|
|
26
|
|
-
|
27
|
|
-
|
28
|
27
|
String playerDecision;
|
29
|
28
|
|
|
29
|
+ boolean win;
|
30
|
30
|
|
31
|
|
- public void playGame() {
|
32
|
|
-
|
33
|
|
- }
|
34
|
|
-
|
35
|
|
- public int drawASingleCard(int cardRank){
|
36
|
|
-
|
37
|
|
- Card card = deck.draw();
|
38
|
|
-
|
39
|
|
- Rank rank = card.getRank();
|
40
|
|
-
|
41
|
|
- int cardInt =rank.getValue();
|
|
31
|
+////////////////////////////////////////////////////////////////////////////////////
|
42
|
32
|
|
43
|
|
- return cardInt;
|
|
33
|
+ //draws the initial two cards
|
|
34
|
+ public List<Card> drawNewHand() {
|
|
35
|
+ List<Card> hand = new ArrayList<>();
|
|
36
|
+ hand.add(deck.draw());
|
|
37
|
+ hand.add(deck.draw());
|
|
38
|
+ return hand;
|
44
|
39
|
}
|
45
|
40
|
|
46
|
|
- //Player making the starting bet
|
47
|
|
- public int wageMoney() {
|
48
|
|
-
|
49
|
|
- totalChips=aPlayer.getTotalChips();
|
50
|
|
-
|
51
|
|
- wageAmount = aConsole.getIntInput("Please make your starting bet:");
|
52
|
|
- if (wageAmount <= totalChips) {
|
53
|
|
- aConsole.println("Your current bet amount is: " + wageAmount);
|
54
|
|
- return wageAmount;
|
|
41
|
+ //get the sum int value of the drawn cards
|
|
42
|
+ public int getHandTotal(List<Card> hand) {
|
|
43
|
+ int sum = 0;
|
|
44
|
+ int countAces = 0;
|
|
45
|
+
|
|
46
|
+ for (Card c : hand) {
|
|
47
|
+ Rank rank = c.getRank();
|
|
48
|
+ int value = rank.getValue();
|
|
49
|
+
|
|
50
|
+ if (value <= 10) {
|
|
51
|
+ sum += value;
|
|
52
|
+ }
|
|
53
|
+ if (rank.equals(Rank.JACK) || rank.equals(Rank.QUEEN) || rank.equals(Rank.KING)) {
|
|
54
|
+ sum += 10;
|
|
55
|
+ }
|
|
56
|
+ if (rank.equals(Rank.ACE)) {
|
|
57
|
+ sum += 0;
|
|
58
|
+ }
|
|
59
|
+ if (rank.equals(Rank.ACE)) {
|
|
60
|
+ countAces += 1;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ if (countAces == 0) {
|
|
64
|
+ return sum;
|
|
65
|
+ }
|
|
66
|
+ if ((sum + 11 + (countAces - 1)) <= 21) {
|
|
67
|
+ return sum + 11 + (countAces - 1);
|
55
|
68
|
} else {
|
56
|
|
- aConsole.println("Insufficient Chips!");
|
57
|
|
- //Recursion - this runs the method again and asks user to make the starting bet again
|
58
|
|
- return wageMoney();
|
|
69
|
+ return sum + countAces;
|
59
|
70
|
}
|
60
|
71
|
}
|
61
|
72
|
|
62
|
|
- //initial player card value
|
63
|
|
- private int startPlayerHand() {
|
64
|
|
-
|
65
|
|
-// Card card = deck.draw();
|
66
|
|
-//
|
67
|
|
-// Rank rank = card.getRank();
|
68
|
|
-//
|
69
|
|
-// int cardInt =rank.getRank();
|
70
|
|
-
|
71
|
|
- //aPlayerScore = drawASingleCard();
|
|
73
|
+ //lets the player know of the starting hand
|
|
74
|
+ public String handToString(List<Card> hand) {
|
|
75
|
+ StringBuilder builder = new StringBuilder();
|
|
76
|
+ for (Card c : hand) {
|
|
77
|
+ String cardString = (c.getRank() + " of " + c.getSuit());
|
|
78
|
+ builder.append(cardString).append("\n");
|
|
79
|
+ }
|
|
80
|
+ return builder.toString();
|
|
81
|
+ }
|
72
|
82
|
|
|
83
|
+ //lets the player know one of house's starting hand
|
|
84
|
+ public String houseHandToString(List<Card> hand, int cardsToShow) {
|
|
85
|
+ StringBuilder builder = new StringBuilder();
|
|
86
|
+ for (int i = 0; i < cardsToShow; i++) {
|
|
87
|
+ builder.append(hand.get(i).getRank() + " of " + hand.get(i).getSuit() + "\n");
|
|
88
|
+ }
|
|
89
|
+ return builder.toString();
|
|
90
|
+ }
|
73
|
91
|
|
74
|
92
|
|
75
|
|
- return aPlayerScore; //= aGetRank + aGetRank;
|
76
|
|
- }
|
|
93
|
+ //draws a singe card
|
|
94
|
+ public Card drawSingleCard() {
|
77
|
95
|
|
78
|
|
- //initial house card value
|
79
|
|
- public int startHouseHand() {
|
80
|
|
- return aHouseScore; //= aGetRank + aGetRank;
|
|
96
|
+ return deck.draw();
|
81
|
97
|
}
|
82
|
98
|
|
|
99
|
+ //Gets the int value of a single drawn card
|
|
100
|
+ public int getSingleCardValue(List<Card> hand) {
|
|
101
|
+ int cardValue = 0;
|
83
|
102
|
|
84
|
|
- //ask for player's next move
|
85
|
|
- public String blackJack(String playerDecision) {
|
86
|
|
- if(startPlayerHand() < 21){
|
87
|
|
- playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand\nRaise Bet");
|
|
103
|
+ for (Card c : hand) {
|
|
104
|
+ Rank rank = c.getRank();
|
|
105
|
+ int value = rank.getValue();
|
|
106
|
+ cardValue += value;
|
88
|
107
|
}
|
89
|
|
- return playerDecision;
|
|
108
|
+ return cardValue;
|
90
|
109
|
}
|
91
|
110
|
|
|
111
|
+ public String drawnCardToString(Card card) {
|
|
112
|
+ return card.getRank() + " of " + card.getSuit();
|
|
113
|
+ }
|
92
|
114
|
|
|
115
|
+////////////////////////////////////////////////////////////////////////////////////
|
93
|
116
|
|
94
|
|
- //Hit
|
95
|
|
- public int hit(int playerHand) {
|
96
|
|
-
|
|
117
|
+ public void playGame() {
|
97
|
118
|
|
98
|
|
- return 0;
|
|
119
|
+ while (playAgain == true) {
|
|
120
|
+
|
|
121
|
+ aConsole.println("*** Welcome " + this.aPlayer.getName() +
|
|
122
|
+ ", let’s play BlackJack! ***\n");
|
|
123
|
+
|
|
124
|
+ int wageAmount = wageMoney();
|
|
125
|
+
|
|
126
|
+ //draw a fresh hand
|
|
127
|
+ List<Card> playerHand = drawNewHand();
|
|
128
|
+
|
|
129
|
+ //get the player's hand total
|
|
130
|
+ int handTotal = getHandTotal(playerHand);
|
|
131
|
+
|
|
132
|
+ //update player score with hand total
|
|
133
|
+ aPlayerScore = handTotal;
|
|
134
|
+
|
|
135
|
+ //show the player their hand
|
|
136
|
+ String handString = handToString(playerHand);
|
|
137
|
+ aConsole.println("\n You have drawn: \n" + handString);
|
|
138
|
+
|
|
139
|
+ //display hand total
|
|
140
|
+ aConsole.println("Your current hand value: " + aPlayerScore + "\n");
|
|
141
|
+
|
|
142
|
+ //draw a fresh hand for house
|
|
143
|
+ List<Card> houseHand = drawNewHand();
|
|
144
|
+
|
|
145
|
+ //get the house hand total
|
|
146
|
+ int houseHandTotal = getHandTotal(houseHand);
|
|
147
|
+
|
|
148
|
+ //update house score with hand total
|
|
149
|
+ aHouseScore = houseHandTotal;
|
|
150
|
+
|
|
151
|
+ //show the player one of house's card
|
|
152
|
+ //I need to figure out how I could only show one card
|
|
153
|
+ String houseHandString = houseHandToString(houseHand, 1);
|
|
154
|
+ aConsole.println("\n The house has drawn: \n" + houseHandString +
|
|
155
|
+ "And has one card faced down");
|
|
156
|
+
|
|
157
|
+ //Decision
|
|
158
|
+ while (aPlayerScore < 21) {
|
|
159
|
+ //playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand\nRaise Bet");
|
|
160
|
+ playerDecision = aConsole.getStringInput("Please make a call:\nHit\nStand");
|
|
161
|
+
|
|
162
|
+ //Hit
|
|
163
|
+ if (playerDecision.equalsIgnoreCase("Hit")) {
|
|
164
|
+ playerHit(playerHand);
|
|
165
|
+ }
|
|
166
|
+ if (playerDecision.equalsIgnoreCase("Stand")) {
|
|
167
|
+
|
|
168
|
+ //House start drawing cards
|
|
169
|
+ houseDraws(houseHand);
|
|
170
|
+ break;
|
|
171
|
+ }
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ //House winning situation
|
|
175
|
+ if (aPlayerScore > 21) {
|
|
176
|
+ aConsole.println("Bust!");
|
|
177
|
+ win = false;
|
|
178
|
+ betChages(bet);
|
|
179
|
+ aConsole.println("Your current available chips: \n" + aPlayer.getTotalChips());
|
|
180
|
+ }else if ((aHouseScore >= aPlayerScore && aHouseScore < 21) || aHouseScore == 21) {
|
|
181
|
+ aConsole.println("You lose\n" + "Your score: " + aPlayerScore +
|
|
182
|
+ "\nHouse score: " + aHouseScore);
|
|
183
|
+ aConsole.println("\n Dealer's hand: \n" + handToString(houseHand));
|
|
184
|
+ win = false;
|
|
185
|
+ betChages(bet);
|
|
186
|
+ aConsole.println("Your current available chips: \n" + aPlayer.getTotalChips());
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ //Player winning situation
|
|
190
|
+ else if (aHouseScore < 21) {
|
|
191
|
+ aConsole.println("You win!");
|
|
192
|
+ win = true;
|
|
193
|
+ betChages(bet);
|
|
194
|
+ aConsole.println("Your current available chips: \n" + aPlayer.getTotalChips());
|
|
195
|
+ } else {
|
|
196
|
+ //aConsole.println("You win!");
|
|
197
|
+ aConsole.println("You win!\n" + "Your score: " + aPlayerScore +
|
|
198
|
+ "\nHouse score: " + aHouseScore);
|
|
199
|
+ win = true;
|
|
200
|
+ betChages(bet);
|
|
201
|
+ aConsole.println("Your current available chips: \n" + aPlayer.getTotalChips());
|
|
202
|
+ }
|
|
203
|
+ if ((aPlayer.getTotalChips() == 0) || aConsole.yesOrNo("Would you like to play again?").equalsIgnoreCase("no")) {
|
|
204
|
+ playAgain = false;
|
|
205
|
+ }
|
|
206
|
+ }
|
99
|
207
|
}
|
100
|
208
|
|
|
209
|
+ private void playerHit(List<Card> playerHand) {
|
|
210
|
+ //draw a new card
|
|
211
|
+ Card drawSingleCard = drawSingleCard();
|
|
212
|
+ playerHand.add(drawSingleCard);
|
101
|
213
|
|
102
|
|
- //Stand
|
103
|
|
- public int stand(int playerHand) {
|
|
214
|
+ //update player score with card value
|
|
215
|
+ aPlayerScore = getHandTotal(playerHand);
|
104
|
216
|
|
|
217
|
+ //show the newly drawn card
|
|
218
|
+ String newCardString = drawnCardToString(drawSingleCard);
|
|
219
|
+ aConsole.println("\n You have drawn: \n" + newCardString);
|
105
|
220
|
|
106
|
|
- return 0;
|
|
221
|
+ //display updated hand total
|
|
222
|
+ aConsole.println("Your current hand value: " + aPlayerScore + "\n");
|
107
|
223
|
}
|
108
|
224
|
|
109
|
|
- //Raise bet
|
110
|
|
- public int raiseBet(int wageAmount) {
|
|
225
|
+ private void houseDraws(List<Card> houseHand) {
|
|
226
|
+ while (aPlayerScore > aHouseScore && aHouseScore < 21) {
|
|
227
|
+ //draw a new card
|
|
228
|
+ Card drawSingleCard = drawSingleCard();
|
|
229
|
+ houseHand.add(drawSingleCard);
|
111
|
230
|
|
|
231
|
+ //update house score with card value
|
|
232
|
+ aHouseScore = getHandTotal(houseHand);
|
112
|
233
|
|
113
|
|
- return 0;
|
|
234
|
+ //show the newly drawn card
|
|
235
|
+ String newCardString = drawnCardToString(drawSingleCard);
|
|
236
|
+ aConsole.println("\n House have drawn: \n" + newCardString);
|
|
237
|
+ }
|
114
|
238
|
}
|
115
|
239
|
|
|
240
|
+ int bet;
|
|
241
|
+ public int wageMoney() {
|
|
242
|
+ //int bet;
|
|
243
|
+ totalChips = aPlayer.getTotalChips();
|
|
244
|
+ bet = aConsole.getIntInput("Please make your starting bet:\n" +
|
|
245
|
+ "You currently have: " + aPlayer.getTotalChips() + " chips.");
|
|
246
|
+ if (bet > 0 && bet <= totalChips) {
|
|
247
|
+ aConsole.println("Your current bet amount is: " + bet);
|
|
248
|
+ return bet;
|
|
249
|
+ } else if (bet == 0 || bet < 0) {
|
|
250
|
+ aConsole.println("You need to bet at least 1 chip");
|
|
251
|
+ return wageMoney();
|
|
252
|
+ } if(bet > totalChips) {
|
|
253
|
+ aConsole.println("Insufficient Chips!");
|
116
|
254
|
|
|
255
|
+ //return wageMoney();
|
|
256
|
+ return wageMoney();
|
|
257
|
+ }
|
|
258
|
+ return bet;
|
|
259
|
+ }
|
117
|
260
|
|
118
|
|
-
|
119
|
|
-
|
120
|
|
-
|
121
|
|
-
|
|
261
|
+ public void betChages(int wageAmount){
|
|
262
|
+ totalChips = aPlayer.getTotalChips();
|
|
263
|
+ if(win == true){
|
|
264
|
+ aPlayer.setTotalChips(totalChips + wageAmount);
|
|
265
|
+ } else{
|
|
266
|
+ aPlayer.setTotalChips(totalChips - wageAmount);
|
|
267
|
+ }
|
|
268
|
+ }
|
122
|
269
|
}
|
123
|
270
|
|
124
|
271
|
|
125
|
|
-// public boolean playGame(Player aPlayer) {
|
126
|
|
-// return false;
|
127
|
|
-// }
|
128
|
|
-//
|
129
|
|
-//
|
130
|
|
-// public int wageMoney() {
|
131
|
|
-// return 0;
|
132
|
|
-// }
|
133
|
|
-//
|
134
|
|
-//
|
135
|
|
-//
|
136
|
|
-// private static final String HOLD_CONSTANT = "hold";
|
137
|
|
-//
|
138
|
|
-//
|
139
|
|
-// public void blackJack (String[] args){
|
140
|
|
-//
|
141
|
|
-// //user input for Int
|
142
|
|
-// int betAmount = 0;
|
143
|
|
-//
|
144
|
|
-// //user input for String
|
145
|
|
-// String decision;
|
146
|
|
-//
|
147
|
|
-//
|
148
|
|
-//
|
149
|
|
-//
|
150
|
|
-//
|
151
|
|
-//
|
152
|
|
-// int aPlayerScore = Game.playerScore;
|
153
|
|
-//
|
154
|
|
-// int aHouseScore = Game.houseScore;
|
155
|
|
-//
|
156
|
|
-//
|
157
|
|
-//
|
158
|
|
-//
|
159
|
|
-// //How do I make this work??
|
160
|
|
-// //Card aCard = new Card();
|
161
|
|
-// //or
|
162
|
|
-// //int aCard = Card.getRank;
|
163
|
|
-//
|
164
|
|
-//
|
165
|
|
-// ////////public int startTheGame(int playerScore, int houseScore, int getRank)
|
166
|
|
-//
|
167
|
|
-// //playerScore = Card.getRank() + Card.getRank();
|
168
|
|
-//
|
169
|
|
-// //houseScore = Card.getRank() + Card.getRank();
|
170
|
|
-//
|
171
|
|
-//
|
172
|
|
-//
|
173
|
|
-//
|
174
|
|
-//
|
175
|
|
-// while(!HOLD_CONSTANT.equals(decision) || playerScore < 21) {
|
176
|
|
-//
|
177
|
|
-// //IF statement that asks for userInput after each loop(?)
|
178
|
|
-// //add sum values of two cards to playerScore hand
|
179
|
|
-// //add sum values of two cards to houseScore hand
|
180
|
|
-//
|
181
|
|
-// decision = aConsole.getStringInput("Please make a call:\nHit\nStand");
|
182
|
|
-//
|
183
|
|
-//
|
184
|
|
-// If(decision.equals("Hit")) {
|
185
|
|
-//
|
186
|
|
-// If(decision.equals("Ace") && playerScore > 10) {
|
187
|
|
-// playerScore + 1;
|
188
|
|
-// }
|
189
|
|
-// else{
|
190
|
|
-// playerScore + 11;
|
191
|
|
-// }
|
192
|
|
-// }
|
193
|
|
-// else if(aConsole.getStringInput().equals("Stand"))
|
194
|
|
-//
|
195
|
|
-//
|
196
|
|
-//
|
197
|
|
-// }
|
198
|
|
-//
|
199
|
|
-// If(playerScore > 21){
|
200
|
|
-//
|
201
|
|
-// }
|
202
|
|
-//
|
203
|
|
-//
|
204
|
|
-// }
|
205
|
|
-
|
206
|
272
|
|
207
|
273
|
|
208
|
274
|
|