-
Notifications
You must be signed in to change notification settings - Fork 0
/
swg_game.c
76 lines (69 loc) · 1.64 KB
/
swg_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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int SnakeWaterGun(char you , char comp){
/*
This function returns 1 if you wins , returns -1 if you lose and returns 0 if match draws.
(returns 404 if there is any invalid input from user)
*/
//Draw conditions
if(you == comp){
return 0;
}
//Non-draw conditions
else if(you == 's' && comp == 'w'){
return 1;
}
else if(you == 's' && comp == 'g'){
return -1;
}
else if(you == 'w' && comp == 'g'){
return 1;
}
else if(you == 'w' && comp == 's'){
return -1;
}
else if(you == 'g' && comp == 's'){
return 1;
}
else if(you == 'g' && comp == 'w'){
return -1;
}
//Error condition
else{
return 404;
}
}
int main()
{
char you, comp;
//Randon number generator (Between 1 to 100)
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);
printf("You chose '%c' and computer chose '%c':", you,comp);
if(result == 0){
printf("Game Draw.\n");
}
else if(result == 1){
printf("Congratulations! You win.\n");
}
else if(result == -1){
printf("You lose \nBetter luck next time.\n");
}
else{
printf("Please Enter a valid input.\n");
}
return 0;
}