"""
-------------------------------------------------
   Project: Take-Away Game
  Standard: 91883 (AS1.7) v.1
    School: Tauranga Boys' College
    Author: Rod William Santiago
      Date: 3/03/2025
    Python: 3.5
-------------------------------------------------
"""
import random

def get_player_name():
    """Prompt the player for their name."""
    return input("Enter your name: ")

def play_round(player_name, round_number):
    """Handles the logic for playing a single round of the game."""
    target_number = random.randint(1, 1000)
    guesses_left = 10
    round_winner = None

    print(f"\nRound {round_number}: {player_name}, try to guess the number between 1 and 1000.")

    while guesses_left > 0:
        guess = int(input(f"You have {guesses_left} guesses left. Enter your guess: "))
        
        if guess == target_number:
            round_winner = player_name
            print(f"Correct! {player_name} guessed the number {target_number} correctly!")
            break
        elif guess < target_number:
            print("Higher!")
        else:
            print("Lower!")

        guesses_left -= 1

    if round_winner is None:
        print(f"Sorry {player_name}, you couldn't guess the number. It was {target_number}.")
    return round_winner

def display_scores(player_name, player_score, computer_score, rounds_played):
    """Displays the current scores and progress."""
    print(f"\nAfter {rounds_played} rounds:")
    print(f"{player_name} - {player_score} | Computer - {computer_score}")

def main():
    """Main function to start and manage the entire game."""
    player_name = get_player_name()
    player_score = 0
    computer_score = 0
    rounds_played = 0

    # Main game loop, runs until either player wins 2 rounds or 3 rounds are completed
    while rounds_played < 3:
        rounds_played += 1
        round_winner = play_round(player_name, rounds_played)

        # Update the score based on the round result
        if round_winner == player_name:
            player_score += 1
        else:
            computer_score += 1

        # Show current scores after each round
        display_scores(player_name, player_score, computer_score, rounds_played)

        # Early termination if a player wins 2 rounds
        if player_score == 2:
            print(f"\n{player_name} wins the game with a score of {player_score}!")
            break
        elif computer_score == 2:
            print(f"\nThe computer wins the game with a score of {computer_score}!")
            break

    # Final game summary
    if player_score != 2 and computer_score != 2:
        print(f"\nGame Over! Final score: {player_name} {player_score} - {computer_score} Computer.")
        if player_score > computer_score:
            print(f"Congratulations {player_name}, you won the game!")
        else:
            print(f"Sorry {player_name}, you lost the game.")

if __name__ == "__main__":
    main()