Advertisement
ansakoy

Blackjack

Oct 26th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. # Mini-project #6 - Blackjack
  2.  
  3. import simplegui
  4. import random
  5.  
  6. # load card sprite - 936x384 - source: jfitz.com
  7. CARD_SIZE = (72, 96)
  8. CARD_CENTER = (36, 48)
  9. card_images = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/cards_jfitz.png")
  10.  
  11. CARD_BACK_SIZE = (72, 96)
  12. CARD_BACK_CENTER = (36, 48)
  13. card_back = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/card_jfitz_back.png")    
  14.  
  15. # initialize some useful global variables
  16. in_play = False
  17. outcome = ""
  18. score = 0
  19.  
  20.  
  21. # define globals for cards
  22. SUITS = ('C', 'S', 'H', 'D')
  23. RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
  24. 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}
  25.  
  26.  
  27. # define card class
  28. class Card:
  29.     def __init__(self, suit, rank):
  30.         if (suit in SUITS) and (rank in RANKS):
  31.             self.suit = suit
  32.             self.rank = rank
  33.         else:
  34.             self.suit = None
  35.             self.rank = None
  36.             print "Invalid card: ", suit, rank
  37.  
  38.     def __str__(self):
  39.         return self.suit + self.rank
  40.  
  41.     def get_suit(self):
  42.         return self.suit
  43.  
  44.     def get_rank(self):
  45.         return self.rank
  46.  
  47.     def draw(self, canvas, pos):
  48.         card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
  49.                     CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
  50.         canvas.draw_image(card_images, card_loc, CARD_SIZE,
  51.                           [pos[0] + CARD_CENTER[0],
  52.                            pos[1] + CARD_CENTER[1]], CARD_SIZE)
  53.        
  54. # define hand class
  55. class Hand:
  56.     def __init__(self):
  57.         self.card_list = []
  58.  
  59.     def __str__(self):
  60.         converted = ""  # return a string representation of a hand
  61.         for card in self.card_list:
  62.             converted += str(card) + " "
  63.         message = "Hand contains " + converted
  64.         return message
  65.  
  66.     def add_card(self, card):
  67.         self.card_list.append(card)     # add a card object to a hand
  68.         return self.card_list
  69.  
  70.     def get_value(self):
  71.         # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
  72.         values_list = []
  73.         rank_list = []
  74.         for i in self.card_list:
  75.             i = str(i)
  76.             rank_list.append(i[1])
  77.             values_list.append(VALUES.get(i[1]))
  78.         if "A" in rank_list:
  79.             if sum(values_list) + 10 <= 21:
  80.                 return sum(values_list) + 10
  81.             else:
  82.                 return sum(values_list)
  83.         else:
  84.             return sum(values_list)
  85.            
  86.    
  87.     def draw(self, canvas, pos):
  88.         pass    # draw a hand on the canvas, use the draw method for cards
  89.  
  90.        
  91. # define deck class
  92. class Deck:
  93.     def __init__(self):
  94.         # create a Deck object
  95.         self.deck_list = []
  96.         for suit in SUITS:
  97.             for rank in RANKS:
  98.                 self.deck_list.append(Card(suit, rank))  
  99.        
  100.     def shuffle(self):
  101.         # shuffle the deck
  102.         random.shuffle(self.deck_list)
  103.  
  104.     def deal_card(self):
  105.         # deal a card object from the deck
  106.         new_card = self.deck_list.pop(-1)
  107.         return new_card
  108.    
  109.     def __str__(self):
  110.         # return a string representing the deck
  111.         str_deck = ""
  112.         for i in self.deck_list:
  113.             str_deck += str(i) + " "
  114.         message = "Deck contains " + str_deck
  115.         return message
  116.  
  117.  
  118.  
  119. #define event handlers for buttons
  120. def deal():
  121.     global outcome, in_play, shuffled_deck, player_hand, dealer_hand
  122.     shuffled_deck = Deck()
  123.     shuffled_deck.shuffle()
  124.     player_hand = Hand()
  125.     dealer_hand = Hand()
  126.     player_hand.add_card(shuffled_deck.deal_card())
  127.     player_hand.add_card(shuffled_deck.deal_card())
  128.     dealer_hand.add_card(shuffled_deck.deal_card())
  129.     dealer_hand.add_card(shuffled_deck.deal_card())
  130.     print "Player", player_hand
  131.     print "Dealer", dealer_hand
  132.     in_play = True
  133.  
  134. def hit():
  135.     if player_hand.get_value() <= 21:
  136.         player_hand.add_card(shuffled_deck.deal_card())
  137.         print "Hit. Player", player_hand
  138.     else:
  139.         print "You have busted"
  140.    
  141.     # if busted, assign a message to outcome, update in_play and score
  142.        
  143. def stand():
  144.     pass        # replace with your code below
  145.    
  146.     # if hand is in play, repeatedly hit dealer until his hand has value 17 or more
  147.  
  148.     # assign a message to outcome, update in_play and score
  149.  
  150. # draw handler    
  151. def draw(canvas):
  152.     # test to make sure that card.draw works, replace with your code below
  153.    
  154.     card = Card("S", "A")
  155.     card.draw(canvas, [300, 300])
  156.  
  157.  
  158. # initialization frame
  159. frame = simplegui.create_frame("Blackjack", 600, 600)
  160. frame.set_canvas_background("Green")
  161.  
  162. #create buttons and canvas callback
  163. frame.add_button("Deal", deal, 200)
  164. frame.add_button("Hit",  hit, 200)
  165. frame.add_button("Stand", stand, 200)
  166. frame.set_draw_handler(draw)
  167.  
  168.  
  169. # get things rolling
  170. deal()
  171. frame.start()
  172.  
  173.  
  174. # remember to review the gradic rubric
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement