Blackjack Python

  • Upload
    ftapos

  • View
    6

  • Download
    0

Embed Size (px)

DESCRIPTION

This is a text file containing code in Python to be implemented in CodeSkulptor.

Citation preview

#This is a program designed to run in codeskulptor#In your browser go to www.codeskulptor.org and paste the program bellow and hit the run button (first from the left).#Blackjackimport simpleguiimport random# load card sprite - 936x384 - source: jfitz.comCARD_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 variablesin_play = Falseoutcome = ""score = 0# define globals for cardsSUITS = ('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 classclass 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 classclass Hand: def __init__(self): self.hand = [] # create Hand object def __str__(self): # return a string representation of a hand handstr = " " for x in self.hand: handstr = handstr + str(x) + " " return "Hand contains" + handstr def add_card(self, card): self.hand.append(card) # add a card object to a hand 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 handvalue = 0 aces = 0 for x in self.hand: if x.get_rank() == 'A': aces += 1 handvalue += VALUES.get(x.get_rank()) if aces > 0 and (handvalue + 10) 21: outcome = "You have busted. New deal?" score -= 1 in_play = False return score, outcome, in_play else: outcome = "Hit or stand?" def stand(): # replace with your code below global outcome, score, in_play in_play = False if player_hand.get_value() > 21: outcome = "You have busted. New deal?" score -= 1 return score, outcome, in_play else: while dealer_hand.get_value() < 17: dealer_hand.add_card(my_deck.deal_card()) else: if dealer_hand.get_value() > 21: outcome = "Dealer busts, you win. New deal?" score += 1 return score, outcome, in_play elif dealer_hand.get_value() >= player_hand.get_value(): outcome = "Dealer wins. New deal?" score -= 1 return score, outcome, in_play else: outcome = "You win. New deal?" score += 1 return score, outcome, in_play # 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 dealer_hand.draw(canvas, [0, 100]) player_hand.draw(canvas, [0, 300]) if in_play: canvas.draw_image(card_back, CARD_BACK_CENTER, CARD_BACK_SIZE, [0 + CARD_BACK_CENTER[0], 100 + CARD_BACK_CENTER[1]], CARD_SIZE) canvas.draw_text(outcome,[300,275],20,"Black") canvas.draw_text("Score: "+str(score),[300,250],20,"Black") canvas.draw_text("BlackJack",[250,50],35,"Black") canvas.draw_text("Dealer",[50,77],25,"Black") canvas.draw_text("Player",[50,275],25,"Black")# initialization frameframe = simplegui.create_frame("Blackjack", 600, 600)frame.set_canvas_background("Green")#create buttons and canvas callbackframe.add_button("Deal", deal, 200)frame.add_button("Hit", hit, 200)frame.add_button("Stand", stand, 200)frame.set_draw_handler(draw)# get things rollingdeal()frame.start()