-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathproject2_game.c
85 lines (78 loc) · 1.56 KB
/
project2_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
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int game(char y, char c);
int main()
{
char you, comp;
srand(time(0));
int number = rand() % 100 + 1;
if (number <= 33)
{
comp = 'r';
}
else if (number > 33 && number <= 66)
{
comp = 'p';
}
else if (number > 66 && number <= 100)
{
comp = 's';
}
printf("Enter 'r' for rock, 'p' for paper & 's' for scissor: ");
scanf("%c", &you);
if (you != 'r' && you != 'p' && you != 's')
{
printf("No such choice, Please enter correct choice\n");
}
int g = game(you, comp);
printf("You chose '%c' , Computer chose '%c'\n", you, comp);
if (g == 0)
{
printf("DRAW!!\n");
}
else if (g == 1)
{
printf("YOU WON!!\n");
}
else if (g == -1)
{
printf("YOU LOSE!!\n");
}
return 0;
}
int game(char y, char c)
{
if (y == c) //same choices
{
return 0;
}
//possibile choices
// r p
// p s // ofcourse vice versa of these choices as well
// s r
if (y == 'p' && c == 'r') //returns 1 when user wins else returns -1
{
return 1;
}
else if (y == 'r' && c == 'p')
{
return -1;
}
if (y == 'r' && c == 's')
{
return 1;
}
else if (y == 's' && c == 'r')
{
return -1;
}
if (y == 's' && c == 'p')
{
return 1;
}
else if (y == 'p' && c == 's')
{
return -1;
}
}