|
| 1 | +import simplegui |
| 2 | +import random |
| 3 | + |
| 4 | +CARD_SIZE = (72, 96) |
| 5 | +CARD_CENTER = (36, 48) |
| 6 | +card_images = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/cards_jfitz.png") |
| 7 | + |
| 8 | +in_play = False |
| 9 | +outcome = "" |
| 10 | +score = 0 |
| 11 | + |
| 12 | +SUITS = ('C', 'S', 'H', 'D') |
| 13 | +RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K') |
| 14 | +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} |
| 15 | + |
| 16 | +class Card: |
| 17 | + def __init__(self, suit, rank): |
| 18 | + if (suit in SUITS) and (rank in RANKS): |
| 19 | + self.suit = suit |
| 20 | + self.rank = rank |
| 21 | + else: |
| 22 | + self.suit = None |
| 23 | + self.rank = None |
| 24 | + print "Invalid card: ", suit, rank |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return self.suit + self.rank |
| 28 | + |
| 29 | + def get_suit(self): |
| 30 | + return self.suit |
| 31 | + |
| 32 | + def get_rank(self): |
| 33 | + return self.rank |
| 34 | + |
| 35 | + def draw(self, canvas, pos): |
| 36 | + card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank), |
| 37 | + CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit)) |
| 38 | + canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE) |
| 39 | + |
| 40 | +def string_list_join(string,string_list): |
| 41 | + ans = string + " contains " |
| 42 | + for i in range(len(string_list)): |
| 43 | + ans += str(string_list[i]) + " " |
| 44 | + return ans |
| 45 | + |
| 46 | +class Hand: |
| 47 | + def __init__(self): |
| 48 | + self.hand = [] |
| 49 | + |
| 50 | + def __str__(self): |
| 51 | + return string_list_join("Hand",self.hand) |
| 52 | + |
| 53 | + def add_card(self, card): |
| 54 | + self.hand.append(card) |
| 55 | + |
| 56 | + def get_value(self): |
| 57 | + var = [] |
| 58 | + self.hand_value = 0 |
| 59 | + for card in self.hand: |
| 60 | + card = str(card) |
| 61 | + if card[1] in VALUES: |
| 62 | + self.hand_value += VALUES[card[1]] |
| 63 | + var.append(card[1]) |
| 64 | + if 'A' not in var: |
| 65 | + return self.hand_value |
| 66 | + if self.hand_value + 10 <= 21: |
| 67 | + return self.hand_value + 10 |
| 68 | + else: |
| 69 | + return self.hand_value |
| 70 | + |
| 71 | + def draw(self, canvas, pos): |
| 72 | + for card in self.hand: |
| 73 | + card = str(card) |
| 74 | + Card(card[0],card[1]).draw(canvas, pos) |
| 75 | + pos[0] += 36 |
| 76 | + |
| 77 | +class Deck: |
| 78 | + def __init__(self): |
| 79 | + self.Deck = [ Card(suit,rank) for suit in SUITS for rank in RANKS ] |
| 80 | + |
| 81 | + def shuffle(self): |
| 82 | + random.shuffle(self.Deck) |
| 83 | + |
| 84 | + def deal_card(self): |
| 85 | + return random.choice(self.Deck) |
| 86 | + |
| 87 | + def __str__(self): |
| 88 | + return string_list_join("Deck",self.Deck) |
| 89 | + |
| 90 | +def deal(): |
| 91 | + global outcome, in_play, score1, score2, player_card, dealer_card, deck |
| 92 | + outcome = "" |
| 93 | + player_card = Hand() |
| 94 | + dealer_card = Hand() |
| 95 | + deck = Deck() |
| 96 | + for i in range(2): |
| 97 | + player_card.add_card(deck.deal_card()) |
| 98 | + dealer_card.add_card(deck.deal_card()) |
| 99 | + |
| 100 | + in_play = True |
| 101 | + score1 = str(player_card.get_value()) |
| 102 | + score2 = str(dealer_card.get_value()) |
| 103 | + |
| 104 | +def stand(): |
| 105 | + if in_play == True : |
| 106 | + while dealer_card.get_value() < 17: |
| 107 | + dealer_card.add_card(deck.deal_card()) |
| 108 | + if dealer_card.get_value() > 21: |
| 109 | + outcome = "you won!!" |
| 110 | + elif player_card.get_value() <= dealer_card.get_value(): |
| 111 | + outcome = "you lose" |
| 112 | + else: |
| 113 | + outcome = "you won!!" |
| 114 | + score1 = str(player_card.get_value()) |
| 115 | + score2 = str(dealer_card.get_value()) |
| 116 | + |
| 117 | +def hit(): |
| 118 | + global outcome, in_play, score1, score2,player_card, dealer_card, deck |
| 119 | + if in_play == True: |
| 120 | + player_card.add_card(deck.deal_card()) |
| 121 | + |
| 122 | + if player_card.get_value() > 21: |
| 123 | + outcome = "you are busted" |
| 124 | + in_play = False |
| 125 | + |
| 126 | + score1 = str(player_card.get_value()) |
| 127 | + score2 = str(dealer_card.get_value()) |
| 128 | + |
| 129 | +def draw(canvas): |
| 130 | + canvas.draw_text(outcome , [250, 150],25 , 'White') |
| 131 | + canvas.draw_text("BlackJack" , [250, 50], 40, 'Black') |
| 132 | + canvas.draw_text(score1 , [100, 100] , 40, 'Red') |
| 133 | + |
| 134 | + player_card.draw(canvas , [20 ,300]) |
| 135 | + dealer_card.draw(canvas , [300 ,300]) |
| 136 | + canvas.draw_text(score2 , [400, 100] , 40, 'Red') |
| 137 | + |
| 138 | +frame = simplegui.create_frame("Blackjack", 600, 600) |
| 139 | +frame.set_canvas_background("Green") |
| 140 | + |
| 141 | +frame.add_button("Deal", deal, 200) |
| 142 | +frame.add_button("Hit", hit, 200) |
| 143 | +frame.add_button("Stand", stand, 200) |
| 144 | +frame.set_draw_handler(draw) |
| 145 | + |
| 146 | +deal() |
| 147 | +frame.start() |
0 commit comments