如何使用旋转playOrder实现takeTurn?

时间:2011-08-29 04:01:27

标签: java

这个takeTurn方法如何与playOrder嵌套以实现以下玩家/回合组合?

{
 ...
 for(int i=0;i<NUM_PLAYERS;i++)
{
players[i].takeTurn();

}    ....

    playOrder

2Players
turn1 Player1 Player2 Player1 player2 turn2 Player2 Player1 Player2 Player1

3Players turn1 Player1 Player2 Player3 player1 turn2 Player2 Player3 Player1 player2 turn3 Player3 Player1 Player2 player3

4Players turn1 Player1 Player2 Player3 player4 turn2 Player2 Player3 Player4 player1 turn3 Player3 Player4 Player1 player2 turn4 Player4 Player1 Player2 player3

3 个答案:

答案 0 :(得分:1)

for(int i=0;i<NUM_PLAYERS;i++) {
  for(int playOrder = 0; playOrder < 4; playOrder++) {
    players[(i + playOrder) % NUM_PLAYERS].takeTurn();
  }
}

..或者

for(int i=0;i<NUM_PLAYERS;i++) {
  players[(i + turnNumber) % NUM_PLAYERS].takeTurn();
}

答案 1 :(得分:0)

你可以在C ++中取一个队列,这是一个可以从两端推送和弹出的队列,

dequeue<int> turns;

然后按顺序推送四名球员

for( int i=0; i<4; ++i )
  turns.push_back( i%NUM_PLAYERS + 1 );

while( take_turns() )
{
     print("Turn\n");
     print_dequeue();
     turns.push_back(turns.front());
     turns.pop_front();
}

答案 2 :(得分:0)

如果你想要真正光滑,你可以使用Java列表(ArrayList工作)来轻松地做到这一点

(假设您的班级名为“玩家”,并且您将所有内容保存在名为“玩家”的列表中)

while(continueTurns()) {

   for(Player p : players) {
      p.takeTurn();
   }

   players.add(players.remove(0));

}