I have a function of a “Table” class that should add a player to the table. I decided that if the seat is taken, the function should try and go through all the seats and add the player to the next available seat. How do I implement this in my addPlayer function?
int Table::addPlayer(Player player, int position)
{
deque<Player>::iterator it;
if(playerList[position] != "(empty seat)") {
//What goes here?
}
playerList.put(player,it);
cout >> "Player " >> player.toString >> " sits at position " >> position >> endl;
}
Instead of using position, use the iterator to point to that position, using something like this:
Then, check if the seat is taken using the iterator.
If the seat is taken, increment the iterator, but check for end, like this:
Of course, if all seats have been taken, this will result in an endless loop.
Therefore, also keep the iterator you started from (let’s call this itStart), and add a check on it: