Editor

Python Program for Snake, Water, Gun

Beginner · Games and random numbers

Snake, water, gun is the Indian version of rock, paper, scissors: the snake drinks the water, the gun sinks in the water, and the gun shoots the snake. If both players choose the same thing, the round is a draw. In this program the computer picks its moves at random and plays five rounds against a list of player moves, keeping score.

Program

snake_water_gun.py
import random
names = {"s": "Snake", "w": "Water", "g": "Gun"}
beats = {"s": "w", "w": "g", "g": "s"} # each move -> the move it beats
# The player's moves. To play for real, ask each round:
# player = input("Snake (s), Water (w) or Gun (g)? ").lower()
player_moves = ["s", "g", "w", "w", "g"]
player_score = 0
computer_score = 0
for round_no, player in enumerate(player_moves, start=1):
computer = random.choice("swg")
print(f"Round {round_no}: you {names[player]}, computer {names[computer]}", end=" -> ")
if player == computer:
print("Draw")
elif beats[player] == computer:
print("You win the round")
player_score += 1
else:
print("Computer wins the round")
computer_score += 1
print(f"Final score: you {player_score}, computer {computer_score}")
if player_score > computer_score:
print("You win the game!")
elif computer_score > player_score:
print("The computer wins the game!")
else:
print("The game is a tie!")
Output
Round 1: you Snake, computer Snake -> Draw
Round 2: you Gun, computer Gun -> Draw
Round 3: you Water, computer Snake -> Computer wins the round
Round 4: you Water, computer Snake -> Computer wins the round
Round 5: you Gun, computer Water -> Computer wins the round
Final score: you 0, computer 3
The computer wins the game!

How it works

  1. beats holds the rules: each move maps to the one move it defeats, so beats["s"] is "w" because the snake drinks the water.
  2. random.choice("swg") picks one letter of the string at random, so the computer moves, and the whole output, change on every run.
  3. A round is a draw when both moves match, a win when beats[player] is the computer's move, and otherwise a loss.
  4. end=" -> " keeps the result on the same line as the two moves.
  5. After five rounds, the scores decide who wins the game.

Another way to write it

Using numbers and the remainder

Using numbers and the remainder
import random
moves = ["Snake", "Water", "Gun"] # each move beats the one after it, wrapping round
for player in [0, 1, 2]:
computer = random.randint(0, 2)
diff = (computer - player) % 3
if diff == 0:
result = "Draw"
elif diff == 1:
result = "You win"
else:
result = "Computer wins"
print(moves[player], "vs", moves[computer], "->", result)
Output
Snake vs Gun -> Computer wins
Water vs Gun -> You win
Gun vs Gun -> Draw

Numbering the moves so that each one beats the next lets (computer - player) % 3 decide the round: 0 is a draw, 1 means you win and 2 means the computer wins.

Try it yourself

Stop the game as soon as either side has won 3 rounds.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises