-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiproject-Week6--Blackjack
381 lines (321 loc) · 11.7 KB
/
miniproject-Week6--Blackjack
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
#
# Mini-project #6 - Blackjack
# October 2014
# --agf
#
# This is a heinously rudimentary version of Blackjack.
#
# Every new deal of the cards is from a fresh deck.
# Dealer wins ties.
# Scoring is 1 point each time a hand is won, by either
# the player or the dealer.
#
# No real bells or whistles this time, except the
# standard grey/white color scheme I've used for
# all of my projects so far. Cards are not grey/white,
# but that's OK.
#
# All feedback appreciated!
#
#imports
import simplegui, random, time
# load card sprite - 936x384 - source: jfitz.com
CARD_SIZE = (72, 96)
CARD_CENTER = (36, 48)
card_images = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/cards_jfitz.png")
CARD_BACK_SIZE = (72, 96)
CARD_BACK_CENTER = (36, 48)
card_back = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/card_jfitz_back.png")
# initialize global variables
in_play = False
outcome = ""
playerScore = 0
dealerScore = 0
DSTR = "! -- 'Deal' starts a new game!"
# define globals for cards
SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
# define card class
class Card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
self.suit = None
self.rank = None
print "Invalid card: ", suit, rank
def __str__(self):
return self.suit + self.rank
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def draw(self, canvas, pos):
card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
# define hand class
class Hand:
def __init__(self):
# create Hand object
self.hand = []
def __str__(self):
# return a string representation of a hand
handstr = ""
for c in range(len(self.hand)):
handstr += str(self.hand[c]) + " "
return "Hand contains: " + handstr
def add_card(self, card):
# add a card object to a hand
self.hand.append(card)
def get_value(self):
# count the values of the cards.
# special check for aces, too.
handval = 0
aces = 0
for c in range(len(self.hand)):
if self.hand[c].get_rank() == "A":
aces += 1
handval += VALUES[self.hand[c].get_rank()]
# now check the aces situation
# and adjust card count as necessary
if aces > 0:
if handval + 10 <= 21:
handval += 10
return handval
def draw(self, canvas, pos):
# draw a hand on the canvas, using the draw method for cards
for c in range(len(self.hand)):
self.hand[c].draw(canvas, [pos[0] + (c % 8) * CARD_SIZE[0], pos[1] + (c // 8) * CARD_SIZE[1]])
# define deck class
class Deck:
def __init__(self):
# create a Deck object
self.deck = []
#populate the deck with cards
for suit in SUITS:
for rank in RANKS:
self.deck.append(Card(suit,rank))
self.shuffle()
def shuffle(self):
# shuffle the deck
random.shuffle(self.deck)
def deal_card(self):
# get the number of cards still in the deck
l = len(self.deck)
# as long as there is at least one card
# pull it from the deck
#
# the check here should never actually
# happen, because we create a fresh deck
# with every new round we play
if l <= 0:
print "ERROR, out of cards"
return
return self.deck.pop(0)
def __str__(self):
# return a string representing the deck
# initialize the string we're building
deckstr = ""
# build a string of cards in the deck,
# separated by a space
for c in range(len(self.deck)):
deckstr += str(self.deck[c]) + " "
return deckstr
#define event handlers for buttons
def deal():
global outcome, in_play, newDeck, playerHand, dealerHand, dealerScore
# in_play should not be true here; if it is,
# then someone's trying to re-deal the game
# and they lose a point to the house for this
if in_play == True:
outcome = "Player FOLDS" + DSTR
dealerScore +=1
in_play = False
return
# start the deal
# create a new deck of cards, create two hands
# for player and dealer
in_play = True
outcome = "Game in progress -- 'Hit' or 'Stand'?"
newDeck = Deck()
playerHand = Hand()
dealerHand = Hand()
# each hand starts with two cards;
for count in range(2):
playerHand.add_card(newDeck.deal_card())
dealerHand.add_card(newDeck.deal_card())
def hit():
global playerHand
# if the hand is in play, hit the player
if in_play:
# allow the player the ability to screw
# himself over by allowing to hit at 21;
# any aces can still revert to value of 1
# anyway, as necessary
if playerHand.get_value() < 22:
playerHand.add_card(newDeck.deal_card())
# if busted, assign a message to outcome, update in_play and score
if playerHand.get_value() > 21:
updateScore()
def updateScore():
# checks win conditions, sets outcome strings
# and adjusts scores
global outcome, in_play, dealerScore, playerScore
# save myself some typing here
dval = dealerHand.get_value()
pval = playerHand.get_value()
# win conditions/checks
# outcome messages should make these checks
# pretty obvious as to what they are/do
if dval > 21:
outcome = "Dealer BUSTS with " + str(dval) + DSTR
playerScore +=1
in_play = False
elif dval <= 21 and dval > pval:
outcome = "Dealer WINS with " + str(dval) + DSTR
dealerScore +=1
in_play = False
elif dval == pval:
outcome = "Dealer WINS a TIE with " + str(dval) + DSTR
dealerScore +=1
in_play = False
elif pval > 21:
outcome = "Player BUSTS with " + str(pval) + DSTR
dealerScore +=1
in_play = False
elif pval <= 21 and pval > dval:
outcome = "Player WINS with " + str(pval) + DSTR
playerScore +=1
in_play = False
def stand():
global in_play, dealerHand
# do not allow standing if game's not active
if in_play == False:
return
# stand ends the play for the round
in_play = False
# here we repeatedly hit the dealer until his
# hand has value 17 or more
while dealerHand.get_value() < 17:
dealerHand.add_card(newDeck.deal_card())
# assign a message to outcome, update in_play and score
updateScore()
# draw handler
def draw(canvas):
global outcome
# Text is drawn twice; a black layer offset a bit to enhance
# visibility between the white text and the grey background
# Also, too many magic numbers throughout for positions, imo
# BLACKJACK game title text
canvas.draw_text('B L A C K J A C K', [12,52], 36,'Black', 'monospace')
canvas.draw_text('B L A C K J A C K', [10,50], 36,'White', 'monospace')
# outcome message text
# this time.time() trick blinks the outcome text during a
# game; in_play check leaves it solid after a win/loss occurs
outcomepos = [12,270]
if int(time.time() * 2) % 2 == 0 or not(in_play):
canvas.draw_text(outcome, [outcomepos[0] + 2, outcomepos[1] + 2], 18,'Black','monospace')
canvas.draw_text(outcome, outcomepos, 18,'White','monospace')
else:
canvas.draw_text("", [outcomepos[0] + 2, outcomepos[1] + 2], 18,'Black','monospace')
canvas.draw_text("", outcomepos, 18,'White','monospace')
# display scores; two-digit padded with 0 for uniformity's sake
canvas.draw_text("Player: " + str(playerScore).rjust(2,'0'), [452,37], 18,'Black','monospace')
canvas.draw_text("Player: " + str(playerScore).rjust(2,'0'), [450,35], 18,'White','monospace')
canvas.draw_text("Dealer: " + str(dealerScore).rjust(2,'0'), [452,57], 18,'Black','monospace')
canvas.draw_text("Dealer: " + str(dealerScore).rjust(2,'0'), [450,55], 18,'White','monospace')
# display hand headers with card counts
canvas.draw_text("Player hand: (" + str(playerHand.get_value()) + ")", [14,112], 12,'Black','monospace')
canvas.draw_text("Player hand: (" + str(playerHand.get_value()) + ")", [12,110], 12,'White','monospace')
# don't give away dealer's card count, though,
# if a game's still going on
if in_play:
# hide the dealer count
canvas.draw_text("Dealer hand: (??)", [14,322], 12,'Black','monospace')
canvas.draw_text("Dealer hand: (??)", [12,320], 12,'White','monospace')
else:
# show the dealer count
canvas.draw_text("Dealer hand: (" + str(dealerHand.get_value()) + ")", [14,322], 12,'Black','monospace')
canvas.draw_text("Dealer hand: (" + str(dealerHand.get_value()) + ")", [12,320], 12,'White','monospace')
# draw player hands
playerHand.draw(canvas,[10, 120])
dealerHand.draw(canvas,[10, 330])
# this keeps the dealer hole covered during hand play
if in_play:
canvas.draw_image(card_back, [CARD_BACK_CENTER[0], CARD_BACK_CENTER[1]], [CARD_BACK_SIZE[0], CARD_BACK_SIZE[1]],[10 + CARD_BACK_CENTER[0], 330 + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)
def new_game():
global playerScore, dealerScore, in_play
# resets the scores that aren't reset each Deal
# and restarts the game
playerScore = 0
dealerScore = 0
in_play = False
deal()
def keydown(key):
# keyboard shortcuts
if key == simplegui.KEY_MAP['S']:
stand()
elif key == simplegui.KEY_MAP['H']:
hit()
elif key == simplegui.KEY_MAP['D']:
deal()
else:
return
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("#505050")
# set up the frame
frame.add_label("")
frame.add_label("B L A C K J A C K")
frame.add_label("A 'greatly simplified' version")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_button("Deal", deal, 200)
frame.add_label("")
frame.add_button("Hit", hit, 200)
frame.add_label("")
frame.add_button("Stand", stand, 200)
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("Optional keyboard shortcuts:")
frame.add_label("[D] - Deal")
frame.add_label("[H] - Hit")
frame.add_label("[S] - Stand")
# there really needs to be a better way
# than this for positioning these things
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_label("")
frame.add_button("Reset game/scores", new_game, 200)
# draw and key handlahs
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
# get things rolling
frame.start()
deal()
print """B L A C K J A C K
------------------
Buttons:
[Deal] Starts a new round
[Hit] Deals player another card
[Stand] Calls for dealer to show
[Reset] resets all scores
and starts a new game
optional keyboard shortcuts:
[D] - Deal
[H] - Hit
[S] - Stand
Thanks for playing my game!
I appreciate any feedback. :)"""
## END ####################