help with creating a loop

I'm writing a dice game where each player has chips. The game is over when only one player has chips left. The number of players is 3 or more. I've got the majority of each turn figured out, but I'm having a hard time figuring out what type of loop to use and how to set it up. Also, I have a "current" integer variable that needs to keep track of which player's turn it is so it can be passed into certain functions. I thought maybe the best way to do the loop would to create a boolean function hasWon() and find a way for this to return true when only one person has chips left. Then put this as the condition for a while loop? And then maybe at the end of the turn loop write some code that will add to "current" unless it is equal to the length of the players array, in which case it will set it back to 0. Does this sound like a good idea, and if so, how would I write the hasWon() function?

Below is my turn function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void GameSetup::playerTurn(Player players[],int current)
{
	std::cout << "It is " << players[current].name << "'s turn." << std::endl;

	if (players[current].checkChips() == 0)
	{
		std::cout << "You have no chips, your turn is skipped!" << std::endl;
		return;
	}
	else
	{
		std::cout << "You have " << players[current].checkChips() << " chips." << std::endl;

		switch (Dice::rollDice())
		{
		case 1:
		{
			players[current].subtractChip();
			players[current - 1].addChip();
			std::cout << "You rolled an L. You pass one chip to " << players[current - 1].name << std::endl;
			break;
		}
		case 2:
		{
			players[current].subtractChip();
			std::cout << "Your rolled a C. You pass one chip into the center pile." << std::endl;
			break;
		}
		case 3:
		{
			players[current].subtractChip();
			players[current + 1].addChip();
			std::cout << "You rolled a R. You pass one chip to " << players[current + 1].name << std::endl;
			break;
		}
		default: std::cout << "You rolled a dot. You get to keep all your chips this time!" << std::endl;
		}
	}
	std::cout << "Your turn is over." << std::endl;
}
If you want to make a variable increment up to a certain maximum and then start at 0 again you may want to have a look at the modulo operator (%). This gives you the remainder after a division, as a result current in the example below would be 0,1,2,3,4,0,1,2,3,4,0,1 and so on.
1
2
3
4
5
6
int current = 0;
int someMaximum = 5;
while (true)
{
       current= ( current+1 ) % someMaximum;
}


I think your hasWon function is a good idea, I would go for a do-while-loop in this case.
1
2
3
4
5
6
7
int current = 0;
do
{
       playerTurn(players, current);
       current= ( current+1 ) % THE_NUMBER_OF_PLAYERS;
}
while (hasWon() == false);


In this case, the hasWon function should check every position of the players array and return false as soon as the second non-zero value is found, or true if only one non-zero value exists.

Hope that helps a bit.
Last edited on
Topic archived. No new replies allowed.