-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack_mini_project.py
300 lines (229 loc) · 8.23 KB
/
blackjack_mini_project.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
# Mini-project #6 - Blackjack
import simplegui
import random
# 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 some useful global variables
in_play = False
outcome = ""
score = 0
# 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, state):
card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
if in_play and state == 0:
canvas.draw_image(card_back, (CARD_BACK_CENTER[0],CARD_BACK_CENTER[1]), CARD_BACK_SIZE, [pos[0] + CARD_BACK_CENTER[0], pos[1] + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)
else:
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, play_type):
self.card = []
self.play_type = play_type
pass # create Hand object
def __str__(self):
hand = ""
for card in self.card:
hand = hand + " " + card.get_suit() + card.get_rank()
return "Hand contains " + hand
pass # return a string representation of a hand
def add_card(self, card):
self.card.append(card)
pass # add a card object to a hand
def get_value(self):
hand_value = 0
i = 0
# print len(self.card)
while i < len(self.card):
rank = self.card[i].get_rank()
for key, value in VALUES.items():
if rank == key:
hand_value += value
# print hand_value
if i == len(self.card) - 1:
if rank == 'A':
if hand_value + 10 < 21:
hand_value += 10
# print hand_value
elif hand_value + 10 > 21:
hand_value -= 10
i += 1
return hand_value
# count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
pass # compute the value of the hand, see Blackjack video
def draw(self, canvas, pos):
if self.play_type == "dealer":
i = 0
j = 0
for card in self.card:
if in_play and j == 0:
card.draw(canvas, (5 + i, 50), 0)
j += 1
else:
card.draw(canvas, (5 + i, 50), 1)
i += 72
if self.play_type == "player":
i = 0
for card in self.card:
card.draw(canvas, (5 + i, 195), 1)
i += 72
pass # draw a hand
# define deck class
class Deck:
def __init__(self):
self.deck = []
for suit in SUITS:
for rank in RANKS:
self.deck.append(Card(suit, rank))
pass # create a Deck object
def shuffle(self):
random.shuffle(self.deck)
# shuffle the deck
pass # use random.shuffle()
def deal_card(self):
return self.deck.pop()
pass # deal a card object from the deck
def __str__(self):
deck = ""
for card in self.deck:
deck = deck + " " + card.get_suit() + card.get_rank()
return "Deck contains " + deck
pass # return a string representing the deck
#define event handlers for buttons
def deal():
global outcome, in_play, player, dealer, deck, score
# your code goes here
deck = Deck()
deck.shuffle()
player = Hand("player")
dealer = Hand("dealer")
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
print dealer, player
player.get_value()
dealer.get_value()
outcome = ""
if in_play == True:
score -= 1
in_play = False
deck = Deck()
deck.shuffle()
player = Hand("player")
dealer = Hand("dealer")
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
card = deck.deal_card()
player.add_card(card)
card = deck.deal_card()
dealer.add_card(card)
print dealer, player
player.get_value()
dealer.get_value()
in_play = True
def hit():
global player, deck, outcome, in_play, score
if in_play == True:
if player.get_value() <= 21:
card = deck.deal_card()
player.add_card(card)
if player.get_value() > 21:
print "You have busted"
outcome = "You have busted"
score -= 1
in_play = False
print outcome, player
pass # replace with your code below
# if the hand is in play, hit the player
# if busted, assign a message to outcome, update in_play and score
def stand():
global dealer, deck, player, outcome, in_play, score
if in_play == True:
while dealer.get_value() < 17:
card = deck.deal_card()
dealer.add_card(card)
if dealer.get_value() > 21:
print "Dealer busted"
if player.get_value() <= 21:
print "Player wins"
outcome = "Dealer busted Player wins"
score += 1
in_play = False
else:
outcome = "Player and dealer busted"
score -= 1
in_play = False
elif player.get_value() <= dealer.get_value():
print "Dealer wins"
score -= 1
outcome = "Dealer wins"
in_play = False
else:
print "Player wins"
score += 1
outcome = "Player wins"
in_play = False
print outcome, dealer
pass # replace with your code below
# if hand is in play, repeatedly hit dealer until his hand has value 17 or more
# assign a message to outcome, update in_play and score
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
global outcome, in_play
# card = Card("S", "A")
# card.draw(canvas, [300, 300])
dealer.draw(canvas, [36, 48])
player.draw(canvas, [36, 48])
canvas.draw_text('Blackjack', (250, 25), 25, 'white', 'serif')
canvas.draw_text('Player', (5, 185), 25, 'white', 'serif')
canvas.draw_text('Dealer', (5, 40), 25, 'white', 'serif')
canvas.draw_text(outcome, (150, 450), 25, 'white', 'serif')
canvas.draw_text("Score = " + str(score), (150, 400), 25, 'white', 'serif')
if in_play == True:
canvas.draw_text("Hit of Stand?", (150, 350), 25, 'white', 'serif')
else:
canvas.draw_text("New Deal?", (150, 350), 25, 'white', 'serif')
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("Green")
#create buttons and canvas callback
frame.add_button("Deal", deal, 200)
frame.add_button("Hit", hit, 200)
frame.add_button("Stand", stand, 200)
frame.set_draw_handler(draw)
# get things rolling
deal()
frame.start()
# remember to review the gradic rubric