-
Notifications
You must be signed in to change notification settings - Fork 3
/
BlackJack.py
484 lines (413 loc) · 27.4 KB
/
BlackJack.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import random
from enum import Enum, auto
class PlayOptions(Enum):
STAY = 1
HIT = 2
DOUBLE = 3
SURRENDER = 5
SPLIT = 4
class DealerResults(Enum):
BLACKJACK = 1
BUSTED = 2
STAY = 3
class HandType(Enum):
NORMAL = 1
SPLIT = 2
DOUBLE = 3
class HandResults(Enum):
WIN = auto()
DOUBLEWIN = auto()
DOUBLELOSS = auto()
BLACKJACK = auto()
LOST = auto()
PUSH = auto()
SURRENDER = auto()
# Define the matricies for basic strategy
hardHand2CardLookup = [
# columns are based on dealer's face card from 1 (Ace) to 10, rows based on player's total (no ace) 5 to 21
[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT]
,[PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.SURRENDER, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.SURRENDER]
,[PlayOptions.SURRENDER, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.SURRENDER, PlayOptions.SURRENDER]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
]
hardHandLookup = [
# columns are based on dealer's face card from 1 (Ace) to 10, rows based on player's total (no ace) 5 to 21
[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
]
softHand2CardLookup = [
# columns are based on dealer's face card from 1 (Ace) to 10, rows based on player's total with at least 1 ace counted as 11, totals of 14 to 21
[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
]
softHandLookup = [
# columns are based on dealer's face card from 1 (Ace) to 10, rows based on player's total with at least 1 ace counted as 11, totals of 14 to 21
[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.HIT, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
]
pairHandLookup = [
# columns are based on dealer's face card from 1 (Ace) to 10, rows based on player's pair card, 1 through 10
[PlayOptions.SURRENDER, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT]
,[PlayOptions.HIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.DOUBLE, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.HIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.HIT, PlayOptions.HIT, PlayOptions.HIT]
,[PlayOptions.SURRENDER, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT]
,[PlayOptions.STAY, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.STAY, PlayOptions.SPLIT, PlayOptions.SPLIT, PlayOptions.STAY]
,[PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY, PlayOptions.STAY]
]
class Shoe:
def __init__(self, d = 1) -> None:
self.cards = []
self.decks = d
if (self.decks <= 0) or (self.decks > 10):
self.decks = 1
for i in range(0, 52*self.decks):
self.cards.append(i%52+1)
def getCard(self):
card = self.cards.pop(random.randrange(len(self.cards))) % 13 + 1
if card > 10:
card = 10
return card
def needShuffle(self):
# Return true if less than 1/3 of the shoe is remaining
return (len(self.cards) / (self.decks*52)) < .33
def shuffle(self):
self.__init__(self.decks)
class Hand:
def __init__(self) -> None:
self.cards = []
self.total = 0
self.actions = []
self.handType = HandType.NORMAL
self.result = None
def addCard(self, card):
self.cards.append(card)
def getHandTotal(self, softHand = False):
self.total = 0
for card in self.cards:
if card >= 10:
self.total += 10
elif card > 1:
self.total += card
else: # card is an ace
if self.total <= 10: # if total <= 10, count the ace as an 11, and set softHand
softHand = True
self.total += 11
else: # if total > 10, must count ace as a 1
self.total += 1
if self.total > 21 and softHand: # if total > 21 and we have aces counting as 11...
self.total -= 10
softHand = False
return self.total
def basicStrategyPlay(self, dealerFaceCard):
softHand = False
handTotal = self.getHandTotal(softHand)
if len(self.cards) < 2:
return PlayOptions.HIT
elif handTotal > 21:
self.actions.append(PlayOptions.STAY)
return PlayOptions.STAY
elif len(self.cards) == 2:
if self.cards[0] == self.cards[1]:
# use pair hand lookup
self.actions.append(pairHandLookup[self.cards[0]-1][dealerFaceCard-1])
return pairHandLookup[self.cards[0]-1][dealerFaceCard-1]
elif softHand:
# Use the soft hand table since there's an ace counted as 11
self.actions.append(softHand2CardLookup[handTotal-13][dealerFaceCard-1])
return softHand2CardLookup[handTotal-13][dealerFaceCard-1]
else:
# Use the hand hand lookup table since everything else is a hard hand
self.actions.append(hardHand2CardLookup[handTotal-5][dealerFaceCard-1])
return hardHand2CardLookup[handTotal-5][dealerFaceCard-1]
else:
# Hand has more than 2 cards
if softHand:
# Use the soft hand table since there's an ace counted as 11
self.actions.append(softHandLookup[handTotal-13][dealerFaceCard-1])
return softHandLookup[handTotal-13][dealerFaceCard-1]
else:
# Use the hand hand lookup table since everything else is a hard hand
self.actions.append(hardHandLookup[handTotal-5][dealerFaceCard-1])
return hardHandLookup[handTotal-5][dealerFaceCard-1]
def randomPlay(self): # Returns a random allowable action
# First, let's make sure the hand has at least 2 cards in it
# This action is NOT recorded as first 2 cards are not player's choice
if len(self.cards) < 2:
return PlayOptions.HIT
if len(self.cards) == 2: # First 2 cards, we might have different options
if self.getHandTotal() == 21: # Player has blackjack
self.actions.append(PlayOptions.STAY)
return PlayOptions.STAY
elif self.cards[0] == self.cards[1]: # Two of the same cards
action = random.choice(list(PlayOptions)) # Randomly select action based on all available actions
self.actions.append(action)
return action
else:
action = random.choice([PlayOptions.HIT, PlayOptions.STAY, PlayOptions.DOUBLE, PlayOptions.SURRENDER])
self.actions.append(action)
return action
if self.getHandTotal() < 21: # randomly play something unless we have 21
action = random.choice([PlayOptions.HIT, PlayOptions.STAY])
self.actions.append(action)
return action
else:
self.actions.append(PlayOptions.STAY)
return PlayOptions.STAY
def dealerPlay(self): # Returns an action based on using BJ dealer rules
# Assumes dealer has already received at least 2 cards
softHand = False
handTotal = self.getHandTotal(softHand)
if len(self.cards) == 2 and handTotal == 21: # Dealer has blackjack
return PlayOptions.STAY
if handTotal < 17 or (handTotal == 17 and softHand): # Hit until we have 17, also hit on soft-17
return PlayOptions.HIT
return PlayOptions.STAY
def isBlackJack(self):
return (len(self.cards) == 2) and (self.getHandTotal() == 21)
def reset(self):
self.__init__()
class Player:
def __init__(self) -> None:
self.hands = []
self.hands.append(Hand()) # Start player with 1 hand
def reset(self):
self.__init__()
class Dealer:
def __init__(self) -> None:
self.hand = Hand()
def reset(self):
self.__init__()
class Table:
def __init__(self) -> None:
self.shoe = Shoe(8) #Table's shoe has 8 decks
self.players = []
self.dealer = Dealer()
def dealFirstTwoCards(self):
# First, give each player a card, then the dealer
for p in self.players:
p.hands[0].addCard(self.shoe.getCard())
# Dealer's first card
self.dealer.hand.addCard(self.shoe.getCard())
# Give each player their 2nd card
for p in self.players:
p.hands[0].addCard(self.shoe.getCard())
# Dealer's 2nd card
self.dealer.hand.addCard(self.shoe.getCard())
def playRound(self):
self.activeHands = len(self.players) # Set active hands to the number of players
# check to see if shoe needs to be shuffled
if self.shoe.needShuffle():
self.shoe.shuffle()
self.dealFirstTwoCards()
# Players play their hands if dealer doesn't have blackjack
if not self.dealer.hand.isBlackJack():
# Have each player play their hand
for i in range (0, len(self.players)):
p = self.players[i]
self.playPlayerHand(p.hands[0], p, self.dealer.hand.cards[0])
# If there are any active hands left, then dealer plays
if self.activeHands > 0:
while True:
dealerChoice = self.dealer.hand.dealerPlay()
if dealerChoice == PlayOptions.STAY:
break
if dealerChoice == PlayOptions.HIT:
self.dealer.hand.addCard(self.shoe.getCard())
self.recordHandResults()
def playPlayerHand(self, hand, player, dealerFaceCard):
if hand.isBlackJack():
# This hand has won and doesn't need to continue
self.activeHands -= 1
return
else:
while True:
playerChoice = hand.basicStrategyPlay(dealerFaceCard)
if playerChoice in [PlayOptions.HIT, PlayOptions.DOUBLE]:
hand.addCard(self.shoe.getCard())
if playerChoice == PlayOptions.SPLIT:
self.activeHands += 1 # New active hand as a result of split
newHand = Hand()
newHand.handType = HandType.SPLIT # Recording hand type in case of different rules for split hands
hand.handType = HandType.SPLIT
newHand.cards.append(hand.cards.pop(0)) # Remove a card from old hand to create new hand
player.hands.append(newHand)
self.playPlayerHand(newHand, player, dealerFaceCard)
if playerChoice in [PlayOptions.STAY, PlayOptions.DOUBLE, PlayOptions.SURRENDER]:
break
if playerChoice == PlayOptions.DOUBLE:
hand.handType = HandType.DOUBLE
elif playerChoice == PlayOptions.SURRENDER:
# Player busted, active player count decreases
self.activeHands -= 1
hand.result = HandResults.SURRENDER
elif hand.getHandTotal() > 21:
# Player busted, active player count decreases
self.activeHands -= 1
if hand.handType == HandType.DOUBLE:
hand.result = HandResults.DOUBLELOSS
else:
hand.result = HandResults.LOST
def recordHandResults(self):
# Go through each hand to see if it has won
for i in range (0, len(self.players)):
p = self.players[i]
for j in range (0, len(p.hands)):
h = p.hands[j]
# Check to see if hand already has a result (because of surrender or bust)
if h.result == None:
# Check for dealer blackjack
if self.dealer.hand.isBlackJack():
if h.isBlackJack():
h.result = HandResults.PUSH
else:
h.result = HandResults.LOST
# Check for hand blackjack (must be normal hand, not a split, and dealer must not have blackjack)
elif h.isBlackJack() and h.handType == HandType.NORMAL:
h.result = HandResults.BLACKJACK
# Check if player has busted (should already be checked when player is playing, but just in case)
elif h.getHandTotal() > 21:
if h.handType == HandType.DOUBLE:
h.result = HandResults.DOUBLELOSS
else:
h.result = HandResults.LOST
# Check if dealer has busted
elif self.dealer.hand.getHandTotal() > 21:
# dealer has busted, all hands win
if h.handType == HandType.DOUBLE:
h.result = HandResults.DOUBLEWIN
else:
h.result = HandResults.WIN
# Check for push
elif self.dealer.hand.getHandTotal() == h.getHandTotal():
h.result = HandResults.PUSH
# Player has better hand
elif h.getHandTotal() > self.dealer.hand.getHandTotal():
if h.handType == HandType.DOUBLE:
h.result = HandResults.DOUBLEWIN
else:
h.result = HandResults.WIN
# Dealer has better hand
else: #self.dealer.hand.getHandTotal() > h.getHandTotal():
if h.handType == HandType.DOUBLE:
h.result = HandResults.DOUBLELOSS
else:
h.result = HandResults.LOST
def printVerboseResults(self):
result = "Dealer FaceCard = " + str(self.dealer.hand.cards[0])
result += ", Total = " + str(self.dealer.hand.getHandTotal())
result += ", BJ = " + str(self.dealer.hand.isBlackJack())
result += ", Bust = " + str(self.dealer.hand.getHandTotal() > 21)
result += ", Cards = " + str(self.dealer.hand.cards)
for i in range (0, len(self.players)):
p = self.players[i]
result += ", Player " + str(i+1)
for j in range (0, len(p.hands)):
h = p.hands[j]
result += ", Hand " + str(j+1)
result += ", Total = " + str(h.getHandTotal())
result += ", BJ = " + str(h.getHandTotal() == 21 and len(h.cards) == 2)
result += ", Bust = " + str(h.getHandTotal() > 21)
result += ", Cards = " + str(h.cards)
result += ", Actions = " + str(h.actions)
result += ", " + str(h.result)
return result
def printShortResults(self):
result = str(self.dealer.hand.cards[0])
for i in range (0, len(self.players)):
p = self.players[i]
for j in range (0, 1): # Only doing first hand
h = p.hands[j]
result += ", " + str(h.cards[0]) + ", " + str(h.cards[1])
firstTwoCards = h.cards[0] + h.cards[1]
softHand = False
if h.cards[0] == 1 or h.cards[1] == 1:
firstTwoCards += 10
softHand = True
result += ", " + str(firstTwoCards)
result += ", " + str(softHand) # Has an ace
result += ", " + str(h.getHandTotal() == 21 and len(h.cards) == 2) # Is blackjack
result += ", " + str(h.getHandTotal() > 21) # Busted
if len(h.actions) > 0:
result += ", " + str(h.actions[0])
else:
result += ", NO ACTION"
result += ", " + str(h.result)
# Calculate value of hand based on results
value = 0.0
if h.result == HandResults.WIN:
value = 1.0
elif h.result == HandResults.DOUBLEWIN:
value = 2.0
elif h.result == HandResults.DOUBLELOSS:
value = -2.0
elif h.result == HandResults.BLACKJACK:
value = 1.2 # Based on 6:5 blackjack return. Change to 1.5 for 3:1
elif h.result == HandResults.LOST:
value = -1.0
elif h.result == HandResults.PUSH:
value = 0.0
elif h.result == HandResults.SURRENDER:
value = -0.5
result += ", " + str(value)
return result
def reset(self):
for player in self.players:
player.reset()
self.dealer.reset()
if self.shoe.needShuffle():
self.shoe.shuffle()
table = Table()
table.players.append(Player())
print("Round, Dealer FaceCard, Player Card 1, Card 2, Total, Soft Hand, Is BlackJack, Busted, Action, Result, Value")
for i in range(0, 1000): # Simulates 1,000 rounds of BlackJack
table.playRound()
print(str(i) + ", " + table.printShortResults())
# print("Round " + str(i) + ": " + table.printVerboseResults()) # More verbose printout of each hand played
# print("\n")
table.reset()