|
| 1 | +""" |
| 2 | +Snake-Water-Gun Game |
| 3 | +
|
| 4 | +This program allows a user to play the classic Snake-Water-Gun game against the computer. |
| 5 | +The user can play multiple rounds, and scores are displayed at the end. |
| 6 | +
|
| 7 | +Fixes Issue: #12987 |
| 8 | +""" |
| 9 | + |
| 10 | +import random |
| 11 | +from typing import Literal |
| 12 | + |
| 13 | +def snake_water_gun(user_choice: Literal['s', 'w', 'g'], computer_choice: Literal['s', 'w', 'g']) -> str: |
| 14 | + """ |
| 15 | + Determines the winner between user and computer. |
| 16 | +
|
| 17 | + Parameters: |
| 18 | + user_choice (str): 's' for Snake, 'w' for Water, 'g' for Gun |
| 19 | + computer_choice (str): 's' for Snake, 'w' for Water, 'g' for Gun |
| 20 | +
|
| 21 | + Returns: |
| 22 | + str: Result message - 'Draw', 'You win!', or 'Computer wins!' |
| 23 | +
|
| 24 | + >>> snake_water_gun('s', 'w') |
| 25 | + 'You win!' |
| 26 | + >>> snake_water_gun('g', 's') |
| 27 | + 'You win!' |
| 28 | + >>> snake_water_gun('w', 'w') |
| 29 | + 'Draw' |
| 30 | + """ |
| 31 | + if user_choice == computer_choice: |
| 32 | + return "Draw" |
| 33 | + if (user_choice, computer_choice) in [('s', 'w'), ('w', 'g'), ('g', 's')]: |
| 34 | + return "You win!" |
| 35 | + return "Computer wins!" |
| 36 | + |
| 37 | +def main() -> None: |
| 38 | + """ |
| 39 | + Main function to run the Snake-Water-Gun game with multiple rounds. |
| 40 | + """ |
| 41 | + print("Welcome to Snake-Water-Gun Game!") |
| 42 | + rounds = 0 |
| 43 | + |
| 44 | + while True: |
| 45 | + try: |
| 46 | + rounds = int(input("Enter the number of rounds you want to play (1-10): ").strip()) |
| 47 | + if 1 <= rounds <= 10: |
| 48 | + break |
| 49 | + else: |
| 50 | + print("Please enter a number between 1 and 10.") |
| 51 | + except ValueError: |
| 52 | + print("Invalid input. Please enter an integer.") |
| 53 | + |
| 54 | + score_user = 0 |
| 55 | + score_computer = 0 |
| 56 | + choices = ['s', 'w', 'g'] |
| 57 | + |
| 58 | + for round_number in range(1, rounds + 1): |
| 59 | + print(f"\nRound {round_number}:") |
| 60 | + while True: |
| 61 | + user_input = input("Enter your choice: s for Snake, w for Water, g for Gun: ").strip().lower() |
| 62 | + if user_input in choices: |
| 63 | + break |
| 64 | + else: |
| 65 | + print("Invalid choice. Please enter 's', 'w', or 'g'.") |
| 66 | + |
| 67 | + computer_input = random.choice(choices) |
| 68 | + print(f"You chose {user_input}, computer chose {computer_input}.") |
| 69 | + |
| 70 | + result = snake_water_gun(user_input, computer_input) |
| 71 | + print(result) |
| 72 | + |
| 73 | + if result == "You win!": |
| 74 | + score_user += 1 |
| 75 | + elif result == "Computer wins!": |
| 76 | + score_computer += 1 |
| 77 | + |
| 78 | + print("\nGame Over!") |
| 79 | + print(f"Your score: {score_user}") |
| 80 | + print(f"Computer score: {score_computer}") |
| 81 | + |
| 82 | + if score_user > score_computer: |
| 83 | + print("Congratulations! You won the game!") |
| 84 | + elif score_user < score_computer: |
| 85 | + print("Computer won the game! Better luck next time!") |
| 86 | + else: |
| 87 | + print("It's a tie!") |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments