-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblackjack.py
More file actions
396 lines (337 loc) · 19.6 KB
/
blackjack.py
File metadata and controls
396 lines (337 loc) · 19.6 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
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
import click
from shoe import Shoe
from player import Player
from count import HiLoCount
from hand import Hand
from card import Card, CardValue
from dealer import Dealer, HouseRules
from strategies import BasicStrategy, CasinoStrategy, RandomStrategy, GameActions
from bet import spread1_50, spread1_6
from typing import List
from collections import deque
import matplotlib.pyplot as plt
import numpy as np
isVerbose = False
vprint = print if isVerbose else lambda *a, **k,: None
@click.command()
@click.option('-s', '--shoesize', default=6, help='An integer representing the amount of decks to use in the shoe. Default is 6 decks.')
@click.option('-b', '--bankroll', default=10000, help='Determines the amount of dollars that each player begins the game with in their bankroll. Default is $1000.')
@click.option('-h', '--hands', default=1000, help='Determines the number of hands to deal. Default is 1000.')
@click.option('-t', '--tablemin', default=10, help='Determines the minimum table bet. Default is $10.')
@click.option('-p', '--penetration', default=0.84, help='Dictates the deck penetration by the dealer. Default is 0.84 which means that the dealer will penetrate 84 percent of the shoe before re-shuffling')
@click.option('-d', '--dealersettings', default="[17, True, True, True, True]", help='Assigns the dealer rules.')
@click.option('-v', '--verbose', default=False, help='Prints all player, hand, and game information.')
def main(shoesize, bankroll, hands, tablemin, penetration, dealersettings, verbose):
print("Running blackjack simulation with variables:")
print("Shoe size: ", shoesize, " | Bankroll: ", bankroll, " | Number of hands to simulate: ", hands, " | Minimum Table Bet: ", tablemin)
houseRules = HouseRules(standValue=dealersettings[0], DASoffered=dealersettings[1], RSAoffered=dealersettings[2], LSoffered=dealersettings[3], doubleOnSoftTotal=dealersettings[4])
game = BlackJackGame(shoesize, bankroll, hands, tablemin, penetration, houseRules)
global isVerbose
isVerbose = verbose
game.startGame()
gamedata = GameData(game)
gamedata.getDealerStatistics()
gamedata.getPlayerStatistics()
gamedata.plotBankrollTime()
class GameData:
def __init__(self, game):
self.game: BlackJackGame = game
self.players = self.game.players
self.dealer = self.game.dealer
self.bankrollData = {}
self.getPlayerBankrollSnapshots()
def getPlayerBankrollSnapshots(self):
for player in self.players:
self.bankrollData.update({player.name: player.bankrollSnapshots})
def getDealerStatistics(self):
print(" - - - - - ")
print("Dealer losses: $", self.dealer.losses, " | Dealer gains: $", self.dealer.gains, " | Profit: $", self.dealer.gains - self.dealer.losses)
print(" - - - - - ")
def getPlayerStatistics(self):
for player in self.players:
handsPlayed = len(player.bankrollSnapshots) - 1
winRate = player.handData[0] / handsPlayed * 100
loseRate = player.handData[1] / handsPlayed * 100
drawRate = player.handData[2] / handsPlayed * 100
endBankroll = player.bankroll
initialBankroll = player.bankrollSnapshots[0]
diff = endBankroll - initialBankroll
earningsPerHand = diff / handsPlayed
percentChange = (endBankroll - initialBankroll) / initialBankroll * 100
print(player.name, " | Win %", winRate, " | Lose %", loseRate, " | Draw %", drawRate)
print("Earnings: ", diff, '(Percent Increase %', percentChange, ") | Average payout per hand: $", earningsPerHand)
def plotBankrollTime(self):
numHands = self.game.numHands
playerNames = []
fig, ax = plt.subplots()
for player in self.players:
ax.plot([item for item in range(1, len(player.bankrollSnapshots) + 1)], player.bankrollSnapshots, label=player.name)
playerNames.append(player.name)
ax.set_title("Plot of players' bankroll over time in a blackjack game of "+str(numHands)+" rounds")
ax.set_xlabel("Round number")
ax.set_ylabel("Bankroll ($)")
# Add the legend
pos = ax.get_position()
ax.set_position([pos.x0, pos.y0, pos.width * 0.9, pos.height])
ax.legend(loc='center right', bbox_to_anchor=(1.5, 0.5), title="Players")
plt.tight_layout()
plt.show()
class BlackJackGame:
def __init__(self, shoeSize, bankroll, hands, tableMin, penetration, houseRules):
vprint("Initializing game...")
self.shoeSize = shoeSize
self.numHands = hands
self.tableMin = tableMin
vprint("Dealer has rules: ")
vprint("Deck Penetration %: ", penetration, " | Minimum table bet: $", tableMin)
self.dealer = Dealer(penetration, shoeSize, houseRules, CasinoStrategy(houseRules, isCounting=False, accuracy=1), isVerbose)
self.players = [Player("Counting with 1-6 Bet Spread", bankroll, BasicStrategy(houseRules, isCounting=True, accuracy=1), spread1_6(), isVerbose),
Player("Counting with 1-50 Bet Spread", bankroll, BasicStrategy(houseRules, isCounting=True, accuracy=1), spread1_50(), isVerbose),
Player('Counting with 1-6 Bet Spread, 50% Accurate Basic Strategy', bankroll, BasicStrategy(houseRules, isCounting=True, accuracy=0.50), spread1_6(), isVerbose),
Player("Perfect Basic Strategy", bankroll, BasicStrategy(houseRules, isCounting=False, accuracy=1), spread1_6(), isVerbose),
Player('99% Accurate Basic Strategy', bankroll, BasicStrategy(houseRules, isCounting=False, accuracy=0.99), spread1_50(), isVerbose),
Player('95% Accurate Basic Strategy', bankroll, BasicStrategy(houseRules, isCounting=False, accuracy=0.95), spread1_50(), isVerbose),
Player('75% Accurate Basic Strategy', bankroll, BasicStrategy(houseRules, isCounting=False, accuracy=0.75), spread1_50(), isVerbose),
Player('Casino Rules', bankroll, CasinoStrategy(houseRules, isCounting=False, accuracy=1), spread1_6(), isVerbose),
Player("Random", bankroll, RandomStrategy(houseRules, isCounting=False, accuracy=1), spread1_6(), isVerbose)]
vprint("There are ", len(self.players), " players in the game.")
def clearAllCards(self, players: List[Player]):
# Collect the cards from each player before moving onto the next round and put the cards in the
# discard pile
for player in players:
for hand in player.hands:
if isVerbose: hand.printHand(player.name)
self.dealer.discardPlayersCards(hand, player.name)
player.clearAllHands()
# Discard the dealer's cards and move them to the discard pile
self.dealer.discardDealersCards()
def dealDealersHand(self, count):
# Deal out the dealers cards
upcard = self.dealer.dealCard()
self.dealer.setUpCard(upcard)
count.updateRunningCount(upcard.getValue())
# The hidden card is not added to the count yet as only the dealer knows this information
hiddenCard = self.dealer.dealCard()
dealerHand = Hand([upcard, hiddenCard], 0)
self.dealer.updateHand(dealerHand)
vprint("Dealer shows:")
if isVerbose: upcard.printCard()
vprint("Dealer hides:")
if isVerbose: hiddenCard.printCard()
def dealPlayersHands(self, players, count):
vprint("Dealing hands...")
for player in players:
betSize = player.calculateBetSize(self.tableMin, self.getTrueCount(count))
card1: Card = self.dealer.dealCard()
card2: Card = self.dealer.dealCard()
count.updateRunningCount(card1.getValue())
count.updateRunningCount(card2.getValue())
player.updateBankroll(-1 * betSize)
player.updateHand(Hand([card1, card2], betSize))
if isVerbose: player.getStartingHand().printHand(player.name)
def doubleDown(self, player: Player, hand: Hand, count: HiLoCount):
vprint("Doubling down!")
player.updateBankroll(-1 * hand.getInitialBet())
hand.doubleDown()
self.hit(player, hand, count)
def getTrueCount(self, count: HiLoCount):
decksRemaining = self.dealer.shoe.getDecksRemaining()
trueCount = count.getTrueCount(decksRemaining)
return trueCount
def handleBustHand(self, player: Player, hand: Hand):
vprint("Hand went bust.")
self.dealer.updateGains(hand.getInitialBet())
self.dealer.discardPlayersCards(hand, player.name)
player.clearHand(hand)
def handleDealerBlackjack(self, players: List[Player], count: HiLoCount):
# Need to update the count as the dealer reveals the hidden card to show blackjack
# Guaranteed to have a count value of -1
count.updateRunningCount(10)
for player in players:
for hand in player.hands:
if hand.isBlackjack():
vprint("Player ", player.name, " pushes with another blackjack.")
player.updateBankroll(hand.betSize)
elif hand.isInsured:
vprint("Player ", player.name, "'s hand is insured!")
player.updateBankroll(hand.betSize + hand.insuranceBet)
else:
self.dealer.updateGains(hand.betSize)
def handleInsurance(self, players: List[Player], count: HiLoCount):
vprint("Dealer shows ace - Insurance offered")
trueCount = self.getTrueCount(count)
for player in players:
for hand in player.hands:
if player.strategy.willTakeInsurance(trueCount) and not hand.isBlackjack():
player.updateBankroll(-1 * hand.betSize / 2)
hand.insureHand()
vprint("Player ", player.name, " has insured their hand.")
vprint("Insurance closed.")
def handlePlayerBlackjack(self, player: Player, hand: Hand):
payout = self.dealer.handlePayout(hand.betSize, isBlackjack=True)
vprint("Blackjack! Initial bet: $", hand.getInitialBet(), " Payout: $", payout)
player.updateBankroll(hand.betSize + payout)
self.dealer.discardPlayersCards(hand, player.name)
player.clearHand(hand)
def handleRemainingHands(self, players: List[Player]):
dealerValue = self.dealer.hand.finalHandValue
for player in players:
for hand in player.hands:
vprint(player.name, " has ", hand.finalHandValue, " against the dealer's ", dealerValue)
if hand.finalHandValue > dealerValue:
vprint("Player wins!")
payout = self.dealer.handlePayout(hand.betSize, isBlackjack=False)
vprint("Initial bet: $", hand.getInitialBet(), " Payout: $", payout)
player.updateBankroll(hand.betSize + payout)
elif hand.finalHandValue < dealerValue:
vprint("Player loses!")
self.dealer.updateGains(hand.betSize)
else:
vprint("Player pushes.")
player.updateBankroll(hand.betSize)
def handleSplitPair(self, player: Player, hand: Hand, dealerUpcard: Card, count: HiLoCount):
vprint("Determining whether or not to split pair based on player's strategy...")
trueCount = self.getTrueCount(count)
if player.strategy.shouldSplitPair(hand.getHandValue() / 2, dealerUpcard.getValue()) and player.calculateBetSize(self.tableMin, trueCount) <= player.bankroll:
vprint("Splitting pair!")
splitHand = player.splitPair(hand)
return splitHand
vprint("Player decided not to split pair.")
return None
def hit(self, player: Player, hand: Hand, count: HiLoCount):
hitCard = self.dealer.dealCard()
count.updateRunningCount(hitCard.getValue())
hand.addCard(hitCard)
vprint(player.name, " has new hand: ")
if isVerbose: hand.printHand(player.name)
def playDealerHand(self, count):
vprint("Dealer is now playing their hand...")
action: GameActions = None
softTotalDeductionCount = 0
while (action != GameActions.STAND.value):
if self.dealer.hand.isBust():
if softTotalDeductionCount < self.dealer.hand.getAcesCount():
vprint("Dealer busts! Ace now becomes 1. Old hand value: ", self.dealer.hand.getHandValue(), " New value: ", self.dealer.hand.getHandValue() - 10)
softTotalDeductionCount += 1
else:
vprint("Dealer busts! All players are rewarded")
break
if self.dealer.hand.isSoftTotal(softTotalDeductionCount) and softTotalDeductionCount < self.dealer.hand.getAcesCount():
vprint("Dealer has soft total.")
action = self.dealer.strategy.softTotalOptimalDecision(self.dealer.hand, self.dealer.upcard, softTotalDeductionCount)
else:
vprint("Dealer has hard total...")
action = self.dealer.strategy.hardTotalOptimalDecision(self.dealer.hand, self.dealer.upcard.getValue(), softTotalDeductionCount)
if (action == GameActions.HIT.value):
vprint("Dealer hits...")
hitCard = self.dealer.dealCard()
count.updateRunningCount(hitCard.getValue())
self.dealer.hand.addCard(hitCard)
vprint("Dealer now has hand: ")
if isVerbose: self.dealer.hand.printHand("Dealer")
elif (action == GameActions.STAND.value):
vprint("Dealer will stand...")
self.dealer.hand.setFinalHandValue(self.dealer.hand.getHandValue() - softTotalDeductionCount * 10)
break
def playHands(self, player: Player, dealerUpcard: Card, handNumber, count):
dealtHand = player.getStartingHand()
vprint(player.name, " is playing their hand...")
# Check if the dealt hand is a blackjack and payout immediately if it is
if dealtHand.isBlackjack():
self.handlePlayerBlackjack(player, dealtHand)
else:
handQueue = deque()
handQueue.append(player.getStartingHand())
while len(handQueue) > 0:
hand = handQueue.pop()
action: GameActions = None
softTotalDeductionCount = 0
while (action != GameActions.STAND.value):
if hand.isBust():
if softTotalDeductionCount < hand.getAcesCount():
vprint("BUST! Ace now becomes 1. Old hand value: ", hand.getHandValue(), " New value: ", hand.getHandValue() - 10)
softTotalDeductionCount += 1
else:
vprint("BUST! Value is: ", hand.getHandValue() - softTotalDeductionCount * 10)
self.handleBustHand(player, hand)
break
if hand.isPair():
vprint("We have a pair...")
splitHand = self.handleSplitPair(player, hand, dealerUpcard, count)
if splitHand is not None:
handQueue.append(splitHand)
handQueue.append(hand)
break
if hand.isSoftTotal(softTotalDeductionCount) and softTotalDeductionCount < hand.getAcesCount():
vprint("We have a soft total...")
action = player.strategy.softTotalOptimalDecision(hand, dealerUpcard.getValue(), softTotalDeductionCount)
else:
# Get hard total value
vprint("We have a hard total of ", hand.getHandValue()- softTotalDeductionCount * 10)
action = player.strategy.hardTotalOptimalDecision(hand, dealerUpcard.getValue(), softTotalDeductionCount)
if (action == GameActions.HIT.value):
vprint("Player is gonna hit!")
self.hit(player, hand, count)
elif (action == GameActions.STAND.value):
vprint("Player will stand")
hand.setFinalHandValue(hand.getHandValue() - softTotalDeductionCount * 10)
break
elif (action == GameActions.DOUBLE.value):
vprint("Double down!")
self.doubleDown(player, hand, count)
vprint(player.name, " has played all of their hands!")
def printRoundInformation(self, players: List[Player], count: HiLoCount, roundNumber: int):
print(" - - - - - - - - - - -")
print(" - - - - - - - - - - -")
print("Round: ", roundNumber, " Running Count: ", count.runningCount)
print(" - - - - - - - - - - -")
print(" - - - - - - - - - - -")
for player in players:
prevIndex = len(player.bankrollSnapshots) - 2
print(player.name, ' has a bankroll of $', player.bankroll, " (Prev hand: $", player.bankrollSnapshots[prevIndex], ")")
def startGame(self):
self.dealer.shuffle()
vprint("Starting new blackjack game!")
handCount = 1
playersInGame: List[Player] = []
playersInBreak: List[Player] = []
for player in self.players:
playersInGame.append(player)
vprint("Player: ", player.name, " has joined the game.")
count = HiLoCount()
# Play the game!
while (handCount <= self.numHands and len(playersInGame) > 0):
if isVerbose:
self.printRoundInformation(playersInGame, count, handCount)
# Deal out the players' and dealer's cards
self.dealPlayersHands(playersInGame, count)
self.dealDealersHand(count)
# If the dealer shows an ace, dealer will offer insurance to all players.
if self.dealer.insuranceIsOffered():
self.handleInsurance(playersInGame, count)
# If the dealer was dealt a blackjack, all players automatically lose UNLESS they too have a blackjack
if self.dealer.hand.isBlackjack():
self.handleDealerBlackjack(playersInGame, count)
else:
# Allow players to play out each of their hands
for player in playersInGame:
self.playHands(player, self.dealer.upcard, handCount, count)
# Now, the dealer will play out their hand
self.playDealerHand(count)
# Next, determine which existing hands beat the dealer and perform all necessary payouts
self.handleRemainingHands(playersInGame)
handCount = handCount + 1
self.clearAllCards(playersInGame)
# Used to debug deck sizes to ensure that no cards are being lost:
self.dealer.ensureDeckCompleteness(isVerbose=True)
# If we have exceeded or reached optimal shoe penetration, reset the shoe and the running count
if self.dealer.deckPenetrationTooHigh():
self.dealer.shuffle()
count.resetCount()
for player in playersInGame:
player.takeBankrollSnapshot()
if player.bankroll < self.tableMin:
vprint(player.name, " has gone broke and is out of the game.")
playersInGame.remove(player)
if __name__ == '__main__':
main()