import random

# Card values
card_values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}

# Create a deck of cards
def create_deck():
    deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] * 4
    random.shuffle(deck)
    return deck

# Calculate hand value
def hand_value(hand):
    value = sum(card_values[card] for card in hand)
    # Adjust for Aces if the value is over 21
    for card in hand:
        if value > 21 and card == 'A':
            value -= 10
    return value

# Play a round
def play_round():
    deck = create_deck()

    # Deal cards to player and dealer
    player_hand = [deck.pop(), deck.pop()]
    dealer_hand = [deck.pop(), deck.pop()]

    print("Your hand:", player_hand)
    print("Dealer's hand:", dealer_hand[0], " and a hidden card.")

    # Player's turn to hit or stand
    while hand_value(player_hand) < 21:
        move = input("Do you want to (h)it or (s)tand? ").lower()
        if move == 'h':
            player_hand.append(deck.pop())
            print("Your hand:", player_hand)
        elif move == 's':
            break

    # Check if the player busts
    if hand_value(player_hand) > 21:
        print("You busted! Dealer wins.")
        return

    # Dealer's turn (Dealer must hit if their hand value is less than 17)
    while hand_value(dealer_hand) < 17:
        dealer_hand.append(deck.pop())

    print("Dealer's hand:", dealer_hand)

    # Determine the winner
    player_score = hand_value(player_hand)
    dealer_score = hand_value(dealer_hand)

    if dealer_score > 21:
        print("Dealer busts! You win!")
    elif player_score > dealer_score:
        print("You win!")
    elif player_score < dealer_score:
        print("Dealer wins!")
    else:
        print("It's a tie!")

# Start the game
def main():
    print("Welcome to the Game of 21!")
    while True:
        play_round()
        again = input("Do you want to play again? (y/n): ").lower()
        if again != 'y':
            break
    print("Thanks for playing!")

# Run the game
if __name__ == "__main__":
    main()