#--------------------------------------------------------------------------
#  Name:       21 Game
#  Purpose:    11DGT
#  Author:     Tyson Allerby
#  Created:    18/3/2024
#  Copyright:  © Tyson Allerby 18/3/2024
#--------------------------------------------------------------------------

def main():
    # Display welcome message
    print("Welcome to the 21 Game!")

    # Input player names
    player1 = input("Enter Player 1's name: ")
    player2 = input("Enter Player 2's name: ")

    # Input number of rounds
    rounds = int(input("How many rounds do you want to play? "))

    # Print game rules
    print("​<light>The 21 game starts with 21 chips. Each player can take 1, 2, or 3 chips. The player who takes the last chip wins.</light>")

    # Initialize player scores
    player_scores = {player1: 0, player2: 0}

    # Start the game rounds
    for round_number in range(1, rounds + 1):
        print(f"\nRound {round_number} begins!")

        # Initialize chips for each round
        chips = 21
        print(f"{chips} chips remaining.")

        # Set the current player for the round
        current_player = player1

        # Game logic for each round
        while chips > 0:
            print(f"\n{current_player}'s turn. Pick 1, 2, or 3 chips.")

            # Input the number of chips taken by the player
            chips_taken = int(input("Chips taken: "))

            # Validate the input
            if chips_taken < 1 or chips_taken > 3 or chips_taken > chips:
                print("Invalid input. Please pick 1, 2, or 3 chips.")
                continue

            # Update the remaining chips
            chips -= chips_taken
            print(f"{chips} chips remaining.")

            # Check if the current player wins the round
            if chips == 0:
                print(f"\n{current_player} wins this round!")
                player_scores[current_player] += 1
                break

            # Switch to the other player
            current_player = player2 if current_player == player1 else player1

    # Print final game results
    print("\nGame over!")
    print("Final scores:")
    for player, score in player_scores.items():
        print(f"{player}: {score} point(s)")

    # Determine the winner or declare a tie
    if player_scores[player1] > player_scores[player2]:
        print(f"\n{player1} wins!")
    elif player_scores[player1] < player_scores[player2]:
        print(f"\n{player2} wins!")
    else:
        print("\nIt's a tie!")

if __name__ == "__main__":
    main()