Skip to content

Instantly share code, notes, and snippets.

@shivanshu-semwal
Last active September 26, 2020 08:37
Show Gist options
  • Save shivanshu-semwal/64d0f2e380dfd564f75930dcabf2ce12 to your computer and use it in GitHub Desktop.
Save shivanshu-semwal/64d0f2e380dfd564f75930dcabf2ce12 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <time.h>
#ifdef _WIN32
#define CLEAR_SCREEN "cls"
#include <windows.h>
#else
#include <unistd.h>
#define CLEAR_SCREEN "clear"
#endif // _WIN32
void sleepcp(int milliseconds) {
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif // _WIN32
}
using namespace std;
class batter {
public:
enum OUT_MODE { NOT_OUT,
RUN_OUT,
HIT_WICKET,
END };
string printOut(batter::OUT_MODE a) { //return the out mode string
string arr[] = {"-", "Run out", "Hit wicket"}; //sync this array with the out mode enum
return arr[a];
}
void display() {
cout << name << "\t" << runs << "\t" << ((isOut) ? "Y" : "N") << "\t" << batter::printOut(_out_mode) << "\n";
}
batter() {
name = "NO_NAME";
runs = 0;
isOut = false;
_out_mode = batter::OUT_MODE::NOT_OUT;
}
void out(batter::OUT_MODE a) {
_out_mode = a;
isOut = true;
}
void inc_run(int a) {
if (!isOut) {
if (a <= 6 && a >= 0) {
runs += a;
}
}
}
void setName(string s) {
name = s;
}
int getRuns() {
return runs;
}
private:
string name; // name of the batter
int runs; // number of runs scored
bool isOut; // is the batter out
batter::OUT_MODE _out_mode; //should be below after declaration of enum
};
class bowler {
public:
bowler() {
name = "NO_NAME";
overs = 0;
runs_given = 0;
maiden_overs = 0;
wicket_taken = 0;
balls_thrown = 0;
_maiden = true;
}
void display() {
cout << name << "\t" << overs << "\t" << runs_given << "\t" << maiden_overs << "\t" << wicket_taken << "\n";
}
void setName(string s) {
name = s;
}
void ball(int run, bool o = false) {
balls_thrown++;
overs = balls_thrown / 6;
wicket_taken = wicket_taken + (o) ? 1 : 0;
if (run <= 6 && run >= 0) {
runs_given += run;
_maiden = false;
}
if (balls_thrown % 6 == 0 && _maiden) {
maiden_overs++;
} else {
_maiden = true;
}
}
private:
string name; //name of the bowler
int overs; //number of overs
int maiden_overs; // maiden overs
int runs_given; // runs given
int wicket_taken; // wicket taken
int balls_thrown; //
bool _maiden; //
};
class score_board {
public:
batter ba[10]; // batters array
bowler bo[10]; //bowlers array
score_board() {
// ba = new batter[10]; // make the array pointers to dynamically initialize them
// bw = new bowler[10];
}
int getRun() {
int total = 0;
for (int i = 0; i < 10; i++) {
total += ba[i].getRuns();
}
return total;
}
void displayBatter() {
cout << "-------------------------------------------------------------\n";
cout << "NAME"
<< "\t"
<< "RUNS"
<< "\t"
<< "OUT"
<< "\t"
<< "OUT-MODE\n";
for (int i = 0; i < 10; i++) {
ba[i].display();
}
cout << "-------------------------------------------------------------\n";
}
void displayBowler() {
cout << "-------------------------------------------------------------\n";
cout << "NAME"
<< "\t"
<< "OVERS"
<< "\t"
<< "R.G."
<< "\t"
<< "M.O."
<< "\t"
<< "W.T."
<< "\n";
for (int i = 0; i < 10; i++) {
bo[i].display();
}
cout << "*R.G. - runs-given *M.O. - maiden-overs *W.T. - wicket-taken\n";
cout << "-------------------------------------------------------------\n";
}
void input_names() {
string s;
for (int i = 0; i < 10; i++) {
cin >> s;
ba[i].setName(s);
bo[i].setName(s);
}
}
};
int random(int a, int b) {
// produce random number between [a,b)
int r = (rand() % (b - a)) + a;
return r;
}
int main() {
srand((unsigned)time(NULL));
score_board s1;
score_board s2;
s1.input_names();
s2.input_names();
// s.displayBatter();
// s.displayBowler();
const int no_of_overs = 20;
// first team playing
int b = 0;
for (int i = 0; i < no_of_overs && b < 10; i++) {
int cur_baller = random(5, 10); // current random baller
for (int j = 0; j < 6; j++) {
int run = random(0, 106);
// now bias the run
if (run <= 20)
run = 0;
else if (run <= 40)
run = 1;
else if (run <= 60)
run = 2;
else if (run <= 80)
run = 3;
else if (run <= 90)
run = 4;
else if (run <= 100)
run = 6;
else if (run <= 105)
run = -1; // means out
s1.bo[cur_baller].ball(run, (run == -1) ? true : false);
s2.ba[b].inc_run(run);
if (run == -1) {
s2.ba[b].out((batter::OUT_MODE)random(1, batter::OUT_MODE::END));
b++;
}
s2.displayBatter();
s1.displayBowler();
sleepcp(100);
system(CLEAR_SCREEN);
}
}
int total1 = s2.getRun();
int total2 = 0;
bool win2 = false;
// second team playing
b = 0;
for (int i = 0; i < no_of_overs && b < 10 && total2 <= total1; i++) {
int cur_baller = random(5, 10); // current random baller
for (int j = 0; j < 6; j++) {
int run = random(0, 106);
// now bias the run
if (run <= 20)
run = 0;
else if (run <= 40)
run = 1;
else if (run <= 60)
run = 2;
else if (run <= 80)
run = 3;
else if (run <= 90)
run = 4;
else if (run <= 100)
run = 6;
else if (run <= 105)
run = -1; // means out
s2.bo[cur_baller].ball(run, (run == -1) ? true : false);
s1.ba[b].inc_run(run);
if (run == -1) {
s1.ba[b].out((batter::OUT_MODE)random(1, batter::OUT_MODE::END));
b++;
}
s1.displayBatter();
s2.displayBowler();
sleepcp(100);
system(CLEAR_SCREEN);
total2 = s2.getRun();
if (total2 > total1) {
win2 = true;
break;
}
}
}
if (win2) {
printf("Second team won!!!!!!!!!!!!!\n");
} else {
printf("First team won!!!!!!!!!!\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment