-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBlackJack.txt
380 lines (328 loc) · 12.2 KB
/
TestBlackJack.txt
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
368
369
370
371
372
373
374
375
376
377
378
379
380
//Padraig Kennedy, 13204999 , Assignment 2
package blackjack;
import java.util.Scanner;
import java.util.Vector;
public class BlackJack
{
private static Scanner scanner = new Scanner(System.in);
private int[] deck; // An array of 52 Cards, representing the deck.
private int currentPosition; // Current position in the deck
private Vector hand; // The cards in the hand.
public static void main(String[] args)
{
new BlackJack().run();
}
public void run()
{
/*
This program lets the user play Blackjack. The computer
acts as the dealer. The user has a stake of €100, and
makes a bet on each game. The user can leave at any time,
or will be kicked out when he loses all the money.
House rules: The dealer hits on a total of 16 or less
and stands on a total of 17 or more. Dealer wins ties.
A new deck of cards is used for each game.
*/
int money; // Amount of money the user has.
int bet; // Amount user bets on a game.
boolean userWins; // Did the user win the game?
System.out.println("Welcome User to the game of blackjack.");
System.out.println();
money = 100; // User starts with $100.
while (true)
{
System.out.println("You have " + money + " euros.");
do
{
System.out.println("How many euros do you want to bet min. bet €5 - max. bet €25? (Enter 0 to leave game.)");
bet = scanner.nextInt();
if (bet < 0 || bet > money)
{
System.out.println("Your answer must be between 0 and " + money + '.');
}
} while (bet < 0 || bet > money);
if (bet == 0)
{
break;
}
userWins = setupNewGame();
if (userWins)
{
money = money + bet;
} else
{
money = money - bet;
}
System.out.println();
if (money == 0)
{
System.out.println("Looks like you're out of money!");
break;
}
}
System.out.println();
System.out.println("You leave with €" + money + '.');
} // end main()
private boolean setupNewGame()
{
// Let the user play one game of Blackjack.
// Return true if the user wins, false if the user loses.
Vector dealerHand; // The dealer's hand.
Vector userHand; // The user's hand.
// Create an unshuffled deck of cards.
deck = new int[52];
int cardCt = 0; // How many cards have been created so far.
for (int suit = 0; suit <= 3; suit++)
{
for (int value = 1; value <= 13; value++)
{
deck[cardCt] = value;
cardCt++;
}
}
currentPosition = 0;
dealerHand = new Vector();
userHand = new Vector();
/* Shuffle the deck, then deal two cards to each player. */
shuffle();
dealerHand.addElement(dealCard());
dealerHand.addElement(dealCard());
userHand.addElement(dealCard());
userHand.addElement(dealCard());
System.out.println();
System.out.println();
/* Check if one of the players has Blackjack (two cards totaling to 21).
The player with Blackjack wins the game. Dealer wins ties.
*/
if (value(dealerHand) == 21)
{
System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
System.out.println();
System.out.println("Dealer has Blackjack. Dealer wins.");
return false;
}
if (value(userHand) == 21)
{
System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
System.out.println();
System.out.println("You have Blackjack. You win.");
return true;
}
/* If neither player has Blackjack, play the game. The user gets a chance
to draw cards (i.e., to "Hit"). The while loop ends when the user
chooses to "Stand" or when the user goes over 21.
*/
while (true)
{
/* Display user's cards, and let user decide to Hit or Stand. */
System.out.println();
System.out.println();
System.out.println("Your cards are:");
for (int i = 0; i < userHand.size(); i++)
{
System.out.println(" " + showCard(getCard(userHand, i)));
}
System.out.println("Your total is " + value(userHand));
System.out.println();
System.out.println("Dealer is showing the " + showCard(getCard(dealerHand, 0)));
System.out.println();
System.out.print("Hit (H) or Stand (S)? ");
char userAction; // User's response, 'H' or 'S'.
do
{
userAction = Character.toUpperCase(scanner.next().charAt(0));
if (userAction != 'H' && userAction != 'S')
{
System.out.print("Please respond H or S: ");
}
} while (userAction != 'H' && userAction != 'S');
/* If the user Hits, the user gets a card. If the user Stands, the
dealer gets a chance to draw and the game ends.
*/
if (userAction == 'S')
{
// Loop ends; user is done taking cards.
break;
} else
{ // userAction is 'H'.
// Give the user a card. If the user goes over 21, the user loses.
int newCard = dealCard();
userHand.addElement(newCard);
System.out.println();
System.out.println("User hits.");
System.out.println("Your card is the " + showCard(newCard));
System.out.println("Your total is now " + value(userHand));
if (value(userHand) > 21)
{
System.out.println();
System.out.println("You busted by going over 21. You lose.");
System.out.println("Dealer's other card was the " + showCard(getCard(dealerHand, 1)));
return false;
}
}
} // end while loop
/* If we get to this point, the user has Stood with 21 or less. Now, it's
the dealer's chance to draw. Dealer draws cards until the dealer's total is > 16.
*/
System.out.println();
System.out.println("User stands.");
System.out.println("Dealer's cards are");
System.out.println(" " + showCard(getCard(dealerHand, 0)));
System.out.println(" " + showCard(getCard(dealerHand, 1)));
while (value(dealerHand) <= 16)
{
int newCard = dealCard();
System.out.println("Dealer hits and gets the " + showCard(newCard));
dealerHand.addElement(newCard);
}
System.out.println("Dealer's total is " + value(dealerHand));
/* Now, the winner can be declared. */
System.out.println();
if (value(dealerHand) > 21)
{
System.out.println("Dealer busted by going over 21. You win.");
return true;
} else
{
if (value(dealerHand) == value(userHand))
{
System.out.println("Dealer wins on a tie. You lose.");
return false;
} else
{
if (value(dealerHand) > value(userHand))
{
System.out.println("Dealer wins, " + value(dealerHand) + " points to " + value(userHand) + ".");
return false;
} else
{
System.out.println("You win, " + value(userHand) + " points to " + value(dealerHand) + ".");
return true;
}
}
}
} // end playBlackjack()
public int dealCard()
{
// Deals one card from the deck and returns it.
if (currentPosition == 52)
{
shuffle();
}
currentPosition++;
return deck[currentPosition - 1];
}
public void shuffle()
{
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for (int i = 51; i > 0; i--)
{
int rand = (int) (Math.random() * (i + 1));
int temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
currentPosition = 0;
}
public int getCard(Vector hand, int position)
{
// Get the card from the hand in given position, where positions
// are numbered starting from 0. If the specified position is
// not the position number of a card in the hand, then null
// is returned.
if (position >= 0 && position < hand.size())
{
return ((Integer)hand.elementAt(position)).intValue();
} else
{
return 0;
}
}
public int value(Vector hand)
{
// Returns the value of this hand for the
// game of Blackjack.
int val; // The value computed for the hand.
boolean ace; // This will be set to true if the
// hand contains an ace.
int cards; // Number of cards in the hand.
val = 0;
ace = false;
cards = hand.size();
for (int i = 0; i < cards; i++)
{
// Add the value of the i-th card in the hand.
int card; // The i-th card;
int cardVal; // The blackjack value of the i-th card.
card = getCard(hand, i);
cardVal = getCardValue(card); // The normal value, 1 to 13.
if (cardVal > 10)
{
cardVal = 10; // For a Jack, Queen, or King.
}
if (cardVal == 1)
{
ace = true; // There is at least one ace.
}
val = val + cardVal;
}
// Now, val is the value of the hand, counting any ace as 1.
// If there is an ace, and if changing its value from 1 to
// 11 would leave the score less than or equal to 21,
// then do so by adding the extra 10 points to val.
if (ace == true && val + 10 <= 21)
{
val = val + 10;
}
return val;
}
public int getCardValue(int card)
{
int result = card;
switch (card)
{
case 11:
case 12:
case 13:
result = 10;
}
return result;
}
public String showCard(int card)
{
switch (card)
{
case 1:
return "Ace";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
case 9:
return "9";
case 10:
return "10";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return "??";
}
}
} // end class