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
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
beatsholds the rules: each move maps to the one move it defeats, sobeats["s"]is"w"because the snake drinks the water.random.choice("swg")picks one letter of the string at random, so the computer moves, and the whole output, change on every run.- A round is a draw when both moves match, a win when
beats[player]is the computer's move, and otherwise a loss. end=" -> "keeps the result on the same line as the two moves.- After five rounds, the scores decide who wins the game.
Another way to write it
Using numbers and the remainder
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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises