#-------------------------------------------------
#   Project: Take-Away Game
#  Standard: 91883 (AS1.7) v.1
#    School: Tauranga Boys' College
#    Author: Alex Kho
#      Date: THE DATE YOU COMPLETED THE CODE
#    Python: 3.5
#-------------------------------------------------

import random
import time

START_CHIPS = 21

total_rounds = 0
current_round = 1
chips = 0
taken = 0
p1_win = 0
p2_win = 0

#asks for player 1 and player 2 name and checks validity
while True:
    player1 = input( "Player 1 name: " )
    player2 = input( "Player 2 name: ")
    if player1.isalpha() == True and player2.isalpha() == True:
        break
    else:
        print( "No, that's not a valid name..." )
        print()

#verifies rounds to be played
while True:
    try:
        total_rounds = int(input( "How many rounds would you like to play? (1-5): " ) )
        if total_rounds > 0 and total_rounds < 6:
            break
        else:
            print()
            print( "1-5, guys.. lets try again" )

    except:
        print()
        print( "Thats not a number, buddy." )
print()
print( "Take-Away")
print( "Be the One to reach 0 chips!" )

#plays number of rounds specified
while current_round <= total_rounds:

    print()
    print( "Round {}".format(current_round) )
    chips = START_CHIPS
    time.sleep(1)

    while chips > 0:
        
        #player 1 turn
        while True:
            try:
                print()
                print( "{}'s Turn:".format(player1) )
                time.sleep(0.3)
                print( "Chips: {}".format(chips) )
                time.sleep(0.3)
                taken = int( input( "Take one, two or three chips: " ) ) 
                if taken < 4 and taken > 0:
                        chips = chips - taken
                        break

                else:
                    print()
                    print( "!Thats not how it works, dimwit!")
            except:
                print()
                print( "Yes, you do need to put a number down." )      

        #player 1 win check
        if chips <= 0:
            current_round = current_round + 1
            p1_win = p1_win + 1
            break    
        
        #player 2 turn
        while True:
            try:
                print()
                print( "{}'s Turn:".format(player2) )
                time.sleep(0.3)
                print( "Chips: {}".format(chips) )
                time.sleep(0.3)
                taken = int( input( "Take one, two or three chips: " ) ) 
                if taken < 4 and taken > 0:
                    chips = chips - taken
                    break

                else:
                    print()
                    print( "!Thats not how it works, dimwit!")
            except:
                print()
                print( "Yes, you do need to put a number down." )

        #player 2 win check
        if chips <= 0:
            current_round = current_round + 1
            p2_win = p2_win + 1
            break

#end scene  
print()
print( "       FINAL SCORE" )
print( "|========================|" )
print( " {} won {} out of {} games!".format(player1, p1_win, total_rounds) )
print( " {} won {} out of {} games!".format(player2, p2_win, total_rounds) )
#outputs winner
while True:
    if p1_win > p2_win:
        print( " {} wins!".format(player1) )
        break
    if p2_win > p1_win:
        print( " {} wins!".format(player2) )
        break
    else:
        print( " Its a tie!" )
        break
print( "|========================|" )