import time """ ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: Harvey Lee Church Date: THE DATE YOU COMPLETED THE CODE Python: 3.5 ------------------------------------------------- """ print("Kia ora, welcome to the 21 Game! Please enter your names below.") # Constants MAX_CHIPS = 21 player_one_score = 0 player_two_score = 0 chips_left = MAX_CHIPS # Get player names name1 = input("Player 1, what is your name? ") name2 = input("Player 2, what is your name? ") print(f"Kia ora, {name1} and {name2}!") # Get the number of rounds to play while True: try: rounds = int(input("How many rounds do you want to play? (Choose between 1-5): ")) if 1 <= rounds <= 5: break else: print("Please select a number of rounds between 1 and 5.") except ValueError: print("Invalid input! Please enter a valid number for the rounds.") print("Game starting in 5 seconds...") time.sleep(5) # Game loop for round_number in range(1, rounds + 1): print(f"\n--- Round {round_number} ---") chips_left = MAX_CHIPS # Reset chips at the beginning of each round player_turn = 1 # Player 1 starts the game while chips_left > 0: print(f"\nChips left: {chips_left}") # Player 1's turn if player_turn == 1: print(f"{name1}'s turn!") while True: try: chips_taken = int(input(f"How many chips would you like to take (1-4)? ")) if 1 <= chips_taken <= 3 and chips_taken <= chips_left: chips_left -= chips_taken player_one_score += chips_taken # Keep track of score break else: print(f"Please take between 1 and 4 chips, and there are only {chips_left} chips left.") except ValueError: print("Invalid input! Please enter a valid number between 1 and 4.") # Player 2's turn elif player_turn == 2: print(f"{name2}'s turn!") while True: try: chips_taken = int(input(f"How many chips would you like to take (1-4)? ")) if 1 <= chips_taken <= 3 and chips_taken <= chips_left: chips_left -= chips_taken player_two_score += chips_taken # Keep track of score break else: print(f"Please take between 1 and 4 chips, and there are only {chips_left} chips left.") except ValueError: print("Invalid input! Please enter a valid number between 1 and 4.") # Switch turns player_turn = 1 if player_turn == 2 else 2 # Announce the winner for this round if chips_left == 0: if player_turn == 1: winner = name2 else: winner = name1 print(f"\n{winner} wins Round {round_number}!") print(f"Scores after Round {round_number}: {name1}: {player_one_score}, {name2}: {player_two_score}") # Announce the final winner print("\nGame Over!") if player_one_score > player_two_score: print(f"{name1} is the overall winner with a score of {player_one_score}!") elif player_two_score > player_one_score: print(f"{name2} is the overall winner with a score of {player_two_score}!") else: print(f"It's a tie! Both players scored {player_one_score}.")