0

I am working on programming a small game in c++, but I have a problem with how to give the role to the players. For example, I have four players. The role will start in the first player, and when it reaches the fourth player, the role must return to the first player. Also, in other stages of the game, the role can stop at the third player and then return the role to the second, and so on. Like you are in a circular ring. Is there a way to write the code? I try with this way, but it is not well effective

int i=0;
void AnzahlderPlayers()
    {
    
    if (i < 3)
        {
            i++;
            


        }
        else {
            i = 0;
        }
        
    }
Ahmad PS
  • 3
  • 1
  • Can you be a bit more specific about what your code is trying to do? what is the expected result of```AnzahlderPlayers()``` and how is that different than what you are getting? – W-B Jul 27 '20 at 22:19

1 Answers1

0

I think what you are looking for is a way to reset i to 0 when it reaches four

The way to do that is:

int i = 0;
void AnzahlderPlayers() {
   i = (i+1)%4;
}


W-B
  • 850
  • 5
  • 16
  • thanx bro that what I need ,but if I have more the 4 players ,for example n player ,do you think that work like that ''' i = (i+1)%n; – Ahmad PS Jul 29 '20 at 01:29
  • @AhmadPS Yep! `i = (i+1)%n` would work. [Here is more on modulo(%) operator](https://www.geeksforgeeks.org/modulo-operator-in-c-cpp-with-examples/) – W-B Jul 29 '20 at 02:49