Skip to content

Instantly share code, notes, and snippets.

@aswearin
Last active September 28, 2015 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aswearin/58c44ba19ab876022b0d to your computer and use it in GitHub Desktop.
Save aswearin/58c44ba19ab876022b0d to your computer and use it in GitHub Desktop.
Coursera Week 6 mini-project - in progress - suggestions welcome
# Mini-project #6 - Blackjack
#codeskulptor URL http://www.codeskulptor.org/#user40_OeuznosqZM_6., http://www.codeskulptor.org/#user40_Mc8uD9d4lL_3.py
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):
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 = []
def __str__(self):
text = "Hand contains"
for i in range(len(self.hand)):
text += ' ' + str(self.hand[i])
return text
def add_card(self, card):
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
# compute the value of the hand, see Blackjack video
hand_value = 0
card_ranks = []
for card in self.hand:
hand_value += VALUES[card.rank]
for card in self.hand:
card_ranks.append(card.rank)
if 'A' in card_ranks and hand_value <= 11:
return hand_value + 10
return hand_value
def draw(self, canvas, pos):
pass # draw a hand on the canvas, use the draw method for cards
# define deck class
class Deck:
def __init__(self):
self.deck = []
for i in range(len(SUITS)):
for j in range(len(RANKS)):
self.deck.append(Card(SUITS[i],RANKS[j]))
def shuffle(self):
random.shuffle(self.deck)
def deal_card(self):
return self.deck.pop(0)
def __str__(self):
text = "Deck contains"
for i in range(len(self.deck)):
text += ' ' + str(self.deck[i])
return text
#define event handlers for buttons
def deal():
global deck, outcome, in_play, score, hand_player, hand_dealer
in_play = True
hand_player = Hand()
hand_dealer = Hand()
deck = Deck()
deck.shuffle()
hand_player.add_card(deck.deal_card())
hand_player.add_card(deck.deal_card())
hand_dealer.add_card(deck.deal_card())
hand_dealer.add_card(deck.deal_card())
print hand_player
print hand_dealer
def hit():
# replace with your code below
# if the hand is in play, hit the player
global deck, outcome, in_play, score, hand_player, hand_dealer, hand_value
if in_play == True and hand_player.get_value() == 21:
score += 1
in_play = False
print "Well played, sir"
elif in_play == True and hand_player.get_value() > 21:
in_play = False
print "Pack it in"
if in_play == True and hand_player.get_value() < 21:
print "Hitting..."
hand_player.add_card(deck.deal_card())
print hand_player
# if busted, assign a message to outcome, update in_play and score
def stand():
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
def stand():
# replace with your code below
global deck, outcome, in_play, score, hand_player, hand_dealer, hand_value
while in_play == True and hand_dealer.get_value() >= 17:
hand_dealer.add_card(deck.deal_card())
if hand_dealer.get_value() == 21:
print "Dealer wins"
elif hand_player.get_value() <= hand_dealer.get_value():
print "Dealer wins"
elif hand_dealer.get_value() > 21:
score += 1
print "Player wins"
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
card = Card("S", "A")
card.draw(canvas, [300, 300])
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment