Hi. I am making a craps game "simulator" and i have to keep statistics on win/lose and all kinds of other things. There is NO input at all the game only simulates 1000 games of craps and keeps stats on it. Including.
1. How many games were won on the first roll? Second roll? etc.... after more than 20 rolls.
2. How many games were lost on the first roll? "... ... "
I have this method below that takes care of these two statistics and i was wondering if its an acceptable way to do this. The comments on the method should explain enough.
I tried not to use Global Arrays but then I was passing the arrays through 2 functions just to keep them accessable. The approach itself is a little odd but it works fine and I believe it makes sense once you can understand how it is working. Ill provide my "gameplay" prototypes as well. Another note, I can not be using Classes or I would have. The chapter this is about arrays/vectors and thats what I should practice with this program.
/* GAMEPLAY FUNCTIONS */
// Below is the method. NOTE: This method is ONLY called
// when a player is determined to have won/lost. This is also
// the only method that modifys the arrays.
* updateStats will modify the Global Arrays "gamesWon and gamesLost"
* These two arrays store how many games were won/lost and on what
* roll they were won/lost.
* Index 1 might hold 5. This would mean 5 games were won/lost on the 1st roll.
* Index 20 might hold 2. This would mean 2 games were won/lost on the 20th roll.
* Index 0 will hold games that were won/lost using more than 20 rolls.
* Perameters: int rolls, indicates how many rolls it took to finish the game.
* bool status, indicates a win or lose. true=win false=lose.
void updateStats(int rolls, bool status)
/* Checking if it took more than 20 rolls to win/lose.
* This is stored in Index 0.*/
if(rolls > 20)
/* If the Game was a win
* Increment the win array, with the winning roll # */
/* If the Game was a lose
* Increment the lose array, with the loosing roll # */
My instructor really has something against global arrays/variables and i understand why there could be an issue. But i cant seem to get around them for this program without, completly redesigning. The Gameplay works fine but introducing the "stat keeping" is a pain. Again my question was whether using this method is a good idea for keeping track of the two statistics posted at the top. Thanks
Last edited by g.eckert; March 24th, 2009 at 01:09 PM.
Yes i thought about using a struct to group the statistical data but that is basically the same as a class. We could, use classes for this project but we shouldnt. Because the chapter being covered is on arrays/vectors and we have not learned about classes/structs yet. I was using a function with static arrays/variables to keep track of the stats. The static "variables" method worked great to just store the information but it gave me all kinds of issues when i needed to acces it at the end to print. I think i am going to go back and try that again.
void stats(int rolls, bool status)
static int gamesWon;
static int gamesLost;
static int totalGames=0;
//Store all the statistical information.
You should not use classes or structs since you have not officially learnt to use them.
You should not use global variables because of the usual reasons, and also because your instructor frowns upon them.
You are having problems using local static arrays, and that does not seem like a good design here anyway.
I think that you should just pass the arrays around; they will be converted to pointers to their respective first elements. It seems like you only need two arrays anyway, so it is not much of a problem.
Your right, its not a huge problem i just found it very ugly. Because I would end up passing the arrays through 2 functions.
From main() --> play() --> shootersPoint();
I just re-read the assignment again and it does explicitly state " Create Functions to keep track of game statistics". So passing arrays around might not fill that req. I also wanted to try to keep the gameplay and the statistical parts seperate. I have 6 days to find a good solution so I have plenty of time to figure it out.