First ever lines of code in Py. For testing go to cloud www.codesculptor.org and paste the code in canvas, then press run. Week 6. Mini-project #6 - Blackjack
# Mini-project #6 - Blackjack 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 pos=[ 30, 100] # 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): global cards_table cards_table=0 self.hand_list=[] def __str__(self): retu='' for b in range(len(self.hand_list)/2): i = b*2 #+ 1 #i = iter(self.hand_list) item_su = self.hand_list[i] #.next() # fetch first value item_ra = self.hand_list[i+1]#i.next() retu = retu + str(item_su) + str(item_ra)+ ' ' return retu #pass # return a string representation of a hand def add_card(self, card): global cards_table, pos cards_table += 1 card_a=( card.get_suit(), card.get_rank()) self.hand_list.extend(card_a) pos [0]= pos[0] + 100 # self.draw(canvas, pos) def get_value(self): sum_hand=0 aces=0 for i in range(len(self.hand_list)/2): #print self.hand_list[i*2+1] a=str(self.hand_list[i*2+1]) a=VALUES[a] if a==1: aces = 1 sum_hand += a if aces == 0: retu= sum_hand else: if sum_hand+10<=21: retu= sum_hand+ 10 else: retu= sum_hand return retu # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust pass # compute the value of the hand, see Blackjack video def draw(self, canvas, pos): card.draw(canvas, pos) #or i in range(len(self.hand_list)/2): # card.draw(canvas, pos) # pos #draw(canvas, pos) #pass # draw a hand on the canvas, use the draw method for cards # define deck class class Deck: def __init__(self): self.hand_list=[] for i in SUITS: for j in RANKS: suit=i rank=j fu = Card(suit, rank) self.hand_list.append(fu) #pass # create a Deck object def shuffle(self): random.shuffle(self.hand_list) # add cards back to deck and shuffle pass # use random.shuffle() to shuffle the deck def deal_card(self): return self.hand_list.pop() #pass # deal a card object from the deck def __str__(self): #print self.hand_list st='' for i in range(len(self.hand_list)): st += str(self.hand_list[i])+ ' ' return st #define event handlers for buttons def deal(): global outcome, in_play, cards_list, player_hand, dealer_hand, new_deck new_deck=Deck() cards_list = new_deck.shuffle() player_hand = Hand() dealer_hand = Hand() player_hand.add_card(new_deck.deal_card()) player_hand.add_card(new_deck.deal_card()) dealer_hand.add_card(new_deck.deal_card()) dealer_hand.add_card(new_deck.deal_card()) print "Player's hand " + str(player_hand) print "dealer's hand " + str(dealer_hand)#.str() print "Player's hand value " + str(player_hand.get_value()) print "dealer's hand value " + str(dealer_hand.get_value() ) # your code goes here in_play = True def hit(): if player_hand.get_value()<=21: player_hand.add_card(new_deck.deal_card()) if player_hand.get_value()>21: print "You have busted" # replace with your code below # if the hand is in play, hit the player # if busted, assign a message to outcome, update in_play and score def stand(): global score, dealer_hand, player_hand if player_hand.get_value()>21: print "You have busted" score -=1 else: while dealer_hand.get_value()<17: dealer_hand.add_card(new_deck.deal_card()) if dealer_hand.get_value()>21: print "Dealer was busted" score +=1 else: if player_hand.get_value()<=dealer_hand.get_value() and dealer_hand.get_value()<=21: score -=1 else: score +=1 print score,dealer_hand, player_hand # 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): global score, dealer_hand, player_hand canvas.draw_text("Score: " + str(score), (550,50),12, "White") pos=[50,50] for i in range(len(self.hand_list)/2): dealer_hand.draw(canvas, pos) pos[0] += 100 pos=[50, 350] for i in range(len(self.hand_list)/2): dealer_hand.draw(canvas, pos) pos[0] += 100 # draw(self, canvas, pos) # test to make sure that card.draw works, replace with your code below #ard = Card("S", "A") #ard.draw(canvas, [300, 300]) # cards_table += 1 # card_a=( card.get_suit(), card.get_rank()) # self.hand_list.extend(card_a) # pos [0]= pos[0] + 100 # self.draw(canvas, pos) # 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 frame.start() # remember to review the gradic rubric
Leave a Comment