Skip to content

Commit 42a7425

Browse files
committed
Refactored snake_game.py with doctests and type hints for automated testing
1 parent 241376a commit 42a7425

1 file changed

Lines changed: 88 additions & 20 deletions

File tree

snake-game/snake_game.py

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,107 @@
11
"""
22
Snake-Water-Gun Game
33
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.
66
7-
Fixes Issue: #12987
7+
Reference: https://en.wikipedia.org/wiki/Rock_paper_scissors
88
"""
99

1010
import random
1111
from typing import Literal
1212

1313

14+
<<<<<<< HEAD
1415
def snake_water_gun(
1516
user_choice: Literal["s", "w", "g"], computer_choice: Literal["s", "w", "g"]
1617
) -> 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)
1721
"""
18-
Determines the winner between user and computer.
22+
Randomly returns the computer's choice.
1923
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'.")
2346

24-
Returns:
25-
str: Result message - 'Draw', 'You win!', or 'Computer wins!'
2647

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'
3372
"""
3473
if user_choice == computer_choice:
74+
<<<<<<< HEAD
3575
return "Draw"
3676
if (user_choice, computer_choice) in [("s", "w"), ("w", "g"), ("g", "s")]:
3777
return "You win!"
3878
return "Computer wins!"
3979

4080

4181
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)
4298
"""
43-
Main function to run the Snake-Water-Gun game with multiple rounds.
99+
Runs the Snake-Water-Gun game.
44100
"""
45-
print("Welcome to Snake-Water-Gun Game!")
46-
rounds = 0
101+
user_choice = get_user_choice()
102+
computer_choice = get_computer_choice()
47103

104+
<<<<<<< HEAD
48105
while True:
49106
try:
50107
rounds = int(
@@ -93,9 +150,20 @@ def main() -> None:
93150
print("Congratulations! You won the game!")
94151
elif score_user < score_computer:
95152
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)
96163
else:
97-
print("It's a tie!")
164+
print("Computer wins!")
165+
98166

99167

100168
if __name__ == "__main__":
101-
main()
169+
play_game()

0 commit comments

Comments
 (0)