"""
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   Project: Take Away Game
  Standard: 91883 (AS1.7) v.1
    School: Tauranga Boys' College
    Author: Aiden Hall
      Date: 03/03/25
    Python: 3.5
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This game allows for 2 players to play a chip based game. The first player to insert there name will always start on the first round, alternating between even and odd rounds. The players are
faced with the option to take from 1, 2 or 3 chips from a pile of 21. As chips dwindle down the player to take the last chip will gain a point. The amount of rounds are chosen by the player/s
between 1 - 5 allowing for total domination, small wins or possible ties. The code is structured by importing features, defining variables / asking the player for input variables. This is quickly
followed by the main section of the code where it inputs the actual game using while loops ensuring the game continues until they meet there maxed rounds chosen. The game then used greater than 
and less than features. I encountered multiple problems such as making my code wrong by giving the player who took the last chip not a point and the other person, dealing with while loops and 
other small bugs. 

Multiple limitations were found during the game such as, the first player to pick there chips will nearly always win as well with a very boring and repetitive game in general. I believe fixes
for this may be made by using import random, random.randint for chips to create a more fair and stable game. Also you could randomise who starts and implement a feature where you could 
have more than 2 people if wanted to creating a more havoc game. Also having a AI feature to play against if you dont have any friends. 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
"""
import time #Imports the time feature to allow the type_writer command to work
import sys #Imports sys for the type_writer command to work

P1_S = 0 #Base points at the start of the game so everyone starts with 0 and will scale up over the course of the game
P2_S = 0


def type_writer(text, delay=0.05): # The following 5 lines of code changes the print command to type writer inserting a 1 by 1 text chain, this also defines the speed the text will type out
    for char in text:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(delay)
    print()

#This input allows for the variable to know the names of each player. Also it validates number ensuring they have no spaces or numbers in it
while True:
    P1 = input("What is Player 1's name? ")
    P2 = input("What is Player 2's name? ")
    if P1.isalpha() and P2.isalpha() == True: #If the name contains any numbers, _'s or spaces it breaks and re asks
        break
    else:
        print("Please do not enter a name containing anything but letters")
#This variable gives a base value of the chips so its not a random number

start_P = P1 #Provides who will start the game on the first rounds
chips = 21 #Evertyime a new round starts it will be 21 chips
current_P = start_P 
next_P = P1 if start_P == P2 else P2 #Knows which player is next

type_writer(f"Welcome {P1} and {P2}") #Welcomes each player
while True:
    Round = int(input("How many rounds would you like to play? 1 - 5: ")) #Asks how many rounds the user wants to play and this is used later
    if Round < 1 or Round > 5: #Makes it so if they insert a number under 1 or above 5 it will break and re ask
        type_writer(f"You must choose rounds inbetween 1 and 5")
    else:
        break
Remain = Round #This makes it so when I print remain its the same amount as the rounds the players picked at the start

type_writer(f"Awesome you selected {Round} rounds") #Re ensuring the players knowledge on how many rounds they picked

while chips > 0 and Remain != 0 : #This makes it so the code will work until the chips go to 0 or below and will initate the score phase / end the round / end the game
    print(f"\nYou must now choose to take 1 or 3 chips from the chip pile in the middle")

    while Remain != 0 : #While the game is still running and has remaining rounds the following code plays
        while True:
            try:
                print(f"----------------------------{current_P}'s turn----------------------------") #This prints the current players turn so they know whos meant to be going
                steal = int(input(f"How many chips would you like to take {current_P}? 1 - 3: ")) #Input for the player to choose between 1 - 3 chips to take from the pile 
                current_P, next_P = next_P, current_P # Alternate who starts the round 
                start_P = next_P #Alternates player
            except ValueError: #If they dont put a number it breaks
                type_writer("Enter a number between 1 - 3 please.")
            if steal < 1 or steal > 3: #If they put a number over / under 1 - 3 it breaks the code and re asks for the input
                type_writer("Its between three numbers you.")
            else:
                break #Restarts the statement back to the start
        if steal == 1: #If they choose 1 the follow statement will play
            chips = chips - 1 #If chosen 1 it will minus 1 chip from the chips remaining
            if chips <= 0:
                chips = 0
            else:
                type_writer(f"You took 1 chip from the pile {chips} chips remain")
        if steal == 2: #If they choose 2 the follow statement will play
            chips = chips - 2 #If chosen 2 it will minus 2 chip from the chips remaining
            if chips <= 0:
                chips = 0
            else:
                type_writer(f"You took 2 chips from the pile {chips} chips remain")
        if steal == 3: #If they choose 3 the follow statement will play
            chips = chips - 3 #If chosen 3 it will minus 3 chip from the chips remaining
            if chips <= 0:
                chips = 0
            else:
                type_writer(f"You took 3 chips from the pile {chips} chips remain")
        if chips < 1: #Stops the game when the chips are bellow 1
            chips = 21 #Restarts the chips to 21
            Remain = Remain - 1 #Removes 1 of the remainding rounds 
            print(f"You have {Remain} rounds left")
            if next_P == P1: #If the player removed the last chip it will play the following statement
                P1_S = P1_S + 1 #Gives a point to Player 1 if they were they were the last one to take the chip
                print(f"The current scores are \nPlayer 1 = {P1_S}\nPlayer 2 = {P2_S}")
            else: #Same as the comments above but for player 2
                P2_S = P2_S + 1
                print(f"The current scores are \nPlayer 1 = {P1_S}\nPlayer 2 = {P2_S}")
            break 

    if P1_S > P2_S: #If player 1 has a higher score than player 2 it prints the following statement
        print(f"{P1}\nYou absolutely swept {P2}")
    elif P2_S > P1_S: #If player 2 has a higher score than player 1 it prints the following statement
        print(f"{P2}\nThat was an absolutely great game, you destroyed {P1}")
    else: #If neither player has a higher score the game will know its a draw and prints the following statement
        print("TIE!!")