Shahmir S Shahmir S - 5 months ago 12
Java Question

Randomly generated numbers won't work after while loop is applied

I am trying to make a combat sequence, but I need to repeat the actual combat. Yet, the HP and enemyHP won't seem to subtract from the randomly generated numbers to give the total after first loop.

I am sorry, if this doesn't make sense. I'm not really good at explaining things...

Here is the code:

import java.util.Random;
import java.util.Scanner;

public class Hayyan {

public int hp = 100;
public int choice = 0;

public static void main(String[] args) {

combatHayyan bob = new combatHayyan();
Hayyan junl = new Hayyan();

while(junl.choice < 10){
System.out.println("Choose a weapon to attack with, type sword for a sword, or axe for an axe ");
bob.attack();
bob.defend();
System.out.println();

if(junl.hp < 0 || bob.enemyHP < 0){
junl.choice = 10;
}

}
}
}

class combatHayyan {

public int enemyHP = 50;
public int yourTotal;

public void attack(){

weapons weapon = new weapons();

Scanner bob = new Scanner(System.in);
switch(bob.nextLine()){

case "sword":

weapon.sword();
System.out.println("Enemy now has " + weapon.total + "HP left!");
break;


case "axe":

weapon.axe();
System.out.println("Enemy now has " + weapon.total + "HP left!");
break;

}
}

public void defend(){

Hayyan lost = new Hayyan();

Random bob = new Random();
int randomness = bob.nextInt(11) + 10;
yourTotal = lost.hp - randomness;
System.out.println("You now have " + yourTotal + "HP left!");

}
}

class weapons {

public int total;

public void sword(){

int bob = 5 + (int)(Math.random() * ((10 - 5) + 1));
combatHayyan llama = new combatHayyan();
total = llama.enemyHP - bob;

}

public void axe(){

int randomGenerate = 5 + (int)(Math.random() * ((10 - 5) + 1));
combatHayyan llama = new combatHayyan();
total = llama.enemyHP - randomGenerate;


}


}

Answer

Your question is little broad, but I believe you need to change this:

total = llama.enemyHP - bob;

with this:

total = total - bob;

Just initialize total to llama.enemyHP first. Same thing for defend, you are doing:

yourTotal = lost.hp - randomness;

and I believe you'd rather want:

yourTotal = yourTotal - randomness;

Otherwise, your variables are always recalculated at every pass in the loop. With my recommandation, you need to refactore your code for it to make sense, but basically, you are not keeping the value modified with the random value, so you are re-doing the random calculation over and over.

EDIT: You should consider refactoring your code and use a little more OO concepts. Take a look at this, I've refactored it without going too far from your design, so you can follow it and compare to your solution:

class Hayyan {
    public static void main(String[] args) {
        CombatHayyan combatHayyan = new CombatHayyan();

        Scanner scanner = new Scanner(System.in);
        while (combatHayyan.bothCombatantsAlive()) {
            System.out.println("Choose a weapon to attack with, type sword for a sword, or axe for an axe ");
            combatHayyan.attack(scanner);
            combatHayyan.defend();
            System.out.println();
        }

        scanner.close();
        combatHayyan.printWinner();
    }
}

class CombatHayyan {
    public int enemyHP = 50;
    public int yourHp = 100;

    Weapons weapon = new Weapons();

    public void attack(Scanner scanner) {
        int damage = 0;
        switch (scanner.nextLine()) {
        case "sword":
            damage = weapon.sword();
            break;

        case "axe":
            damage = weapon.axe();
            break;
        }
        enemyHP = enemyHP - damage;
        System.out.println("Enemy now has " + enemyHP + "HP left!");
    }

    public void printWinner() {
        String winner = yourHp>0?"You":"The enemy";
        int hp = yourHp>0?yourHp:enemyHP;
        System.out.println(winner + " won! with " + hp + "HP remaining");
    }

    public boolean bothCombatantsAlive() {
        return enemyHP > 0 && yourHp > 0;
    }

    public void defend() {
        Random random = new Random();
        int randomness = random.nextInt(11) + 10;
        yourHp = yourHp - randomness;
        System.out.println("You now have " + yourHp + "HP left!");

    }
}

class Weapons {
    public int sword() {
        return 5 + (int) (Math.random() * ((10 - 5) + 1));
    }

    public int axe() {
        return 5 + (int) (Math.random() * ((10 - 5) + 1));
    }
}
Comments