-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
399 lines (340 loc) · 15.1 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
#!/usr/bin/env python
# encoding: utf-8
"""
blackjack.py
Created by James on 2014-07-03.
"""
import random
import matplotlib.pyplot as plt
# credits not updating properly
class Analytics():
"""Analyze playing patterns and probabilities"""
def __init__(self, player):
self.player = player
def plot_outcome_vs_risk(self):
moves = self.player.history['moves']
risk = []
for m in moves:
risk.append(moves[m][1])
plt.scatter(self.player.history['outcome'],risk)
plt.suptitle("Hand outcome vs. bust probability")
plt.show()
def plot_credit_decay(self):
credit = self.player.history['credits']
plt.plot(credit)
plt.ylabel("Player Credits")
plt.xlabel("Hand number")
plt.suptitle("Credits over time")
plt.show()
def plot_move_vs_risk(self):
moves = self.player.history['moves']
choice=[]
risk=[]
for m in moves:
choice.append(moves[m][0])
risk.append(moves[m][1])
plt.scatter(choice,risk)
plt.ylabel("Bust probability")
plt.xlabel('Player choice - 0=stand, 1=hit, 2=double')
plt.suptitle("Player move vs. bust probability")
plt.show()
class Game():
def __init__(self, player, start_credit=100):
"""Initializes a new game with one player and dealer. Start credits = 100"""
self.dealer = Dealer()
self.deck = Deck()
self.player = Player(player, start_credit)
def get_player_bet(self):
"""Prompt the player to make bet. Bet must be $1 or greater and cannot exceed current balance."""
initial_bet = 0
while initial_bet < 1 or initial_bet > self.player.credits:
try:
initial_bet = int(
raw_input('How much would you like to bet, '+ self.player.name+'? You have ' + str(self.player.credits) + ' credits. > '))
if initial_bet < 1:
print('Please bet at least 1 credit')
if initial_bet > self.player.credits:
print('You do not have sufficient credits to make this wager. You have ' + str(
self.player.credits) + ' credits left.')
except ValueError:
print('That was an invalid number. Please enter a value >= 1')
self.player.bet = initial_bet
return initial_bet
def deal_cards(self):
"""Randomly serve two cards to player and dealer"""
for i in range(2):
self.player.hand.draw_from_deck(self.deck)
self.dealer.hand.draw_from_deck(self.deck)
def check_for_blackjack(self):
"""Verifies if the player or dealer got a blackjack at the beginning of the round. Returns 1,0,-1 for a win, tie, and loss respectively. Otherwise returns None"""
winner = None
blackjack = False
if self.player.hand.total == 21 and len(self.player.hand.cards) == 2:
if self.dealer.hand.total == 21 and len(self.dealer.hand.cards) == 2:
print('Push!')
winner = 0
else:
print(self.player.name + ' blackjack!')
winner = 1
blackjack = True
if self.dealer.hand.total == 21 and len(self.dealer.hand.cards) == 2:
self.dealer.show_hand(True)
print("Dealer blackjack!")
winner = -1
return (winner, blackjack)
def check_for_bust(self, player):
"""See if a hand's total exceeds 21"""
bust = False
if player.hand.total > 21:
bust = True
return bust
def get_winner(self):
"""Pick the winner based on hand totals returns 1, 0, -1, for player wins, tie, and dealer wins respectively"""
if self.check_for_bust(self.dealer):
print('Dealer bust')
return 1
if self.dealer.hand.total >= 17 and self.dealer.hand.total > self.player.hand.total:
print('Dealer wins')
return -1
if self.dealer.hand.total < self.player.hand.total:
print(self.player.name + (' wins!'))
return 1
if self.dealer.hand.total == self.player.hand.total:
print('Push!')
return 0
def deal_two_cards_each(self, dealer_turn):
"""Pick two random cards for both player and dealer and display them."""
self.deal_cards()
self.player.show_hand()
self.dealer.show_hand(dealer_turn)
def play_one_round(self):
""" Play a single hand. Both player and dealer take turns after drawing two cards from the deck."""
blackjack = False
self.player.bet = self.get_player_bet()
self.player.credits = self.player.credits - self.player.bet #remove the bet amount from the player's account
print('You bet ' + str(self.player.bet))
dealer_turn = False
self.deal_two_cards_each(dealer_turn)
winner, is_blackjack = self.check_for_blackjack() #check to see if anyone got a blackjack outright
if not winner == None:
self.update_credits(winner, is_blackjack)
if winner == None:
self.player.play_hand(self.deck, self.dealer)
if self.check_for_bust(self.player):
print('Player bust!')
winner = -1
if winner == None:
print('Player stands. Dealer turn')
dealer_turn = True
self.dealer.show_hand(dealer_turn)
self.dealer.play_hand(self.deck, dealer_turn, self.player)
winner = self.get_winner()
self.update_credits(winner, is_blackjack)
assert not winner == None #if there is no winner at this stage something is wrong!
self.player.hand.return_to_deck(self.deck)
self.dealer.hand.return_to_deck(self.deck)
return winner
def update_credits(self, winner, is_blackjack):
"""Credit the player's account if they win or tie"""
if winner == 1 and is_blackjack:
self.player.credits = self.player.credits + (2.5 * self.player.bet)
if winner == 1 and (not is_blackjack):
self.player.credits = self.player.credits + (2 * self.player.bet)
if winner == 0:
self.player.credits = self.player.credits + self.player.bet
def start(self):
"""This is where the game flow is controlled."""
round_number = 1
loss_streak = 0
while self.player.credits >= 1:
self.deck.shuffle()
print('### Round ' + str(round_number) + ' ###')
winner = self.play_one_round()
loss_streak = self.update_streak(loss_streak, winner)
self.record_player_history(winner)
round_number = round_number + 1
def update_streak(self, loss_streak, winner):
"""Keep tabs on the player's winning/losing streak to offer help"""
if winner == -1:
loss_streak = loss_streak + 1
else:
loss_streak = 0
if loss_streak > 0 and loss_streak % 3 == 0:
help = None
while not help == "y" and not help == "n" and self.player.help == False:
help = raw_input('You seem to be struggling. You have lost ' + str(
loss_streak) + ' hands in a row. Would you like some assistance? Enter "y" for [Y]es or "n" for [N]o > ')
if help == "y":
self.player.help = True
return loss_streak
def record_player_history(self, winner):
"""Keep track of player history, bets, and outcomes (win/lose/tie)."""
self.player.history['outcome'].append(winner)
self.player.history['bets'].append(self.player.bet)
self.player.history['rounds'] = self.player.history['rounds'] + 1
self.player.history['credits'].append(self.player.credits)
class Dealer():
def __init__(self):
"""Initialize a new Dealer with an empty hand"""
self.hand = Hand()
def show_hand(self, dealer_turn):
"""Prints the dealer's hand and only shows a single card if it's no the dealer's turn"""
if dealer_turn:
print('Dealer current hand: ')
for c in self.hand.cards:
print(c)
print('For a total of: ' + str(self.hand.total))
else:
print(
'Dealer shows: ' + str(self.hand.cards[0]) + ' and <card face down>') #don't show the card in the hole
def play_hand(self, deck, dealer_turn, player):
"""The dealer playing logic. Keep hitting until above 17. Hit also if you have a soft 17 e.g. 17 with Ace."""
while self.hand.total < 17 or (self.hand.total == 17 and any(c.face == 'Ace' for c in self.hand.cards)):
print('Dealer draws card...')
self.hand.draw_from_deck(deck)
self.show_hand(dealer_turn)
class Deck():
"""A standard 52-card deck."""
SUITS = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
FACES = ['Ace', 'King', 'Queen', 'Jack']
def __init__(self):
"""Create a deck with all suits and values."""
self.cards = []
for s in self.SUITS:
for val in range(9):
self.cards.append(Card(val + 2, s))
for f in self.FACES:
self.cards.append(Card(f, s))
def shuffle(self):
"""Randomly shuffle the deck."""
random.shuffle(self.cards)
def next_card(self):
"""Return the card on top of the deck."""
return self.cards.pop(0)
class Player():
"""A class to handle player logic such as available credits and game decisions"""
def __init__(self, name, credits):
"""Initialize a new player and record its moves"""
self.credits = credits
self.name = name
self.hand = Hand()
self.bet = 0
self.help = False
self.move_number = 0
self.history = {'rounds': 0, 'bets': [], 'outcome': [], 'moves': {},
'credits': []} #use later to log playing history
def show_hand(self):
"""Print the value of the player's hand."""
print(self.name + ' current hand: ')
for c in self.hand.cards:
print(c)
print('For a total of: ' + str(self.hand.total))
def play_hand(self, deck, dealer):
"""Controls the player logic to hit, stand, or double down. Returns player choice to handle case with double dowm"""
hit = 1
while self.hand.total < 21 and hit == 1:
hit = self.hit_or_stand(self.help, deck, dealer)
self.move_number = self.move_number + 1
if not hit == 1:
break
self.hand.draw_from_deck(deck)
self.show_hand()
if hit == 2:
self.credits = self.credits - self.bet #remove the additional wager from player account
self.bet = self.bet * 2 #update the bet field to reflect double down
print(self.name + " doubles down. Total wager: " + str(self.bet))
self.hand.draw_from_deck(deck)
self.show_hand()
return
if hit == 0 or self.hand.total >= 21:
return
def get_bust_probability(self, deck, dealer):
margin = 21 - self.hand.total
deck.cards.append(dealer.hand.cards[
1]) #we need to put back the dealer's hidden card since we cannot account for it in the probabilities
over_margin = len([c for c in deck.cards if c.value > margin]) * 1.0
deck.cards.remove(dealer.hand.cards[
1]) #remove the dealer's hidden card that we had inserted to compute accurate probabilities
return round((over_margin / len(Deck().cards)) * 100.0) #in case there is more than a single deck
def hit_or_stand(self, help, deck, dealer):
"""Prompt the player to hit, stand, or double her wager. Give bust probabilities if the player asked for help"""
risk = self.get_bust_probability(deck, dealer)
move = 1 #assume the player hits
if len(self.hand.cards) == 2:
if not help:
choice = raw_input('Press any key to Hit, "s" to [s]tand, or "d" to [d]ouble down > ')
else:
choice = raw_input('Press any key to Hit, "s" to [s]tand, or "d" to [d]ouble down. You have a ' + str(
risk) + ' percent probability of busting >')
else: #if your have more than two cards you can't double down
if not help:
choice = raw_input('Press any key to Hit or "s" to [s]tand > ')
else:
choice = raw_input('Press any key to Hit or "s" to [s]tand. You have a ' + str(
risk) + ' percent probability of busting >')
if choice == "s":
move = 0
if choice == "d" and len(self.hand.cards) == 2:
move = 2
self.log_move_and_risk(move, risk)
return move
def log_move_and_risk(self, move, risk):
self.history['moves'].update({self.move_number: [move, risk]})
class Card():
"""A class to handle card logic"""
FACES = {'Ace': 1, 'Jack': 10, 'King': 10, 'Queen': 10}
def __init__(self, face, suit):
"""initialize a Card object with a face and suit e.g. Card(Ace,Spades)"""
self.face = face
self.suit = suit
def __str__(self):
"""string representation of a Card e.g. Ace of Spades"""
return "{0.face} of {0.suit}".format(self)
@property
def value(self):
"""Return the face value of a card e.g. 7"""
return self.FACES.get(self.face, self.face)
class Hand():
"""A list of Card objects"""
def __init__(self):
"""Initializes an empty list of Card objects"""
self.cards = []
def draw_from_deck(self, deck):
"""Draws the card on top of the deck."""
self.cards.append(deck.next_card())
def return_to_deck(self, deck):
"""Clears the hand and places the cards in hand back into the deck."""
for c in self.cards:
deck.cards.append(c)
del self.cards[:]
@property
def total(self):
"""Returns the total value of the cards in the hand. It also checks if there is an Ace to handle soft/hard."""
if any(c.face == 'Ace' for c in self.cards):
total_of_non_ace_cards = sum(c.value for c in self.cards if c.face != 'Ace')
if total_of_non_ace_cards <= 10:
for i in range(len(self.cards)):
if self.cards[i].face == 'Ace':
self.cards[i].value = 11
break
else:
for i in range(len(self.cards)):
if self.cards[i].face == 'Ace' and self.cards[i].value == 11:
self.cards[i].value = 1
break
return sum(c.value for c in self.cards)
else:
return sum(c.value for c in self.cards)
def main():
name = raw_input("Welcome to TextJack! What is your name? > ")
print('Nice to meet you, ' + name + '! Less begin, shall we?')
g = Game(name, 100)
g.start()
print('The game is over. Let us look at your stats. Pictures are better than boring text...')
#show player analytics
a = Analytics(g.player)
a.plot_credit_decay()
a.plot_move_vs_risk()
a.plot_outcome_vs_risk()
if __name__ == '__main__':
main()