# Project: Take-Away Game # Standard: 91883 (AS1.7) v.1 # School: Tauranga Boys' College # Author: Philipp KLochko # Date: 25/03/2024 # Python: 3.5 import random # Constants MAX_CHIPS = 21 # Function to get valid integer input from user def get_valid_input(prompt): while True: user_input = input(prompt) if user_input.isdigit(): return int(user_input) else: print("Please enter a valid integer.") # Function to check if a string contains only numeric characters def is_numeric(input_string): return input_string.isdigit() # Function to get valid player name input from user def get_player_name(prompt): while True: name = input(prompt) if name.strip() and not is_numeric(name): # Check if name is not empty or whitespace and not numeric return name else: print("Please enter a valid name.") # Function to play a round of the game def play_round(player_name): chips_left = MAX_CHIPS while chips_left > 0: print("Chips left:", chips_left) chips_taken = get_valid_input(f"{player_name}, choose how many chips to take: ") # Check if chips taken is within valid range if 1 <= chips_taken <= min(3, chips_left): chips_left -= chips_taken else: print("Please choose between 1 and", min(3, chips_left)) # Check if the player has won if chips_left == 0: return player_name # Header block comment """ This program simulates a game where two players take turns removing chips from a pile. The player who takes the last chip(s) wins the round. """ player_one_name = get_player_name("Enter Player One's name: ") player_two_name = get_player_name("Enter Player Two's name: ") # Get number of rounds rounds = get_valid_input("Enter the number of rounds: ") if rounds <= 0: print("Number of rounds must be positive.") exit() # Play rounds player_scores = {player_one_name: 0, player_two_name: 0} for round_num in range(1, rounds + 1): print("\nRound", round_num) winner = play_round(player_one_name) player_scores[winner] += 1 winner = play_round(player_two_name) player_scores[winner] += 1 # Determine the winner if player_scores[player_one_name] > player_scores[player_two_name]: print(f"\n{player_one_name} wins with a score of {player_scores[player_one_name]}!") elif player_scores[player_two_name] > player_scores[player_one_name]: print(f"\n{player_two_name} wins with a score of {player_scores[player_two_name]}!") else: print("\nIt's a tie!")