# Using Global Arrays to keep stats on a game.

• March 24th, 2009, 01:05 PM
g.eckert
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.

Code:

```/* GAMEPLAY FUNCTIONS */ void play(); void shootersPoint(int); int rollDice(); bool firstRollWin(int); bool firstRollLose(int); // 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.  * EXAMPLE.  *    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)         rolls=0;     /* If the Game was a win     *  Increment the win array, with the winning roll # */     if(status)         gamesWon[rolls]++;     /* If the Game was a lose     *  Increment the lose array, with the loosing roll # */     else         gamesLost[rolls]++; }```
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
• March 24th, 2009, 08:09 PM
Codeplug
Re: Using Global Arrays to keep stats on a game.
Not being able to use classes is strange....but you can simulate it. You're going to have to package up your globals and send em along as paramters - there's no way around it with free functions.
Code:

```struct GameStats {     size_t gamesWon[N];     size_t gamesLost[N]; };//GameStats void updateStats(int rolls, bool status, GameStats &stats) { ...     if (status)         stats.gamesWon[rolls]++;     else         stats.gamesLost[rolls]++; }```
gg
• March 24th, 2009, 09:17 PM
g.eckert
Re: Using Global Arrays to keep stats on a game.
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.

Code:

``` void stats(int rolls, bool status) {     static int gamesWon[21];     static int gamesLost[21];     static int totalGames=0;     totalGames++;     //Store all the statistical information. }```
• March 25th, 2009, 01:11 AM
laserlight
Re: Using Global Arrays to keep stats on a game.
Let's see:
• 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.
• March 25th, 2009, 06:35 AM
g.eckert
Re: Using Global Arrays to keep stats on a game.
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.

thanks
Gerald
• March 25th, 2009, 09:39 AM
laserlight
Re: Using Global Arrays to keep stats on a game.
Quote:

Originally Posted by g.eckert
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();

That is normal. The ugly part is not being able to use proper abstractions with structs and classes.

Quote:

Originally Posted by g.eckert
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.

In my opinion, that sentence does not forbid passing arrays around.