-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex45.blackjack.py
401 lines (331 loc) · 12 KB
/
ex45.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
# Learn Python the Hard Way
# Exercise 45 - Blackjack
from sys import exit
import random
class Engine(object):
def run(self):
print "-" * 35
print "Welcome to Wayne's Blackjack table!"
print "-" * 35
self.menu()
def menu(self):
print """
Menu:
1. Start Game
2. Rules
3. Exit
"""
try:
menu_choice = int(raw_input("> "))
except ValueError:
print "Invalid Menu Choice. Try again."
self.menu()
if menu_choice == 1:
Blackjack().start()
elif menu_choice == 2:
Rules().show()
self.menu()
elif menu_choice == 3:
print "Thanks for playing!"
exit(1)
else:
print "Invalid Menu Choice. Try again."
self.menu()
class Rules(object):
def show(self):
print """
Objective: Beat the dealer by getting a count as close to 21 as possible, without going over 21.\n
Rules:
\t1. Card Values/Scoring - It is up to each individual player if an ace is worth 1 or 11.
\t2. Face cards are 10 and any other card is its pip value.
\t3. You'll be dealt two cards face up.
\t4. If you want another card to try and get you closer to 21, you 'Hit'.
\t5. To stick with what you've got, you 'Stand'.
\t6. If you're dealt an ace and 10 as your first two cards, that's a blackjack.
\t7. This is an automatic win for you unless the dealer gets the same. If this happens, it's called a push and no one wins.
"""
class Blackjack(object):
game_point = 0
current_score = [0, 0]
dealer_hand = []
player_hand = []
limit_hand_value = 21
stand_hand_value = 17
power_cards = ["Ace", "10", "Jack", "Queen", "King"]
def get_game_point(self):
return self.game_point
def set_game_point(self, gp):
self.game_point = gp
def reset_score(self):
self.current_score = [0, 0]
def get_current_score(self):
return self.current_score
def set_dealer_score(self, dealer_score):
self.current_score[0] = dealer_score
def set_player_score(self, player_score):
self.current_score[1] = player_score
def get_hand_value(self, hand):
total_value = 0
num_of_aces = 0
num_of_deductions_avail = 0
num_of_cards_in_hand = len(hand)
for each_card in hand:
value = each_card.split(' ', 1)[0]
if value == self.power_cards[0]:
num_of_aces += 1
num_of_deductions_avail += 1
total_value += 11
elif value in self.power_cards:
total_value += 10
else:
total_value += int(value)
while total_value > self.limit_hand_value and num_of_aces > 0 and num_of_deductions_avail > 0:
total_value -= 10 # Ace value reduced from 11 to 1
num_of_deductions_avail -= 1
return total_value
def start(self):
print "How many games to victory?"
try:
self.game_point = int(raw_input("> "))
except ValueError:
print "Invalid. Input integer."
return self.start()
self.set_game_point(self.game_point)
self.reset_score()
deck_of_cards = Deck().get_cards()
while self.game_point != max(self.current_score):
self.show_score()
self.shuffle_deck(deck_of_cards)
self.deal(deck_of_cards)
self.show_table(False)
if self.check_blackjack():
continue
else:
pass
player_stand = False
player_busted = False
while not player_stand and not player_busted:
player_choice = self.player_decision()
if player_choice == 1:
self.hit(deck_of_cards, self.player_hand)
self.show_table(False)
player_busted = self.if_player_busted(self.player_hand)
elif player_choice == 2:
player_stand = True
else:
pass
if player_busted:
print "Dealer wins the round!"
self.set_dealer_score(self.current_score[0]+1)
continue
else:
pass
self.dealer_decision(deck_of_cards)
self.show_hand()
raw_input("\nPress Enter to Start Next Hand\n")
self.show_score()
self.finished()
def finished(self):
final_score = self.get_current_score()
if final_score[0] > final_score[1]:
print "You Lose! Dealer Win!"
elif final_score[0] < final_score[1]:
print "Congratulations! You Won!"
else:
print "It's a draw!"
Engine().menu()
def show_score(self):
print """
\tGame to %d
Score:
\tDealer: %d
\tPlayer: %d
""" % (self.game_point, self.current_score[0], self.current_score[1])
def shuffle_deck(self, deck):
deck.extend(self.player_hand)
deck.extend(self.dealer_hand)
self.player_hand[:] = []
self.dealer_hand[:] = []
random.shuffle(deck)
def deal(self, deck):
for i in range(0,2):
self.player_hand.append(deck.pop(0))
self.dealer_hand.append(deck.pop(0))
def show_table(self, show_hand):
if show_hand:
print "\nDealer have: ", self.dealer_hand
print "Hand Value:", self.get_hand_value(self.dealer_hand)
print "You have: ", self.player_hand
print "Hand value:", self.get_hand_value(self.player_hand)
else:
print "\nDealer have: ['HIDDEN']", self.dealer_hand[1:len(self.dealer_hand)]
print "You have: ", self.player_hand
print "Hand value:", self.get_hand_value(self.player_hand)
def check_blackjack(self):
d_bj = self.dealer_has_blackjack()
p_bj = self.player_has_blackjack()
if d_bj and p_bj:
print "Both of you got blackjacks!"
self.set_dealer_score(self.current_score[0]+1)
self.set_player_score(self.current_score[1]+1)
return True
elif d_bj:
print "Dealer has a blackjack!"
self.set_dealer_score(self.current_score[0]+1)
return True
elif p_bj:
print "Player got a blackjack!"
self.set_player_score(self.current_score[1]+1)
return True
else:
return False
def dealer_has_blackjack(self):
first_card = self.dealer_hand[0].split(' ', 1)[0]
second_card = self.dealer_hand[1].split(' ', 1)[0]
if (first_card == self.power_cards[0] and second_card in self.power_cards) or (first_card in self.power_cards and second_card == self.power_cards[0]):
return True
else:
return False
def player_has_blackjack(self):
first_card = self.player_hand[0].split(' ', 1)[0]
second_card = self.player_hand[1].split(' ', 1)[0]
if (first_card == self.power_cards[0] and second_card in self.power_cards) or (first_card in self.power_cards and second_card == self.power_cards[0]):
return True
else:
return False
def player_decision(self):
options = [
"1. Hit",
"2. Stand"
]
must_hit = None
player_hand_value = self.get_hand_value(self.player_hand)
if player_hand_value < self.stand_hand_value:
must_hit = True
print options[0]
else:
must_hit = False
print options[0]
print options[1]
while True:
try:
choice = int(raw_input("> "))
if must_hit == True and choice != 1:
continue
elif choice != 1 and choice != 2:
continue
else:
pass
break
except ValueError:
print "Invalid. Input integer choice."
return choice
def hit(self, deck, hand):
hand.append(deck.pop(0))
def if_player_busted(self, hand):
exposed_hand_value = self.get_hand_value(hand[1:len(hand)])
if exposed_hand_value >= self.limit_hand_value:
return True
else:
return False
def dealer_decision(self, deck):
dealer_hand_value = self.get_hand_value(self.dealer_hand)
dealer_busted = False
while dealer_hand_value < self.stand_hand_value:
self.hit(deck, self.dealer_hand)
self.show_table(False)
dealer_hand_value = self.get_hand_value(self.dealer_hand)
# Algorithm to implement in order to beat human
exposed_player_hand_value = self.get_hand_value(self.player_hand[1:len(self.player_hand)])
player_likely_to_bust = self.limit_hand_value - exposed_player_hand_value < 3
dealer_busted = self.get_hand_value(self.dealer_hand) > self.limit_hand_value
dealer_likely_to_bust = self.limit_hand_value - dealer_hand_value < 3
while not dealer_busted and not dealer_likely_to_bust and not player_likely_to_bust:
dealer_hand_value = self.get_hand_value(self.dealer_hand)
if dealer_hand_value - exposed_player_hand_value < 6:
self.hit(deck, self.dealer_hand)
self.show_table(False)
return self.dealer_decision(deck)
else:
break
dealer_busted = self.get_hand_value(self.dealer_hand) > self.limit_hand_value
def show_hand(self):
self.show_table(True)
dealer_hand_value = self.get_hand_value(self.dealer_hand)
player_hand_value = self.get_hand_value(self.player_hand)
dealer_overlimit = dealer_hand_value > self.limit_hand_value
player_overlimit = player_hand_value > self.limit_hand_value
if (dealer_overlimit and player_overlimit) or (dealer_hand_value == player_hand_value):
print "Draw!"
elif dealer_overlimit:
print "Player wins the round!"
self.set_player_score(self.current_score[1]+1)
elif player_overlimit:
print "Dealer wins the round!"
self.set_dealer_score(self.current_score[0]+1)
elif dealer_hand_value > player_hand_value:
print "Dealer wins the round!"
self.set_dealer_score(self.current_score[0]+1)
elif player_hand_value > dealer_hand_value:
print "Player wins the round!"
self.set_player_score(self.current_score[1]+1)
else:
pass
class Deck(object):
cards = [
"Ace of Diamonds",
"2 of Diamonds",
"3 of Diamonds",
"4 of Diamonds",
"5 of Diamonds",
"6 of Diamonds",
"7 of Diamonds",
"8 of Diamonds",
"9 of Diamonds",
"10 of Diamonds",
"Jack of Diamonds",
"Queen of Diamonds",
"King of Diamonds",
"Ace of Clubs",
"2 of Clubs",
"3 of Clubs",
"4 of Clubs",
"5 of Clubs",
"6 of Clubs",
"7 of Clubs",
"8 of Clubs",
"9 of Clubs",
"10 of Clubs",
"Jack of Clubs",
"Queen of Clubs",
"King of Clubs",
"Ace of Hearts",
"2 of Hearts",
"3 of Hearts",
"4 of Hearts",
"5 of Hearts",
"6 of Hearts",
"7 of Hearts",
"8 of Hearts",
"9 of Hearts",
"10 of Hearts",
"Jack of Hearts",
"Queen of Hearts",
"King of Hearts",
"Ace of Spades",
"2 of Spades",
"3 of Spades",
"4 of Spades",
"5 of Spades",
"6 of Spades",
"7 of Spades",
"8 of Spades",
"9 of Spades",
"10 of Spades",
"Jack of Spades",
"Queen of Spades",
"King of Spades"
]
def get_cards(self):
return self.cards
Engine().run()