#------------------------------------------------- # Project: Take-Away Game # Standard: 91883 (AS1.7) v.1 # School: Tauranga Boys' College # Author: Korban Ian Anthony Tuck # Date: 22/03/2024 # Python: 3.5 #------------------------------------------------- import time # getting the name of player 1 and validating it player1 = str(input("What is the name of player 1?: ")) while True: time.sleep(0.5) if player1.isalpha() == True : print("Hello " + player1) print() break else: print( "That name does not work, please try again") player1 = str(input("What is the name of player 1?: ")) # getting the name of player 2 and validating it player2 = str(input("What is the name of player 2?: ")) while True: time.sleep(0.5) if player1.isalpha() == True : print("Hello " + player1) print() break else: print( "That name does not work, please try again") # asks the user how many rounds they wish to play, and making sure its valid while True: try: rounds = int(input("How many rounds would you guys like to play?: ")) if rounds < 1 or rounds > 5 : print( "Please choose a number between 1 and 5") else: break except: print( "You have to give me a number!") # setting the variables maxchips = 21 player1Score = 0 player2Score = 0 chipsLeft = 0 roundsPlayed = 0 # function to setup the variables for a new round def setup(): global roundsPlayed global chipsLeft chipsLeft = maxchips roundsPlayed += 1 print() time.sleep(0.5) print( "We will now begin round number " + str(roundsPlayed)) # this function executes a full round def round(): global chipsLeft, roundsPlayed setup() while chipsLeft > 0: player1turn() time.sleep(0.5) print() print("There are " + str(chipsLeft) + " Chips left") if chipsLeft <= 0: print() print(player1 + " has won this round") break else: player2turn() time.sleep(0.5) print() if chipsLeft > 0: print("There are " + str(chipsLeft) + " Chips left") else: print(player2 + " has won this round") # shows who has more points by the end of each round time.sleep(0.5) print() print(player1 + " has " + str(player1Score) + " points") print() time.sleep(0.5) print(player2 + " has " + str(player2Score) + " points") # function for player 1's turn and making sure the number of chips they input is valid def player1turn(): global chipsLeft global player1Score print(player1 + "'s turn") time.sleep(0.5) print() chipsTaken = int(input("How many chips would " + player1 + " like to take?")) if chipsTaken > 3 or chipsTaken < 1: while True: print("Please input a valid number between 1 and 3") time.sleep(0.5) print() chipsTaken = int(input("How many chips would " + player1 + " like to take?")) if chipsTaken > 3 or chipsTaken < 1: True else: break print() print(player1 + " has taken " + str(chipsTaken)) chipsLeft -= chipsTaken if chipsLeft <= 0: player1Score = player1Score + 1 else: print() print(player1 + " has taken " + str(chipsTaken)) chipsLeft -= chipsTaken if chipsLeft <= 0: player1Score = player1Score + 1 # function for player 2's turn and making sure the number of chips they input is valid def player2turn(): global chipsLeft global player2Score print(player2 + "'s turn") time.sleep(0.5) print() chipsTaken = int(input("How many chips would " + player2 + " like to take?")) if chipsTaken > 3 or chipsTaken < 1: while True: print("Please input a valid number between 1 and 3") time.sleep(0.5) print() chipsTaken = int(input("How many chips would " + player2 + " like to take?")) if chipsTaken > 3 or chipsTaken < 1: True else: break print() print(player2 + " has taken " + str(chipsTaken)) chipsLeft -= chipsTaken if chipsLeft <= 0: player2Score = player2Score + 1 else: print() print(player2 + " has taken " + str(chipsTaken)) chipsLeft -= chipsTaken if chipsLeft <= 0: player2Score = player2Score + 1 # funtion for executing the whole game, including the ending def play(): for r in range(rounds): round() if player1Score > player2Score : time.sleep(1) print() print(player1 + " has won the game with " + str(player1Score) + " points well done!") elif player2Score > player1Score : time.sleep(1) print() print(player2 + " has won the game with " + str(player2Score) + " points well done!") else: time.sleep(1) print() print( "The game has ended in a draw, Your both equally as good!") # calls the function to actually play the game play()