开发者

How to implement takeTurn with a rotating playOrder?

开发者 https://www.devze.com 2023-03-31 22:44 出处:网络
How could this takeTurn method be nested with a playOrder to achieve the following player/turn combinations?

How could this takeTurn method be nested with a playOrder to achieve the following player/turn combinations?

{
 ...
 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 Player开发者_StackOverflow社区4 player1 turn3 Player3 Player4 Player1 player2 turn4 Player4 Player1 Player2 player3


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

.. or perhaps

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


you can take a dequeue in C++ which is a queue where you can push and pop from both the ends,

dequeue<int> turns;

then push your sequence for four players

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();
}


If you want to be really slick you can use a Java list(ArrayList works) to do this really easily

(Assuming your class is called Player, and you have everything saved in a list called players)

while(continueTurns()) {

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

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消