Ok, so I can get the 'run' and 'attack' commands to do what they're supposed to by doing this:
public void combat(){
System.out.println("Type 'attack' to attack the opponent, and 'run' to run away from combat.");
input = new Scanner(System.in);
while(opponent1.isAlive() && player1.isAlive()) {
if(input.hasNext("run")){
run();
break;}
// else if(input.hasNext("attack")) { else if(input.hasNext("attack")) { //Player attacks opponent opponent1.changeHealth(-attack(opponent1.getName())); System.out.println("Player attacks opponent. Opponent's health is now: " + opponent1.getHealth()); System.out.println(); input.next();
//Checks if opponent is dead
if (opponent1.isAlive() == false) {
System.out.println("You won the fight! Congratulations!");
break;
}
//Opponent attacks player
player1.changeHealth(-attack(player1.getName()));
System.out.println("Opponent attacks player. Player's health is now: " + player1.getHealth());
System.out.println();
input.nextLine();
//Checks if player is dead
if (player1.isAlive() == false) {
System.out.println("Player died. Game over.");
break;
}
}
}
}
However, if I do this both the attack and run commands ceases to have any effect. I know I'm probably overlooking something extremely basic here, but I can't figure out what:
public void combat(){
System.out.println("Type 'attack' to attack the opponent, and 'run' to run away from combat.");
input = new Scanner(System.in);
while(opponent1.isAlive() && player1.isAlive()) {
if(input.nextLine().isEmpty()){
System.err.println("Type either 'attack' or 'run.");
input.nextLine();
}
else if(input.hasNext("run")){
run();
break;}
// else if(input.hasNext("attack")) { else if(input.hasNext("attack")) { //Player attacks opponent opponent1.changeHealth(-attack(opponent1.getName())); System.out.println("Player attacks opponent. Opponent's health is now: " + opponent1.getHealth()); System.out.println(); input.next();
//Checks if opponent is dead
if (opponent1.isAlive() == false) {
System.out.println("You won the fight! Congratulations!");
break;
}
//Opponent attacks player
player1.changeHealth(-attack(player1.getName()));
System.out.println("Opponent attacks player. Player's health is now: " + player1.getHealth());
System.out.println();
input.nextLine();
//Checks if player is dead
if (player1.isAlive() == false) {
System.out.println("Player died. Game over.");
break;
}
}
}
}