|
| 1 | +# IMPORT MODULES AND DEFINE VARIABLES |
| 2 | + |
| 3 | +import random |
| 4 | + |
| 5 | +playing = True |
| 6 | + |
| 7 | + |
| 8 | +# CLASSES |
| 9 | + |
| 10 | +class Card: # Creates all the cards |
| 11 | + |
| 12 | + def __init__(self, suit, rank): |
| 13 | + self.suit = suit |
| 14 | + self.rank = rank |
| 15 | + |
| 16 | + def __str__(self): |
| 17 | + return self.rank + ' of ' + self.suit |
| 18 | + |
| 19 | + |
| 20 | +class Deck: # creates a deck of cards |
| 21 | + |
| 22 | + suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs') |
| 23 | + ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace') |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + self.deck = [] # haven't created a deck yet |
| 27 | + for suit in Deck.suits: |
| 28 | + for rank in Deck.ranks: |
| 29 | + self.deck.append(Card(suit, rank)) |
| 30 | + |
| 31 | + def __str__(self): |
| 32 | + deck_comp = '' |
| 33 | + for card in self.deck: |
| 34 | + deck_comp += '\n ' + card.__str__() |
| 35 | + return 'The deck has: ' + deck_comp |
| 36 | + |
| 37 | + def shuffle(self): # shuffle all the cards in the deck |
| 38 | + random.shuffle(self.deck) |
| 39 | + |
| 40 | + def deal(self): # pick out a card from the deck |
| 41 | + single_card = self.deck.pop() |
| 42 | + return single_card |
| 43 | + |
| 44 | + |
| 45 | +class Hand: # show all the cards that the dealer and player have |
| 46 | + |
| 47 | + values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, |
| 48 | + 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11} |
| 49 | + |
| 50 | + def __init__(self): |
| 51 | + self.cards = [] |
| 52 | + self.value = 0 |
| 53 | + self.aces = 0 # keep track of aces |
| 54 | + |
| 55 | + def add_card(self, card): # add a card to the player's or dealer's hand |
| 56 | + self.cards.append(card) |
| 57 | + self.value += Hand.values[card.rank] |
| 58 | + if card.rank == 'Ace': |
| 59 | + self.aces += 1 |
| 60 | + |
| 61 | + def adjust_for_ace(self): |
| 62 | + while self.value > 21 and self.aces: |
| 63 | + self.value -= 10 |
| 64 | + self.aces -= 1 |
| 65 | + |
| 66 | + |
| 67 | +class Chips: # keep track of chips |
| 68 | + |
| 69 | + def __init__(self): |
| 70 | + self.total = 100 |
| 71 | + self.bet = 0 |
| 72 | + |
| 73 | + def win_bet(self): |
| 74 | + self.total += self.bet |
| 75 | + |
| 76 | + def lose_bet(self): |
| 77 | + self.total -= self.bet |
| 78 | + |
| 79 | + |
| 80 | +# FUNCTIONS |
| 81 | + |
| 82 | +def take_bet(chips): # ask for user's bet |
| 83 | + |
| 84 | + while True: |
| 85 | + try: |
| 86 | + chips.bet = int(input("How many chips would you like to bet? ")) |
| 87 | + except ValueError: |
| 88 | + print("Sorry! Please can you type in a number: ") |
| 89 | + else: |
| 90 | + if chips.bet > chips.total: |
| 91 | + print("You bet can't exceed 100!") |
| 92 | + else: |
| 93 | + break |
| 94 | + |
| 95 | + |
| 96 | +def hit(deck, hand): |
| 97 | + hand.add_card(deck.deal()) |
| 98 | + hand.adjust_for_ace() |
| 99 | + |
| 100 | + |
| 101 | +def hit_or_stand(deck, hand): # hit or stand |
| 102 | + global playing |
| 103 | + |
| 104 | + while True: |
| 105 | + ask = input("\nWould you like to hit or stand? Please enter 'h' or 's': ") |
| 106 | + |
| 107 | + if ask[0].lower() == 'h': |
| 108 | + hit(deck, hand) |
| 109 | + elif ask[0].lower() == 's': |
| 110 | + print("Player stands, Dealer is playing.") |
| 111 | + playing = False |
| 112 | + else: |
| 113 | + print("Sorry! I did not understand that! Please try again!") |
| 114 | + continue |
| 115 | + break |
| 116 | + |
| 117 | + |
| 118 | +def show_some(player, dealer): |
| 119 | + print("\nDealer's Hand: ") |
| 120 | + print(" <card hidden>") |
| 121 | + print("", dealer.cards[1]) |
| 122 | + print("\nPlayer's Hand: ", *player.cards, sep='\n ') |
| 123 | + |
| 124 | + |
| 125 | +def show_all(player, dealer): |
| 126 | + print("\nDealer's Hand: ", *dealer.cards, sep='\n ') |
| 127 | + print("Dealer's Hand =", dealer.value) |
| 128 | + print("\nPlayer's Hand: ", *player.cards, sep='\n ') |
| 129 | + print("Player's Hand =", player.value) |
| 130 | + |
| 131 | + |
| 132 | +# Game endings |
| 133 | + |
| 134 | +def player_busts(player, dealer, chips): |
| 135 | + print("PLAYER BUSTS!") |
| 136 | + chips.lose_bet() |
| 137 | + |
| 138 | + |
| 139 | +def player_wins(player, dealer, chips): |
| 140 | + print("PLAYER WINS!") |
| 141 | + chips.win_bet() |
| 142 | + |
| 143 | + |
| 144 | +def dealer_busts(player, dealer, chips): |
| 145 | + print("DEALER BUSTS!") |
| 146 | + chips.win_bet() |
| 147 | + |
| 148 | + |
| 149 | +def dealer_wins(player, dealer, chips): |
| 150 | + print("DEALER WINS!") |
| 151 | + chips.lose_bet() |
| 152 | + |
| 153 | + |
| 154 | +def push(player, dealer): |
| 155 | + print("Its a push! Player and Dealer tie!") |
| 156 | + |
| 157 | + |
| 158 | +# Gameplay! |
| 159 | + |
| 160 | +while True: |
| 161 | + print("Welcome to BlackJack!") |
| 162 | + |
| 163 | + # create an shuffle deck |
| 164 | + deck = Deck() |
| 165 | + deck.shuffle() |
| 166 | + |
| 167 | + player_hand = Hand() |
| 168 | + player_hand.add_card(deck.deal()) |
| 169 | + player_hand.add_card(deck.deal()) |
| 170 | + |
| 171 | + dealer_hand = Hand() |
| 172 | + dealer_hand.add_card(deck.deal()) |
| 173 | + dealer_hand.add_card(deck.deal()) |
| 174 | + |
| 175 | + # set up the player's chips |
| 176 | + player_chips = Chips() |
| 177 | + |
| 178 | + # ask player for bet |
| 179 | + take_bet(player_chips) |
| 180 | + |
| 181 | + # show cards |
| 182 | + show_some(player_hand, dealer_hand) |
| 183 | + |
| 184 | + while playing: |
| 185 | + |
| 186 | + hit_or_stand(deck, player_hand) |
| 187 | + show_some(player_hand, dealer_hand) |
| 188 | + |
| 189 | + if player_hand.value > 21: |
| 190 | + player_busts(player_hand, dealer_hand, player_chips) |
| 191 | + break |
| 192 | + |
| 193 | + if player_hand.value <= 21: |
| 194 | + |
| 195 | + while dealer_hand.value < 17: |
| 196 | + hit(deck, dealer_hand) |
| 197 | + |
| 198 | + show_all(player_hand, dealer_hand) |
| 199 | + |
| 200 | + if dealer_hand.value > 21: |
| 201 | + dealer_busts(player_hand, dealer_hand, player_chips) |
| 202 | + |
| 203 | + elif dealer_hand.value > player_hand.value: |
| 204 | + dealer_wins(player_hand, dealer_hand, player_chips) |
| 205 | + |
| 206 | + elif dealer_hand.value < player_hand.value: |
| 207 | + player_wins(player_hand, dealer_hand, player_chips) |
| 208 | + |
| 209 | + if player_hand.value > 21: |
| 210 | + player_busts(player_hand, dealer_hand, player_chips) |
| 211 | + |
| 212 | + print("\nPlayer's winnings stand at", player_chips.total) |
| 213 | + |
| 214 | + new_game = input("\nWould you like to play again? Enter 'y' or 'n': ") |
| 215 | + if new_game[0].lower() == 'y': |
| 216 | + playing = True |
| 217 | + continue |
| 218 | + else: |
| 219 | + print("\nThanks for playing!") |
| 220 | + break |
| 221 | + |
0 commit comments