-
Notifications
You must be signed in to change notification settings - Fork 1
/
blackjack.py
203 lines (170 loc) · 6.38 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
import simplegui
import random
# load card sprite - 949x392 - 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")
# initialize global variables
card_in_play = False
message = ""
outcome = ""
score = 0
removed = []
player = []
dealer = []
deck = []
# 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}
# Card class. Hand class calls this draw method for rendering card images onto canvas
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
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)
# Hand class used for adding card objects from Deck() and for getting the value of hands
class Hand:
def __init__(self):
self.player_hand = []
def __str__(self):
s = ''
for c in self.player_hand:
s = s + str(c) + ' '
return s
def add_card(self, card):
self.player_hand.append(card)
return self.player_hand
def get_value(self):
value = 0
for card in self.player_hand:
rank = card.get_rank()
value = value + VALUES[rank]
for card in self.player_hand:
rank = card.get_rank()
if rank == 'A' and value <= 11:
value += 10
return value
def draw(self, canvas, p):
pos = p
for card in self.player_hand:
card.draw(canvas, p)
pos[0] = pos[0] + 90
if card_in_play == True:
canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [115.5,184], CARD_BACK_SIZE)
# Deck class used for re-shuffling between hands and giving card objects to Hand as called
class Deck:
def __init__(self):
removed = []
self.cards = [Card(suit, rank) for suit in SUITS for rank in RANKS]
self.shuffle()
def __str__(self):
s = ''
for c in self.cards:
s = s + str(c) + ' '
return s
def shuffle(self):
random.shuffle(self.cards)
def deal_card(self):
removed = self.cards.pop(0)
return removed
def deal():
# deal function deals initial hands and adjusts message.
global card_in_play, player, dealer, deck, message, score, outcome
if card_in_play == True:
# if player clicks Deal button during a hand, player loses hand in progress
message = "Here is the new hand"
score -= 1
deck = Deck()
player = Hand()
dealer = Hand()
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
if card_in_play == False:
# starts a new hand
deck = Deck()
player = Hand()
dealer = Hand()
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
player.add_card(deck.deal_card())
dealer.add_card(deck.deal_card())
message = "New Hand. Hit or Stand?"
card_in_play = True
outcome = ""
def hit():
# deals player a new hand and ends hand if it causes a bust.
global card_in_play, score, message
if card_in_play == True:
player.add_card(deck.deal_card())
message = "Hit or Stand?"
if player.get_value() > 21:
card_in_play = False
message = "Player busted! You Lose! Click deal to play again."
score -= 1
outcome = "Dealer: " + str(dealer.get_value()) + " Player: " + str(player.get_value())
def stand():
# hits dealer until >=17 or busts. Determines winner of hand and adjusts score, game state, and messages
global card_in_play, score, message, outcome
if card_in_play == False:
message = "The hand is already over. Deal again."
else:
while dealer.get_value() < 17:
dealer.add_card(deck.deal_card())
if dealer.get_value() > 21:
message = "Dealer busted. You win! Click deal to play again."
score += 1
card_in_play = False
elif dealer.get_value() > player.get_value():
message = "Dealer wins! Click deal to play again."
score -= 1
card_in_play = False
elif dealer.get_value() == player.get_value():
message = "You Tie! Dealer wins! Click deal to play again."
score -= 1
card_in_play = False
elif dealer.get_value() < player.get_value():
message = "You win! Click deal to play again."
score += 1
card_in_play = False
outcome = "Dealer: " + str(dealer.get_value()) + " Player: " + str(player.get_value())
# draw handler
def draw(canvas):
canvas.draw_text("Blackjack", [270,50], 48, "Black")
canvas.draw_text("Score : " + str(score), [80,520], 36, "Black")
canvas.draw_text("Dealer :", [80,110], 30, "Green")
canvas.draw_text("Player :", [80,300], 30, "Blue")
canvas.draw_text(message, [150,480], 26, "Green")
canvas.draw_text(outcome, [80,560], 28, "Blue")
dealer.draw(canvas, [80,135])
player.draw(canvas, [80,325])
# initialization frame
frame = simplegui.create_frame("Blackjack game", 700, 600)
frame.set_canvas_background("Yellow")
# 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)
# deals initial hand
deal()
# get things rolling
frame.start()