-2

So, I am trying to make a text based RPG. A good starter project, as I am new to all of this. I want to make a critical hit, and math.random seems like the best way to go. Only one problem; when it loops, it doesn't refresh the random. I can assume that that's because it calls it once, and once it's called in, that's it. That's it's value. I want to know of an alternative so it refreshes every time the while loop starts over. I know there are some other bugs in my code; but that's not what I'm focusing on right now. Here's the entire method:

    public void battleStart(Enemy enemy, Player player)
    {

        while (enemy.health != 0)
        {
            String battle = null;

            int criticalHt = (int) (Math.random() * 10) + 0;

            boolean T = true;
            while (T = true)
            {

                battle = Game.console.next();

                enemy.attackplayer(player);

                if (battle.toLowerCase().contains("skip"))
                {

                    System.out.println(criticalHt);

                    if (criticalHt == 1)
                    {

                        player.hitPoints -= enemy.dmg * 2;

                        System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + " for a critical level of " + enemy.dmg * 2 + "!.\n Your health is now " + player.hitPoints + ".");

                    }

                    else
                    {

                        player.hitPoints -= enemy.dmg;

                        System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + ".\n Your health is now " + player.hitPoints + ".");

                        System.out.println("Your turn to attack! Use an attack, or type \"skip\" to let your enemy strike.");
                    };
                    if (battle.contains("skp"))
                    {

                    };

                }
            }
        }
Tedil
  • 1,935
  • 28
  • 32

1 Answers1

1

Move the code

 int criticalHt = (int)(Math.random() * 10) + 0;

INSIDE the while loop

while(T = true){
  int criticalHt = (int)(Math.random() * 10) + 0;
  battle = Game.console.next();
  ...
JP Moresmau
  • 7,388
  • 17
  • 31