Skip to content

Instantly share code, notes, and snippets.

@beaucarnes
Created November 7, 2019 20:41
Show Gist options
  • Save beaucarnes/8f7e11f805274813f280fb353b95a776 to your computer and use it in GitHub Desktop.
Save beaucarnes/8f7e11f805274813f280fb353b95a776 to your computer and use it in GitHub Desktop.
import random
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return f"{self.rank['rank']} of {self.suit}"
class Deck:
def __init__(self):
self.cards = []
suits = ["♠", "♣️", "♥️", "♦️"]
ranks = [
{"rank": "A", "value": 11},
{"rank": "2", "value": 2},
{"rank": "3", "value": 3},
{"rank": "4", "value": 4},
{"rank": "5", "value": 5},
{"rank": "6", "value": 6},
{"rank": "7", "value": 7},
{"rank": "8", "value": 8},
{"rank": "9", "value": 9},
{"rank": "10", "value": 10},
{"rank": "J", "value": 10},
{"rank": "Q", "value": 10},
{"rank": "K", "value": 10},
]
for suit in suits:
for rank in ranks:
self.cards.append(Card(suit, rank))
def deal(self):
if len(self.cards) > 1:
return self.cards.pop(0)
def shuffle(self):
if len(self.cards) > 1:
random.shuffle(self.cards)
class Hand:
def __init__(self, dealer=False):
self.dealer = dealer
self.cards = []
self.value = 0
def add_card(self, card):
self.cards.append(card)
def calculate_value(self):
self.value = 0
has_ace = False
for card in self.cards:
self.value += int(card.rank["value"])
if card.rank["rank"] == "A":
has_ace = True
if has_ace and self.value > 21:
self.value -= 10
def get_value(self):
self.calculate_value()
return self.value
def is_blackjack(self):
return self.get_value() == 21
def display(self):
if self.dealer:
print(self.cards[0]) if self.is_blackjack() else print("hidden")
print(self.cards[1])
else:
for card in self.cards:
print(card)
print("Value:", self.get_value())
class Game:
def play(self):
playing = True
game_number = 0
games_to_play = 0
while games_to_play <= 0:
try:
games_to_play = int(input('How many games do you want to play? '))
except:
print('You must enter a number.')
while playing:
game_number += 1
deck = Deck()
deck.shuffle()
player_hand = Hand()
dealer_hand = Hand(dealer=True)
for i in range(2):
player_hand.add_card(deck.deal())
dealer_hand.add_card(deck.deal())
print(f"* Game {game_number} of {games_to_play} *")
print("Your hand:")
player_hand.display()
print()
print("Dealer's hand:")
dealer_hand.display()
game_over = False
while not game_over:
if player_hand.is_blackjack() or dealer_hand.is_blackjack():
game_over = True
self.show_blackjack_results(player_hand.is_blackjack(), dealer_hand.is_blackjack())
continue
choice = input("Please choose 'Hit' or 'Stand' ").lower()
while choice not in ["h", "s", "hit", "stand"]:
choice = input("Please enter 'hit' or 'stand' (or H/S) ").lower()
if choice in ['hit', 'h']:
player_hand.add_card(deck.deal())
player_hand.display()
if player_hand.get_value() > 21:
print("You busted. Dealer wins!")
game_over = True
else:
player_hand_value = player_hand.get_value()
dealer_hand_value = dealer_hand.get_value()
print("Final Results")
print("Your hand:", player_hand_value)
print("Dealer's hand:", dealer_hand_value)
if player_hand_value > dealer_hand_value:
print("You win!")
elif player_hand_value == dealer_hand_value:
print("Tie!")
else:
print("Dealer wins!")
game_over = True
if game_number >= games_to_play:
print("Thanks for playing!")
playing = False
else:
print()
game_over = False
def show_blackjack_results(self, player_has_blackjack, dealer_has_blackjack):
if player_has_blackjack and dealer_has_blackjack:
print("Both players have blackjack! Draw!")
elif player_has_blackjack:
print("You have blackjack! You win!")
elif dealer_has_blackjack:
print("Dealer has blackjack! Dealer wins!")
g = Game()
g.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment