在回合制遊戲中,我想找到上一位玩家。 要查找下一個球員,我只需鍵入:如何寫這段代碼更乾淨
int lastPlayer = match.currentPlayer - 1;
的問題是當currentPlayer是播放機1。lastPlayer變爲0,這是不對的。它應該是player6。
爲了解決這個問題,我可以這樣做:
int lastPlayer = match.currentPlayer - 1;
if (lastPlayer == 0)
lastPlayer = match.numberOfPlayers;
我的問題是如何在一個更清潔的方式寫這篇文章。我知道遊戲中心,基於代碼做類似:
(currentIndex + 1) % match.participants.count];
我該如何重寫我的代碼做同樣的事情?
在此先感謝
確實從零開始計數? – 2012-08-15 09:05:06
該球員來自1 - x – BlackMouse 2012-08-15 09:08:43
可能更好的問這個http://codereview.stackexchange.com/ – Abizern 2012-08-15 09:28:03