""" ------------------------------------------------- Project: Take-Away Game Standard: TE PUNGA - Programming Assessment School: Tauranga Boys' College Author: Zack Norwood Date: 22/03/2024 Python: 3.5 ------------------------------------------------- """ #Imports the time module import time #Sets the constant value for the max chips maxChips = 21 #Prints the instructions for the game print( "Welcome to the Take-Away Game!" ) time.sleep(1) print( "To play, you will play against a friend, and take away 1, 2 or 3 chips from 21, and whoever takes the last chip wins! " ) time.sleep(3.5) #Asks the two players names p1name = input( "Who's player 1? " ) p2name = input( "Who's player 2? " ) #Asks the player how many rounds they'd like to play rounds = int( input( "How many rounds would you like to play? " ) ) #Defines a function that resets the round at the start of each round def setup() : global chips chips = maxChips #Defines a function for how each round will play out def round() : global p1Wins, p2Wins, rounds #Resets the chips before the round is played setup() #Keeps running alternating turns until there are no chips left while chips > 0 : player1Turn() if chips == 0 : break else : player2Turn() if chips >= 0 : print( "There are", chips, "chips left. " ) else : print( "There are 0 chips left. " ) #After there are no chips left, it takesaway from the rounds value to ensure the right amount of rounds are played rounds = rounds - 1 #Prints the amount of rounds each player has won print( p1name, "has", p1Wins, "wins and", p2name, "has", p2Wins, "wins." ) time.sleep(1) #Defines a function to check that the amount of chips taken is acceptable def check() : global chipsTaken, chips chipsTaken = int( input( "Enter the number of chips that you'd like to take away! (1-3) " ) ) #Checks whether the chips taken is 1, 2 or 3 while chipsTaken > 3 or chipsTaken < 1 : print( "Please enter a valid value" ) chipsTaken = int( input( "Enter the number of chips that you'd like to take away! (1-3) " ) ) #Once it shows the requested amount is valid, it takes away the amount of chips requested chips = chips - chipsTaken #Defines a function for player 1s turn def player1Turn() : global p1Wins #Tells the player its their turn print( "It's your turn", p1name ) #Checks how many chips the player wants to take check() #If the player takes the last chip it adds one to the amount of rounds they've won if chips == 0 : p1Wins = p1Wins + 1 #Defines a function for player 2s turn def player2Turn() : global p2Wins print( "It's your turn", p2name ) #Checks how many chips the player wants to take check() #If the player takes the last chip it adds one to the amount of rounds they've won if chips == 0 : p2Wins = p2Wins + 1 #Defines a function for how the game will play out and the order of functions ran def play() : global maxChips, p1Wins, p2Wins #Sets the starting constant values maxChips = 21 p1Wins = 0 p2Wins = 0 setup() #Keeps running the round function until you've played the amount that has been requested while rounds != 0 : round() #Determines the winner after all rounds have been played if p1Wins > p2Wins : print( p1name, "wins!" ) elif p2Wins > p1Wins : print( p2name, "wins!" ) elif p1Wins == p2Wins : print( "It's a draw!") time.sleep(1) #Runs the play function and starts the game play() #Asks the player if they'd like to play again, and keeps repeating the code until they input something other than y playAgain = input( "Would you like to play again? (y/n) " ) while playAgain == "y" : #Asks the player how many rounds they'd like to play rounds = int( input( "How many rounds would you like to play? " ) ) play() playAgain = input( "Would you like to play again? (y/n) " ) print( "Thanks for playing!!" )