In a turn-based game, I’d like to find the previous player.
To find the next player, I can just type:
int lastPlayer = match.currentPlayer - 1;
The problem is when the currentPlayer is player 1. Then lastPlayer becomes 0, which is wrong. It should be player6.
To fix this, I can do:
int lastPlayer = match.currentPlayer - 1;
if (lastPlayer == 0)
lastPlayer = match.numberOfPlayers;
My question is how to write this in a cleaner way. I know game center, turn based code do something like:
(currentIndex + 1) % match.participants.count];
How can I rewrite my code to do the same?
Thanks in advance
How about that?