""" 
-------------------------------------------------
   Project:   Pick-Up Game
  Standard:   TE PUNGA - Programming Assessment
    School:   Tauranga Boys' College
    Author:   Xander Mauri
      Date:   March 2024
    Python:   3.7.4
-------------------------------------------------
"""
# Importing the required libraries for this program
import time 

# Setting important variables and constants
MAX_CHIPS = 21
player1_score = 0
player2_score = 0
player1_round_wins=0
player2_round_wins=0

# Introducing the game to the players and explaining the rules
print("\t\nWelcome to 'Pick Up Game'!")
time.sleep(1)
print("This is a game of skill, luck and strategy.")
time.sleep(2)
print("You and your friend will be verseing each other.")
time.sleep(2)
print("There are ",MAX_CHIPS," chips in total.")
time.sleep(1)
print("Your goal is to be the on with the last chip.")
time.sleep(2)
print("You will only be able to pick up 1 to 3 chips at a time.")
time.sleep(3)
print("You can pick how many rounds you want to play (minimum 1 round -  maximum 5 rounds).")
time.sleep(1)
print("So good luck to you both\n and may the best win.")

# Asking players what they want to be callled and how many rounds they wish to play

player1_name = input("\tPLAYER 1, please enter your name: \n")    
player2_name = input("PLAYER 2, please enter your name: \n")

total_rounds = int(input("\tHow many rounds would you like? (min 1 - max 5)")) 

#  Checking if the number of rounds entered by the player is within the valid range
while not (1 <= total_rounds <= 5):
    print("Invalid number. Please enter a number between 1 and 5.")
    total_rounds = int(input("How many rounds would you like? "))

for round_num in range(1, total_rounds + 1):
    print(f"\nRound {round_num} - {player1_name} vs {player2_name}")

    chips_left = MAX_CHIPS          #Turning the variable into a loop counter so it resets after every round

    #The while loop continues until there are no more chips left or one player has won
    while chips_left > 0:
        print(f"\nChips left: {chips_left}")        
        print(f"{player1_name}'s turn")
        chips_taken = int(input("Please choose the number of chips you would like to take (1-3): "))

        if 1 <= chips_taken <= 3 and chips_taken <= chips_left:
            chips_left -= chips_taken
            player1_score += chips_taken
        else:
            print("Invalid input. Please choose a valid number of chips.")
            continue

        if chips_left == 0 or (chips_left == 1 and round_num == total_rounds):
            break

        print(f"\nChips left: {chips_left}")
        print(f"{player2_name}'s turn")
        chips_taken = int(input("Please choose the number of chips you would like to take (1-3): "))

        if 1 <= chips_taken <= 3 and chips_taken <= chips_left:
            chips_left -= chips_taken
            player2_score += chips_taken
        else:
            print("Invalid input. Please choose a valid number of chips.")
            continue

#Checking who won the round and updating their score accordingly
    if player1_score>player2_score:
        player1_round_wins+=1
    elif player2_score>player1_score:
        player2_round_wins+=1
    elif round_num < total_rounds:
        print(f"\nEnd of Round {round_num}. Scores: {player1_name} - {player1_score}, {player2_name} - {player2_score}")
        time.sleep(1)

print(f"\nFinal Scores:\n{player1_name}: {player1_round_wins}\n{player2_name}: {player2_round_wins}")

# Seeing who won the game by comparing scores
if player1_score > player2_score:
    print(f"\nCongratulations {player1_name}! You won the game.")
elif player2_score > player1_score:
    print(f"\nCongratulations {player2_name}! You won the game.")
else:
    print("\nIt's a draw.")