Rhys
Rhys

Reputation: 3

I'm having an issue with my game

So I'm making a basic game, and I'm having an issue it's that battle two won't start, battle one is working really well, but for some reason battle two won't start...

Heres the code:

import java.util.Scanner;

public class Game {

public int Health = 10;
public int Enemy_Health = 25;
public int Your_Health = 20;
public int Battle = 1;
public int Move = 0;

public void Method() {

    System.out.println("A random battle started!" + "\n");
    System.out.println("It's health is currently at " + Health + "\n");
    System.out.println("Your health is currently at " + Your_Health + "\n");
    System.out.println("Would you like to use bite or slash?" + "\n");
    //-----Battle 1-----\\
    while (Battle == 1) {

        if (Battle == 1) {

            Scanner Scan = new Scanner(System.in);
            String a = Scan.nextLine();

            if (a.equals("bite")) {
                System.out.println("You used bite!" + "\n");
                System.out.println("You did 5 damage!" + "\n");
                Health -= 5;
                Move++;
                System.out.println("It's health is now at " + Health + "\n");

            } else if (a.equals("slash")) {
                System.out.println("You used slash!" + "\n");
                System.out.println("You did 2 Damage!" + "\n");
                Health -= 2;
                System.out.println("It's health is now at " + Health + "\n");
                System.out.println("You did 3 Damage!" + "\n");
                Health -= 3;
                Move ++;

                System.out.println("It's health is now at " + Health + "\n");
                }
                if (Move == 1) {
                     System.out.println("---------------------------" + "\n");
                     System.out.println("It used Bite" + "\n");
                     System.out.println("It did 5 damage!" + "\n");
                     Your_Health -= 5;
                     System.out.println("Your health is now at " + Your_Health + "\n");
                     System.out.println("---------------------------" + "\n");
                     System.out.println("Would you like to use bite or slash?" + "\n");

                }
            if (Health == 0) {

                System.out.println("You won!" + "\n");
                System.out.println("You gained 100$!" + "\n" + "You now have 150$!");
                System.out.println("You gained 100 Xp!" + "\n" + "You leveled up!" + "\n" + "50 Xp left tell Level 2" + "\n" + "New move learned: " + "\n" +
                        "Knock Out!" +  "\n"  + "Your health went up by 5!" + "\n");
                System.out.println("Swirl will make the enemy dizzy for one round!" + "\n" + "\n");

                Your_Health += 5;
                Battle -= 0;
                Move = 0;
                Health = -1;

            }
            while (Battle == 0){
                Battle = 2;
            }

            //-----Battle 2-----\\
                while (Battle == 2) {

                    if (Enemy_Health == 25) {

                        System.out.println("A random battle has started!" + "\n");
                        System.out.println("It's health is currently at " + Enemy_Health + "\n");
                        System.out.println("Your health is currently at " + Your_Health + "\n");
                        System.out.println("Would you like to use bite, slash or swirl?" + "\n");

                        if (a.equals("bite")) {
                            System.out.println("You used bite!" + "\n");
                            System.out.println("You did 5 damage!" + "\n");
                            Health -= 5;
                            System.out.println("It's health is now at " + Health + "\n");
                        } else if (a.equals("slash")) {
                            System.out.println("You used slash!" + "\n");
                            System.out.println("You did 2 Damage!" + "\n");
                            Health -= 2;
                            Move++;
                            System.out.println("It's health is now at " + Health + "\n");
                            System.out.println("You did 3 Damage!" + "\n");
                            Health -= 3;
                            Move++;
                            System.out.println("It's health is now at " + Health + "\n");

                        } else if (a.equals("swirl")) {
                            System.out.println("You used Swirl" + "\n");
                            System.out.println("It got dizzy for one round." + "\n");
                        }
                        if (Move == 1) {
                            System.out.println("---------------------------" + "\n");
                            System.out.println("It used Slash" + "\n" + "It did 2 damage!" + "\n" + "It did 3 damage!" + "\n");
                            Your_Health -= 5;
                            System.out.println("Your health is now at " + Your_Health + "\n");
                            System.out.println("---------------------------" + "\n");
                            System.out.println("Would you like to use bite or slash?" + "\n");
                        }
                        if (Move == 2){
                            System.out.println("---------------------------" + "\n");
                            System.out.println("It used Bite" + "\n" + "It did 5 damage!" + "\n");
                            Your_Health -= 5;
                            System.out.println("Your health is now at " + Your_Health + "\n");
                            System.out.println("---------------------------" + "\n");
                            System.out.println("Would you like to use bite or slash?" + "\n");
                        }
                        if (Move == 3){
                            System.out.println("---------------------------" + "\n");
                            System.out.println("It used Head Butt" + "\n" + "It did 10 damage!" + "\n" + "It took 5 damage" + "\n");
                            Your_Health -= 10;
                            Enemy_Health -= 5;
                            System.out.println("Your health is now at " + Your_Health + "\n");
                            System.out.println("---------------------------" + "\n");
                            System.out.println("Would you like to use bite or slash?" + "\n");
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Nitek
Nitek

Reputation: 2614

After the first run through your while loop Health will be < 10, so your if block is not run anymore. Since I don't see any benefit from it, I'd suggest you just remove the if(Health==10) block.

Also as mentioned in the comments: Health and battle should be int not float.

After fixing the other issues you will notice that the battle never ends and you can never win by bite: Your check for the win condition has to be outside of the other ifs and set Battle to something != 1

In total your game should probably looks something like this:

public class Game {

    public int Health = 10;
    public int Battle = 1;

    public void Method() {

        System.out.println("A random battle started!" + "\n");
        System.out.println("It's health is currently at " + Health + "\n");
        System.out.println("Would you like to use bite or slash?" + "\n");

        while (Battle == 1) {
                Scanner Scan = new Scanner(System.in);
                String a = Scan.nextLine();

                if (a.equals("bite")) {
                    System.out.println("You used bite!"+ "\n");
                    System.out.println("You did 5 damage!"+ "\n");
                    Health -= 5;
                    System.out.println("It's health is now at " + Health + "\n");
                    System.out.println("Would you like to use bite or slash?"+ "\n");
                } else if (a.equals("slash")) {
                    System.out.println("You used slash!"+ "\n");
                    System.out.println("You did 2 Damage!"+ "\n");
                    Health -= 2;
                    System.out.println("It's health is now at " + Health+ "\n");
                    System.out.println("You did 2 Damage!"+ "\n");
                    Health -= 2;
                    System.out.println("It's health is now at " + Health+ "\n");
                    System.out.println("Would you like to use bite or slash?"+ "\n");
                }
                if (Health <= 0) {
                    System.out.println("You won!"+ "\n");
                    System.out.println("You gained 100$!" + "\n" + "You now have 150$!");
                    Battle = 0;
                }
            }
        }
      }
}

Upvotes: 1

Related Questions