class Card: def __init__(self, value, suit): self.value = value self.suit = suit def __str__(self): return f"{self.value} of {self.suit}" def __repr__(self): return f"{self.value} of {self.suit}" def __eq__(self, other): return self.value == other.value and self.suit == other.suit def __ne__(self, other): return self.value != other.value or self.suit != other.suit def __lt__(self, other): return self.value < other.value def __le__(self, other): return self.value <= other.value def __gt__(self, other): return self.value > other.value def __ge__(self, other): return self.value >= other.value def __add__(self, other): return self.value + other.value def __sub__(self, other): return self.value - other.value class Deck: def __init__(self): self.cards = [] for suit in ['Hearts', 'Diamonds', 'Clubs', 'Spades']: for value in range(1, 14): self.cards.append(Card(value, suit)) def __str__(self): return f"{len(self.cards)} cards in deck" def __repr__(self): return f"{len(self.cards)} cards in deck" def __len__(self): return len(self.cards) def shuffle(self): import random random.shuffle(self.cards) def deal(self): import random return random.choice(self.cards) class Hand: def __init__(self): self.cards = [] def __str__(self): return f"{len(self.cards)} cards in hand" def __repr__(self): return f"{len(self.cards)} cards in hand" def add_card(self, card): self.cards.append(card) def score(self): score = 0 num_aces = 0 for card in self.cards: if card.value == 1: score += 11 num_aces += 1 elif card.value in [11, 12, 13]: score += 10 else: score += card.value while score > 21 and num_aces > 0: score -= 10 num_aces -= 1 return score def __lt__(self, other): return self.score() < other.score() def __le__(self, other): return self.score() <= other.score() def __gt__(self, other): return self.score() > other.score() def __ge__(self, other): return self.score() >= other.score() def __eq__(self, other): return self.score() == other.score() def __ne__(self, other): return self.score() != other.score() def __add__(self, other): return self.score() + other.score() def __sub__(self, other): return self.score() - other.score() def __len__(self): return len(self.cards) def __getitem__(self, key): return self.cards[key] def __setitem__(self, key, value): self.cards[key] = value class BlacjackGame: def __init__(self): self.deck = Deck() self.deck.shuffle() self.player = Hand() self.dealer = Hand() self.game_over = False self.player_busted = False self.dealer_busted = False self.player_blackjack = False self.dealer_blackjack = False self.player_stands = False self.dealer_stands = False self.player_wins = False self.dealer_wins = False self.tie = False self.player.add_card(self.deck.deal()) self.player.add_card(self.deck.deal()) self.dealer.add_card(self.deck.deal()) self.dealer.add_card(self.deck.deal()) def player_hit(self): if not self.player_busted and not self.player_blackjack and not self.player_stands: self.player.add_card(self.deck.deal()) if self.player_score() > 21: self.player_busted = True self.dealer_wins = True self.game_over = True elif self.player_score() == 21: self.player_blackjack = True self.player_wins = True self.game_over = True def player_stand(self): if not self.player_busted and not self.player_blackjack: self.player_stands = True def dealer_turn(self): while not self.dealer_busted and not self.dealer_blackjack and not self.dealer_stands and self.dealer_score() < 17: self.dealer_hit() if not self.dealer_busted and not self.dealer_blackjack and not self.dealer_stands: self.dealer_stand() def dealer_hit(self): if not self.dealer_busted and not self.dealer_blackjack and not self.dealer_stands: self.dealer.add_card(self.deck.deal()) if self.dealer_score() > 21: self.dealer_busted = True self.player_wins = True self.game_over = True elif self.dealer_score() == 21: self.dealer_blackjack = True self.dealer_wins = True self.game_over = True def dealer_stand(self): if not self.dealer_busted and not self.dealer_blackjack: self.dealer_stands = True def check_winner(self): if self.player_stands and self.dealer_stands: if self.player_score() > self.dealer_score(): self.player_wins = True self.game_over = True elif self.player_score() < self.dealer_score(): self.dealer_wins = True self.game_over = True else: self.tie = True self.game_over = True def player_score(self): return self.player.score() def dealer_score(self): return self.dealer.score() def __str__(self): return f"Player score: {self.player_score()}\nDealer score: {self.dealer_score()}" def __repr__(self): return f"Player score: {self.player_score()}\nDealer score: {self.dealer_score()}" def __len__(self): return len(self.deck) def __getitem__(self, key): return self.deck[key] def __setitem__(self, key, value): self.deck[key] = value def __eq__(self, other): return self.player_score() == other.player_score() and self.dealer_score() == other.dealer_score() def __ne__(self, other): return self.player_score() != other.player_score() or self.dealer_score() != other.dealer_score() def __lt__(self, other): return self.player_score() < other.player_score() and self.dealer_score() < other.dealer_score() def __le__(self, other): return self.player_score() <= other.player_score() and self.dealer_score() <= other.dealer_score() def __gt__(self, other): return self.player_score() > other.player_score() and self.dealer_score() > other.dealer_score() def __ge__(self, other): return self.player_score() >= other.player_score() and self.dealer_score() >= other.dealer_score() def __add__(self, other): return self.player_score() + other.player_score() + self.dealer_score() + other.dealer_score() def user_plays(game): print(f"Player hand: {game.player_score()}") for card in game.player: print(card) print("Dealer hand:") print(game.dealer[0]) action = input("Do you want to hit or stand? ") if action == "hit": game.player_hit() elif action == "stand": game.player_stand() def best_strategy_game(game): if game.player_score() < 17: game.player_hit() else: game.player_stand() if __name__ == "__main__": game = BlacjackGame() while not game.game_over: print(game) print("") if game.player_stands: game.dealer_turn() game.check_winner() if not game.game_over: best_strategy_game(game) print(game) if game.player_wins: print("Player wins!") elif game.dealer_wins: print("Dealer wins!")