"""
-------------------------------------------------
   Project: Take-Away Game
  Standard: 91883 (AS1.7) v.1
    School: Tauranga Boys' College
    Author: Seb Drew
      Date: 24/0/23
    Python: 3.5
-------------------------------------------------
"""

#Reseting the variables for the start of the game
MAX_CHIPS = 21 #this is a constant variable and is not going to change
score1 = 0 #This is the score for player 1, it 0 as player 1 has not scored yet
score2 = 0 #This is the score for player 2, it 0 as player 2 has not scored yet
chips_left = 0 #This is the variable to tell how many chips are left in the pots
chips_taken = 0 #This is the amount of how many chips each player takes
rounds_played = 1 #Variable to tell how many rounds we play. Start at one and add 1 each round until it is the same as the variable "rounds"

#Greeting the players
print("Hello and welcome to my game. It is a take-away game. Good luck")
print("")

#Making the name functions and making sure they don't have numbers in them
def names():
  global name1, name2 #nameing the variables that i will use in this function
  
  while True:
    name1 = input("What is Player 1's name? ") #ask for player 1's name
    if name1.isalpha() == True: #making sure name1 is a str
        break
    else:
        print("Please enter a string input")
  while True:
  
    while True:
      name2 = input("What is Player 2's name? ") #ask for player 2's name
      if name2.isalpha() == True: #making sure name2 is a str
        break
       
      else:
         print("Please enter a string input")

    if name2 != name1: #making sure name2 is not = name1
      break
    else:
      print("Player 2's name cannot be the same as player 1 ({})".format(name1))


#Creating the rounds function and making sure it is a number. Then I can just call on it later on
def round():
  global rounds #nameing the variables that i will use in this function
  
  while True:
    rounds = input("How many rounds would you like to play? ")# asking for rounds from users
    if rounds.isnumeric() == True:  # making sure ronds variable is a int
     rounds = int(rounds)
     break
    else:
      print("That is not a number. ")
  


#Calling on the name and round functions. This will activate them
names()

round()

#Making the functions for chips taken
def taken1():
  global chips_taken, chips_left #nameing the variables that i will use in this function

  while True:
    while True:
  
      while True:
        try:
          chips_taken = int(input("{}, it is your turn, enter a number (1 - 3)  ".format(name1))) #asking for chips taken from name1
          break
  
        except ValueError:
          print("That is not a number") #not a number eg: k
  
      if chips_taken > 0 and chips_taken < 4: # making sure chips_taken is (1-3)
        break
      
      else:
        print("That is not between (1 - 3)  ")

    if chips_taken <= chips_left: #making sure chips taken is less than or = to chips left
      break

    elif chips_taken >= chips_left:
      print("You idiot, you cannot take {} chips as there is/are only {} chip/s in the pot".format(chips_taken, chips_left))
  
def taken2():
  global chips_taken, chips_left #nameing the variables that i will use in this function

  while True:
    
    while True:
  
      while True:
        try:
          chips_taken = int(input("{}, it is your turn, enter a number (1 - 3)  ".format(name2))) #asking for chips taken from name2
          break
  
        except ValueError:
          print("That is not a number") #not a number eg: k
  
      if chips_taken > 0 and chips_taken < 4: # making sure chips_taken is (1-3)
        break
      
      else:
        print("That is not between (1 - 3)  ")

    if chips_taken <= chips_left:  #making sure chips taken is less than or = to chips left
      break

    elif chips_taken >= chips_left:
      print("You idiot, you cannot take {} chips as there is/are only {} chip/s in the pot".format(chips_taken, chips_left))
  

#Starting the rounds loop
while rounds > 0:
  chips_left = MAX_CHIPS #making chips left variable = to the constant variable, MAXCHIPS
  print("")

#Outputing scores so far
  print("Scores are: ")
  print("{}: {} ".format(name1, score1))
  print("{}: {} ".format(name2, score2))


  print("")
  print("Round  ", rounds_played)

#Starting the chips left code
  while chips_left > 0:
    print("")
    print("Chips left:  {}".format(chips_left))

#Calling upon my function to ask how many chips player 1 wants to take
    taken1()

    chips_left = chips_left - chips_taken #finding the true amount chips left after chips taken

#If chips_left is <= 0, player 1 wins the round
    if chips_left == 0:
      print("")
      print("Round {} has been won by {}".format(rounds_played, name1))
      score1 = score1 + 1
      rounds_played = rounds_played + 1
      break
    
#Output of how many chips are left    
    print("")
    print("Chips left:  {}".format(chips_left))

#Calling upon my function to ask how many chips player 2 wants to take
    taken2()

    chips_left = chips_left - chips_taken#finding the true amount chips left after chips taken

#If chips_left is <= 0, player 2 wins the round
    if chips_left == 0:
      print("")
      print("Round {} has been won by {}".format(rounds_played, name2))
      score2 = score2 + 1
      rounds_played = rounds_played + 1
      break

  rounds = rounds - 1

#Outputting the final scores
print("")
print("Final Scores:")
print("{}: {} ".format(name1, score1))
print("{}: {} ".format(name2, score2))
print("")


#Stating who won overall
if score1 > score2:
  print("{} has won the game for the chosen amount of rounds".format(name1))
  print("{} is a loser".format(name2))
  print("")


if score1 < score2:
  print("{} has won the game for the chosen amount of rounds".format(name2))
  print("{} is a loser".format(name1))
  print("")


if score1 == score2:
  print("It is a DRAWWWWW!!!!!")
  print("")