|
1 | 1 | """ |
2 | 2 | Snake-Water-Gun Game |
3 | 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. |
| 4 | +A simple game where the user plays against the computer. |
| 5 | +Choices: 's' for Snake, 'w' for Water, 'g' for Gun. |
6 | 6 |
|
7 | | -Fixes Issue: #12987 |
| 7 | +Reference: https://en.wikipedia.org/wiki/Rock_paper_scissors |
8 | 8 | """ |
9 | 9 |
|
10 | 10 | import random |
11 | 11 | from typing import Literal |
12 | 12 |
|
13 | 13 |
|
| 14 | +<<<<<<< HEAD |
14 | 15 | def snake_water_gun( |
15 | 16 | user_choice: Literal["s", "w", "g"], computer_choice: Literal["s", "w", "g"] |
16 | 17 | ) -> str: |
| 18 | +======= |
| 19 | +def get_computer_choice() -> Literal["s", "w", "g"]: |
| 20 | +>>>>>>> b5d62810 (Refactored snake_game.py with doctests and type hints for automated testing) |
17 | 21 | """ |
18 | | - Determines the winner between user and computer. |
| 22 | + Randomly returns the computer's choice. |
19 | 23 |
|
20 | | - Parameters: |
21 | | - user_choice (str): 's' for Snake, 'w' for Water, 'g' for Gun |
22 | | - computer_choice (str): 's' for Snake, 'w' for Water, 'g' for Gun |
| 24 | + Returns |
| 25 | + ------- |
| 26 | + str |
| 27 | + 's' for snake, 'w' for water, or 'g' for gun |
| 28 | + """ |
| 29 | + return random.choice(["s", "w", "g"]) |
| 30 | + |
| 31 | + |
| 32 | +def get_user_choice() -> Literal["s", "w", "g"]: |
| 33 | + """ |
| 34 | + Prompts the user to enter their choice and validates it. |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + str |
| 39 | + 's' for snake, 'w' for water, or 'g' for gun |
| 40 | + """ |
| 41 | + while True: |
| 42 | + choice = input("Enter your choice: s for snake, w for water, g for gun: ").strip().lower() |
| 43 | + if choice in ("s", "w", "g"): |
| 44 | + return choice |
| 45 | + print("Invalid input! Please enter 's', 'w', or 'g'.") |
23 | 46 |
|
24 | | - Returns: |
25 | | - str: Result message - 'Draw', 'You win!', or 'Computer wins!' |
26 | 47 |
|
27 | | - >>> snake_water_gun('s', 'w') |
28 | | - 'You win!' |
29 | | - >>> snake_water_gun('g', 's') |
30 | | - 'You win!' |
31 | | - >>> snake_water_gun('w', 'w') |
32 | | - 'Draw' |
| 48 | +def determine_winner(user_choice: str, computer_choice: str) -> str: |
| 49 | + """ |
| 50 | + Determines the winner of the Snake-Water-Gun game. |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + user_choice : str |
| 55 | + The user's choice |
| 56 | + computer_choice : str |
| 57 | + The computer's choice |
| 58 | +
|
| 59 | + Returns |
| 60 | + ------- |
| 61 | + str |
| 62 | + "draw", "user", or "computer" indicating the winner |
| 63 | +
|
| 64 | + Examples |
| 65 | + -------- |
| 66 | + >>> determine_winner("s", "w") |
| 67 | + 'user' |
| 68 | + >>> determine_winner("w", "s") |
| 69 | + 'computer' |
| 70 | + >>> determine_winner("g", "g") |
| 71 | + 'draw' |
33 | 72 | """ |
34 | 73 | if user_choice == computer_choice: |
| 74 | +<<<<<<< HEAD |
35 | 75 | return "Draw" |
36 | 76 | if (user_choice, computer_choice) in [("s", "w"), ("w", "g"), ("g", "s")]: |
37 | 77 | return "You win!" |
38 | 78 | return "Computer wins!" |
39 | 79 |
|
40 | 80 |
|
41 | 81 | def main() -> None: |
| 82 | +======= |
| 83 | + return "draw" |
| 84 | + |
| 85 | + wins = { |
| 86 | + "s": "w", # snake drinks water |
| 87 | + "w": "g", # water damages gun |
| 88 | + "g": "s", # gun kills snake |
| 89 | + } |
| 90 | + |
| 91 | + if wins[user_choice] == computer_choice: |
| 92 | + return "user" |
| 93 | + return "computer" |
| 94 | + |
| 95 | + |
| 96 | +def play_game() -> None: |
| 97 | +>>>>>>> b5d62810 (Refactored snake_game.py with doctests and type hints for automated testing) |
42 | 98 | """ |
43 | | - Main function to run the Snake-Water-Gun game with multiple rounds. |
| 99 | + Runs the Snake-Water-Gun game. |
44 | 100 | """ |
45 | | - print("Welcome to Snake-Water-Gun Game!") |
46 | | - rounds = 0 |
| 101 | + user_choice = get_user_choice() |
| 102 | + computer_choice = get_computer_choice() |
47 | 103 |
|
| 104 | +<<<<<<< HEAD |
48 | 105 | while True: |
49 | 106 | try: |
50 | 107 | rounds = int( |
@@ -93,9 +150,20 @@ def main() -> None: |
93 | 150 | print("Congratulations! You won the game!") |
94 | 151 | elif score_user < score_computer: |
95 | 152 | print("Computer won the game! Better luck next time!") |
| 153 | +======= |
| 154 | + print(f"You chose: {user_choice}") |
| 155 | + print(f"Computer chose: {computer_choice}") |
| 156 | + |
| 157 | + winner = determine_winner(user_choice, computer_choice) |
| 158 | + if winner == "draw": |
| 159 | + print("It's a draw!") |
| 160 | + elif winner == "user": |
| 161 | + print("You win!") |
| 162 | +>>>>>>> b5d62810 (Refactored snake_game.py with doctests and type hints for automated testing) |
96 | 163 | else: |
97 | | - print("It's a tie!") |
| 164 | + print("Computer wins!") |
| 165 | + |
98 | 166 |
|
99 | 167 |
|
100 | 168 | if __name__ == "__main__": |
101 | | - main() |
| 169 | + play_game() |
0 commit comments