-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.java
More file actions
367 lines (291 loc) · 10 KB
/
Blackjack.java
File metadata and controls
367 lines (291 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
class Card {
// Specify data fields for an individual card
public static final int SPADES = 0;
public static final int HEARTS = 1;
public static final int CLUBS = 2;
public static final int DIAMONDS = 3;
public static final int ACE = 1;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
int suit;
int rank;
// Fill in constructor method
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
// To String code
// use character unicode
public String toString() {
String s = "";
switch (this.rank) {
case ACE : s += "ACE";break;
case JACK : s += "JACK";break;
case QUEEN : s+= "QUEEN"; break;
case KING : s+= "KING"; break;
default : s += this.rank;
}
s += " of ";
switch (this.suit) {
case SPADES : s += "SPADES"; break;
case HEARTS : s += "HEARTS"; break;
case CLUBS : s += "CLUBS"; break;
case DIAMONDS : s += "DIAMONDS"; break;
}
return s;
}
}
public class BlackJack {
// fill in code here
// define data members
public static final int[] SUIT_ORDER = {Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS};
public static final int[] RANK_ORDER = {Card.ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, Card.JACK, Card.QUEEN, Card.KING};
Random random;
public static void buildDeck(ArrayList<Card> deck) {
// fill in code here
// Given an empty deck, construct a standard deck of playing cards
deck.clear();
for (int suitPos = 0; suitPos < SUIT_ORDER.length; suitPos++) {
for (int rankPos = 0; rankPos < RANK_ORDER.length ; rankPos++) {
Card card = new Card(RANK_ORDER[rankPos], SUIT_ORDER[suitPos]);
deck.add(card);
}
}
}
public static void initialDeal(ArrayList<Card> deck, ArrayList<Card> playerHand, ArrayList<Card> dealerHand){
// fill in code here
// Deal two cards from the deck into each of the player's hand and dealer's hand
playerHand.clear();
dealerHand.clear();
Random random = new Random();
for (int i= 0; i< 2; i++) {
Card card = deck.get(random.nextInt(deck.size()));
playerHand.add(card);
deck.remove(card);
}
for (int i= 0; i< 2; i++) {
Card card = deck.get(random.nextInt(deck.size()));
dealerHand.add(card);
deck.remove(card);
}
}
public static void dealOne(ArrayList<Card> deck, ArrayList<Card> hand){
// fill in code here
// this should deal a single card from the deck to the hand
Random random = new Random(); //?????? do we need to initialize random each time??
Card card = deck.get(random.nextInt(deck.size()));
hand.add(card);
deck.remove(card);
}
public static boolean checkBust(ArrayList<Card> hand){
// fill in code here
// This should return whether a given hand's value exceeds 21
// JQK are all equal to 10
// ACE is either 1 or 11
if (Score(hand) > 21) {
return true;
}
else {
return false;
}
}
public static boolean dealerTurn(ArrayList<Card> deck, ArrayList<Card> hand){
// fill in code here
// This should conduct the dealer's turn and
// Return true if the dealer busts; false otherwise
// hits or stand here
int a = Score(hand);
while (a < 17) {
dealOne(deck,hand);
a = Score(hand);
}
if (checkBust(hand)){
return true;
}
else {
return false;
}
}
public static int whoWins(ArrayList<Card> playerHand, ArrayList<Card> dealerHand){
// fill in code here
// This should return 1 if the player wins and 2 if the dealer wins
// who's sum is bigger if they do not burst
if (Score(playerHand) > Score(dealerHand) && (!checkBust(playerHand))){
return 1;
}
else {
return 2;
}
//clear the arraylist maybe here
}
public static String displayCard(ArrayList<Card> hand){
// fill in code here
// Return a string describing the card which has index 1 in the hand
Card card = hand.get(1);
String s = card.toString();
return s;
}
public static String displayHand(ArrayList<Card> hand){
// fill in code here
// Return a string listing the cards in the hand
String s = "";
for (int i=0; i<hand.size();i++) {
Card card = hand.get(i);
s += card.toString() + '\n';
}
return s;
}
public static int Score(ArrayList<Card> hand) {
// calculate the sum of the game
int sum = 0;
boolean hasACE = false;
for (int i=0; i<hand.size();i++) {
if (hand.get(i).rank == Card.JACK || hand.get(i).rank == Card.QUEEN || hand.get(i).rank == Card.KING) {
sum += 10;
}
else if (hand.get(i).rank >= 2 && hand.get(i).rank <=10) {
sum += hand.get(i).rank;
}
else if (hand.get(i).rank == Card.ACE) {
sum += 11;
}
}
for (int i = 0; i < hand.size(); i++) {
if (hand.get(i).rank == Card.ACE) {
hasACE = true;
}
}
if (hasACE && (sum >21)) {
sum -= 10;
}
return sum;
}
// fill in code here (Optional)
// feel free to add methods as necessary
public static void main(String[] args) {
int playerChoice, winner;
String bettingChoice;
int initialmoney;
int bettingmoney;
ArrayList<Card> deck = new ArrayList<Card>();
playerChoice = JOptionPane.showConfirmDialog(
null,
"Ready to Play Blackjack?",
"Blackjack",
JOptionPane.OK_CANCEL_OPTION
);
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
initialmoney = 50;
Object[] options = {"Hit","Stand"};
boolean isBusted; // Player busts?
boolean dealerBusted;
boolean isPlayerTurn;
ArrayList<Card> playerHand = new ArrayList<>();
ArrayList<Card> dealerHand = new ArrayList<>();
do{ // Game loop
buildDeck(deck); // Initializes the deck for a new game
initialDeal(deck, playerHand, dealerHand);
isPlayerTurn=true;
isBusted=false;
dealerBusted=false;
bettingmoney = 50;
bettingChoice = JOptionPane.showInputDialog(null,
"Now you have $" + initialmoney + " and your initial hand is \n" + displayHand(playerHand) + "\n Please enter the amount of money you want to bet in this game",
"Ready to bet?",
JOptionPane.INFORMATION_MESSAGE
);
if (bettingChoice == null)
System.exit(0);
else {
bettingmoney = Integer.parseInt(bettingChoice);
}
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
while(isPlayerTurn){
// Shows the hand and prompts player to hit or stand
playerChoice = JOptionPane.showOptionDialog(
null,
"Dealer shows " + displayCard(dealerHand) + "\n Your hand is: "
+ displayHand(playerHand) + "\n Sum total is: " + Score(playerHand) + "\n What do you want to do?",
"Hit or Stand",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]
);
if(playerChoice == JOptionPane.CLOSED_OPTION)
System.exit(0);
else if(playerChoice == JOptionPane.YES_OPTION){
dealOne(deck, playerHand);
isBusted = checkBust(playerHand);
if(isBusted){
// Case: Player Busts
playerChoice = JOptionPane.showConfirmDialog(
null,
"Player has busted!",
"You lose",
JOptionPane.OK_CANCEL_OPTION
);
initialmoney -= bettingmoney;
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
isPlayerTurn=false;
}
}
else{
isPlayerTurn=false;
}
}
if(!isBusted){ // Continues if player hasn't busted
dealerBusted = dealerTurn(deck, dealerHand);
if(dealerBusted){ // Case: Dealer Busts
playerChoice = JOptionPane.showConfirmDialog(
null,
"The dealer's hand: " +displayHand(dealerHand) + "\n Sum total is: " + Score(dealerHand) + "\n \n Your hand: "
+ displayHand(playerHand) + "\n Sum total is: " + Score(playerHand) + "\nThe dealer busted.\n Congrautions!",
"You Win!!!",
JOptionPane.OK_CANCEL_OPTION
);
initialmoney += bettingmoney;
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
}
else{ //The Dealer did not bust. The winner must be determined
winner = whoWins(playerHand, dealerHand);
if(winner == 1){ //Player Wins
playerChoice = JOptionPane.showConfirmDialog(
null,
"The dealer's hand: " +displayHand(dealerHand) + "\n Sum total is: " + Score(dealerHand)+ "\n \n Your hand: "
+ displayHand(playerHand) + "\n Sum total is: " + Score(playerHand)+"\n Congrautions!",
"You Win!!!",
JOptionPane.OK_CANCEL_OPTION
);
initialmoney += bettingmoney;
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
}
else{ //Player Loses
playerChoice = JOptionPane.showConfirmDialog(
null,
"The dealer's hand: " +displayHand(dealerHand) + "\n Sum total is: " + Score(dealerHand)+ "\n \n Your hand: "
+ displayHand(playerHand) + "\n Sum total is: " + Score(playerHand)+"\n Better luck next time!",
"You lose!!!",
JOptionPane.OK_CANCEL_OPTION
);
initialmoney -= bettingmoney;
if((playerChoice == JOptionPane.CLOSED_OPTION) || (playerChoice == JOptionPane.CANCEL_OPTION))
System.exit(0);
}
}
}
}while(initialmoney > 0);
JOptionPane.showMessageDialog(null, "Sorry! you don't have money, you lose!");
System.exit(0);
}
}