This site requires JavaScript, please enable it in your browser!
Greenfoot back
Windeycastle
Windeycastle wrote ...

2021/10/19

Weird switch behaviour

Windeycastle Windeycastle

2021/10/19

#
In my game you cannot win, it is always a draw, according to the program. The code seems perfectly fine. Anyone here that can spot what I am doing wrong?
private void gameOver(int winner)
    {
        switch (winner)
        {
            case 1 : 
                Menu menu1 = new Menu();
                menu1.setImage("Crosses wins.png");
                getWorld().addObject(menu1,1,1);
            case -1 : 
                Menu menu2 = new Menu();
                menu2.setImage("Noughts wins.png");
                getWorld().addObject(menu2,1,1);
            case 0 : 
                Menu menu3 = new Menu();
                menu3.setImage("Draw.png");
                getWorld().addObject(menu3,1,1);
        }
        while(!Greenfoot.isKeyDown("enter"))
            wait(5);
        Greenfoot.setWorld(new MyWorld());
        wait(20);
    }
I checked and the winner argument is correctly called.
Windeycastle Windeycastle

2021/10/19

#
I saw I missed some {} action for the case thingies, but adding those did not solve it?
Spock47 Spock47

2021/10/20

#
The switch statement follows the fall-through principle. That means that after entering a case, the program will execute all statements until a "break" is reached. Especially, it will not automatically break at the end of a case block. In your source code: For example with (winner == -1), the three lines to setup menu2 will be executed, but after that the switch does not end, instead the three lines to setup menu3 will be executed, too. As a result, although the "Noughts" menu is actually in the world, it is covered by the "Draw" menu, which is added "in front" of the "Noughts" menu. If you don't want a case to fall-through to the next case, you can use a "break;" statement. This means that the program will immediately jump to the end of the switch, when reaching the break statement:
    private void gameOver(int winner)
    {
        switch (winner)
        {
            case 1 : 
                Menu menu1 = new Menu();
                menu1.setImage("Crosses wins.png");
                getWorld().addObject(menu1,1,1);
                break;
            case -1 : 
                Menu menu2 = new Menu();
                menu2.setImage("Noughts wins.png");
                getWorld().addObject(menu2,1,1);
                break;
            case 0 : 
                Menu menu3 = new Menu();
                menu3.setImage("Draw.png");
                getWorld().addObject(menu3,1,1);
                break;
        }
        while(!Greenfoot.isKeyDown("enter"))
        {
            wait(5);
        }
        Greenfoot.setWorld(new MyWorld());
        wait(20);
    }
This should do the trick. Note, that the break after the last case is functionally not necessary. I only added it to conform to the coding style rule "what behaves similar, should look similar" - basically, since the other two cases got a break, the third one gets one, too. Live long and prosper, Spock47
Windeycastle Windeycastle

2021/10/20

#
Aha! That's what I forgot! Thank you very much for the clear explanation!
You need to login to post a reply.