|
@@ -1,21 +1,24 @@
|
1
|
1
|
package io.zipcoder.casino;
|
2
|
2
|
import java.util.ArrayList;
|
|
3
|
+import java.util.Arrays;
|
3
|
4
|
|
4
|
5
|
public class BlackJack {
|
5
|
6
|
private ArrayList<Card> playerHand = new ArrayList<Card>();
|
6
|
7
|
private ArrayList<Card> dealerHand = new ArrayList<Card>();
|
7
|
8
|
private int currentBet;
|
8
|
9
|
private boolean playingBJ = false;
|
|
10
|
+ private boolean playingHands = true;
|
|
11
|
+ private boolean askToPlayAgain = false;
|
9
|
12
|
private CardDeck deck = new CardDeck();
|
10
|
13
|
private Player player;
|
11
|
14
|
|
12
|
15
|
public BlackJack() {
|
13
|
|
- Player player = new Player("Eugene");
|
|
16
|
+ this.player = new Player("Eugene");
|
14
|
17
|
}
|
15
|
18
|
|
16
|
19
|
public void start() {
|
17
|
20
|
playingBJ = true;
|
18
|
|
-
|
|
21
|
+ this.deck.shuffle();
|
19
|
22
|
}
|
20
|
23
|
|
21
|
24
|
private void dealToPlayerHand(int amountOfCards) {
|
|
@@ -30,10 +33,55 @@ public class BlackJack {
|
30
|
33
|
}
|
31
|
34
|
}
|
32
|
35
|
|
|
36
|
+ private void setPlayingHands(boolean bool) {
|
|
37
|
+ this.playingHands = bool;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ private String getPlayerHand() {
|
|
41
|
+ return Arrays.toString(this.playerHand.toArray());
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private String getDealerHand() {
|
|
45
|
+ return Arrays.toString(this.dealerHand.toArray());
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ private String showDealerHand() {
|
|
49
|
+ Card[] filteredArray = this.dealerHand.toArray(new Card[this.dealerHand.size()]);
|
|
50
|
+ filteredArray[0] = new Card();
|
|
51
|
+ return Arrays.toString(filteredArray);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ private int getCurrentBet() {
|
|
56
|
+ return this.currentBet;
|
|
57
|
+ }
|
|
58
|
+
|
33
|
59
|
private void setCurrentBet(int bet) {
|
34
|
60
|
this.currentBet = bet;
|
35
|
61
|
}
|
36
|
62
|
|
|
63
|
+ private void setAskToPlayAgain(boolean bool) {
|
|
64
|
+ this.askToPlayAgain = bool;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ private void resetHands() {
|
|
68
|
+ this.playerHand = new ArrayList<Card>();
|
|
69
|
+ this.dealerHand = new ArrayList<Card>();
|
|
70
|
+ }
|
|
71
|
+ private void startHands() {
|
|
72
|
+ this.deck = new CardDeck();
|
|
73
|
+ this.deck.shuffle();
|
|
74
|
+ resetHands();
|
|
75
|
+ dealToPlayerHand(2);
|
|
76
|
+ dealToDealerHand(2);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ private void startNextHand() {
|
|
80
|
+ setCurrentBet(0);
|
|
81
|
+ setAskToPlayAgain(true);
|
|
82
|
+ setPlayingHands(false);
|
|
83
|
+ }
|
|
84
|
+
|
37
|
85
|
private int getCardValue(Card card) {
|
38
|
86
|
Rank rank = card.getRank();
|
39
|
87
|
int calcValue = 0;
|
|
@@ -80,59 +128,95 @@ public class BlackJack {
|
80
|
128
|
private int calcHand(ArrayList<Card> hand) {
|
81
|
129
|
int totalScore = 0;
|
82
|
130
|
for (int i = 0; i < hand.size(); i++) {
|
83
|
|
- totalScore = getCardValue(hand.get(i));
|
|
131
|
+ totalScore += getCardValue(hand.get(i));
|
84
|
132
|
}
|
85
|
133
|
return totalScore;
|
86
|
134
|
}
|
87
|
135
|
|
88
|
136
|
public void startGame() {
|
89
|
|
- while(playingBJ)
|
90
|
|
- {
|
91
|
|
- Console.output("Welcome to BlackJack " + player.getName() + ". I am your dealer Vince and I am here to take your money!");
|
92
|
|
- int bet = Console.numberFromString(Console.askForInput("How much would you like to bet?"));
|
93
|
|
- setCurrentBet(bet);
|
94
|
|
- dealToPlayerHand(2);
|
95
|
|
- dealToDealerHand(2);
|
96
|
|
- if (calcHand(dealerHand) == 21) {
|
97
|
|
- Console.output("Dealer has BlackJack! You lose!");
|
98
|
|
- player.subtractFromBankroll(currentBet);
|
99
|
|
- currentBet = 0;
|
100
|
|
- deck.draw();
|
101
|
|
- } else if (calcHand(playerHand) == 21) {
|
102
|
|
- Console.output("You have BlackJack! You win!");
|
103
|
|
- winBet((currentBet * 3) / 2);
|
104
|
|
- currentBet = 0;
|
105
|
|
- deck.deal();
|
106
|
|
-
|
|
137
|
+ start();
|
|
138
|
+ Console.output("Starting BlackJack...");
|
|
139
|
+ while (playingBJ) {
|
|
140
|
+ while (askToPlayAgain) {
|
|
141
|
+ String answer = Console.askForInput("Would you like to play again? (Y/N)");
|
|
142
|
+ if (answer.equals("y")) {
|
|
143
|
+ setPlayingHands(true);
|
|
144
|
+ setAskToPlayAgain(false);
|
|
145
|
+ } else {
|
|
146
|
+ setPlayingHands(false);
|
|
147
|
+ setAskToPlayAgain(false);
|
|
148
|
+ }
|
107
|
149
|
}
|
108
|
|
- while (calcHand(playerHand) < 21) {
|
109
|
|
- String action = Console.askForInput("Dealer is showing: " + "You have " + playerHand + ". Please enter 'hit' or 'stay'.");
|
110
|
|
- if (action.equals("hit")) {
|
111
|
|
- drawCard();
|
112
|
|
- cardValue += playerHand;
|
113
|
|
- if (calcHand(playerHand) > 21) {
|
114
|
|
- Console.output("You have " + playerHand + " and bust, you suck!");
|
115
|
|
- loseBet(currentBet);
|
116
|
|
- currentBet = 0;
|
|
150
|
+ while (playingHands) {
|
|
151
|
+ int bet = Console.numberFromString(Console.askForInput("Starting bet amount: "));
|
|
152
|
+ setCurrentBet(bet);
|
|
153
|
+ startHands();
|
|
154
|
+
|
|
155
|
+ Console.output(getPlayerHand());
|
|
156
|
+
|
|
157
|
+ if (calcHand(dealerHand) == 21) {
|
|
158
|
+ Console.output("Dealer has BlackJack! You lose!");
|
|
159
|
+ player.subtractFromBankroll(currentBet);
|
|
160
|
+ startNextHand();
|
|
161
|
+ } else if (calcHand(playerHand) == 21) {
|
|
162
|
+ Console.output("You have BlackJack! You win!");
|
|
163
|
+ player.addToBankroll((currentBet * 3) / 2);
|
|
164
|
+ startNextHand();
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ int stay = 0;
|
|
168
|
+ while ((calcHand(playerHand) <= 21 && calcHand(dealerHand) <= 21) && stay < 2) {
|
|
169
|
+ System.out.println(stay);
|
|
170
|
+ stay = 0;
|
|
171
|
+ String action = Console.askForInput("Hit or Stay? (H/S)");
|
|
172
|
+
|
|
173
|
+ if (action.equals("h")) {
|
|
174
|
+ dealToPlayerHand(1);
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ if (action.equals("s")) {
|
|
178
|
+ stay++;
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ if (calcHand(dealerHand) < 17) {
|
|
182
|
+ dealToDealerHand(1);
|
|
183
|
+ } else {
|
|
184
|
+ stay++;
|
117
|
185
|
}
|
118
|
|
- } else if (action.equals("stay")) {
|
119
|
|
- break;
|
|
186
|
+
|
|
187
|
+ Console.output("Player Hand: " + getPlayerHand());
|
|
188
|
+ Console.output("Dealer Hand: " + showDealerHand());
|
120
|
189
|
}
|
121
|
|
- }
|
122
|
|
- while (calcHand(dealerHand) < 17 && calcHand(dealerHand) < 21) {
|
123
|
|
- Console.output("Dealer has " + dealerHand + " and hits.");
|
124
|
|
- drawcard();
|
125
|
|
- cardValue += dealerHand;
|
126
|
|
- if (calcHand(dealerHand > 21) {
|
127
|
|
- Console.output("Dealer has " + dealerHand + " and busts, you win!");
|
128
|
|
- winBet(currentBet);
|
129
|
|
- currentBet = 0;
|
130
|
|
- } else if (dealerHand >= 17 && dealerHand < 21) {
|
131
|
|
- Console.output("Dealer has " + dealerHand + " and stands.");
|
|
190
|
+
|
|
191
|
+ if (calcHand(playerHand) == calcHand(dealerHand)) {
|
|
192
|
+ Console.output("It's a tie!");
|
|
193
|
+ Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
|
|
194
|
+ startNextHand();
|
|
195
|
+ } else if (calcHand(playerHand) > 21) {
|
|
196
|
+ Console.output("Dealer wins! You bust.");
|
|
197
|
+ Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
|
|
198
|
+ startNextHand();
|
|
199
|
+ } else if (calcHand(dealerHand) > 21) {
|
|
200
|
+ Console.output("You win! Dealer bust.");
|
|
201
|
+ Console.output("Dealer Hand: " + getDealerHand() + ", " + calcHand(dealerHand));
|
|
202
|
+ startNextHand();
|
|
203
|
+ } else if (calcHand(playerHand) > calcHand(dealerHand)) {
|
|
204
|
+ Console.output("Player has higher hand.");
|
|
205
|
+ Console.output("Player Hand: " + getPlayerHand() + ", " + calcHand(playerHand));
|
|
206
|
+ startNextHand();
|
|
207
|
+ } else if (calcHand(dealerHand) > calcHand(playerHand)) {
|
|
208
|
+ Console.output("Dealer has higher hand.");
|
|
209
|
+ Console.output("Player Hand: " + getDealerHand() + ", " + calcHand(playerHand));
|
|
210
|
+ startNextHand();
|
132
|
211
|
}
|
133
|
212
|
}
|
134
|
213
|
}
|
135
|
214
|
}
|
|
215
|
+
|
|
216
|
+ public static void main(String[] args) {
|
|
217
|
+ BlackJack bj = new BlackJack();
|
|
218
|
+ bj.startGame();
|
|
219
|
+ }
|
136
|
220
|
}
|
137
|
221
|
|
138
|
222
|
|