March 24th, 2009, 01:05 PM
Using Global Arrays to keep stats on a game.
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.
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
/* 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 # */
Last edited by g.eckert; March 24th, 2009 at 01:09 PM.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.