# Mini-project #6 - Blackjack # by Sean 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 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): 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): self.hand = [] hand_value = 0 def __str__(self): return str(self.hand) def hit(self, card): self.hand.append(card) # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust def get_value(self): self.hand_value = sum([VALUES[card.get_rank()] for card in self.hand]) if 'A' not in [card.get_rank() for card in self.hand]: return self.hand_value else: if self.hand_value + 10 <= 21: return self.hand_value + 10 else: return self.hand_value def busted(self): return not self.get_value() <= 21 def draw(self, canvas, p): space = 100 for i in range(len(self.hand)): self.hand[i].draw(canvas, [p[0] + space * i, p[1]]) if in_play: if p[1] == 175: canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [p[0] + CARD_CENTER[0], p[1] + CARD_CENTER[1]], CARD_SIZE) # define deck class class Deck: def __init__(self): self.deck = [Card(suit, rank) for suit in SUITS for rank in RANKS] # add cards back to deck and shuffle def shuffle(self): random.shuffle(self.deck) def deal_card(self): return self.deck.pop() #define event handlers for buttons def deal(): global outcome, in_play, deck, dealer, player outcome = "" deck = Deck() dealer = Hand() player = Hand() deck.shuffle() for i in range(0, 2): dealer.hit(deck.deal_card()) player.hit(deck.deal_card()) in_play = True def hit(): global outcome, in_play, score if in_play: if not player.busted(): player.hit(deck.deal_card()) if player.get_value() > 21: outcome = "You have busted and lose." in_play = False score -= 1 def stand(): # if hand is in play, repeatedly hit dealer until his hand has value 17 or more global outcome, in_play, dealer, score if in_play: while dealer.get_value() < 17: dealer.hit(deck.deal_card()) if dealer.get_value() > 21 or dealer.get_value() < player.get_value(): outcome = "You win!" score += 1 else: outcome = "You lose." score -= 1 in_play = False # draw handler def draw(canvas): # test to make sure that card.draw works, replace with your code below canvas.draw_text("Blackjack", [120, 90], 35, "Aqua") canvas.draw_text("Dealer", [100, 150], 25, "Black") canvas.draw_text("Player", [100, 350], 25, "Black") canvas.draw_text(outcome, [250, 150], 20, "Black") if in_play: canvas.draw_text("Hit or stand?", [250, 350], 20, "Black") else: canvas.draw_text("New deal?", [250, 350], 20, "Black") canvas.draw_text("Score " + str(score), [400, 90], 20, "Black") dealer.draw(canvas, [100, 175]) player.draw(canvas, [100, 375]) # 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) # deal an initial hand deal() # get things rolling frame.start()