0

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.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Tom
  • 63
  • 8
  • Possible duplicate of [Use a 'goto' in a switch?](https://stackoverflow.com/questions/4756084/use-a-goto-in-a-switch) – Tecnologer Jan 22 '19 at 01:41
  • I feel like you would have found this through debugging if it were the case, but wouldn't you want to check if player 3 (and not 2) is bankrupt if you're deciding what player comes after 2? – Phil M Jan 22 '19 at 01:53
  • I feel like maybe a redesign is in order. Make a Player class (with their cash amount, flag for bankrupt, etc), make a List of active players, and when a player goes bankrupt, remove them from the List. Then nextId is always just `(currentID+1) % listSize`. – Phil M Jan 22 '19 at 02:00
  • The only problem is the whole project is for a uni assignment. We haven't covered classes or anything related to OOP, and so they expect us to not use them for the project, hence why I'm doing it this way even if it's not exactly the most efficient. Thanks anyway though – Tom Jan 22 '19 at 02:03

1 Answers1

1
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.

Phil M
  • 1,619
  • 1
  • 8
  • 10