""" ------------------------------------------------- Project: Python Takeaway Game Standard: 91883 School: Tauranga Boys College Author: Jean-Luke van der Berg Date: 14/3/25 Python: 3.11.9 ------------------------------------------------- This game is called the take away game, how it works is there are 2 player, 21 chips, and each player can only take between 1 and 3 chips when it is there turn, the person to take the last chip wins the round, it allows the user too pick the amount of rounds and their names too make the game a bit more accsesible, and customizable. ------------------------------------------------- """ #Setup this includes the starting score of player 1 and 2(p1 and p2), what the current round is(current_r = 0), imported time varieble, the palyer turn(p_turn = 2), the round win varieble(round_win = 2) and the number of starting chips which is a constant. then it prints a short description of how the game works for new players. import time current_r = 0 p1 = 0 p2 = 0 chips = 21 p_turn = 2 round_win = 2 #Description of game so that new playes will know how the game works. print("This game is called the take away game, how it works is there are 2 player, 21 chips, and each player can only take between 1 and 3 chips when it is there turn, the person to take the last chip wins the round ") #Checking for validation so that it only accepts names without numbers or spaces. it uses the isalpha code to verify that the code is a string and if it is it allows it then stops the code, if the name is not a string and isalpha = false it prints that the number is not acceptable. while True: name1 = str(input("Name of player 1: ")) if name1.isalpha() == True: break else: print("That is not an acceptable name, please try again. ") while True: name2 = str(input("Name of player 2: ")) if name2.isalpha() == True: break else: print("That is not an acceptable name, please try again. ") #Checking for integer validation this time it uses the greater than and less than symbols to heck if the number is between 1 and 5, or else it will make you put in a number again. It uses the except code to check if the user put in a number, and if the didn't it will say you didn't put in a number and make you do it again. while True: try: rounds = int(input("How many rounds do you two want to play, between 1 and 5? ")) if rounds < 1 or rounds > 5: print("Please choose a number between 1 and 5") else: break except: print("You didn't put in a number, please make sure to put in a number this time.") # Start of main game loop which will change the player scores, current round, current chips, and who wins the game, this is very important. first it makes sure that current rounds is not equal to rounds then it runs the start of the game, after it says the players score, then finnaly if the final round ends it tells you the score and who won the game. while current_r != rounds : chips_r = chips print('Current chips: ', chips_r) while chips_r > 0: p_turn = 3 - p_turn round_win = 3 - round_win print("There are", chips_r, "remaining") if p_turn == 1: print(name1) else: print(name2) #This is the loop that checks that checks all the validation for the amount of chips a person can take, and makes sure they can only take from the amount of chips there are left. by saying that chips taken can only be between 1 and 3 chips as the first restriction, and that the chips taken has to be withing the amount of chips remaining. Then it uses the except varible in the case that a player has not picked a number. while True: try: ct = int(input("How many chips would you like to take (1-3): ")) if ct < 1 or ct > 3: print("Invalid number, please pick a number between 1 and 3") elif chips_r < ct: print("This is greater than the amount of chips there are remaining, please take from the amount of chips remaining, which is", chips_r) else: break except: print("You didn't put in a number, please try again.") chips_r = chips_r - ct round_win == p_turn #This loop adds the player scores and runs the rounds until the current rounds is equal to the rounds the players put in at the start of the game. only if the round win varieble is equal to 1 will it add the player score to the correct person then it will display both players scores at the end of a round. if round_win == 1: print("Congragulations", name1, ", you won that round") p1 = p1 + 1 print(name1, "score is", p1) print(name2, "score is", p2) else: print("Congragulations", name2, ", you won that round") p2 = p2 + 1 print(name1, "score is", p1) #Score of player 1 print(name2, "score is", p2) #Score of player 2 current_r += 1 #This checks the overall game winner and if there is a case of a tie, it tells them. by using the if, and elif blocks the code checks which one is true by running each block of code, and once the game winner has been displayed, it uses the time.sleep import to wait 2 seconds then thank the players for playing my game. if current_r == rounds and p1 > p2: print("Good job", name1, "you won") elif current_r == rounds and p2 > p1: print("Good job", name2, "you won the game") break elif current_r == rounds and p2 == p1 : print("The game was a tie, hope you both enjoyed") break time.sleep(2) print("Thanks for playing the game!")