"""
-------------------------------------------------
   Project: 
  Standard: 91883 (AS1.7) v.1
    School: Tauranga Boys' College
    Author: Monty Page
      Date: THE DATE YOU COMPLETED THE CODE
    Python: 3.5
-------------------------------------------------
"""
import random
import sys, os
import time


#Variables
maxchips = 21
roundsplayed = 0
score = 0
score2 = 0
chipsleft = maxchips
player_1_score = 0
player_2_score = 0


#code
while True:
    name = input("What's the name chosen for player one?")
    print("Hey", name)
    break

while True:
    name2 = input("What's the name chosen for player two?")
    print("Hey",name2)
    break

#check for input validation 
while True:
  try:
    rounds = int(input("You may choose how many rounds you'd like to play! (1-5)"))
    if rounds <1 or rounds >5: 
      print("That is not an accepted range!")
    else:
      break
  except:
    print("Please make sure to choose a number!")
    time.sleep(2)
    break

while True:
  print("You have", chipsleft, "chips left")
  try:
    chipstaken = int(input("How many chips would you like to take"))
    if chipstaken <1 or chipstaken >3:
      print("Please choose a number between 1 and 3")
    elif chipstaken > chipsleft:
      print("You don't have enough chips")
    else:
      chipsleft -= chipstaken
      if chipsleft == 0:
        print("Game over")
      break
  except ValueError:
      print("ERROR")
  print("Please try again, maybe put just a number this time.")

# Start of the game loop
while rounds > 0:
   chips_left = maxchips
   print("\nRounds left:", rounds)

   while chips_left > 0: #ONly playes if the chips are more then 0
       # Player 1's turn to pick
       print("Chips left:", chips_left)
       while True:
           try:
               print(name)
               chips_taken = int(input("choose an amount of chips to take (1-3): ")) #the int input puts the stroig into a integer
               if 0 < chips_taken <4: #makes sure that the player cant take more than 3 ans less than 1 chips
                   chips_left -= chips_taken
                   break
               else:
                   print("PLwase take between 1 and 3 chips")
           except ValueError: #if it isnt a number
               print("Please try again, maybe put just a number this time.")

       if chips_left == 0:
           print(name, "wins!")
           player_1_score += 1
           print(name, "score is",player_1_score, "and", name2, "score is", player_2_score)
           break
      

       # Player 2's turn
       print("Chips left:" ,chips_left)
       while True:
           try:
               print(name2)
               chips_taken = int(input("choose an amount of chips to take (1-3): "))
               if 0 <= chips_taken <= 4:
                   chips_left -= chips_taken
                   break
               else:
                   print("PLwase take between 1 and 3 chips")
           except ValueError: #if the player enters something that isnt a number
               print("please enter a proper number.")

       if chips_left == 0: #This code if when thr amount if chips reaches 0 and then the game either ends or goes to anothor round
           print(name2, "wins this round!")
           player_2_score += 1 #Adds 1 to score after each round
           print(name, "score is",player_1_score, "and", name2, "score is", player_2_score)
           break
      

   rounds -= 1  # takes away 1 form each Rounds

# Shows the scores after evry gamw
print("\nGame Over!")
if player_1_score > player_2_score:
   print(name, "wins the game!")
elif player_2_score > player_1_score:
   print(name2, "wins the game!")
else: #Displays who wins at the end
   print("Its a tie you both win")