""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: Yasin Bamyani Date: 27/03/2024 Python: 3.5 ------------------------------------------------- """ # Import the time module to add delays for better user experience import time # Print welcome message and rules of the game print("WELCOME TO TAKE AWAY GAME") time.sleep(2) print("--- RULES OF THE GAME ---") time.sleep(1) print("There are 21 Chips.") time.sleep(1) print("Every player can take a number of chips between 1 and 3.") time.sleep(1) print("Player 1 always takes the first chip in every round.") time.sleep(1) print("By the way it doesn't matter who got more chips,The player who takes the last chip wins.") time.sleep(1) print("---- START OF GAME -----\n") time.sleep(2) # Function to handle chip taking by players def take_chips(player_name, leftover_chips): while True: try: # Determine the maximum number of chips a player can take based on the number of remaining chips if leftover_chips == 1: max_chips = 1 else: max_chips = 3 # Prompt the player to input the number of chips they want to take chips_taken = int(input(f"{player_name}, How many chips do you want to take (between 1 and {max_chips})? ")) # Check if the input is valid if 1 <= chips_taken <= max_chips and chips_taken <= leftover_chips: return chips_taken else: print(f"Invalid number. Please choose between 1 and {max_chips} chips.") except ValueError: print("Invalid input. Please enter a number.") # Function to play the game def play_game(player1_name, player2_name, number_rounds): player1_score = 0 player2_score = 0 # Loop through the specified number of rounds for round_number in range(1, number_rounds + 1): leftover_chips = 21 print(f"\nRound {round_number}:") # Loop until there are no chips left while leftover_chips > 0: print(f"\nChips remaining: {leftover_chips}") # Player 1's turn chips_taken = take_chips(player1_name, leftover_chips) leftover_chips -= chips_taken if leftover_chips <= 0: print(f"{player1_name} wins the round!") player1_score += 1 break # Player 2's turn chips_taken = take_chips(player2_name, leftover_chips) leftover_chips -= chips_taken if leftover_chips <= 0: print(f"{player2_name} wins the round!") player2_score += 1 break # Print current scores and game result print(f"\nCurrent scores - {player1_name}: {player1_score}, {player2_name}: {player2_score}") print("\nGame over!") if player1_score > player2_score: print(f"{player1_name} wins the game!") elif player2_score > player1_score: print(f"{player2_name} wins the game!") else: print("It's a tie!") # Main function to start the game def main(): # Get player names player1_name = input("Enter Player 1's name: ").strip() while not player1_name.isalpha(): print("Please, enter a valid name.") player1_name = input("Enter Player 1's name: ").strip() player2_name = input("Enter Player 2's name: ").strip() while not player2_name.isalpha(): print("Please, enter a valid name.") player2_name = input("Enter Player 2's name: ").strip() # Get the number of rounds to play number_rounds = int(input("Enter the number of rounds you wish to play (between 1 and 5): ").strip()) while not 1 <= number_rounds <= 5: print("Invalid number. The number of rounds should be between 1 and 5.") number_rounds = int(input("Enter the number of rounds you wish to play (between 1 and 5): ").strip()) # Start the game play_game(player1_name, player2_name, number_rounds) # Run the main function if __name__ == "__main__": main()