-4

I've been working on a game, which requires placing armies on countries. Placing the armies and all that works, but for some reason I can't break out of the switch statement. When I try to break, it jumps up to random lines in the code and I end up with a strange output, which makes no sense and still not exited from the function/switch statement. I'm relatively new to Java so is there something I'm missing in regards to how switch statements work? I only want to break out of the last case. Many thanks.

public void terrPlacement(Card[] deck, gpp gp, UI ui, Player player1, Player player2, Player neutral1, Player neutral2, Player neutral3, Player neutral4, Board board, ArrayList p1Terr, ArrayList p2Terr, ArrayList n1Terr, ArrayList n2Terr, ArrayList n3Terr, ArrayList n4Terr) { 
    String locationEntered;
    int playerId =0, countryId=0;
    switch(roll) {

    case "player1":
        while (player1.armyCount>0) {
            ui.displayString("\nEnter the name of the territory you wish to place units on\n");
            locationEntered=ui.getCommand();
            ui.displayString("> " + locationEntered);

            if (p1Terr.contains(locationEntered)) {                     // Compares what was entered to array list of country names
                for (int r=0; r<42; r++) {                              // then gets countryId of that country
                     if (locationEntered.equals(deck[r].cardName())){
                         countryId=deck[r].countryId;
                     }
                }
                board.addUnits(countryId, playerId, 3);                 // uses countryId to place units on country entered above
                player1.armyCount-=3;
                ui.displayString("Your new army count is: " + player1.getArmyCount());      // if army count isn't 0 it continues in loop of adding more units
                ui.displayMap();
                terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr);           
            }
            else if (!p2Terr.contains(locationEntered)) {
                ui.displayString("You either do not own this territory or "     // if territory name isnt one they own it throws an error
                        + "you spelt the territory name incorrectly, please try again");
            }
        }

        if (player2.armyCount!=0) {                     // when army count is 0 it moves on to player 2, but only if player two hasn't already gone
            roll="player2";
            ui.displayString("\nPlayer 2 place your army units");
            terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr); 
        }
        else if (player2.armyCount==0){                 // if player two has already gone it moves onto neutral 1
            roll="neutral1";
            ui.displayString("\nPlease place the armies units for neutral 1 now");
            terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr); 
        }


    case "player2": 
                                            // same principle as above case
        while (player2.armyCount>0) {
            ui.displayString("\nEnter the name of the territory you wish to place units on");
            locationEntered=ui.getCommand();
            ui.displayString("> " + locationEntered);
            playerId=1;
            countryId=0;
            roll="player2";

            if (p2Terr.contains(locationEntered)) {
                for (int r=0; r<42; r++) {  
                     if (locationEntered.equals(deck[r].cardName())){
                         countryId=deck[r].countryId;
                     }
                }
                board.addUnits(countryId, playerId, 3);
                player2.armyCount-=3;               
                ui.displayString("Your new army count is: " + player2.getArmyCount());
                ui.displayMap();

            }
            else if (!p2Terr.contains(locationEntered)) {
                ui.displayString("You either do not own this territory or "
                        + "you spelt the territory name incorrectly, please try again");    
            }
        }
        ui.displayString("You're out of armies!");
        if (player1.armyCount!=0) {
            roll="player1";
            ui.displayString("\nPlayer 1 place your army units");
            terrPlacement(deck, gp, ui, player1, player2, neutral1, neutral2, neutral3, neutral4, board, p1Terr, p2Terr, n1Terr, n2Terr, n3Terr, n4Terr); 
        }

    case "neutral1":
        while (neutral1.armyCount>0) {
            ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
            locationEntered=ui.getCommand();
            ui.displayString("> " + locationEntered);
            playerId=2;
            countryId=0;
            if (n1Terr.contains(locationEntered)) {
                for (int r=0; r<42; r++) {  
                     if (locationEntered.equals(deck[r].cardName())){
                         countryId=deck[r].countryId;
                     }
                }
                board.addUnits(countryId, playerId, 1);
                neutral1.armyCount--;
                ui.displayString("Your new army count is: " + neutral1.getArmyCount());
                ui.displayMap();
            }

            else if (!n1Terr.contains(locationEntered)) {
                ui.displayString("You either do not own this territory or "
                        + "you spelt the territory name incorrectly, please try again");
            }
        }
        ui.displayString("\nNeutral 1 is out of armies");


    case "neutral2":        
        while (neutral2.armyCount>0) {
            ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
            locationEntered=ui.getCommand();
            ui.displayString("> " + locationEntered);
            playerId=3;
            countryId=0;
            if (n2Terr.contains(locationEntered)) {
                for (int r=0; r<42; r++) {  
                     if (locationEntered.equals(deck[r].cardName())){
                         countryId=deck[r].countryId;
                     }
                }
                board.addUnits(countryId, playerId, 1);
                neutral2.armyCount--;
                ui.displayString("Your new army count is: " + neutral2.getArmyCount());
                ui.displayMap();
            }

            else if (!n2Terr.contains(locationEntered)) {
                ui.displayString("You either do not own this territory or "
                        + "you spelt the territory name incorrectly, please try again");
            }
        }
        ui.displayString("\nNeutral 2 is out of armies");


    case "neutral3":

        while (neutral3.armyCount>0) {
            ui.displayString("\nEnter the name of the territory you wish to place a unit on\n");
            locationEntered=ui.getCommand();
            ui.displayString("> " + locationEntered);
            playerId=4;
            countryId=0;
            if (n3Terr.contains(locationEntered)) {
                for (int r=0; r<42; r++) {  
                     if (locationEntered.equals(deck[r].cardName())){
                         countryId=deck[r].countryId;
                     }
                }
                board.addUnits(countryId, playerId, 1);
                neutral3.armyCount--;
                ui.displayString("Your new army count is: " + neutral3.getArmyCount());
                ui.displayMap();
            }

            else if (!n3Terr.contains(locationEntered)) {
                ui.displayString("You either do not own this territory or "
                        + "you spelt the territory name incorrectly, please try again");
            }
        }
        ui.displayString("\nNeutral 3 is out of armies");
    }
    break;
}

Output:

    Welcome to Error 420's Risk!

    Enter the name of Player 1
    > Rob
    Enter the name of Player 2
    > Peter

    Player 1's territory cards are:
        Great Britain
        W United States
        Quebec
        Ural
        Middle East
        Brazil
        W Europe
        Ontario
        Central America

    Player 2's territory cards are:
        W Australia
        Egypt
        S Africa
        Alaska
        Japan
        New Guinea
        Afghanistan
        NW Territory
        Siam

    Neutral 1's territory cards are:
        N Europe
        India
        Scandinavia
        Irkutsk
        Madagascar
        Ukraine

    Neutral 2's territory cards are:
        Greenland
        Indonesia
        Argentina
        Mongolia
        Siberia
        Kamchatka

    Neutral 3's territory cards are:
        Peru
        Alberta
        Iceland
        E Australia
        E United States
        E Africa

    Neutral 4's territory cards are:
        Yakutsk
        Congo
        S Europe
        Venezuela
        China
        N Africa

    All initial territory cards have been drawn.
    Players will now place their armies...

    Peter will reinforce their territories first, as they rolled a 11 and Rob only rolled a 9

    Enter the name of the territory you wish to place units on
    > Japan
    Your new army count is: 0
    You're out of armies!

    Player 1 place your army units

    Enter the name of the territory you wish to place units on

    > Ural
    Your new army count is: 0
    You're out of armies!

    Enter the name of the territory you wish to place a unit on

    > India
    Your new army count is: 0

    Neutral 1 is out of armies

    Enter the name of the territory you wish to place a unit on

    > Siberia
    Your new army count is: 0

    Neutral 2 is out of armies

    Enter the name of the territory you wish to place a unit on

    > Peru
    Your new army count is: 0

    Neutral 3 is out of armies
    p142
    p242
    You're out of armies!

    Neutral 1 is out of armies

    Neutral 2 is out of armies

    Neutral 3 is out of armies
    p142
    p242

    Neutral 1 is out of armies

    Neutral 2 is out of armies

    Neutral 3 is out of armies
    p142
    p242
    p142
    p242

    Peter will play first as they rolled a 6 and Rob only rolled a 3
RJBo
  • 33
  • 1
  • 1
  • 7
  • 1
    How do you expect to break from the switch statement if you have no break statements? – Eran Mar 27 '16 at 13:27
  • what about the `break` statement? – krstf Mar 27 '16 at 13:29
  • Sorry I had tried to use a function to break out of it when trying to get it to work, I've updated it to where the break should be now. – RJBo Mar 27 '16 at 13:30
  • @RJBo I can't see any `break` statements in your loops! – user2004685 Mar 27 '16 at 13:32
  • @user2004685 It's in the last case – RJBo Mar 27 '16 at 13:32
  • @RJBo How are you making sure that your loops are not running infinitely? You decrement the counter in some `if ()` condition. What if that condition is never being executed? Also, you should have a `break;` after end of every `case`. – user2004685 Mar 27 '16 at 13:34
  • @user2004685 I want it to run all the cases, but when it reaches the last one I want it to break out and stop. The reason I used the switch statement is because player 1 does not always go first, sometimes player 2 does – RJBo Mar 27 '16 at 13:42
  • @RJBo *"How are you making sure that your loops are not running infinitely? You decrement the counter in some `if ()` condition. What if that condition is never being executed?"* – user2004685 Mar 27 '16 at 13:44
  • @user2004685 Well the output works perfectly up to when I want to break in the last case, surely the loops are working fine then if it gets that far? and even if they're weren't why would it suddenly start outputting random lines from different cases completely. – RJBo Mar 27 '16 at 13:48

1 Answers1

1

If you want to break out of switch statement, you need to add break statement. Else it will keep running subsequent case statements until it finds break statement or runs all lines in switch. This is called fall-through in Java.

Example from Oracle site:

int month = 8;

        switch (month) {
            case 1:  futureMonths.add("January");
            case 2:  futureMonths.add("February");
            case 3:  futureMonths.add("March");
            case 4:  futureMonths.add("April");
            case 5:  futureMonths.add("May");
            case 6:  futureMonths.add("June");
            case 7:  futureMonths.add("July");
            case 8:  futureMonths.add("August");
            case 9:  futureMonths.add("September");
            case 10: futureMonths.add("October");
            case 11: futureMonths.add("November");
            case 12: futureMonths.add("December");
                     break;
            default: break;
        }

Above code will print

August
September
October
November
December

If you want it to print only August, you need to add break statement in case for August, not after Decembet. You can read on fall-through in any standard Java book

Break statement in Java switch

Oracle tutorial - switch statement

Community
  • 1
  • 1
Yogesh
  • 1,307
  • 3
  • 13
  • 18