-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
109 lines (84 loc) · 1.82 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int random(int n){
int c;
srand(time(NULL));
c= rand()%n;
return c;
}
int check(char a, char b){
if((a=='R') && (b=='S')){
return 1;
}
else if((a=='P') && (b=='R')){
return 1;
}
else if((a=='S') && (b=='P')){
return 1;
}
else if((a=='S') && (b=='R')){
return -1;
}
else if((a=='R') && (b=='P')){
return -1;
}
else if((a=='P') && (b=='S')){
return -1;
}
else if((a=='S') && (b=='S')){
return 0;
}
else if((a=='P') && (b=='P')){
return 0;
}
else if((a=='R') && (b=='R')){
return 0;
}
}
int main()
{
char name[15];
char playerChoice, compChoice;
int c;
int z=0,x=0;
char arr[]={'R','P','S'};
printf("Enter your name \n");
gets(name);
printf("Welcome %s\n",name);
printf("We Will Play Best of 3\n\n");
for (int i = 0; i < 3; i++)
{
/* code */
printf("Enter Your choice \n 1.Rock 2.Paper 3.Scissor\n");
scanf("%d",&playerChoice);
printf("You choice is %c\n\n",arr[playerChoice-1]);
printf("Computer choice is %c\n\n",arr[random(3)]);
c=check(arr[playerChoice-1],arr[random(3)]);
if(c==1){
z++;
printf("You Got it !! \n\n");
}
else if(c==-1){
x++;
printf("Computer Got it !!\n\n");
}
else if(c==0){
printf("Its a draw \n\n");
}
printf("Scores\n\n");
printf("You :- %d \n",z);
printf("Comp :- %d \n\n",x);
}
if(z>x){
printf("Congrasts !! You Won The Game\n");
}
else if(x>z){
printf("You Lost it !! Comp Won The Game\n");
}
else{
printf("Its a Draw \n");
}
return 0;
}