Reputation: 83
so I'm trying to create a monopoly game in C#. The game works for the most part however I'm trying to deal with players leaving the game once they go bankrupt and it's not working as expected. I'm trying to use a goto in order to proceed to the next player if the current one is bankrupt, although it doesn't seem to be going to the correct case.
private void changeActivePlayer()
{
switch (activePlayerID)
{
case 1:
if (player1Bankrupt == true)
{
goto case 2;
}
else
{
activePlayerID = 2;
break;
}
case 2:
if (player2Bankrupt == true)
{
goto case 3;
}
else
{
activePlayerID = 3;
break;
}
case 3:
if (player3Bankrupt == true)
{
goto case 4;
}
else
{
activePlayerID = 4;
break;
}
case 4:
if (player4Bankrupt == true)
{
goto case 1;
}
else
{
activePlayerID = 1;
break;
}
}
}
I've tried testing my code by immediately making two players bankrupt upon starting the game. Player 1 and 2 can take their turns normally. However once it gets to player 3's turn, rather than following through the switch statement to case 4 and then to case 1 again, it is instead allowing player 3 to still take their turn and once it's over it reverts back to player 2's turn and creates and endless cycle. I understand this is probably not the best way to go about this but any help resolving this issue would be much appreciated as I cannot work out what's wrong. Thanks.
Upvotes: 0
Views: 60
Reputation: 1619
case 2:
if (player2Bankrupt == true)
{
goto case 3;
}
else
{
activePlayerID = 3;
break;
}
You go into this switch as player 2, and check if player 2 is bankrupt, and then decide who follows based on that.
But what you want is to check if player 3 is bankrupt, and then decide based on that.
case 2:
if (player3Bankrupt == true)
{
goto case 3;
}
else
{
activePlayerID = 3;
break;
}
And similar fixes for the other cases.
Upvotes: 1