Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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 = ""
- 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.card_list = []
- def __str__(self):
- converted = "" # return a string representation of a hand
- for card in self.card_list:
- converted += str(card) + " "
- message = "Hand contains " + converted
- return message
- def add_card(self, card):
- self.card_list.append(card) # add a card object to a hand
- return self.card_list
- 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
- values_list = []
- rank_list = []
- for i in self.card_list:
- i = str(i)
- rank_list.append(i[1])
- values_list.append(VALUES.get(i[1]))
- if "A" in rank_list:
- if sum(values_list) + 10 <= 21:
- return sum(values_list) + 10
- else:
- return sum(values_list)
- else:
- return sum(values_list)
- 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):
- # create a Deck object
- self.deck_list = []
- for suit in SUITS:
- for rank in RANKS:
- self.deck_list.append(Card(suit, rank))
- def shuffle(self):
- # shuffle the deck
- random.shuffle(self.deck_list)
- def deal_card(self):
- # deal a card object from the deck
- new_card = self.deck_list.pop(-1)
- return new_card
- def __str__(self):
- # return a string representing the deck
- str_deck = ""
- for i in self.deck_list:
- str_deck += str(i) + " "
- message = "Deck contains " + str_deck
- return message
- #define event handlers for buttons
- def deal():
- global outcome, in_play, shuffled_deck, player_hand, dealer_hand
- shuffled_deck = Deck()
- shuffled_deck.shuffle()
- player_hand = Hand()
- dealer_hand = Hand()
- player_hand.add_card(shuffled_deck.deal_card())
- player_hand.add_card(shuffled_deck.deal_card())
- dealer_hand.add_card(shuffled_deck.deal_card())
- dealer_hand.add_card(shuffled_deck.deal_card())
- print "Player", player_hand
- print "Dealer", dealer_hand
- in_play = True
- def hit():
- if player_hand.get_value() <= 21:
- player_hand.add_card(shuffled_deck.deal_card())
- print "Hit. Player", player_hand
- else:
- print "You have busted"
- # 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
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement