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