So I’m having some trouble with a programming assignment about players sitting around a table. The program should be able to add players after the player who just took a turn. The assignment is supposed to show us how to add data into a linked list anywhere. So my problem occurs when I use a PLAY command. This should allow a single player to take a turn.
For example, if there are players A, B, and C, and the PLAY command is executed it will display to the console “Player A Takes a turn”. If PLAY is executed again, it will display “Player B takes a turn.”
My code allows the first player in the list to play, but does not move to the next node/player.
void CircleList::play()
{
LinkedListOfPlayersNode *p=(*pFront).pNext;
if (p->pData!=NULL)
{
cout<<p->pData->getName()+" takes a turn\n";
p-> pNext; //My attempt to move to the next node.
}
else
{
cout<<"There are no players. Please ADD a player.\n";
}
}
So this obviously does not work. Can someone explain to me how I would move to the next player?
PS – code is in C++
You’ll need a member in your class that retains the last player that took a turn.
initially, this is set to
pFrontand you move it to the next player every timeplayis called.