|
| 1 | +""" |
| 2 | +Snake Water Gun Game |
| 3 | +
|
| 4 | +This is a simple implementation of the Snake Water Gun game (similar to Rock Paper Scissors). |
| 5 | +The rules are: |
| 6 | +- Snake drinks Water (Snake wins) |
| 7 | +- Water rusts Gun (Water wins) |
| 8 | +- Gun shoots Snake (Gun wins) |
| 9 | +
|
| 10 | +Author: TheAlgorithms/Python |
| 11 | +""" |
| 12 | + |
| 13 | +import random |
| 14 | + |
| 15 | + |
| 16 | +def get_computer_choice() -> str: |
| 17 | + """ |
| 18 | + Generate a random choice for the computer. |
| 19 | + |
| 20 | + Returns: |
| 21 | + str: Computer's choice ('s' for snake, 'w' for water, 'g' for gun) |
| 22 | + |
| 23 | + Examples: |
| 24 | + >>> choice = get_computer_choice() |
| 25 | + >>> choice in ['s', 'w', 'g'] |
| 26 | + True |
| 27 | + """ |
| 28 | + choices = ['s', 'w', 'g'] |
| 29 | + return random.choice(choices) |
| 30 | + |
| 31 | + |
| 32 | +def get_choice_name(choice: str) -> str: |
| 33 | + """ |
| 34 | + Convert choice letter to full name. |
| 35 | + |
| 36 | + Args: |
| 37 | + choice (str): Choice letter ('s', 'w', or 'g') |
| 38 | + |
| 39 | + Returns: |
| 40 | + str: Full name of the choice |
| 41 | + |
| 42 | + Examples: |
| 43 | + >>> get_choice_name('s') |
| 44 | + 'snake' |
| 45 | + >>> get_choice_name('w') |
| 46 | + 'water' |
| 47 | + >>> get_choice_name('g') |
| 48 | + 'gun' |
| 49 | + >>> get_choice_name('x') |
| 50 | + 'unknown' |
| 51 | + """ |
| 52 | + choice_names = { |
| 53 | + 's': 'snake', |
| 54 | + 'w': 'water', |
| 55 | + 'g': 'gun' |
| 56 | + } |
| 57 | + return choice_names.get(choice, 'unknown') |
| 58 | + |
| 59 | + |
| 60 | +def determine_winner(player_choice: str, computer_choice: str) -> str: |
| 61 | + """ |
| 62 | + Determine the winner based on game rules. |
| 63 | + Rules: Snake drinks Water, Water rusts Gun, Gun shoots Snake |
| 64 | + |
| 65 | + Args: |
| 66 | + player_choice (str): Player's choice ('s', 'w', or 'g') |
| 67 | + computer_choice (str): Computer's choice ('s', 'w', or 'g') |
| 68 | + |
| 69 | + Returns: |
| 70 | + str: Result of the game ('win', 'lose', or 'draw') |
| 71 | + |
| 72 | + Examples: |
| 73 | + >>> determine_winner('s', 's') |
| 74 | + 'draw' |
| 75 | + >>> determine_winner('s', 'w') # Snake drinks Water |
| 76 | + 'win' |
| 77 | + >>> determine_winner('w', 'g') # Water rusts Gun |
| 78 | + 'win' |
| 79 | + >>> determine_winner('g', 's') # Gun shoots Snake |
| 80 | + 'win' |
| 81 | + >>> determine_winner('w', 's') # Snake drinks Water |
| 82 | + 'lose' |
| 83 | + >>> determine_winner('g', 'w') # Water rusts Gun |
| 84 | + 'lose' |
| 85 | + >>> determine_winner('s', 'g') # Gun shoots Snake |
| 86 | + 'lose' |
| 87 | + """ |
| 88 | + if player_choice == computer_choice: |
| 89 | + return 'draw' |
| 90 | + |
| 91 | + # Define winning conditions for player |
| 92 | + winning_conditions = { |
| 93 | + 's': 'w', # Snake drinks Water |
| 94 | + 'w': 'g', # Water rusts Gun |
| 95 | + 'g': 's' # Gun shoots Snake |
| 96 | + } |
| 97 | + |
| 98 | + if winning_conditions[player_choice] == computer_choice: |
| 99 | + return 'win' |
| 100 | + else: |
| 101 | + return 'lose' |
| 102 | + |
| 103 | + |
| 104 | +def play_single_game() -> None: |
| 105 | + """ |
| 106 | + Play a single round of Snake Water Gun game. |
| 107 | + """ |
| 108 | + print("\n=== Snake Water Gun Game ===") |
| 109 | + print("Rules:") |
| 110 | + print("- Snake drinks Water (Snake wins)") |
| 111 | + print("- Water rusts Gun (Water wins)") |
| 112 | + print("- Gun shoots Snake (Gun wins)") |
| 113 | + print() |
| 114 | + |
| 115 | + # Get player input |
| 116 | + while True: |
| 117 | + player_choice = input("Enter your choice: s for snake, w for water, g for gun: ").lower().strip() |
| 118 | + if player_choice in ['s', 'w', 'g']: |
| 119 | + break |
| 120 | + print("Invalid choice! Please enter 's', 'w', or 'g'.") |
| 121 | + |
| 122 | + # Get computer choice |
| 123 | + computer_choice = get_computer_choice() |
| 124 | + |
| 125 | + # Display choices |
| 126 | + print(f"You chose {get_choice_name(player_choice)}") |
| 127 | + print(f"Computer chose {get_choice_name(computer_choice)}") |
| 128 | + |
| 129 | + # Determine and display result |
| 130 | + result = determine_winner(player_choice, computer_choice) |
| 131 | + |
| 132 | + if result == 'draw': |
| 133 | + print("It's a draw!") |
| 134 | + elif result == 'win': |
| 135 | + print("You win!") |
| 136 | + else: |
| 137 | + print("You lose!") |
| 138 | + |
| 139 | + |
| 140 | +def play_multiple_games() -> None: |
| 141 | + """ |
| 142 | + Play multiple rounds and keep score. |
| 143 | + """ |
| 144 | + player_score = 0 |
| 145 | + computer_score = 0 |
| 146 | + draws = 0 |
| 147 | + |
| 148 | + print("\n=== Snake Water Gun - Multiple Rounds ===") |
| 149 | + |
| 150 | + while True: |
| 151 | + try: |
| 152 | + rounds = int(input("How many rounds would you like to play? ")) |
| 153 | + if rounds > 0: |
| 154 | + break |
| 155 | + print("Please enter a positive number.") |
| 156 | + except ValueError: |
| 157 | + print("Please enter a valid number.") |
| 158 | + |
| 159 | + for round_num in range(1, rounds + 1): |
| 160 | + print(f"\n--- Round {round_num} ---") |
| 161 | + |
| 162 | + # Get player input |
| 163 | + while True: |
| 164 | + player_choice = input("Enter your choice: s for snake, w for water, g for gun: ").lower().strip() |
| 165 | + if player_choice in ['s', 'w', 'g']: |
| 166 | + break |
| 167 | + print("Invalid choice! Please enter 's', 'w', or 'g'.") |
| 168 | + |
| 169 | + # Get computer choice |
| 170 | + computer_choice = get_computer_choice() |
| 171 | + |
| 172 | + # Display choices |
| 173 | + print(f"You chose {get_choice_name(player_choice)}") |
| 174 | + print(f"Computer chose {get_choice_name(computer_choice)}") |
| 175 | + |
| 176 | + # Determine result |
| 177 | + result = determine_winner(player_choice, computer_choice) |
| 178 | + |
| 179 | + if result == 'draw': |
| 180 | + print("It's a draw!") |
| 181 | + draws += 1 |
| 182 | + elif result == 'win': |
| 183 | + print("You win this round!") |
| 184 | + player_score += 1 |
| 185 | + else: |
| 186 | + print("Computer wins this round!") |
| 187 | + computer_score += 1 |
| 188 | + |
| 189 | + # Display current score |
| 190 | + print(f"Score - You: {player_score}, Computer: {computer_score}, Draws: {draws}") |
| 191 | + |
| 192 | + # Final results |
| 193 | + print(f"\n=== Final Results ===") |
| 194 | + print(f"Your score: {player_score}") |
| 195 | + print(f"Computer score: {computer_score}") |
| 196 | + print(f"Draws: {draws}") |
| 197 | + |
| 198 | + if player_score > computer_score: |
| 199 | + print("🎉 Congratulations! You won overall!") |
| 200 | + elif computer_score > player_score: |
| 201 | + print("💻 Computer wins overall! Better luck next time!") |
| 202 | + else: |
| 203 | + print("🤝 It's a tie overall! Great game!") |
| 204 | + |
| 205 | + |
| 206 | +def main() -> None: |
| 207 | + """ |
| 208 | + Main function to run the Snake Water Gun game. |
| 209 | + """ |
| 210 | + print("Welcome to Snake Water Gun Game!") |
| 211 | + |
| 212 | + while True: |
| 213 | + print("\nChoose game mode:") |
| 214 | + print("1. Single game") |
| 215 | + print("2. Multiple rounds") |
| 216 | + print("3. Exit") |
| 217 | + |
| 218 | + choice = input("Enter your choice (1/2/3): ").strip() |
| 219 | + |
| 220 | + if choice == '1': |
| 221 | + play_single_game() |
| 222 | + elif choice == '2': |
| 223 | + play_multiple_games() |
| 224 | + elif choice == '3': |
| 225 | + print("Thanks for playing! Goodbye!") |
| 226 | + break |
| 227 | + else: |
| 228 | + print("Invalid choice! Please enter 1, 2, or 3.") |
| 229 | + |
| 230 | + # Ask if player wants to continue |
| 231 | + if choice in ['1', '2']: |
| 232 | + play_again = input("\nWould you like to play again? (y/n): ").lower().strip() |
| 233 | + if play_again != 'y': |
| 234 | + print("Thanks for playing! Goodbye!") |
| 235 | + break |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == "__main__": |
| 239 | + main() |
0 commit comments