# number of players

• October 21st, 2013, 02:03 PM
dthunderchunky
number of players
i am writing a game that is 2 to 4 players. i am wondering how to ask how many players, and make the score stay with each player. it is a rip off of zombie dice, this is what i have so far the playerScore array is to hold the score for each player and the turn array is hold hold the values that they rolled that turn, i also dont know how to clear out the turn array when it goes to the next player.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main()
{
int playerScore[4];
int turn[3];

return 0;
}

void roll()
{
srand (time(0));
int s;

for (int x=1,x<4, x++)
{
s = rand()%13;

if (s<6)
{
greenDice;
}
else if(s>5 && s<9)
{
yellowDice;
}
else if(s>8)
{
redDice;
}
}
}
void greenDice()
{
srand (time(0));
int g;

g = rand()%6;

if (g<3)
{
fish;
}
else if(g>2 && g<5)
{
hook;
}
else if(g>4)
{
boot;
}
}
void yellowDice()
{
srand (time(0));
int y;

y = rand()%6;

if (y<2)
{
fish;
}
else if(y>1 && y<4)
{
hook;
}
else if(y>3)
{
boot;
}
}
void redDice()
{
srand (time(0));
int r;

r = rand()%6;

if (r<1)
{
fish;
}
else if(r>0 && r<3)
{
hook;
}
else if(r>2)
{
boot;
}
}
int fish()
{
cout<< "You rolled a fish";
++ turn[1];
}
int hook()
{
cout << "You rolled a hook";
++ turn[2];
}
int boot()
{
cout <<"You rolled a boot.";
++ turn[3];
}
• October 21st, 2013, 02:37 PM
2kaud
Re: number of players
• October 21st, 2013, 02:57 PM
GCDEF
Re: number of players
As 2kaud said, you'll get more help when your code is properly formatted and readable.

Just at a quick glance, this is not how you call a function.
boot;

Also, you should only call srand once in your program.
• October 21st, 2013, 03:00 PM
Paul McKenzie
Re: number of players
Quote:

Originally Posted by dthunderchunky
and make the score stay with each player.

The quick suggestion to you is to learn what a struct or class is before going further.

If you want to keep like items together, then the facility to do that is to use struct/class. Right now, your code has variables for different things, but the issue is that all these variables are basically "loose" -- there is no cohesion between them. A struct allows you to group several items under one aggregation.
Code:

```struct PlayerInfo {     std::string playerName;     int playerScore;     //... };```
So for example, in the code above, I have a PlayerInfo struct that holds both the player's name and the player's score.

Regards,

Paul McKenzie