blake
School Cricketer
Wasn't sure where to post this - hopefully this is the right forum for it.
Anyway, I'm fairly new to C++. Been learning it a little bit from some online tutorials for a while now - although nothing consistent. I decided it was about time that I had a crack at trying to whip up a quick cricket simulator.
Now, for a C++ beginner, making a simulator is no easy feat. I'm struggling a lot and need some help with my code.
I thought it might have a chance to work - but when I run it, it doesn't seem to work.
So, let me run through the program for you.
Basically, there is no teams, or any players with the exception of Ponting and Flintoff. I have just tried to create a 1 v 1 type scenario with Flintoff bowling to Ponting each ball and then an amount of runs being determined.
Here is my algorithm to determine the amount of runs for each ball:
Batsman is given fixed values for each area. This represents their ability to play a shot in that location. These are the areas: wide outside off, outside off, off stump, middle stump, leg stump and outside leg. There is no other stats for the batsmen except for these. There is a rough number for each. Ricky Ponting's ability wide outside off is 6.125, his ability outside off is 4.1875, off stump is 4, straight is 4.875, leg stump is 6.875 and outside leg stump is 8.125.
These are the only batting values required.
For the bowlers, they have two values. Consistency and skill. Flintoff's consistency is 67.6, and his skill is 68.5. For now, I ruled them like this:
Skill = 100 - FC Bowling Average
Consistency = 100 - FC Bowling Strike Rate / 2
Here is how each ball is determined (a tricky process).
A random number between 1-50 is generated for the bowler. In my program, the variable for this is 'randcons'.
The bowler's consistency is multiplied by 1.66. Then, the random number just generated between 1-50 is added to the consistency * 1.66.
For example:
Flintoff's consistency = 67.6
Random number = 30
67.6 * 1.66 = 112.2
112.2 + 30 = 142.2
142.2 = 142
Final number = 142
This final number is assigned to the variable 'bowlnum'.
Now, we use the bowlnum to find out which area the ball has landed in. The ideal area is off stump, and then outside off is the next best area to bowl in.
Here's an example for this - if the 'bowlnum' is less than 100, the ball is delivered outside leg. If it is between 101-110. it is leg stump. (This might need to be a little more random, because this means it is impossible for Flintoff to bowl on leg stump or outside leg because his consistency is too high. Still, I'll leave it for now.)
So, now all those formulas have calculated the area of the ball. A lot of work just to find the area, I know. Confusing.
Now, just like the batsman has values for each area of the pitch, there are also DEFAULT values for each area of the pitch for the bowler. These do not change. Wherever the ball lands is given the default value for that amount of the pitch. So, if it lands on off stump, the default value for a bowler is 5. Now, simply, we multiply the bowler's skill by the value.
His skill is 68.5. 68.5 * 5 = 342.5
FINAL BOWLBALL = 342.5
Very, very, very confusing and long-winded. Sorry to bore you through all this.
Now, the final 'bowlball' variable is set at 342.5, just for the first ball. This ball has landed in the off stump area.
Time to work out the batsman's now - this is a bit quicker.
First of all, a random number between 4 and 25 is generated. When it is found, it is divided by 10. (Effectively a random number between 0.4 and 2.5)
Now, I mentioned that the batsman has values for each area of how well he plays a ball in that area. Since the ball has landed on off stump, Ponting's value is 4 in that area.
Now, we multiply 4 by the random number (after it has been divided by 10.) In this example, the random number is 1.8.
4 * 1.8 = 7.2.
So, the final BATBALL value is 7.2.
Now, we simply divide BOWLBALL by BATBALL to get FINALBALL.
The higher the FINALBALL = the better the ball. If the finalball is higher than a certain number which I will determine later, then that will mean the batsman is out. If it is say lower than 20, the batsman will score 4 runs. Rough example.
Anyway.. very confusing. Hope you took all that in..
Still..
THE PROBLEM
I have shown the code underneath. Refer to that if you need.
Right now, I haven't bothered making it display how many runs scored or whatever. I JUST want it to say what the FINALBALL variable was for that ball, just so I know how many runs were scored.
The problem? It displays this:
The final area was -1.#IND
Press any key to continue . . .
If this worked, it should say the final area was 50, or 80, or something. I don't know why this is happening though and I am frustrated.
Anyway, this has been a long post - I will be very surprised if anyone can actually help.
If anyone feels up to helping me to work out why it is not displaying properly..
I would be extremely grateful.
Definitely rep and a small vCash donation.
So, please.. pimp my program.
Anyway, I'm fairly new to C++. Been learning it a little bit from some online tutorials for a while now - although nothing consistent. I decided it was about time that I had a crack at trying to whip up a quick cricket simulator.
Now, for a C++ beginner, making a simulator is no easy feat. I'm struggling a lot and need some help with my code.
I thought it might have a chance to work - but when I run it, it doesn't seem to work.
So, let me run through the program for you.
Basically, there is no teams, or any players with the exception of Ponting and Flintoff. I have just tried to create a 1 v 1 type scenario with Flintoff bowling to Ponting each ball and then an amount of runs being determined.
Here is my algorithm to determine the amount of runs for each ball:
Batsman is given fixed values for each area. This represents their ability to play a shot in that location. These are the areas: wide outside off, outside off, off stump, middle stump, leg stump and outside leg. There is no other stats for the batsmen except for these. There is a rough number for each. Ricky Ponting's ability wide outside off is 6.125, his ability outside off is 4.1875, off stump is 4, straight is 4.875, leg stump is 6.875 and outside leg stump is 8.125.
These are the only batting values required.
For the bowlers, they have two values. Consistency and skill. Flintoff's consistency is 67.6, and his skill is 68.5. For now, I ruled them like this:
Skill = 100 - FC Bowling Average
Consistency = 100 - FC Bowling Strike Rate / 2
Here is how each ball is determined (a tricky process).
A random number between 1-50 is generated for the bowler. In my program, the variable for this is 'randcons'.
The bowler's consistency is multiplied by 1.66. Then, the random number just generated between 1-50 is added to the consistency * 1.66.
For example:
Flintoff's consistency = 67.6
Random number = 30
67.6 * 1.66 = 112.2
112.2 + 30 = 142.2
142.2 = 142
Final number = 142
This final number is assigned to the variable 'bowlnum'.
Now, we use the bowlnum to find out which area the ball has landed in. The ideal area is off stump, and then outside off is the next best area to bowl in.
Here's an example for this - if the 'bowlnum' is less than 100, the ball is delivered outside leg. If it is between 101-110. it is leg stump. (This might need to be a little more random, because this means it is impossible for Flintoff to bowl on leg stump or outside leg because his consistency is too high. Still, I'll leave it for now.)
So, now all those formulas have calculated the area of the ball. A lot of work just to find the area, I know. Confusing.
Now, just like the batsman has values for each area of the pitch, there are also DEFAULT values for each area of the pitch for the bowler. These do not change. Wherever the ball lands is given the default value for that amount of the pitch. So, if it lands on off stump, the default value for a bowler is 5. Now, simply, we multiply the bowler's skill by the value.
His skill is 68.5. 68.5 * 5 = 342.5
FINAL BOWLBALL = 342.5
Very, very, very confusing and long-winded. Sorry to bore you through all this.
Now, the final 'bowlball' variable is set at 342.5, just for the first ball. This ball has landed in the off stump area.
Time to work out the batsman's now - this is a bit quicker.
First of all, a random number between 4 and 25 is generated. When it is found, it is divided by 10. (Effectively a random number between 0.4 and 2.5)
Now, I mentioned that the batsman has values for each area of how well he plays a ball in that area. Since the ball has landed on off stump, Ponting's value is 4 in that area.
Now, we multiply 4 by the random number (after it has been divided by 10.) In this example, the random number is 1.8.
4 * 1.8 = 7.2.
So, the final BATBALL value is 7.2.
Now, we simply divide BOWLBALL by BATBALL to get FINALBALL.
The higher the FINALBALL = the better the ball. If the finalball is higher than a certain number which I will determine later, then that will mean the batsman is out. If it is say lower than 20, the batsman will score 4 runs. Rough example.
Anyway.. very confusing. Hope you took all that in..
Still..
THE PROBLEM
I have shown the code underneath. Refer to that if you need.
Right now, I haven't bothered making it display how many runs scored or whatever. I JUST want it to say what the FINALBALL variable was for that ball, just so I know how many runs were scored.
The problem? It displays this:
The final area was -1.#IND
Press any key to continue . . .
If this worked, it should say the final area was 50, or 80, or something. I don't know why this is happening though and I am frustrated.
Anyway, this has been a long post - I will be very surprised if anyone can actually help.
If anyone feels up to helping me to work out why it is not displaying properly..
I would be extremely grateful.
Definitely rep and a small vCash donation.
So, please.. pimp my program.

Code:
//Cricket Game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Functions
void match();
void ball();
void blank();
//Players
/*
Value 0-5 = Batting Area
Value 6 = Consistency
Value 7 = Skill
*/
double ponting0 = 6.125;
double ponting1 = 4.1875;
double ponting2 = 4;
double ponting3 = 4.875;
double ponting4 = 6.875;
double ponting5 = 8.125;
double ponting6 = 51;
double ponting7 = 45;
double flintoff0 = 3.75;
double flintoff1 = 2.5;
double flintoff2 = 2.5;
double flintoff3 = 2.75;
double flintoff4 = 4;
double flintoff5 = 5;
double flintoff6 = 67.6;
double flintoff7 = 68.5;
//Opening Dialog
int main ()
{
cout << "Welcome to Blake's Cricket. \n"
<< "This is Blake's first cricket related project. \n"
<< "First off we will have Ricky Ponting vs Andrew Flintoff. \n\n"
<< "Australia's Batsman: Ricky Ponting \n"
<< "England's Bowler: Andrew Flintoff \n\n";
system("PAUSE");
match();
}
//Go to the match
void match()
{
system("CLS");
cout << "Welcome to the one vs one battle (Ponting vs Flintoff)\n\n";
system("PAUSE");
ball();
}
//Function for every ball
void ball()
{
system("CLS");
double bowlnum;
double bowlarea;
double batarea;
double bowlball;
double finalball;
double batnum;
int cntballs;
cntballs ++ ;
if(cntballs == 6, cntovers++, cntballs = 0);
srand(time(0));
int randcons = rand() % 50 + 1;
int randbat = rand() % 25 + 4;
bowlnum = (flintoff6 * 1.66) + randcons;
batnum = randbat / 10;
//Outside Leg
if(bowlnum <= 100){
bowlarea = 0.75;
batarea = ponting5;
}
//Leg Stump
else if (bowlnum <= 110 && bowlnum >= 101){
bowlarea = 2.125;
batarea = ponting4;
}
//Wide Off
else if (bowlnum <= 117 && bowlnum >= 111){
bowlarea = 2.875;
batarea = ponting0;
}
//Straight
else if (bowlnum <= 125 && bowlnum >= 118){
bowlarea = 4.125;
batarea = ponting3;
}
//Outside Off
else if (bowlnum <= 140 && bowlnum >= 131){
bowlarea = 4.875;
batarea = ponting1;
}
//Off Stump
else if (bowlnum <= 150 && bowlnum >= 141){
bowlarea = 5;
batarea = ponting2;
}
bowlball = flintoff7 * bowlarea;
finalball = batarea / bowlball;
cout << "The final area was " << finalball;
system("PAUSE");
blank();
}
void blank()
{
system("CLS");
ball();
}