-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
1019 lines (971 loc) · 28.3 KB
/
project.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int wickets, overs, currover = 0, total_balls = 0;
int target;
struct player
{
int playerno, ind_runs, ind_wickets;
float strike_rate;
char player_type[15], player_name[20];
};
typedef struct player player;
struct score
{
int team_score;
int total_overs;
int total_wickets;
} team, comp;
typedef struct score score; //Both team(for user) and comp(for computer) are the instances of the structure score
player *createPlayer()
{
player *p = (player *)malloc(sizeof(struct player));
p->ind_runs = p->ind_wickets = 0;
return p;
}
struct node
{
player *data; //data part
struct node *next; //Pointer to the next node
};
struct queue
{
struct node *front;
struct node *rear;
};
typedef struct node node;
typedef struct queue queue;
queue *batting_order;
node *createNode(player *p)
{
node *newNode = (node *)malloc(sizeof(struct node));
newNode->data = p;
newNode->next = NULL;
return newNode;
}
void createQueue()
{
batting_order = (queue *)malloc(sizeof(struct queue));
batting_order->rear = NULL;
batting_order->front = NULL;
}
void lets_start();
void lets_start2();
void compbatting();
void userbattingChase();
void superover();
int allOut() //isEmpty() func
{
return batting_order->front == NULL;
}
void enqueueBatsman(player *p)
{
node *newNode = createNode(p);
if (allOut())
batting_order->front = batting_order->rear = newNode;
else
{
batting_order->rear->next = newNode;
batting_order->rear = newNode;
}
}
void dequeueBatsman(int total_balls)
{
node *temp = batting_order->front;
batting_order->front = batting_order->front->next;
free(temp);
}
void makeEmpty()
{
while (!allOut())
dequeueBatsman(total_balls);
target = team.team_score + 1;
team.team_score = 0;
comp.team_score = 0;
comp.total_overs = 0;
team.total_overs = 0;
comp.total_wickets = 0;
team.total_wickets = 0;
currover = 0;
total_balls = 0;
batting_order->rear = NULL;
batting_order->front = NULL;
}
void makeEmpty_2()
{
while (!allOut())
dequeueBatsman(total_balls);
target = comp.team_score + 1;
team.team_score = 0;
comp.team_score = 0;
comp.total_overs = 0;
team.total_overs = 0;
comp.total_wickets = 0;
team.total_wickets = 0;
currover = 0;
total_balls = 0;
batting_order->rear = NULL;
batting_order->front = NULL;
}
void currentBatsman() //peek() func
{
printf("\033[1;32m");
printf("\nThe batsman on strike : %s\n\n", batting_order->front->data->player_name);
printf("\033[0m");
}
void battingLineUp() //display() func of queue
{
printf("\033[1;36m");
if (allOut())
{
printf("\nALL OUT !!\n");
return;
}
node *curr = batting_order->front;
printf("\nNext to bat :\n");
printf("----------------------------\n");
printf("PLAYER ID\tBATSMAN NAME\n");
printf("----------------------------\n");
while (curr != batting_order->rear)
{
printf("%d\t\t%s\t\n", curr->data->playerno, curr->data->player_name);
curr = curr->next;
}
printf("%d\t\t%s\t\n", curr->data->playerno, curr->data->player_name);
printf("----------------------------\n");
printf("\033[0m");
}
void acceptPlayerDetails(player *p, int num)
{
printf("\033[1;34m");
printf("\nEnter the details of PLAYER %d\n", num);
printf("----------------------------------\n");
printf("\033[1;32m");
printf("Enter the player's name : ");
scanf("%s", p->player_name);
printf("Enter the player's role (batsman/bowler/all-rounder) : ");
scanf("%s", p->player_type);
p->playerno = num;
printf("\n");
printf("\033[0m");
}
void computerPlayerDetails(player *p, int num)
{
char s1[8] = "Player";
char c = 48 + num;
strncat(s1, &c, 1);
strcpy(p->player_name, s1);
p->playerno = num;
}
int print_rules()
{
printf("\033[1;32m");
printf("\n--------------------------------------------------------------Rules Of The Game-------------------------------------------------------------");
printf("\033[1;31m");
printf("\n\n\n1.The User has to spin a coin during the toss,based on the outcome of the toss, the user will bat or field first!");
printf("\n\n3.The User can choose the total number of overs and wickets for the respective match after the toss.");
printf("\n\n4.The User can enter scores only between 0 to 6.");
printf("\n\n5.If the User and computer land up on the same number while batting, the batsman is declared out.");
printf("\n\n6.The team is considered ALL OUT after losing all the wickets during batting.");
printf("\n\n7.If the batsman inputs a 0 and the computer generates any number other than 0,those runs will be added to the batsman's score.");
printf("\n\n8.In case of a 0 by both the batsman and the computer, then the batsman is declared out.");
printf("\n\n9.A batsman is considered to be out when he inputs the same number as that of the number generated by the opponent.");
printf("\n\n10.In case of a tie in the scores, the user will have to play a Super Over against the computer.");
printf("\033[1;32m");
printf("\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------");
printf("\033[0;m");
printf("\n\n");
}
void start()
{
printf("\033[1;33m");
printf("\nEnter the No of wickets(1-5) : ");
scanf("%d", &wickets);
while (wickets > 5 || wickets <= 0)
{
printf("\033[1;31m");
printf("Please enter the No of wickets in the given range (1-5) : ");
scanf("%d", &wickets);
printf("\033[0m");
}
printf("\033[1;33m");
printf("Enter the No of Overs(1-6) : ");
scanf("%d", &overs);
while (overs > 6 || overs <= 0)
{
printf("\033[1;31m");
printf("Please enter the No of overs in the given range (1-6) : ");
scanf("%d", &overs);
printf("\033[0m");
}
printf("\033[0m");
}
int printRandoms(int lower, int upper)
{
srand(time(0));
int num = (rand() % (upper - lower + 1)) + lower;
return num;
}
int choose()
{
int a;
printf("\033[1;32m");
printf("Enter 0 to bat and 1 to bowl: ");
scanf("%d", &a);
if (a == 0)
{
printf("\nYou have decided to bat first\n");
return 1;
}
else
{
printf("\nYou have decided to bowl first\n");
return 0;
}
printf("\033[0m");
}
int syschoose()
{
printf("\033[1;31m");
int toss = 0;
toss = rand() % 2;
if (toss == 0)
{
printf("\nSystem has decided to bat first\n");
return 1;
}
else
{
printf("\nSystem has decided to bowl first\n");
return 0;
}
printf("\033[0m");
}
void toss()
{
printf("\033[1;32m");
int toss = 0, dec, s = 0, x, y;
int call = 0;
srand(time(NULL));
toss = (rand() % (7));
printf("\nToss Time!!!\n");
printf("\nEnter 0 for even and 1 for odd: ");
scanf("%d", &dec);
while (dec > 1 || dec < 0)
{
printf("\033[1;31m");
printf("Please enter either 0 for even or 1 for odd: ");
scanf("%d", &dec);
printf("\033[0m");
}
if (dec == 0 || dec == 1)
{
printf("Enter a number within 0 and 6: ");
scanf("%d", &call);
while (call < 0 || call > 6)
{
printf("\033[1;31m");
printf("Please enter a number within 0 and 6: ");
scanf("%d", &call);
printf("\033[0m");
}
printf("\033[1;32m");
if (call >= 0 && call <= 6)
{
s = toss + call;
if (s % 2 == dec)
{
if (dec == 1)
{
printf("Sum is %d and It's odd\n", s);
}
else
{
printf("Sum is %d and It's even\n", s);
}
printf("\nYou have won the toss\n");
x = choose();
if (x == 1)
{
lets_start();
}
else
{
printf("\nYou have to bowl\n");
lets_start2();
}
}
else
{
printf("\033[1;31m");
if (dec == 1)
{
printf("Sum is %d and It's even\n", s);
}
else
{
printf("Sum is %d and It's odd\n", s);
}
printf("System has won the toss\n");
y = syschoose();
if (y == 1)
{
printf("You have to bowl\n");
lets_start2();
}
else
{
lets_start();
}
}
}
}
printf("\033[0m");
}
void displayPlayerScore(int wickets, player *p[wickets])
{
printf("\033[1;36m");
printf("\n-------INDIVIDUAL SCORES IN THE MATCH-------\n");
printf("\n------------------------------------------------\n");
printf("PLAYER NO.\tPLAYER NAME\tRUNS SCORED");
printf("\n------------------------------------------------\n");
for (int i = 0; i < wickets; i++)
{
printf("%d\t\t%-20s%d\n", p[i]->playerno, p[i]->player_name, p[i]->ind_runs);
}
printf("------------------------------------------------\n\n");
printf("\033[0m");
}
void total_balls_by_each_player()
{
total_balls += 1;
}
void addBallsHuman() //Existing
{
currover += 1;
if (currover % 6 == 0)
{
currover = 0;
team.total_overs += 1;
}
}
void addBallsComputer()
{
currover += 1;
if (currover % 6 == 0)
{
currover = 0;
comp.total_overs += 1;
}
}
void addScoreHuman(int n)
{
team.team_score += n;
batting_order->front->data->ind_runs += n;
}
void addScoreComputer(int n)
{
comp.team_score += n;
batting_order->front->data->ind_runs += n;
}
void addWicketHuman()
{
team.total_wickets += 1;
}
void addWicketComputer()
{
comp.total_wickets += 1;
}
void batting() //users first batting innings(sets the target)
{
printf("\033[1;34m");
int n;
printf("%.1fth ball\n\n", team.total_overs + (currover / 10.0 + 0.1));
printf("Enter a number (0-6) : ");
scanf("%d", &n);
while (n >= 7 || n < 0)
{
printf("\033[1;31m");
printf("Please enter a number in the range 0-6 : ");
scanf("%d", &n);
printf("\033[0m");
}
int computer_random = printRandoms(0, 6);
printf("\033[1;32m");
printf("Computer generated number : %d\n\n", computer_random);
printf("\033[1;34m");
if (n == computer_random)
{
total_balls_by_each_player();
node *temp = batting_order->front;
printf("\033[1;31m");
printf("\n%s is OUT!!\n", temp->data->player_name);
printf("Runs scored : %d\n", temp->data->ind_runs);
temp->data->strike_rate = ((float)temp->data->ind_runs / total_balls) * 100;
printf("Strike rate is %.2f\n\n", temp->data->strike_rate);
printf("\033[1;34m");
dequeueBatsman(total_balls);
total_balls = 0;
addBallsHuman();
addWicketHuman();
if (!allOut())
{
battingLineUp();
currentBatsman();
}
}
else if (n == 0)
{
addScoreHuman(computer_random);
addBallsHuman();
total_balls_by_each_player();
printf("EXTRAS!! %d added to the Score! Score now is: %d/%d\n\n", computer_random, team.team_score, team.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", team.total_overs);
}
else
{
addScoreHuman(n);
addBallsHuman();
total_balls_by_each_player();
printf("%d added to the Score! Score now is: %d/%d\n\n", n, team.team_score, team.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", team.total_overs);
}
printf("\033[0m");
}
void compbattingChase()
{
printf("\033[1;35m");
int n;
printf("\n----Innings 2----\n");
printf("%.1fth ball\n\n", comp.total_overs + (currover / 10.0 + 0.1));
printf("Enter a number (0-6) : ");
scanf("%d", &n);
while (n >= 7 || n < 0)
{
printf("\033[1;31m");
printf("Please enter a number in the range 0-6 : ");
scanf("%d", &n);
printf("\033[1;35m");
}
int computer_random = printRandoms(0, 6);
printf("\033[1;32m");
printf("Computer generated number : %d\n\n", computer_random);
printf("\033[1;35m");
if (n == computer_random)
{
total_balls_by_each_player();
node *temp = batting_order->front;
printf("\033[1;31m");
printf("\n%s is OUT!!\n", temp->data->player_name);
printf("Runs scored : %d\n", temp->data->ind_runs);
temp->data->strike_rate = ((float)temp->data->ind_runs / total_balls) * 100;
printf("Strike rate is %.2f\n\n", temp->data->strike_rate);
printf("\033[1;35m");
dequeueBatsman(total_balls);
total_balls = 0;
addBallsComputer();
addWicketComputer();
if (!allOut())
{
battingLineUp();
currentBatsman();
}
}
else if (computer_random == 0)
{
total_balls_by_each_player();
addScoreComputer(n);
addBallsComputer(comp);
printf("EXTRAS!! %d added to the Score! Score now is: %d/%d\n\n", n, comp.team_score, comp.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", comp.total_overs);
}
else
{
total_balls_by_each_player();
addScoreComputer(computer_random);
addBallsComputer();
printf("%d added to the Score! Score now is: %d/%d\n\n", computer_random, comp.team_score, comp.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", comp.total_overs);
}
printf("\033[0m");
}
void maxRuns(int wickets, player *p[wickets])
{
char *tempName = (char *)malloc(sizeof(char) * 20);
int maxRuns = p[0]->ind_runs;
tempName = p[0]->player_name;
for (int i = 0; i < wickets; i++)
{
if (p[i]->ind_runs > maxRuns)
{
maxRuns = p[i]->ind_runs;
strcpy(tempName, p[i]->player_name);
}
}
printf("\n");
printf("\033[1;32m");
printf("\nMaximum runs in the innings was scored by : %s\n", tempName);
printf("Runs scored = %d\n\n", maxRuns);
printf("\033[0m");
}
void lets_start()
{
player *players[wickets];
createQueue();
for (int i = 0; i < wickets; i++)
{
players[i] = createPlayer();
acceptPlayerDetails(players[i], i + 1);
enqueueBatsman(players[i]);
}
printf("\033[1;34m");
printf("Start Batting!!!\n");
currentBatsman();
printf("\033[1;34m");
printf("%d/%d %.1f overs up\n", team.team_score, team.total_wickets, team.total_overs + (currover / 10.0));
int f = 0;
while (team.total_overs + (currover / 10.0) < overs)
{
batting();
if (allOut())
{
printf("\033[1;31m");
printf("\nALL OUT!!!\n");
f++;
break;
}
}
printf("\033[1;34m");
if (f == 0)
printf("1st Innings finished!!!\n\n");
printf("-------TEAM SCORE-------\n");
printf("%d/%d in %.1f overs\n", team.team_score, team.total_wickets, team.total_overs + (currover / 10.0));
displayPlayerScore(wickets, players);
maxRuns(wickets, players);
makeEmpty();
printf("\033[1;32m");
printf("\nTarget for the computer = %d\n", target);
printf("\033[1;34m");
for (int i = 0; i < wickets; i++)
{
players[i] = createPlayer();
computerPlayerDetails(players[i], i + 1);
enqueueBatsman(players[i]);
}
f = 0;
while ((comp.total_overs + (currover / 10.0) < overs) && comp.team_score < target)
{
compbattingChase();
if (allOut())
{
printf("\033[1;31m");
printf("ALL OUT!!!\n");
f++;
break;
}
}
printf("\033[1;34m");
if (f == 0)
printf("2nd Innings finished!!!\n\n");
printf("-------TEAM SCORE-------\n");
printf("%d/%d in %.1f overs\n", comp.team_score, comp.total_wickets, comp.total_overs + (currover / 10.0));
displayPlayerScore(wickets, players);
maxRuns(wickets, players);
if (comp.team_score < target)
{
printf("\033[1;32m");
printf("\nCongratulations!!You have won the game by %d runs\n", target - 1 - comp.team_score);
}
else if (target - 1 == comp.team_score)
{
superover();
}
else
{
printf("\033[1;31m");
printf("\nComputer has won by %d wickets\n", wickets - comp.total_wickets);
}
makeEmpty();
printf("\033[0m");
}
void lets_start2() //computer first batting
{
player *players[wickets];
createQueue();
for (int i = 0; i < wickets; i++)
{
players[i] = createPlayer();
computerPlayerDetails(players[i], i + 1);
enqueueBatsman(players[i]);
}
printf("Computer Batting!!!\n");
currentBatsman();
printf("\033[1;35m");
printf("%d/%d %.1f overs up\n", comp.team_score, comp.total_wickets, comp.total_overs + (currover / 10.0));
int f = 0;
while (comp.total_overs + (currover / 10.0) < overs)
{
compbatting();
if (allOut())
{
printf("\033[1;31m");
printf("\nALL OUT!!!\n");
f++;
break;
}
}
printf("\033[1;35m");
if (f == 0)
printf("1st Innings finished!!!\n\n");
printf("-------TEAM SCORE-------\n");
printf("%d/%d in %.1f overs\n", comp.team_score, comp.total_wickets, comp.total_overs + (currover / 10.0));
displayPlayerScore(wickets, players);
maxRuns(wickets, players);
makeEmpty_2();
printf("\033[1;32m");
printf("\nTarget for the user = %d\n", target);
printf("\033[1;35m");
for (int i = 0; i < wickets; i++)
{
players[i] = createPlayer();
acceptPlayerDetails(players[i], i + 1);
enqueueBatsman(players[i]);
}
f = 0;
while ((team.total_overs + (currover / 10.0) < overs) && team.team_score < target)
{
userbattingChase();
if (allOut())
{
printf("\033[1;31m");
printf("ALL OUT!!!\n");
f++;
break;
}
}
printf("\033[1;35m");
if (f == 0)
printf("2nd Innings finished!!!\n\n");
printf("-------TEAM SCORE-------\n");
printf("%d/%d in %.1f overs\n", team.team_score, team.total_wickets, team.total_overs + (currover / 10.0));
displayPlayerScore(wickets, players);
maxRuns(wickets, players);
if (team.team_score < target)
{
printf("\033[1;31m");
printf("\nComputer has won by %d runs\n", target - 1 - team.team_score);
}
else if (target - 1 == team.team_score)
{
superover();
}
else
{
printf("\033[1;32m");
printf("\nCongratulations!!You have won the game by %d wickets\n", wickets - team.total_wickets);
}
makeEmpty_2();
printf("\033[0m");
}
void compbatting() //computer first batting innings(sets the target)
{
printf("\033[1;35m");
int n;
printf("%.1fth ball\n\n", comp.total_overs + (currover / 10.0 + 0.1));
printf("Enter a number (0-6) : ");
scanf("%d", &n);
while (n >= 7 || n < 0)
{
printf("\033[1;31m");
printf("Please enter a number in the range 0-6 : ");
scanf("%d", &n);
printf("\033[1;35m");
}
int computer_random = printRandoms(0, 6);
printf("\033[1;32m");
printf("Computer generated number : %d\n\n", computer_random);
printf("\033[1;35m");
if (n == computer_random)
{
total_balls_by_each_player();
node *temp = batting_order->front;
printf("\033[1;31m");
printf("\n%s is OUT!!\n", temp->data->player_name);
printf("Runs scored : %d\n", temp->data->ind_runs);
temp->data->strike_rate = ((float)temp->data->ind_runs / total_balls) * 100;
printf("Strike rate is %.2f\n\n", temp->data->strike_rate);
printf("\033[1;35m");
dequeueBatsman(total_balls);
total_balls = 0;
addBallsComputer();
addWicketComputer();
if (!allOut())
{
battingLineUp();
currentBatsman();
}
}
else if (computer_random == 0)
{
total_balls_by_each_player();
addScoreComputer(n);
addBallsComputer();
printf("EXTRAS!! %d added to the Score! Score now is: %d/%d\n\n", n, comp.team_score, comp.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", comp.total_overs);
}
else
{
total_balls_by_each_player();
addScoreComputer(computer_random);
addBallsComputer();
printf("%d added to the Score! Score now is: %d/%d\n\n", computer_random, comp.team_score, comp.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", comp.total_overs);
}
printf("\033[0m");
}
void userbattingChase()
{
printf("\033[1;34m");
int n;
printf("\n----Innings 2----\n");
printf("%.1fth ball\n\n", team.total_overs + (currover / 10.0 + 0.1));
printf("Enter a number (0-6) : ");
scanf("%d", &n);
while (n >= 7 || n < 0)
{
printf("\033[1;31m");
printf("Please enter a number in the range 0-6 : ");
scanf("%d", &n);
printf("\033[1;34m");
}
int computer_random = printRandoms(0, 6);
printf("\033[1;32m");
printf("Computer generated number : %d\n\n", computer_random);
printf("\033[1;34m");
if (n == computer_random)
{
total_balls_by_each_player();
node *temp = batting_order->front;
printf("\033[1;31m");
printf("\n%s is OUT!!\n", temp->data->player_name);
printf("Runs scored : %d\n", temp->data->ind_runs);
temp->data->strike_rate = ((float)temp->data->ind_runs / total_balls) * 100;
printf("Strike rate is %.2f\n\n", temp->data->strike_rate);
printf("\033[1;34m");
dequeueBatsman(total_balls);
total_balls = 0;
addBallsHuman();
addWicketHuman();
if (!allOut())
{
battingLineUp();
currentBatsman();
}
}
else if (n == 0)
{
addScoreHuman(computer_random);
addBallsHuman();
total_balls_by_each_player();
printf("EXTRAS!! %d added to the Score! Score now is: %d/%d\n\n", computer_random, team.team_score, team.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", team.total_overs);
}
else
{
addScoreHuman(n);
addBallsHuman();
total_balls_by_each_player();
printf("%d added to the Score! Score now is: %d/%d\n\n", n, team.team_score, team.total_wickets);
if (currover == 0)
printf("OVER UP!!!\t %d overs finished\n\n", team.total_overs);
}
printf("\033[0m");
}
void superover()
{
int x1, x2, y1 = 0, y2 = 0, s = 0, c = 0;
printf("\033[1;34m");
printf("\nSuper over!!!\n\n");
printf("1st innings - User Bats\n\n");
printf("\033[1;35m");
for (int i = 0; i < 6; i++)
{
printf("%dth ball :", i + 1);
scanf("%d", &x1);
while (x1 < 0 || x1 > 6)
{
printf("Enter within 0-6\n");
printf("%dth ball :", i + 1);
scanf("%d", &x1);
}
srand(time(NULL));
y1 = (rand() % (7));
printf("\033[1;32m");
printf("Computer generated number: %d\n", y1);
printf("\033[1;35m");
if (x1 != y1)
{
s += x1;
if (i == 5)
{
printf("\nTarget is : %d", s + 1);
break;
}
}
else if (x1 == y1)
{
printf("\nTarget is : %d", s + 1);
break;
}
}
printf("\033[1;34m");
printf("\n\n2nd innings - Computer Bats\n");
for (int i = 0; i < 6; i++)
{
printf("%dth ball :", i + 1);
scanf("%d", &x2);
while (x2 < 0 || x2 > 6)
{
printf("\033[1;31m");
printf("Enter within 0-6\n");
printf("%dth ball :", i + 1);
scanf("%d", &x2);
printf("\033[1;34m");
}
srand(time(NULL));
y2 = (rand() % (7));
printf("\033[1;32m");
printf("Computer generated number: %d\n", y2);
printf("\033[1;34m");
if (x2 != y2)
{
c += y2;
if (c > s)
{
printf("\nComputer Won\n");
break;
}
else if (c < s && i == 5)
{
printf("\nComputer Lost\n");
printf("User wins\n");
break;
}
else if (c == s && i == 5)
{
printf("\nSuper over tied\n");
break;
}
}
else
{
if (c < s)
{
printf("\nComputer Lost\n");
printf("User wins\n");
break;
}
else if (c == s)
{
printf("\nSuper over tied\n");
break;
}
}
}
printf("\033[0m");
}
void clear()
{
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
void main()
{
int choice;
printf("\033[1;33m");
printf("\n---------------------------------------------------------Welcome to Our Key-Cricket Game----------------------------------------------------");
printf("\n\nKey Cricket is a game designed by students of DIST to help students overcome stress and have fun!");
printf("\n\nThe name Key-Cricket is so because you will be playing using your computer/laptop keyboard vs our super intelligent computer!!");
printf("\n\n--------------------------------------------------------------------------------------------------------------------------------------------");
printf("\n\n");
printf("\nLet's Goo!!!\n");
printf("\033[0;m");
int countGame = 0;
char des;
while (1)
{
printf("\033[1;33m");
if (countGame > 0)
{
while ((getchar() != '\n'))
;
printf("\nDo you wanna play again ?\nEnter Y to play again and N to exit : ");
des = getchar();
while (des != 'Y' && des != 'y' && des != 'N' && des != 'n')
{
printf("\033[1;31m");
while ((getchar() != '\n'))
;
printf("\nPlease enter either Y or N !\n\nDo you wanna play again ?\nEnter Y to play again and N to exit : ");
des = getchar();
}
printf("\033[1;33m");
if (des == 'Y' || des == 'y')
clear();
else if (des == 'N' || des == 'n')
{
printf("\033[1;32m");
printf("\nThanks for playing !\n");
printf("\033[0;m");
break;
exit(0);
}
}
printf("\033[1;33m");
printf("\n\n1.Rules of the Game");
printf("\n2.Start The Game");
printf("\n3.EXIT");
printf("\n\nEnter your Choice : ");
scanf("%d", &choice);
printf("\033[0;m");
switch (choice)
{
case 1:
printf("\033[1;33m");
printf("Let's Jump on to the Rules of The game");
printf("\033[0;m");
print_rules();
countGame = 0;
break;
case 2:
clear();
start();