#-------------------------------------------------------------------------- # Name: Hasib Bamyani # Project: Takeaway Game # School: Tauranga Boys College # Author: Hasib # Date Created: 18/3/2024 # Python: The 10% assessment about Takeaway game #-------------------------------------------------------------------------- # Variables that don't change. MAX_CHIPS = 21 MIN_ROUNDS = 1 MAX_ROUNDS = 10 # Some functions. def get_valid_int(prompt, min_value, max_value): """Prompts the user for a valid integer within a specified range.""" while True: try: value = int(input(prompt)) if min_value <= value <= max_value: return value else: print(f"Bro enter a number between {min_value} and {max_value}.") except ValueError: print("Invalid input. Yo Dude enter a number.") def get_valid_string(prompt): """Prompts the user for a string that contains only alphabetic characters.""" while True: value = input(prompt) if value.isalpha(): return value else: print("Invalid input. My man enter your name, containing only alphabetic characters.") def play_round(player1_name, player2_name): """Plays a single round of the game.""" chips_left = MAX_CHIPS # Start with the maximum number of chips current_player = player1_name # Player 1 goes first while chips_left > 0: # Keep playing until there are no chips left print(f"\nChips left: {chips_left}") # Shows how many chips are left chips_to_take = get_valid_int(f"{current_player}'s turn. How many chips do you take (1-3)? ", 1, min(3, chips_left)) chips_left -= chips_to_take # Switch to the other player's turn if current_player == player1_name: current_player = player2_name else: current_player = player1_name if current_player == player1_name: winner = player2_name else: winner = player1_name print(f"\n{winner} won this round!") return winner # Main part of the game print("Welcome to the Take-Away Game try to have fun!") player1_name = get_valid_string("Enter Player 1's name: ") player2_name = get_valid_string("Enter Player 2's name: ") # decidies how many rounds they want to play num_rounds = get_valid_int(f"Enter the number of rounds to play ({MIN_ROUNDS}-{MAX_ROUNDS}): ", MIN_ROUNDS, MAX_ROUNDS) player1_score = 0 player2_score = 0 for round_num in range(num_rounds): print(f"\nRound {round_num + 1}") # say the current round winner = play_round(player1_name, player2_name) if winner == player1_name: player1_score += 1 else: player2_score += 1 print(f"\nFinal scores:") print(f"{player1_name}: {player1_score}") print(f"{player2_name}: {player2_score}") # the overall winner if player1_score > player2_score: winner_name = player1_name elif player2_score > player1_score: winner_name = player2_name else: winner_name = "It's a tie!" print(f"\nLadies and gentelmans, {winner_name} is the winner of the game!")