Sunday, November 17, 2024
Google search engine
HomeLanguagesSnake Water Gun game using Python and C

Snake Water Gun game using Python and C

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:

  1. The code starts by importing the random module.
  2. 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.
  3. 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).
  4. The next line calculates user_win and comp_win.
  5. These are two counters that will keep track of how many computers wins and player wins have been achieved so far in the game.
  6. Next, the code loops through all the possible combinations of snake, water, and gun objects.
  7. 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.
  8. Finally, it stores these values in variables named rounds_with_object1(), rounds_with_object2(), and so on.
  9. 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.
  10. The code will create a game of Snake where each round is divided into two parts.
  11. In the first part, the snake will move on the screen and water will be randomly placed below it.
  12. The gun can be used to shoot the snake and if it hits, the snake will die.
  13. In the second part, the player has to guess which direction the snake is going in order to avoid getting hit by the gun.
  14. If they get it right, they win; if not, they lose.
  15. The code begins by checking to see if the user has inputted a valid option.
  16. If not, the user is prompted for an option and given a chance to correct any errors.
  17. If the user has inputted a valid option, the code checks to see if that option is one of the three required options.
  18. If it isn’t, then the code randomly chooses an option from those three and continues processing.
  19. 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.
  20. The code will print out the following: Round : 3 Snake – ‘s’ Water – ‘w’ Gun – ‘g’
  21. The code starts by assigning the value of the computer to a random variable.
  22. This variable will be used to determine the winner of each round.
  23. 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.
  24. 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.
  25. If the computer is playing as g, then user_win and comp_win are both increased.
  26. Finally, if the player chooses to play as s, both user_win and comp_win are increased again.
  27. The code then loops through all of the rounds in the game and prints out a message indicating who won that particular round.
  28. The code also updates variables for each round so that they reflect how many rounds have been played thus far in the game.
  29. The code will check to see if the computer is playing as either ‘s’ or ‘g’.
  30. If so, it will check to see if the player has won the round.
  31. If not, it will announce that a draw has occurred.
  32. The code also keeps track of how many rounds have been played and updates the appropriate message accordingly.

C Code Explanation:

  1. The code starts by creating two variables, you and comp.
  2. Then it randomly selects a number between 0 and 100.
  3. If the number is less than 33, then comp is set to ‘s’.
  4. Otherwise, if the number is greater than or equal to 33 but less than 66, then comp is set to ‘w’.
  5. Otherwise, comp remains unchanged.
  6. Next, the code prints out some instructions for the player.
  7. The player can enter one of three letters: s for snake, w for water, or g for a gun.
  8. 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).
  9. If they entered an incorrect letter, then the program will print out an error message and terminate.
  10. 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.
  11. This function sets up some basic environment variables before calling rand().
  12. rand() generates a random integer between 0 and 99 using a pseudorandom number generator.
  13. This integer will be used later in this program as part of determining who wins when two snakes fight each other underwater!
  14. The code prints the following results: Enter ‘s’ for snake, ‘w’ for water and ‘g’ for gun.
  15. You enter ‘s’ for snake, ‘w’ for water, and ‘g’ for gun.
  16. You win!
RELATED ARTICLES

Most Popular

Recent Comments