Snake Water Gun game using Python and C
Last Updated :
06 Dec, 2022
Snake Water Gun is one of the famous two-player game played by many people. It is a hand game in which the player randomly chooses any of the three forms i.e. snake, water, and gun. Here, we are going to implement this game using python.
This python project is to build a game for a single player that plays with the computer
Following are the rules of the game:
Snake vs. Water: Snake drinks the water hence wins.
Water vs. Gun: The gun will drown in water, hence a point for water
Gun vs. Snake: Gun will kill the snake and win.
In situations where both players choose the same object, the result will be a draw.
We will use random.choice() method and nested if-else statements to select a random item from a list.
Below is the implementation:
Python3
# Import random module
import random
print('Snake - Water - Gun')
# Input no. of rounds
n = int(input('Enter number of rounds: '))
# List containing Snake(s), Water(w), Gun(g)
options = ['s', 'w', 'g']
# Round numbers
rounds = 1
# Count of computer wins
comp_win = 0
# Count of player wins
user_win = 0
# There will be n rounds of game
while rounds <= n:
# Display round
print(f"Round :{rounds}\nSnake - 's'\nWater - 'w'\nGun - 'g'")
# Exception handling
try:
player = input("Choose your option: ")
except EOFError as e:
print(e)
# Control of bad inputs
if player != 's' and player != 'w' and player != 'g':
print("Invalid input, try again\n")
continue
# random.choice() will randomly choose
# item from list- options
computer = random.choice(options)
# Conditions based on the game rule
if computer == 's':
if player == 'w':
comp_win += 1
elif player == 'g':
user_win += 1
elif computer == 'w':
if player == 'g':
comp_win += 1
elif player == 's':
user_win += 1
elif computer == 'g':
if player == 's':
comp_win += 1
elif player == 'w':
user_win += 1
# Announce winner of every round
if user_win > comp_win:
print(f"You Won round {rounds}\n")
elif comp_win > user_win:
print(f"Computer Won round {rounds}\n")
else:
print("Draw!!\n")
rounds += 1
# Final winner based on the number of wons
if user_win > comp_win:
print("Congratulations!! You Won")
elif comp_win > user_win:
print("You lose!!")
else:
print("Match Draw!!")
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int snakeWaterGun(char you, char comp)
{
// returns 1 if you win, -1 if you lose and 0 if draw
// Condition for draw // Cases
// covered: | snake snake |
// gun gun | water water
if (you == comp) {
return 0;
}
// Non-draw conditions
// Cases covered:// snake gun
// | gun snake | snake water
// | water sanke | gun water | water gun
if (you == 's' && comp == 'g')
{
return -1;
}
else if (you == 'g' && comp == 's')
{
return 1;
}
if (you == 's' && comp == 'w')
{
return 1;
}
else if (you == 'w' && comp == 's')
{
return -1;
}
if (you == 'g' && comp == 'w')
{
return -1;
}
else if (you == 'w' && comp == 'g')
{
return 1;
}
}
// Driver Code
int main()
{
char you, comp;
srand(time(0));
int number = rand() % 100 + 1;
if (number < 33)
{
comp = 's';
}
else if (number > 33 && number < 66)
{
comp = 'w';
}
else
{
comp = 'g';
}
printf("Enter 's' for snake, 'w' for "
"water and 'g' for gun\n");
scanf("%c", &you);
int result = snakeWaterGun(you, comp);
if (result == 0) {
printf("Game draw!\n");
}
else if (result == 1)
{
printf("You win!\n");
}
else
{
printf("You Lose!\n");
}
printf("You choose %c and computer choose %c. ", you,
comp);
return 0;
}
// this code is provided by harsh sinha username-
// harshsinha03
Output:

Python Code Explanation:
- The code starts by importing the random module.
- This module provides a random number generator, which is used in this program to generate different types of objects (snake, water, gun) on each round of the game.
- Next, the code sets up some variables: n (the number of rounds in the game), rounds (the number of rounds played so far), and options (a list containing snake, water, and gun objects).
- The next line calculates user_win and comp_win.
- These are two counters that will keep track of how many computers wins and player wins have been achieved so far in the game.
- Next, the code loops through all the possible combinations of snake, water, and gun objects.
- For each combination, it calculates how many computers wins or player wins would be achieved if that particular object was chosen as part of the strategy for that round.
- Finally, it stores these values in variables named rounds_with_object1(), rounds_with_object2(), and so on.
- This way, when a particular object is chosen during a round of play (by either player), the program can easily determine how many more rounds need to be played before that object can be used again.
- The code will create a game of Snake where each round is divided into two parts.
- In the first part, the snake will move on the screen and water will be randomly placed below it.
- The gun can be used to shoot the snake and if it hits, the snake will die.
- In the second part, the player has to guess which direction the snake is going in order to avoid getting hit by the gun.
- If they get it right, they win; if not, they lose.
- The code begins by checking to see if the user has inputted a valid option.
- If not, the user is prompted for an option and given a chance to correct any errors.
- If the user has inputted a valid option, the code checks to see if that option is one of the three required options.
- If it isn't, then the code randomly chooses an option from those three and continues processing.
- If the user has inputted one of the required options, then the code uses random.choice() to choose an action from among those three: round, snake, or water.
- The code will print out the following: Round : 3 Snake - 's' Water - 'w' Gun - 'g'
- The code starts by assigning the value of the computer to a random variable.
- This variable will be used to determine the winner of each round.
- Next, the code checks to see if the player is playing as either w or g. If so, then different variables are updated based on which player is chosen.
- If the player is not playing as either w or g, then another check is made to see if a computer is playing as w. If it is, then user_win is incremented and comp_win is decreased.
- If the computer is playing as g, then user_win and comp_win are both increased.
- Finally, if the player chooses to play as s, both user_win and comp_win are increased again.
- The code then loops through all of the rounds in the game and prints out a message indicating who won that particular round.
- The code also updates variables for each round so that they reflect how many rounds have been played thus far in the game.
- The code will check to see if the computer is playing as either 's' or 'g'.
- If so, it will check to see if the player has won the round.
- If not, it will announce that a draw has occurred.
- The code also keeps track of how many rounds have been played and updates the appropriate message accordingly.
C Code Explanation:
- The code starts by creating two variables, you and comp.
- Then it randomly selects a number between 0 and 100.
- If the number is less than 33, then comp is set to 's'.
- Otherwise, if the number is greater than or equal to 33 but less than 66, then comp is set to 'w'.
- Otherwise, comp remains unchanged.
- Next, the code prints out some instructions for the player.
- The player can enter one of three letters: s for snake, w for water, or g for a gun.
- Next, the code checks to see if the player entered an appropriate letter by comparing their input with what's expected (in this case 's' equals snake and 'g' equals gun).
- If they entered an incorrect letter, then the program will print out an error message and terminate.
- Once everything looks correct and there are no errors detected in the code so far (which would indicate that everything is working as intended), the main() function gets called.
- This function sets up some basic environment variables before calling rand().
- rand() generates a random integer between 0 and 99 using a pseudorandom number generator.
- This integer will be used later in this program as part of determining who wins when two snakes fight each other underwater!
- The code prints the following results: Enter 's' for snake, 'w' for water and 'g' for gun.
- You enter 's' for snake, 'w' for water, and 'g' for gun.
- You win!
Similar Reads
Snake Game in Python - Using Pygame module Snake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself. Creating a snake game can be taken as a challenge while learning Python or Pygame. It is one of the best beginne
15+ min read
Snake Game Using Tkinter - Python For many years, players all across the world have cherished the iconic video game Snake. The player controls a snake that crawls around the screen while attempting to consume food, avoiding obstacles or running into its own tail. It is a straightforward but addictive game. The snake lengthens each t
12 min read
Using C codes in Python | Set 1 Prerequisite: How to Call a C function in Python Let's discuss the problem of accessing C code from Python. As it is very evident that many of Pythonâs built-in libraries are written in C. So, to access C is a very important part of making Python talk to existing libraries. There is an extensive C p
4 min read
War Card Game in Python The War card game is a classic, simple game that can be a fun way to practice programming skills. The game involves a standard 52-card deck split between two players. Each player reveals the top card of their deck, and the player with the higher card takes both cards and adds them to the bottom of t
5 min read
Tic Tac Toe game with GUI using tkinter in Python Tic-tac-toe (American English), noughts and crosses (British English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3Ã3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.
15+ min read