import random # Define the values of each card card_values = { 'Ace': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'Jack': 10, 'Queen': 10, 'King': 10 } # Define a function to calculate the value of a hand def hand_value(hand): value = sum(card_values[card] for card in hand) # If the hand contains an Ace and the value is greater than 21, reduce the Ace value to 1 if 'Ace' in hand and value > 21: value -= 10 return value # Define a function to deal a card def deal_card(): card = random.choice(list(card_values.keys())) return card # Define a function to create text-based card art def card_art(card): ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] suits = ['♠', '♣', '♥', '♦'] rank = card.split()[0] suit = suits[ranks.index(rank) % 4] value = card_values[rank] return f""" ┌─────────┐ │ {rank:<2} │ │ │ │ {suit} │ │ │ │ {rank:>2} │ └─────────┘ """ # Define the game function def play_game(): # Initialize the deck and the player and dealer hands deck = list(card_values.keys()) * 4 random.shuffle(deck) player_hand = [deal_card(), deal_card()] dealer_hand = [deal_card()] # Player's turn while hand_value(player_hand) < 21: print("Your hand:") for card in player_hand: print(card_art(card), end="") print(f"(value: {hand_value(player_hand)})") hit_or_stand = input("Do you want to hit or stand? ") if hit_or_stand.lower() == "hit": player_hand.append(deal_card()) else: break # Dealer's turn while hand_value(dealer_hand) < 17: dealer_hand.append(deal_card()) # Determine the winner player_value = hand_value(player_hand) dealer_value = hand_value(dealer_hand) print("Your hand:") for card in player_hand: print(card_art(card), end="") print(f"(value: {player_value})") print("Dealer's hand:") for card in dealer_hand: print(card_art(card), end="") print(f"(value: {dealer_value})") if player_value > 21: print("Bust! You lose.") elif dealer_value > 21: print("Dealer bust! You win.") elif player_value > dealer_value: print("You win!") elif dealer_value > player_value: print("You lose.") else: print("It's a tie.") # Play the game while True: play_game() play_again = input("Do you want to play again? ") if play_again.lower() != "yes": break