-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJack.groovy
executable file
·337 lines (290 loc) · 9.39 KB
/
BlackJack.groovy
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
// Blackjack.groovy
// Author: Daniel W. Brodsky
// Free to use or redistribute
/*
* An enumeration of the suits in a deck of cards.
*/
enum Suit
{
clubs, diamonds, hearts, spades
}
/*
* This object represents one card in a deck. It contains the point value,
* the suit, a display name for pretty printing, and a flag indicating
* whether it's an Ace, since those get special handling in points calculations.
*/
class Card
{
final suit
final value
final name
final isAce
Card(Suit suit, int value, String name)
{
this(suit, value, name, false)
}
Card(Suit suit, int value, String name, boolean ace)
{
assert suit != null
assert value > 0 && value < 12
assert name != null
assert name != ""
this.suit = suit
this.value = value
this.name = name
this.isAce = ace
}
String toString()
{
return name + " of " + suit
}
}
// The master deck. We copy this array into the one we're using for the deal
DECK = [new Card(Suit.clubs, 2, "Two"),
new Card(Suit.clubs, 3, "Three"),
new Card(Suit.clubs, 4, "Four"),
new Card(Suit.clubs, 5, "Five"),
new Card(Suit.clubs, 6, "Six"),
new Card(Suit.clubs, 7, "Seven"),
new Card(Suit.clubs, 8, "Eight"),
new Card(Suit.clubs, 9, "Nine"),
new Card(Suit.clubs, 10, "Ten"),
new Card(Suit.clubs, 10, "Jack"),
new Card(Suit.clubs, 10, "Queen"),
new Card(Suit.clubs, 10, "King"),
new Card(Suit.clubs, 11, "Ace", true),
new Card(Suit.diamonds, 2, "Two"),
new Card(Suit.diamonds, 3, "Three"),
new Card(Suit.diamonds, 4, "Four"),
new Card(Suit.diamonds, 5, "Five"),
new Card(Suit.diamonds, 6, "Six"),
new Card(Suit.diamonds, 7, "Seven"),
new Card(Suit.diamonds, 8, "Eight"),
new Card(Suit.diamonds, 9, "Nine"),
new Card(Suit.diamonds, 10, "Ten"),
new Card(Suit.diamonds, 10, "Jack"),
new Card(Suit.diamonds, 10, "Queen"),
new Card(Suit.diamonds, 10, "King"),
new Card(Suit.diamonds, 11, "Ace", true),
new Card(Suit.hearts, 2, "Two"),
new Card(Suit.hearts, 3, "Three"),
new Card(Suit.hearts, 4, "Four"),
new Card(Suit.hearts, 5, "Five"),
new Card(Suit.hearts, 6, "Six"),
new Card(Suit.hearts, 7, "Seven"),
new Card(Suit.hearts, 8, "Eight"),
new Card(Suit.hearts, 9, "Nine"),
new Card(Suit.hearts, 10, "Ten"),
new Card(Suit.hearts, 10, "Jack"),
new Card(Suit.hearts, 10, "Queen"),
new Card(Suit.hearts, 10, "King"),
new Card(Suit.hearts, 11, "Ace", true),
new Card(Suit.spades, 2, "Two"),
new Card(Suit.spades, 3, "Three"),
new Card(Suit.spades, 4, "Four"),
new Card(Suit.spades, 5, "Five"),
new Card(Suit.spades, 6, "Six"),
new Card(Suit.spades, 7, "Seven"),
new Card(Suit.spades, 8, "Eight"),
new Card(Suit.spades, 9, "Nine"),
new Card(Suit.spades, 10, "Ten"),
new Card(Suit.spades, 10, "Jack"),
new Card(Suit.spades, 10, "Queen"),
new Card(Suit.spades, 10, "King"),
new Card(Suit.spades, 11, "Ace", true)]
assert DECK.size == 52
/*
* This method calculates the points value of a hand. It handles Ace
* calculations automatically; if an Ace(s) with value 11 will put the user
* over 21, then it auto-adjusts the value(s) down to 1.
*/
int calculateValue(List<Card> cards)
{
value = 0
aces = 0
for (Card c : cards)
{
if (c.isAce)
{
aces++
}
value += c.value
// This corrects for the case where 11 pts would put them over 21,
// but since it's an Ace, we'll count it as 1 pt to keep them under.
// Works for any number of aces in the hand.
while (value > 21 && aces > 0)
{
value -= 10
aces--
}
}
return value
}
/*
* Method to play the human side of the deal. Will prompt the user whether
* they want to hit or stand until they finally stand or bust.
*/
def playHuman(List player, List cards)
{
playerTurn = true
playerPts = 0
playerFirstRun = true
// player's turn
while (playerTurn)
{
playerPts = calculateValue(player)
println "Your cards: ${player.join(', ')}"
if (playerFirstRun && playerPts == 21)
{
println "Blackjack! 21 points on your first deal. Congrats!"
break
}
playerFirstRun = false
if (playerPts > 21)
{
println "You went bust with $playerPts} points. Too bad.\n"
break
}
choice = System.console().readLine(
"You have $playerPts points. Would you like to (h)it or (s)tand? ")
println "\n"
choice = choice.toLowerCase()
switch(choice)
{
case "h":
case "hit":
player << cards.remove(0)
break
case "s":
case "stand":
playerTurn = false
break
default:
println "Invalid option: ${choice}. Type 'h' to hit; 's' to stand."
}
}
outcome = (playerPts > 21) ? "busts" : "stands"
println "Player ${outcome} with ${playerPts} points.\n"
return [playerPts, playerFirstRun]
}
/*
* Auto-plays the dealer's hand. Since the dealer has fixed rules, this
* method requires no user interaction.
*/
def playDealer(List dealer, List cards)
{
dealerTurn = true
dealerPts = 0
dealerFirstRun = true
// dealer's turn; it's all automated
while (dealerTurn)
{
dealerPts = calculateValue(dealer)
println "Dealer cards: ${dealer.join(', ')}"
if (dealerFirstRun && dealerPts == 21)
{
println "Blackjack! 21 points on the first deal. Go me!"
break
}
dealerFirstRun = false
if (dealerPts > 21)
{
println "Dealer went bust with ${dealerPts} points. Oh well.\n"
break
}
switch(dealerPts)
{
case 1..16:
println("Dealer has ${dealerPts} points and must hit.\n")
dealer << cards.remove(0)
break
case 17..21:
println("Dealer has ${dealerPts} points and must stand.\n")
dealerTurn = false
break
}
}
outcome = (dealerPts > 21) ? "busts" : "stands"
println "Dealer ${outcome} with ${dealerPts} points.\n"
return [dealerPts, dealerFirstRun]
}
/*
* Displays the results of the game, depending on who won. Briefly:
* - If both players bust, dealer wins
* - If a player gets 21 on the initial deal, they win, unless both
* get 21 on first deal, in which case it's a tie
* - If players score the same number of points, it's a tie
* - The player who scores the highest without going over 21 wins
*/
def displayResults(playerPts, playerFirstRun, dealerPts, dealerFirstRun)
{
// Both bust
if (playerPts > 21 && dealerPts > 21)
{
println "Both players bust; dealer wins. Them\'s the rules"
}
// Player busts
else if (playerPts > 21)
{
println "Player busts; dealer wins with ${dealerPts} points. Nice try."
}
// Dealer busts
else if (dealerPts > 21)
{
println "Dealer busts; player wins with ${playerPts} points. Wicked."
}
// Player gets 21 on first deal
else if (playerFirstRun && !dealerFirstRun)
{
println "Player wins with ${playerPts} points on initial deal. Sweet!"
}
// Dealer gets 21 on first deal
else if (dealerFirstRun && !playerFirstRun)
{
println "Dealer wins with ${dealerPts} points on initial deal. Boo-ya!"
}
// Tie game (also handles case where they tie because of 21 on first deal)
else if (playerPts == dealerPts)
{
println "It's a tie, with ${playerPts} points each."
}
// Player wins w/o busts
else if (playerPts > dealerPts)
{
println "Player wins with ${playerPts} points. Well done."
}
// Dealer wins w/o busts
else if (dealerPts > playerPts)
{
println "Dealer wins with ${dealerPts} points. In your face, player!"
}
}
/*
* Manages overall game flow.
*/
void play()
{
// shuffle the deck
cards = new ArrayList<Card>(DECK)
Collections.shuffle(cards)
dealer =[]
player = []
// deal the cards
player << cards.remove(0)
dealer << cards.remove(0)
player << cards.remove(0)
dealer << cards.remove(0)
// this is a nifty reason to use Groovy
def (int playerPts, boolean playerFirstRun) = playHuman(player, cards)
def (int dealerPts, boolean dealerFirstRun) = playDealer(dealer, cards)
displayResults(playerPts, playerFirstRun, dealerPts, dealerFirstRun)
}
keepPlaying = "y"
// play as many games as user would like
while (keepPlaying == "y")
{
play()
keepPlaying = System.console().readLine("\nThat was fun. Play again? (y/n): ")
keepPlaying = keepPlaying.toLowerCase()
println "\n"
}