-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcricket.cpp
299 lines (283 loc) · 10.5 KB
/
cricket.cpp
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
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<string.h>
using namespace std;
//structure named "Details" for capsulating all the details of a team
struct Details
{
char name[30];
char player_name[11][30];
int sixes;
int fours;
int runs_scored[11];
int runs_lost[11];
int total_runs;
int wicket_taken[11];
}team[2];
//global variables
int number_of_overs;
int number_of_players;
int toss;
//----------------------------------------------function prototypes-------------------------------------------------
//to display the welcome message and rules
void welcomeMessage();
//to input both the team names
void getTeamNames();
//to input the number of overs the game has to be played
void inputOvers();
//to decide how many players will be playing the game, it will be deicded on the basis of the number of overs
void playerDecider();
//to input the names of the players of both the teams
void playerNamesInput();
//to calculate the score and display
void scoreboard();
//to display the score board
void displayScoreBoard(char player_name[30],char bowler[30],int sixes,int fours,int runs_scored,int runs_lost,int total_runs,int wicket_taken);
//to display the whole scoreboard
void scorecard();
//-------------------------------------------------main function---------------------------------------------
int main()
{
welcomeMessage();
getTeamNames();
inputOvers();
playerDecider();
playerNamesInput();
scoreboard();
scorecard();
return 0;
}
/*-----------------------------------------------function definitions----------------------------------------*/
//function to print the rules of the game
void welcomeMessage()
{
cout<<"\n\n\t\t\t\t\tWelcome to the Gentlemen's Game....!";
cout<<"\n\n\t\t\t\t\t\tRULES OF THE GAME \n\n";
cout<<"\n1. Firstly, you have to enter both team names.";
cout<<"\n2. You have to choose the number of overs you have to play the game. The number of overs hould not more than 10 overs!";
cout<<"\n3. According to number of overs, the number of players will be decided";
cout<<"\n4. You have to enter the number of players and their names in both the teams";
cout<<"\n5. Team 1 will be batting first and Team 2 will be bowliing first";
cout<<"\n6. Scoreboard will be displayed after each ball.";
cout<<"\n7. The scores will be generated by the program itself.";
cout<<"\n8. If the number 7 is generated, then the batsman playing will be out";
cout<<"\n9. As you keep on pressing any character you will get the updated score.";
cout<<"\n10. If the scores get level, then the match will be drawn";
}
//function to get the names of the team
void getTeamNames()
{
cout<<"\n\n";
for(int i=0;i<2;i++)
{
cout<<"Enter the name of the team "<<i+1<<" : ";
cin>>team[i].name;
}
}
//function to input the number of overs
void inputOvers()
{
cout<<"\nEnter the number of overs you have to play the game : ";
cin>>number_of_overs;
if(number_of_overs>10)
{
cout<<"\nWrong input.....!!";
cout<<"\nThe max overs is set to 10";
number_of_overs=10;
}
}
//function to decide the number of players playing the game
void playerDecider()
{
if(number_of_overs<=5)
{
number_of_players=5;
}
else if(number_of_overs>5 && number_of_overs<=7)
{
number_of_players=7;
}
else
{
number_of_players=11;
}
cout<<"\n\nThe game will be played with "<<number_of_players<<" players!!"<<endl;
}
//function to input the names of the player
void playerNamesInput()
{
char ch;
cout<<"\n\nDo you want to enter names or want to assign them automatically? (press Y to enter names) : ";
cin>>ch;
if(ch=='Y' || ch=='y')
{
for(int i=0;i<2;i++)
{
cout<<"\nEnter the names of player of "<<team[i].name<<" : "<<endl;
for(int j=0;j<number_of_players;j++)
{
cout<<"Enter the name of the player "<<j+1<<" : ";
cin>>team[i].player_name[j];
}
}
cout<<"\n\n";
}
else
{
cout<<"\n\nASSIGNING NAMES......!!\n";
strcpy(team[0].player_name[0],"Virat");
strcpy(team[0].player_name[1],"ABD");
strcpy(team[0].player_name[2],"Gayle");
strcpy(team[0].player_name[3],"Rahul");
strcpy(team[0].player_name[4],"Chahal");
strcpy(team[0].player_name[5],"Zampa");
strcpy(team[0].player_name[6],"Siraj");
strcpy(team[0].player_name[7],"Richardson");
strcpy(team[0].player_name[8],"Sundar");
strcpy(team[0].player_name[9],"Devdutt");
strcpy(team[0].player_name[10],"Ahmed");
strcpy(team[1].player_name[0],"Rohit");
strcpy(team[1].player_name[1],"Pollard");
strcpy(team[1].player_name[2],"Hardik");
strcpy(team[1].player_name[3],"Krunal");
strcpy(team[1].player_name[4],"Bumrah");
strcpy(team[1].player_name[5],"Suryakumar");
strcpy(team[1].player_name[6],"Ishan");
strcpy(team[1].player_name[7],"Kulkarni");
strcpy(team[1].player_name[8],"Chahar");
strcpy(team[1].player_name[9],"Boult");
strcpy(team[1].player_name[10],"Coulter-Nile");
}
}
//function to compute the values of the scorecard
void scoreboard()
{
int i=0,j=0,k=0,RUNS=8,temp;
cout<<"\n\n"<<team[0].name<<" is going to Bat now......!";
displayScoreBoard(team[0].player_name[0],team[1].player_name[0],team[0].sixes,team[0].fours,team[0].runs_scored[0],team[1].runs_lost[0],team[0].total_runs,team[1].wicket_taken[0]);
for(j=0;j<number_of_overs;j++)
{
cout<<"\n\t\t\t\t\t\tOver "<<j+1<<"!";
for(int m=1;m<=6;m++)
{
srand(time(0));
l1:
temp=rand()%RUNS;
if(temp==5)
goto l1;
cout<<"\n\nBOWL "<<m<<" : "<<temp<<endl;
if(temp==7)
{
cout<<team[0].player_name[k]<<" has been bowled by "<<team[1].player_name[j]<<"!!!";
k++;
team[1].wicket_taken[j]++;
if(k==number_of_players)
{
cout<<"\nAll the players of "<<team[0].name<<" has been bowled out.....!!";
break;
}
}
else
{
if(temp==6)
{
team[0].sixes++;
}
if(temp==4)
{
team[0].fours++;
}
team[0].runs_scored[k]+=temp;
team[1].runs_lost[j]+=temp;
team[0].total_runs+=temp;
}
displayScoreBoard(team[0].player_name[k],team[1].player_name[j],team[0].sixes,team[0].fours,team[0].runs_scored[k],team[1].runs_lost[j],team[0].total_runs,team[1].wicket_taken[j]);
}
}
cout<<"\n\n"<<team[1].name<<" is going to Bat now......!";
k=0;
int check=1;
displayScoreBoard(team[1].player_name[0],team[0].player_name[0],team[1].sixes,team[1].fours,team[1].runs_scored[0],team[0].runs_lost[0],team[1].total_runs,team[0].wicket_taken[0]);
for(j=0;j<number_of_overs;j++)
{
cout<<"\n\t\t\t\tOver"<<j+1<<"!\n\n";
for(int m=1;m<=6;m++)
{
srand(time(0));
l2:
temp=rand()%RUNS;
if(temp==5)
goto l2;
cout<<"\n\nBOWL "<<m<<" : "<<temp<<endl;
if(temp==7)
{
cout<<team[1].player_name[k]<<" has been bowled by "<<team[0].player_name[j]<<"!!!";
k++;
team[0].wicket_taken[j]++;
if(k==number_of_players)
{
cout<<"\nAll the players of "<<team[1].name<<" has been bowled out.....!!";
break;
}
}
else
{
if(temp==6)
{
team[1].sixes++;
}
if(temp==4)
{
team[1].fours++;
}
team[1].runs_scored[k]+=temp;
team[0].runs_lost[j]+=temp;
team[1].total_runs+=temp;
}
displayScoreBoard(team[1].player_name[k],team[0].player_name[j],team[1].sixes,team[1].fours,team[1].runs_scored[k],team[0].runs_lost[j],team[1].total_runs,team[0].wicket_taken[j]);
if(team[0].total_runs<team[1].total_runs)
{
cout<<"\n\n\t\t\t\t"<<team[1].name<<" has beaten "<<team[0].name<<"........!";
check=0;
break;
}
}
}
if(team[0].total_runs>team[1].total_runs)
{
cout<<"\n\n\t\t\t\t"<<team[0].name<<" has beaten "<<team[1].name<<"........!";
}
if(team[0].total_runs==team[1].total_runs)
{
cout<<"\n\n\t\t\t\t"<<"MATCH DRAWN"<<endl;
}
}
//function to display the values of score card
void displayScoreBoard(char player_name[30],char bowler[30],int sixes,int fours,int runs_scored,int runs_lost,int total_runs,int wicket_taken)
{
char ch;
cout<<"\n";
cout<<"Current Player"<<"\t\tRuns Scored"<<"\t\tSixes"<<"\t\tFours\t\tTotal Runs"<<endl;
cout<<player_name<<"\t\t\t"<<runs_scored<<"\t\t\t"<<sixes<<"\t\t"<<fours<<"\t\t"<<total_runs<<endl;
cout<<"Bowler Name\t\t"<<"Runs Given\t\t"<<"Wickets Taken"<<endl;
cout<<bowler<<"\t\t\t\t"<<runs_lost<<"\t\t\t"<<wicket_taken<<endl;
cout<<"NEXT ? : ";
cin>>ch;
}
void scorecard()
{
cout<<"\n\n\t\t\t\t---SCORE CARD---"<<endl<<endl;
cout<<"TEAM : "<<team[0].name;
cout<<"\n\nTotal 6s : "<<team[0].sixes;
cout<<"\nTotal 4s : "<<team[0].fours;
cout<<"\nPlayer Name\t\t Runs Scored\t\t Wickets Taken\t\t Runs Given\n\n";
for(int i=0;i<number_of_players;i++)
cout<<team[0].player_name[i]<<"\t\t\t\t"<<team[0].runs_scored[i]<<"\t\t\t"<<team[0].wicket_taken[i]<<"\t\t\t"<<team[0].runs_lost[i]<<endl;
cout<<"\n\nTEAM : "<<team[1].name;
cout<<"\n\nTotal 6s : "<<team[1].sixes;
cout<<"\nTotal 4s : "<<team[1].fours;
cout<<"\nPlayer Name\t\t Runs Scored\t\t Wickets Taken\t\t Runs Given\n\n";
for(int i=0;i<number_of_players;i++)
cout<<team[1].player_name[i]<<"\t\t\t\t"<<team[1].runs_scored[i]<<"\t\t\t"<<team[1].wicket_taken[i]<<"\t\t\t"<<team[1].runs_lost[i]<<endl;
}