http://www.codeskulptor.org/#user38_pPTeGUCnFc_15.py
# 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 = ""
hint_message = ""
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):
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.card_list = []
def __str__(self):
# return a string representation of a hand
ans = "Hand contains "
for i in range(len(self.card_list)):
ans += self.card_list[i].get_suit()+ self.card_list[i].get_rank()+ ' '
return ans
def add_card(self, card):
# add a card object to a hand
self.card_list.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
# compute the value of the hand according to the rules in Blackjack video
hand_value = 0
hasAce = False
for i in range(len(self.card_list)):
hand_value += VALUES[self.card_list[i].get_rank()]
if self.card_list[i].get_rank() == RANKS[0]:
hasAce = True
if not hasAce:
return hand_value
else:
if hand_value + 10 <= 21:
return hand_value + 10
else:
return hand_value
def draw(self, canvas, pos):
# draw a hand on the canvas, use the draw method for cards
for i in range(len(self.card_list)):
card_temp = Card(self.card_list[i].get_suit(),self.card_list[i].get_rank())
card_temp.draw(canvas,(pos[0]+ (i+0.5) * CARD_SIZE[0]+i*30, pos[1]))
# define deck class
class Deck:
def __init__(self):
# create a Deck object
self.deck_list = [] # Deck is a list of cards
for suit in SUITS:
for rank in RANKS:
new_card = Card(suit,rank)
self.deck_list.append(new_card)
def shuffle(self):
# shuffle the deck
random.shuffle(self.deck_list)
def deal_card(self):
# deal a card object from the deck
return self.deck_list.pop(0)
def __str__(self):
ans = "Deck contains "
for card in self.deck_list:
ans += card.get_suit()+card.get_rank()+" "
return ans # return a string representing the deck
#define event handlers for buttons
def deal():
global outcome, in_play,cards_in_Deck,player_hand,dealer_hand,hint_message
player_hand = Hand()
dealer_hand = Hand()
cards_in_Deck = Deck()
cards_in_Deck = Deck()
cards_in_Deck.shuffle()
player_hand.add_card(cards_in_Deck.deal_card())
player_hand.add_card(cards_in_Deck.deal_card())
dealer_hand.add_card(cards_in_Deck.deal_card())
dealer_hand.add_card(cards_in_Deck.deal_card())
in_play = True
hint_message = 'Hit or stand?'
def hit():
global player_hand,hint_message,outcome,score,in_play
in_play = True
# if the hand is in play, hit the player
player_hand.add_card(cards_in_Deck.deal_card())
value_in_hand = player_hand.get_value()
if value_in_hand <=21:
hint_message = 'Hit or stand?'
else: # if busted, assign a message to outcome, update in_play and score
outcome = "You have busted!"
hint_message = "New deal?"
score -=1
def stand():
global dealer_hand,hint_message,outcome,score,in_play
in_play = False
hint_message = "New deal?"
value_of_dealer_hand = dealer_hand.get_value()
while value_of_dealer_hand < 17:
dealer_hand.add_card(cards_in_Deck.deal_card())
value_of_dealer_hand = dealer_hand.get_value()
if dealer_hand.get_value() > 21:
outcome = "You win!"
score +=1
else:
if player_hand.get_value() > dealer_hand.get_value():
outcome = "You win!"
score +=1
else:
outcome = "You lose!"
score -=1
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
global hint_message,outcome,score,player_hand,dealer_hand,in_play
canvas.draw_text('Blackjack', (100, 100), 50, 'Aqua')
canvas.draw_text('score '+str(score), (400, 100), 40,'Black')
canvas.draw_text('Dealer', (100, 200), 40,'Black')
canvas.draw_text('Player', (100, 400), 40,'Black')
canvas.draw_text(outcome, (250, 200), 35,'Red')
canvas.draw_text(hint_message, (300, 400), 40,'Yellow')
player_hand.draw(canvas,(70, 430))
dealer_hand.draw(canvas,(70, 230))
if in_play:
canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [105 + CARD_CENTER[0], 230 + CARD_CENTER[1]], CARD_SIZE)
# 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()