Mastering Snake Water Gun (Rock Paper Scissors) with Python’s ‘random’ module

Rasmi Ranjan Swain
3 min readJul 29, 2023

--

Mastering Snake Water Gun (Rock Paper Scissors) with Python’s ‘random’ module

Introduction

Snake Water Gun, also known as Rock Paper Scissors, is a classic hand game played between two players. The game is a simple yet fun way to make decisions or resolve conflicts. In this blog post, we will explore how to implement the Snake Water Gun game in Python using the ‘random’ module to represent the computer’s choice.

Understanding the Code

import random

def gameWin(comp, you):
# If two values are equal, declare a tie!
if comp == you:
return None

# Check for all possibilities when the computer chose 's'
elif comp == 's':
if you=='w':
return False
elif you=='g':
return True

# Check for all possibilities when the computer chose 'w'
elif comp == 'w':
if you=='g':
return False
elif you=='s':
return True

# Check for all possibilities when the computer chose 'g'
elif comp == 'g':
if you=='s':
return False
elif you=='w':
return True

print("Comp Turn: Snake(s), Water(w), or Gun(g)?")
randNo = random.randint(1, 3)
if randNo == 1:
comp = 's'
elif randNo == 2:
comp = 'w'
elif randNo == 3:
comp = 'g'

you = input("Your Turn: Snake(s), Water(w), or Gun(g)?")
a = gameWin(comp, you)

print(f"Computer chose {comp}")
print(f"You chose {you}")

if a == None:
print("The game is a tie!")
elif a:
print("You Win!")
else:
print("You Lose!")

Explanation

  1. We start by importing the ‘random’ module, which allows us to generate random numbers needed to represent the computer’s choice in the game.
  2. The gameWin(comp, you) function determines the winner of the game. It takes two parameters: 'comp' representing the computer's choice and 'you' representing the player's choice. The function returns True if the player wins, False if the computer wins, and None if it's a tie.
  3. The program prompts the player with “Comp Turn: Snake(s), Water(w), or Gun(g)?” to signify the computer’s turn. The ‘random.randint(1, 3)’ generates a random integer between 1 and 3, inclusive, to represent the computer’s choice. The choices are mapped as follows: 1 for ‘s’ (Snake), 2 for ‘w’ (Water), and 3 for ‘g’ (Gun).
  4. The player’s choice is taken as input with the prompt “Your Turn: Snake(s), Water(w), or Gun(g)?”.
  5. The function ‘gameWin(comp, you)’ is called to determine the result of the game.
  6. The program then prints the choices of the computer and the player.
  7. Depending on the result returned by ‘gameWin()’, the program prints the outcome of the game: “You Win!”, “You Lose!”, or “The game is a tie!”.

Conclusion

In this blog post, we have learned how to implement the Snake Water Gun (Rock Paper Scissors) game in Python using the ‘random’ module to represent the computer’s choice. This simple yet engaging game can be a great addition to your Python projects or simply a fun way to pass the time. Feel free to modify and expand upon the code to make the game even more exciting, such as adding a scoring system or creating a graphical user interface.

Happy coding and have fun playing Snake Water Gun!

Thanks for being a part of our community! Before you go:

  • 👏 Clap for the story and follow the author 👉
  • 🔔 Follow us: LinkedIn

--

--

Rasmi Ranjan Swain
Rasmi Ranjan Swain

Written by Rasmi Ranjan Swain

Python and AI Lover ,Digital Entrepreneur

Responses (2)