Skip to content

Commit a30f6b7

Browse files
committed
feat: add snake water gun game
1 parent bd4324f commit a30f6b7

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

other/snake_water_gun.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
A simple implementation of the Snake, Water, Gun game.
3+
"""
4+
import random
5+
import doctest
6+
7+
8+
def snake_water_gun(player_choice: str, computer_choice: str) -> str:
9+
"""
10+
Determines the winner of a Snake, Water, Gun game round.
11+
12+
Args:
13+
player_choice: The player's choice ('s' for snake, 'w' for water, 'g' for gun).
14+
computer_choice: The computer's choice.
15+
16+
Returns:
17+
A string indicating the result: "Player wins!", "Computer wins!", or "It's a draw!".
18+
19+
Doctests:
20+
>>> snake_water_gun('s', 'w')
21+
'Player wins!'
22+
>>> snake_water_gun('w', 'g')
23+
'Player wins!'
24+
>>> snake_water_gun('g', 's')
25+
'Player wins!'
26+
>>> snake_water_gun('w', 's')
27+
'Computer wins!'
28+
>>> snake_water_gun('s', 's')
29+
"It's a draw!"
30+
"""
31+
if player_choice == computer_choice:
32+
return "It's a draw!"
33+
34+
if (
35+
(player_choice == "s" and computer_choice == "w")
36+
or (player_choice == "w" and computer_choice == "g")
37+
or (player_choice == "g" and computer_choice == "s")
38+
):
39+
return "Player wins!"
40+
else:
41+
return "Computer wins!"
42+
43+
44+
def main():
45+
"""
46+
Main function to run the Snake, Water, Gun game.
47+
"""
48+
print("--- Snake, Water, Gun Game ---")
49+
player_input = (
50+
input("Enter your choice (s for snake, w for water, g for gun): ").lower().strip()
51+
)
52+
53+
if player_input not in ["s", "w", "g"]:
54+
print("Invalid choice. Please choose 's', 'w', or 'g'.")
55+
return
56+
57+
choices = ["s", "w", "g"]
58+
computer_input = random.choice(choices)
59+
60+
print(f"\nYou chose: {player_input}")
61+
print(f"Computer chose: {computer_input}\n")
62+
63+
result = snake_water_gun(player_input, computer_input)
64+
print(result)
65+
66+
67+
if __name__ == "__main__":
68+
doctest.testmod() # Run the doctests
69+
main()

0 commit comments

Comments
 (0)