-
Notifications
You must be signed in to change notification settings - Fork 1
/
blackjack.py
executable file
·236 lines (202 loc) · 6.24 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
"""
Name: Blackjack mini-project.
Author: jraleman
Year: 2013
"""
try:
import simplegui
except ImportError:
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import random
# Load card sprite - 949x392pixels (source: jfitz.com)
CARD_SIZE = (73, 98)
CARD_CENTER = (36.5, 49)
CARD_IMAGES = simplegui.load_image \
("http://commondatastorage.googleapis.com/codeskulptor-assets/cards.jfitz.png")
CARD_BACK_SIZE = (71, 96)
CARD_BACK_CENTER = (35.5, 48)
CARD_BACK = simplegui.load_image \
("http://commondatastorage.googleapis.com/codeskulptor-assets/CARD_BACK.png")
# Define global variables for cards and stats.
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}
IN_PLAY = False
OUTCOME = ""
SCORE = 0
class Card:
"""
Define class for the cards.
"""
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):
"""
Get suit
"""
return self.suit
def get_rank(self):
"""
Get rank.
"""
return self.rank
def draw(self, canvas, pos):
"""
Draw the cards.
"""
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)
class Hand:
"""
Define class for current values (hand).
"""
def __init__(self):
self.hand = []
# Return a string representation of a hand
def __str__(self):
str_hand = ""
i = 0
while i in range(0, len(self.hand)):
str_hand = str_hand + (str(self.hand[i]) + " ")
i += 1
return str_hand
def add_card(self, card):
"""
Append a card to the hand.
"""
self.hand.append(card)
def get_value(self):
"""
Count aces as 1, if the hand has an ace,
then add 10 to hand value if it doesn't bust
"""
value = 0
ace = False
for card in self.hand:
value = value + VALUES[card.get_rank()]
if card.get_rank() == 'A':
ace = True
if ace and value + 10 <= 21:
value += 10
return value
def draw(self, canvas, pos):
"""
Draw a hand on the canvas, use the draw method for cards
"""
c = 0
for card in self.hand:
card.draw(canvas, [pos[0] + CARD_SIZE[0] * c, pos[1]])
c += 1
class Deck:
"""
Define class for the deck.
"""
# Create a Deck object
def __init__(self):
self.deck = []
for suit in SUITS:
for rank in RANKS:
self.deck.append(Card(suit, rank))
def shuffle(self):
"""
Shuffle the deck
"""
random.shuffle(self.deck)
def deal_card(self):
"""
Deal a card object from the deck
"""
return self.deck.pop()
# Return a string representing the deck
def __str__(self):
return str(self.deck)
def deal():
"""
Define event handlers for 'deal' button.
"""
global OUTCOME, IN_PLAY, player, dealer, deck, SCORE
OUTCOME = "Hit or Stand?"
deck = Deck()
deck.shuffle()
player, dealer = Hand(), Hand()
player.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
if IN_PLAY:
SCORE -= 1
IN_PLAY = True
def hit():
"""
Define event handlers for 'hit' button.
"""
global OUTCOME, SCORE, IN_PLAY
if not IN_PLAY:
return
# If the hand is in play, hit the player
if player.get_value() <= 21:
player.add_card(deck.deal_card())
# If busted, assign a message to OUTCOME, update IN_PLAY and SCORE
if player.get_value() > 21:
OUTCOME = "Sorry! You busted... New Deal?"
SCORE -= 1
IN_PLAY = False
def stand():
"""
Define event handlers for 'stand' button.
"""
global OUTCOME, IN_PLAY, SCORE
# If hand is in play, repeatedly hit dealer until
# his hand has a value of 17 or more.
if not IN_PLAY:
return
while dealer.get_value() < 17:
dealer.add_card(deck.deal_card())
# Assign a message to OUTCOME, update IN_PLAY and SCORE
if dealer.get_value() > 21:
OUTCOME = "The Dealer has busted! New Deal?"
SCORE += 1
else:
if player.get_value() <= dealer.get_value():
OUTCOME = "The dealer wins... New Deal?"
SCORE -= 1
else:
OUTCOME = "You win! New Deal?"
SCORE += 1
IN_PLAY = False
def draw(canvas):
"""
Draw handler
"""
canvas.draw_text("Blackjack: The Game", (80, 60), 50, "#8C001A")
canvas.draw_text("Score: " + str(SCORE), (250, 570), 30, "#FCDFFF")
canvas.draw_text("Player's Hand: " + str(player.get_value()), \
(95, 495), 16, "#FFCBA4")
canvas.draw_text("Player:", (75, 350), 25, "#483C32")
canvas.draw_text("Dealer:", (75, 130), 25, "#483C32")
canvas.draw_text(OUTCOME, (125, 290), 25, "#E0FFFF")
player.draw(canvas, [75, 370])
dealer.draw(canvas, [75, 150])
if IN_PLAY:
canvas.draw_image(CARD_BACK, CARD_BACK_CENTER, CARD_BACK_SIZE, \
[75 + CARD_BACK_CENTER[0], 150 + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)
# Get things rolling
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("#347C17")
frame.add_button("Deal", deal, 150)
frame.add_button("Hit", hit, 150)
frame.add_button("Stand", stand, 150)
frame.set_draw_handler(draw)
deal()
frame.start()