-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiProject.cpp
More file actions
508 lines (423 loc) · 16 KB
/
Copy pathminiProject.cpp
File metadata and controls
508 lines (423 loc) · 16 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <thread>
#include <chrono>
#include<algorithm>
using namespace std;
using namespace std::chrono;
// Define constants for simulation
const int MAX_OVERS = 10;
const int MAX_BALLS_PER_OVER = 6;
// Function to clear the console screen
void clearScreen() {
this_thread::sleep_for(3s);
system("cls");
}
int option = 1;
class Player {
public:
string name;
int runsScored = 0;
int ballsFaced = 0;
int wicketsTaken = 0;
int ballsBowled = 0;
int battingOrder;
Player* nextBatsman;
Player* nextBowler;
Player(const string playerName,int order, Player* nextBatsman, Player* nextBowler)
: name(playerName),battingOrder(order),
nextBatsman(nullptr), nextBowler(nullptr) {
this->nextBatsman = nextBatsman;
this->nextBowler = nextBowler;
}
~Player() {}
string getName() {
return name;
}
// Function to calculate and return the player's batting score
int calculateBattingScore() {
// Score between 0 and 6.
int randomScore = rand() % 7;
return randomScore;
}
// Function to calculate and return the player's bowling score
int calculateBowlingScore() {
// Wicket-taking score between 0 and 2.
int randomScore = rand() % 3;
return randomScore;
}
};
class Team {
public:
string name;
Player* captain;
Player* nextPlayer;
int teamscore;
Team(string teamName) : name(teamName), captain(nullptr) {
Player* tempplayer = new Player("sds", 0, nullptr, nullptr);
nextPlayer = tempplayer;
}
~Team(){}
string getTeamName() {
return name;
}
// Add methods to add players, set captains, openers, etc.
void addPlayer(string name, int battingorder) {
if (nextPlayer-> getName() == "sds") {
Player* newplayer = new Player(name,battingorder,nullptr, nullptr);
nextPlayer = newplayer;
}
else if (nextPlayer->nextBatsman == nullptr) {
Player* newplayer = new Player(name, battingorder,nullptr, nextPlayer);
nextPlayer->nextBatsman = newplayer;
}
else {
Player* tempPlayer = nextPlayer;
while (tempPlayer->nextBatsman != nullptr) {
tempPlayer = tempPlayer->nextBatsman;
}
Player* newplayer = new Player(name, battingorder,nullptr, tempPlayer);
tempPlayer->nextBatsman = newplayer;
}
}
// Function to set the captain
void setCaptain(Player* player) {
captain = player;
}
// Function to display team players
void displayPlayers() {
cout << "Players of Team " << name << ":" << endl;
Player* player = nextPlayer;
while (player != nullptr) {
if(player==captain){
cout << player->getName() <<"(Captain)"<< endl;
}
else {
cout << player->getName() << endl;
}
player = player->nextBatsman;
}
}
};
class Scoreboard {
private:
int score = 0;
int overs = 0;
int wickets = 0;
int balls = 0;
Team* battingTeam;
Team* bowlingTeam;
Player* striker;
Player* nonStriker;
Player* bowler;
public:
Scoreboard(Team* teamA, Team* teamB) : battingTeam(teamA), bowlingTeam(teamB) {
striker = battingTeam->nextPlayer;
nonStriker = battingTeam->nextPlayer->nextBatsman;
Player* temp = bowlingTeam->nextPlayer;
while (temp->nextBatsman != nullptr) {
temp = temp->nextBatsman;
}
bowler = temp;
}
~Scoreboard() {}
int getOvers() const {
return overs;
}
int getBalls() {
return balls;
}
// Function to switch striker and non-striker
void switchStrikers(Player* outedbatman) {
if (outedbatman == striker) {
striker = nonStriker;
nonStriker = nonStriker->nextBatsman;
}
else {
nonStriker = nonStriker->nextBatsman;
}
}
// Function to switch bowlers
void switchBowler() {
bowler = bowler->nextBowler;
}
void swapStriker() {
Player* temp = striker;
striker = nonStriker;
nonStriker = temp;
}
void showBatsmans() {
cout << "Striker " << striker->getName() << "\tNon Striker " << nonStriker->getName() << endl;
}
void showBowler() {
cout << "Now bowling " << bowler->getName() << endl;
}
void endOfFirstInnings() {
// Swap two teams
Team* temp = battingTeam;
battingTeam = bowlingTeam;
bowlingTeam = temp;
// Reset batsmen and baller for team 1
striker = battingTeam->nextPlayer;
nonStriker = battingTeam->nextPlayer->nextBatsman;
// Find the last player in team 2 to set as the baller
Player* temphead = bowlingTeam->nextPlayer;
while (temphead->nextBatsman != nullptr) {
temphead = temphead->nextBatsman;
}
bowler = temphead;
}
void play() {
cout << "--------------------Cricket match starts!--------------------" << endl<<endl;
for (overs = 1; overs <= MAX_OVERS; overs++) {
cout << "\nOver " << overs << " begins!" << endl;
for (balls = 1; balls <= 6; balls++) {
cout << "\nBall " << balls << "..." << endl;
this_thread::sleep_for(3s);
int random_event = rand() % 9; // Generate a random number between 0 and 6
// Simulate a ball delivery
if (random_event == 0) {
cout << "Wide Ball!" << endl;
score += 1; // Increase the score by 1
battingTeam->teamscore++; // Increase the team score by 1
}
else if (random_event == 5) {
cout << "No Ball!" << endl;
score += 1; // Increase the score by 1
battingTeam->teamscore++; // Increase the team score by 1
}
else {
// A regular ball
if (random_event == 6) {
cout << "It's a Six!" << endl;
score += 6; // Increase the score by 6
battingTeam->teamscore+=6; // Increase the team score by 6
}
else if (random_event == 7) {
cout << "It's a Four!" << endl;
score += 4; // Increase the score by 4
battingTeam->teamscore += 4; // Increase the team score by 4
}
else if (random_event == 8) { // Check if the batsman is out
cout << "Batsman " << striker->getName() << " is out!" << endl;
wickets += 1;
striker->nextBatsman = nullptr;
// Get the next batsman
switchStrikers(striker);
}
else {
cout << "Batsman plays the ball..." << endl;
score += random_event; // Increase the score by the runs scored
battingTeam->teamscore += random_event; // Increase the team score
}
// Update the batsman's statistics
striker->runsScored += random_event;
striker->ballsBowled += 1;
}
if (random_event != 0 && random_event != 5) {
// Update the overs, wickets, and swap the batsmen if needed
if (random_event % 2 != 0 && random_event != 7) {
swapStriker();
}
striker->ballsBowled += 1;
if (balls == 6) {
swapStriker();
}
}
// Show the current scoreboard
cout << "Score: " << score << "/" << wickets << " in " << overs << " overs" << endl;
showBatsmans();
if (wickets == 10) {
cout << "All out!" << endl;
break; // All out
}
// Show the current baller
showBowler();
if (overs == MAX_OVERS && balls == 6) {
cout << "\n\nFirst Innings Ended!" << endl;
cout << "Score: " << score << "/" << wickets << " in " << overs << " overs" << endl;
break; // End of the first innings
}
}
if (wickets == 10) {
cout << "\n\nFirst Innings Ended!" << endl;
cout << "Score: " << score << "/" << wickets << " in " << overs << " overs" << endl;
break; // End of the first innings
}
}
cout << "\n\n*********Innings Break!*********" << endl;
cout << "Runs to win : " << score+1 << " in " << --overs << " overs" << endl;
// Reset the scoreboard for the second innings
score = 0;
balls = 0;
overs = 0;
wickets = 0;
// Switch teams for the second innings
endOfFirstInnings();
for (overs = 1; overs <= MAX_OVERS; overs++) {
cout << "\nOver " << overs << " begins!" << endl;
for (balls = 1; balls <= 6; balls++) {
cout << "\nBall " << balls << "..." << endl;
this_thread::sleep_for(3s);
int random_event = rand() % 9; // Generate a random number between 0 and 6
// Simulate a ball delivery
if (random_event == 0) {
cout << "Wide Ball!" << endl;
score += 1; // Increase the score by 1
battingTeam->teamscore++; // Increase the team score by 1
}
else if (random_event == 5) {
cout << "No Ball!" << endl;
score += 1; // Increase the score by 1
battingTeam->teamscore++; // Increase the team score by 1
}
else {
// A regular ball
if (random_event == 6) {
cout << "It's a Six!" << endl;
score += 6; // Increase the score by 6
battingTeam->teamscore+=6; // Increase the team score by 6
}
else if (random_event == 7) {
cout << "It's a Four!" << endl;
score += 4; // Increase the score by 4
battingTeam->teamscore += 4; // Increase the team score by 4
}
else if (random_event == 8) { // Check if the batsman is out
cout << "Batsman " << striker->getName() << " is out!" << endl;
wickets += 1;
if (wickets == 10) {
cout << "All out!" << endl;
break; // All out
}
striker->nextBatsman = nullptr;
// Get the next batsman
switchStrikers(striker);
}
else {
cout << "Batsman plays the ball..." << endl;
score += random_event; // Increase the score by the runs scored
battingTeam->teamscore += random_event; // Increase the team
}
// Update the batsman's statistics
striker->runsScored += random_event;
striker->ballsBowled += 1;
}
if (random_event != 0 && random_event != 5) {
// Update the overs, wickets, and swap the batsmen if needed
if (random_event % 2 != 0 && random_event != 7) {
swapStriker();
}
striker->ballsBowled += 1;
if (balls == 6) {
swapStriker();
}
}
// Show the current scoreboard
cout << "Score: " << score << "/" << wickets << " in " << overs << " overs" << endl;
showBatsmans();
// Show the current baller
showBowler();
if (overs == MAX_OVERS && balls == 6) {
cout << "\nSecond Innings Ended!" << endl;
cout << "Score: " << score << "/" << wickets << " in " << overs << " overs" << endl;
cout << endl << endl;
if (battingTeam->teamscore == bowlingTeam->teamscore) { // Determine the match result
cout << "\nThe match ended in a draw!" << endl;
}
break; // End of the second innings
}
}
// Determine the match result
if (battingTeam->teamscore > bowlingTeam->teamscore) {
cout << "\nTeam " << battingTeam->getTeamName() << " WINS!!!" << endl;
break;
}
}
if (battingTeam->teamscore < bowlingTeam->teamscore) {
cout << "\nTeam " << bowlingTeam->getTeamName() << " WINS!!!" << endl;
option--;
}
std::cout<< "\n-------------------Match Over!-------------------" << endl;
}
};
class Commentator {
public:
// Function to generate commentary based on events
static string generateCommentary() {
switch (option) {
case 0:
return "Sri Lanka has emerged victorious in this thrilling match with an exceptional batting performance. They've played brilliantly to secure a well-deserved win!";
case 1:
return "Australia has clinched victory with some sensational bowling! They've managed to defend their target admirably, showing great skill and determination on the field.";
default:
return " ";
}
}
};
class Match{
public:
// Function to simulate a cricket match
void simulateCricketMatch(Team* teamA, Team* teamB) {
Scoreboard scoreboard(teamA, teamB);
Commentator commentator;
string commentary = commentator.generateCommentary();
// Display scoreboard and commentary
scoreboard.play();
cout << "Commentary: " << commentary << endl;
// Pause for a moment before the next ball
this_thread::sleep_for(seconds(2));
//clearScreen(); // Clear the screen for the next ball
}
void textInput(string textline, Team* teamA,Team* teamB) {
string up = textline;
std::transform(up.begin(), up.end(), up.begin(), ::toupper);
string command, team_name, player_name;
int battingorder;
stringstream geek(up);
geek >> command >> team_name;
if (command == "CREATE") {
if (teamA->name == "0") {
teamA->name = team_name;
}
else {
teamB->name = team_name;
}
}
else if (command == "ADD") {
geek >> player_name >> battingorder;
Team* tempteam=new Team("hi");
if (team_name == teamA->name) {
tempteam = teamA;
}
else if (team_name == teamB->name) {
tempteam = teamB;
}
tempteam->addPlayer(player_name,battingorder);
}
}
};
int main() {
// Create two teams
Team* team1 = new Team("0");
Team* team2 = new Team("0");
Match match;
fstream myfile;
myfile.open("inputs.txt", ios::in);
if (myfile.is_open()) {
string line;
while (getline(myfile, line)) {
match.textInput(line,team1,team2);
}
}
else {
cout << "File not found";
}
//Simulate the cricket match
match.simulateCricketMatch(team1, team2);
return 0;
}