#------------------------------------------------- #Project: Take-Away Game #Standard: 91883 (AS1.7) v.1 #School: Tauranga Boys' College #Author: Johan Samuel Joji #Date: 14 - March - 2025 #Python: 3.5 #------------------------------------------------- # Constant value MAX_CHIPS = 21 # making sure that the rounds inputted does not get to a negative number def get_positive_int(prompt): while True: try: value = int(input(prompt)) if value < 1 or value > 3: print("Please enter a number between 1 and 3.") else: return value except ValueError: # if the player exceeds or enters a negative number from 1 to 5 rounds print("Please enter a valid number.") # Players choose the number of chips taken for each round from 1 to 3 def play_round(chips_left, player_name): while True: chips_taken = get_positive_int(player_name + ", how many chips do you want to take? (1-3): ") if chips_taken <= chips_left: chips_left -= chips_taken print(player_name, "took", chips_taken, "chips. Chips left:", chips_left) return chips_left else: # if player chooses a number not given from the list print("Please take between 1 and 3 chips, and no more than", chips_left, "chips.") # Making sure player names are actual letters and not any numbers added onto it def get_player_name(player_number): while True: player_name = input("Enter Player " + str(player_number) + "'s name: ") if player_name.isalpha(): return player_name else: # If names are inputted as numbers print("Invalid name. Please enter a name with only alphabetic characters.") # Gameplay def main(): player_one_score = 0 player_two_score = 0 # Player Names player_one_name = get_player_name(1) player_two_name = get_player_name(2) # Number of rounds between 1 and 5 rounds = get_positive_int("How many rounds do you want to play? (1-5): ") while rounds > 0: print("Round", rounds, "starts! Maximum chips per round:", MAX_CHIPS) # Number of chips at the beginning of the game chips_left = MAX_CHIPS # If player one wins the round while chips_left > 0: chips_left = play_round(chips_left, player_one_name) if chips_left <= 0: print(player_one_name, "wins this round!") player_one_score += 1 break # If player two wins the round chips_left = play_round(chips_left, player_two_name) if chips_left <= 0: print(player_two_name, "wins this round!") player_two_score += 1 break # Reducing the number of rounds after each round rounds -= 1 # Final Score print("Game Over!") if player_one_score > player_two_score: print(player_one_name, "is the winner with", player_one_score, "points!") elif player_two_score > player_one_score: print(player_two_name, "is the winner with", player_two_score, "points!") else: print("It's a tie!") if __name__ == "__main__": main()