forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcricket.c
323 lines (293 loc) · 8.29 KB
/
cricket.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int happens, overs = 1, score1 = 0, score2 = 0, wickets1 = 0, wickets2 = 0,
balls1, balls2, ballType, shotSelection;
int result() {
if (score1 > score2 || score2 > score1) {
return 1;
}
return 0;
}
int generateRandomNum(int n) {
srand(time(NULL));
return rand() % n;
}
int randomNum(int n) {
srand(time(NULL));
return rand() % n;
}
int boundary(int n) {
srand(time(NULL));
return rand() % n;
}
void circle(int r) {
int x, y;
for (int x = -r; x <= r; x++) {
printf("\t\t\t");
for (int y = -r; y <= r; y++) {
if (x * x + y * y - r * r < 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
}
int n, toss, r, option;
char player1Name[10];
char player2Name[10];
char player3Name[10];
char player4Name[10];
char player5Name[10];
int main() {
printf("\n\t\t\t\tWELCOME TO THE WORLD OF C-CRICKET\n\n");
printf("Enter your 'PLAYING-5'\n");
printf("Player-1: ");
scanf("%s", &player1Name);
printf("Player-2: ");
scanf("%s", &player2Name);
printf("Player-3: ");
scanf("%s", &player3Name);
printf("Player-4: ");
scanf("%s", &player4Name);
printf("Player-5: ");
scanf("%s", &player5Name);
printf("\nYour Team \n\n");
printf("%s \n", player1Name);
printf("%s \n", player2Name);
printf("%s \n", player3Name);
printf("%s \n", player4Name);
printf("%s \n", player5Name);
printf("Enter the size of ground :");
scanf("%d", &r);
printf(
" It's time for the 'TOSS'\n Press 0 for 'HEADS' and 1 for 'TAILS'\n");
scanf("%d", &toss);
if (toss == generateRandomNum(2)) {
printf("Press 0 to BAT and any other key to Bowl.\n");
scanf("%d", &option);
if (option == 0) {
printf("You won the toss and opted to BAT first.\n");
circle(r / 5);
printf(" LET'S BEGIN \n");
goto innings2;
} else {
printf("You won the toss and opted to BOWL first.\n");
circle(r / 5);
printf("\t\tLET'S BEGIN \n");
goto innings1;
}
} else {
printf("Computer will BAT first.\n");
circle(r / 5);
printf(" LET'S BEGIN \n");
goto innings1;
}
if (option == 0) {
innings2:
printf("\n%s and %s will open the innings.\n", player1Name, player2Name);
printf("\nPress 0 for 'DEFENCE' 1 for 'DRIVE' 2 for 'LOFTED SHOT' OR "
"any other NUMBER for any different SHOT\n");
nextBall2:
printf("Select your shot.\n");
scanf("%d", &shotSelection);
// you
happens = randomNum(8);
for (int i = 0; i < 5; i++) {
for (int j = 1; j < 7; j++) {
balls2 = balls2 + 1;
if (balls2 >= 50) {
goto inningsOver2;
} else {
goto match2;
}
match2:
switch (happens) {
case 0:
printf(" Good defence , 0 Runs.\n");
score2 = score2 + 0;
goto nextBall2;
break;
case 1:
printf(" well played for a single, 1 Run.\n");
score2 = score2 + 1;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall2;
break;
case 2:
printf(" A double here, 2 Runs.\n");
score2 = score2 + 2;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall2;
break;
case 3:
printf(" Good running between the wickets, 3 Runs.\n");
score2 = score2 + 3;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall2;
break;
case 4:
printf(" It races to the BOUNDARY, 4 Runs.\n");
score2 = score2 + 4;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall2;
break;
case 5:
printf(" It goes for MAXIMUM, a %d Meters SIX.. 6 Runs.\n",
r + boundary(30));
score2 = score2 + 6;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall2;
break;
default:
printf("THAT'S OUT..!!!, Bowler strikes.\n");
wickets2 = wickets2 + 1;
if (wickets2 == 5) {
goto inningsOver2;
} else {
goto nextBall2;
}
break;
}
}
}
inningsOver2:
for (int k = 0; k < 90; k++) {
printf("_");
}
printf("\n\n\t\t\t\tINNINGS OVER..!\n\t\t\t\tTOTAL : %d/%d (%d balls)\n\n",
score2, wickets2, balls2 - 1);
for (int k = 0; k < 90; k++) {
printf("_");
}
if (score1 > 0) {
goto result;
} else {
goto innings1;
}
} else {
innings1:
printf("\n%s will open the attack.\n", player5Name);
printf("\nPress 0 for 'FULL TOSS' Press 1 for 'FULL LENGHT' Press 2 for "
"'SHORT BALL' Press 3 for 'YORKER' OR any other NUMBER for "
"something different\n");
nextBall1:
printf("Select ball type.\n");
scanf("%d", &ballType);
// computer
happens = randomNum(8);
for (int i = 0; i < 5; i++) {
overs = overs + 1;
for (int j = 1; j < 7; j++) {
balls1 = balls1 + 1;
if (balls1 >= 50) {
goto inningsOver1;
} else {
goto match1;
}
match1:
switch (happens) {
case 0:
printf(" Good defence , 0 Runs.\n");
score1 = score1 + 0;
goto nextBall1;
break;
case 1:
printf(" well played for a single, 1 Run.\n");
score1 = score1 + 1;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall1;
break;
case 2:
printf(" A double here, 2 Runs.\n");
score1 = score1 + 2;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall1;
break;
case 3:
printf(" Good running between the wickets, 3 Runs.\n");
score1 = score1 + 3;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall1;
break;
case 4:
printf(" It races to the BOUNDARY, 4 Runs.\n");
score1 = score1 + 4;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall1;
break;
case 5:
printf(" It goes for MAXIMUM, a %d Meters SIX.. 6 Runs.\n",
r + boundary(30));
score1 = score1 + 6;
if (result() && score1 != 0 && score2 != 0 && wickets1 == 5 &&
wickets2 == 5 && balls1 > 49 && balls2 > 49) {
goto result;
}
goto nextBall1;
break;
default:
printf("THAT'S OUT..!!!, Bowler strikes.\n");
wickets1 = wickets1 + 1;
if (wickets1 == 5) {
goto inningsOver1;
} else {
goto nextBall1;
}
break;
}
}
}
inningsOver1:
for (int k = 0; k < 90; k++) {
printf("_");
}
printf("\n\n\t\t\t\tINNINGS OVER..!\n\t\t\t\tTOTAL : %d/%d (%d balls)\n\n",
score1, wickets1, balls1);
for (int k = 0; k < 90; k++) {
printf("_");
}
if (score2 > 0) {
goto result;
} else {
goto innings2;
}
}
result:
if (score1 > score2) {
printf("\n\t\t\tCOMPUTER won the match. WELL TRIED..!\n\n");
} else {
printf("\n\t\t\tYOU won the match. CONGRATULATIONS..!!\n\n");
}
return 0;
}