""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: Ryan Piddock Date: 13/03/25 Python: 3.5 ------------------------------------------------- """ """ The game consists of 2 players taking turns taking chips from a pile of 21. The one who thatkes the remaining number of chips on their turn wins. However, the maximum amount of chips a player can take on their turn is 3. Each turn, the designated player must take at least 1 chips from the pile. If there is less than 3 chips remaining in the pile, the player cannot take more than than there is remaining (eg. taking 3 when there is 1 chip remaining). Before the game, the players choose the number of rounds they wish to play and play that number of rounds. The maximum number of rounds a player can play is 5, and the minimum is 1. Once the correct number of rounds have been played, the player who wins the most rounds will be the overall winner of the game. A tie can happen, and if this occurs, the result will be stated at the end of the game. """ # SETUP - These lines import the time library and set all variables and constants to 0. Player_turn represents the player who's turn it is. Because each round starts with player 1's turn, this value is set to 1. import time player_score1 = 0 player_score2 = 0 chips_remaining = 0 chips_taken = 0 current_round = 0 CHIPS = 21 player_turn = 1 winner = 0 # STARTING INPUTS - These lines represent the names of the two players, and the number of rounds they wish to play. The game requires a string input with no spaces or symbols from the player. For the rounds, it requires a integer between one and five. If either of thse terms are violated, the player is aksed to enter in their prefernces again. This continues until there are two valid names and one valid number of rounds. while True: player_name1 = str(input("Player 1, what is your name? ")) if player_name1.isalpha() == True: break else: print("That is not an acceptable name. Please try again.") while True: player_name2 = str(input("Player 2, what is your name? ")) if player_name2.isalpha() == True: break else: print("That is not an acceptable name. Please try again.") while True: try: rounds = int(input("How many rounds would you like to play? (1-5)")) if rounds < 1 or rounds > 5: print("Please choose a number between 1 and 5.") else: break except: print("Please choose a number.") # GAME - This is the main code that runs the game itself. while current_round != rounds: # This makes sure the games continue until the designated number of rounds have been played. current_round = current_round + 1 chips_remaining = CHIPS # Increases the currend round by 1 and sets the number of remaining chips to 0. print("This is round", current_round) print(player_name1, "'s score is:", player_score1) print(player_name2, "'s score is:", player_score2) # States the current round and the player's scores time.sleep(1) while chips_remaining > 0: # Continues the game until there are no chips remaining print("There are ", chips_remaining, " chips remaining.") # States the number of chips remaining time.sleep(1) if player_turn == 1: print(player_name1) else: print(player_name2) # States the name of the player whose turn it is while True: try: chips_taken = int(input("How many chips do you wish to take? (1-3)")) if chips_taken < 1 or chips_taken > 3: print("You can only take between 1 and 3 chips.") # If they take more than 3 chips or less than 1 chip, the game reminds the player that they can only take between 1 and three chips. They are asked to take again. elif chips_taken > chips_remaining: print("There is not enough chips. Try again.") # If they take more chips than the amount remaining then they are asked to take again. else: break except: print("Please choose a number.") chips_remaining = (chips_remaining - chips_taken) # Removes the number of chips taken by the player from the number of chips remaining. winner = player_turn # Sets the winner of the round to the one who's turn it was when the last chips were taken. player_turn = (3 - player_turn) # Resets the players turn to the alternative player. if winner == 1: print(player_name1, "wins!") player_score1 = (player_score1 + 1) # Increases the player's score by 1 if they win. else: print(player_name2, "wins!") player_score2 = (player_score2 + 1) # Increases the other player's score by 1 if they win instead. print("The game has finished.") time.sleep(1) print("The score is", player_score1, "-", player_score2) # Compares the score of both players if player_score1 > player_score2: print(player_name1, "wins!") elif player_score1 < player_score2: print(player_name2, "wins!") else : print("It was a tie!") # Prints the result based on the player's scores. print("Thanks for playing my game!")