import random # Card values card_values = { 'A': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10 } # Deck of cards deck = [card for card in card_values.keys()] * 4 def deal_card(): """Deal a random card from the deck.""" return random.choice(deck).upper() def calculate_hand_value(hand): """Calculate the total value of a hand.""" value = 0 aces = 0 for card in hand: value += card_values[card] if card == 'A': aces += 1 while value > 21 and aces > 0: value -= 10 aces -= 1 return value def play_round(): """Play a single round of the game.""" global chip_stack chip_stack = 0 # Assign random starting chips for the round chip_stack = random.randint(500, 2000) print("You have {} chips to start with.\n".format(chip_stack)) # Player places a bet bet = int(input("Place your bet: ")) if bet > chip_stack: print("Insufficient chips. Bet adjusted to maximum available chips.") bet = chip_stack # Deal initial two cards to player and dealer player_hand = [deal_card(), deal_card()] dealer_hand = [deal_card(), deal_card()] # Player's turn while True: print(f"Your hand: {player_hand} (Value: {calculate_hand_value(player_hand)})") print(f"Dealer's face-up card: {dealer_hand[0]}") action = input("Enter 'h' to hit, 's' to stand, or 'd' to double down: ").lower() if action == 'h': player_hand.append(deal_card()) if calculate_hand_value(player_hand) > 21: print(f"Busted! Your final hand: {player_hand} (Value: {calculate_hand_value(player_hand)})") break elif action == 's': break elif action == 'd': chip_stack -= bet bet *= 2 player_hand.append(deal_card()) if calculate_hand_value(player_hand) > 21: print(f"Busted! Your final hand: {player_hand} (Value: {calculate_hand_value(player_hand)})") break # Dealer's turn while calculate_hand_value(dealer_hand) < 17: dealer_hand.append(deal_card()) print(f"Your final hand: {player_hand} (Value: {calculate_hand_value(player_hand)})") print(f"Dealer's final hand: {dealer_hand} (Value: {calculate_hand_value(dealer_hand)})") # Determine winner if calculate_hand_value(player_hand) > 21: print("You lose!") elif calculate_hand_value(dealer_hand) > 21: print("The dealer bust, you win!") elif calculate_hand_value(player_hand) > calculate_hand_value(dealer_hand): print("You win!") elif calculate_hand_value(player_hand) < calculate_hand_value(dealer_hand): print("You lose!") else: print("It's a tie!") # Update chip stack if calculate_hand_value(player_hand) < calculate_hand_value(dealer_hand): chip_stack = chip_stack- bet else: chip_stack = chip_stack + bet return chip_stack # Play multiple rounds while True: chip_stack = play_round() print(f"Your current chip stack: {chip_stack}") play_again = input("Do you want to play again? (yes/no): ").lower() if play_again != 'yes': break print(f"Your final chip stack: {chip_stack}")