-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_water_gun_game.c
80 lines (75 loc) · 2.04 KB
/
snake_water_gun_game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//C program should be able to print the result after you choose Snake/Water or Gun.
//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 C Language.
//This C project is to build a game for a single player that plays with the computer
//Rules Water: Snake drinks the water hence wins. 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.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int snakeWaterGun(char you, char computer)
{
if (you == computer)
{
return 0;
}
if (you == 's' && computer == 'g')
{
return -1;
}
else if (you == 'g' && computer == 's')
{
return 1;
}
if (you == 's' && computer == 'w')
{
return 1;
}
else if (you == 'w' && computer == 's')
{
return -1;
}
if (you == 'g' && computer == 'w')
{
return -1;
}
else if (you == 'w' && computer == 'g')
{
return 1;
}
}
int main()
{
//This program generates a random number
char you, computer;
srand(time(0));
int number = rand() % 100 + 1;
//_____________________________________________//
if (number < 33)
{
computer = 's';
}
else if (number > 33 && number < 66)
{
computer = 'w';
}
else
{
computer = 'g';
}
printf("Enter 's' for snake, 'w' for water and 'g' for gun\n");
scanf("%c", &you);
int result = snakeWaterGun(you, computer);
if (result == 0)
{
printf("Game draw!\n");
}
else if (result == 1)
{
printf("You win Congratulation!\n");
}
else
{
printf("You Lose Better Luck Next Time!\n");
}
printf("You chose %c and computer chose %c. ", you, computer);
return 0;
}