Tornike Shelia
Tornike Shelia

Reputation: 59

Java - Store the changing int variable

So I'm trying to write the most simple game , everytime "the monster" attacks "the player" I want the variable "int totalHealth" to be lowered .

public void attack(Player somePlayer) {

    int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

    int remainingHealth = totalHealth - damage;
    if (remainingHealth == 0 || remainingHealth < 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked " + somePlayer.getName() + " and made " + this.damage + " damage");
        System.out.println("Your remaining health is - " + (remainingHealth - somePlayer.getStrength()));
    }

}

But the problem is that variable "remainingHealth" stays same , only the first time when I run the code it lowers , every next time it stays same, I guess the problem is in this line :

int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

I guess everytime I run the code

somePlayer.getHitpoints() 

takes the inserted integer from constructor and that's the problem.

I need to figure out the way to store the remaining health in a variable somehow

Player class :

public class Player implements ISavable{

private String name;
private int hitPoints ;
private int strength ;
private String weapon;
private int damage;
private int totalHealth = hitPoints + strength;

public Player(String name, int damage , int hitPoints , int strength) {
    this.name = name;
    this.damage = damage;
    this.weapon = "Sword";
    this.hitPoints = hitPoints;
    this.strength = strength;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getHitPoints() {
    return hitPoints;
}

public void setHitPoints(int hitPoints) {
    this.hitPoints = hitPoints;
}

public int getStrength() {
    return strength;
}

public void setStrength(int strength) {
    this.strength = strength;
}

public String getWeapon() {
    return weapon;
}

public void setWeapon(String weapon) {
    this.weapon = weapon;
}

public int getTotalHealth() {
    return totalHealth;
}

public void setTotalHealth(int totalHealth) {
    this.totalHealth = totalHealth;
}

@Override
public String toString() {
    return "Player{" +
            "name='" + name + '\'' +
            ", hitPoints=" + hitPoints +
            ", strength=" + strength +
            ", weapon='" + weapon + '\'' +
            '}';
}

@Override
public List<String> write() {
    List<String> values = new ArrayList<String>();
    values.add(0, this.name);
    values.add(1, "" + this.hitPoints);
    values.add(2, "" + this.strength);
    values.add(3, "" + this.weapon);
    values.add(4,"" + this.damage);
    return values;
}

@Override
public void read(List<String> savedValues) {
    if (savedValues != null && savedValues.size()>0){
        this.name = savedValues.get(0);
        this.hitPoints = Integer.parseInt(savedValues.get(1));
        this.strength = Integer.parseInt(savedValues.get(2));
        this.weapon = savedValues.get(3);
    }
}

public void attack(Monster someMonster){
    int health = someMonster.getHitPoints();
    int remainingHealth = health - damage;
    if (remainingHealth == 0 || remainingHealth < 0) {
        System.out.println("You killed the monster !!!");
    } else {
        System.out.println("You attacked the monster " + " and made " + this.damage + " damage");
        System.out.println("Monsters remaining health is - " + remainingHealth);
    }

    if (someMonster.isDead()){
        this.hitPoints = 100;
        this.strength = 50;
    }
}

public void healPlayer(Player somePlayer){
    int hp = somePlayer.getHitPoints();
    hp += 10;
    System.out.println("You healed the player for 10hp , your current hp is " + hp);
}

}

Upvotes: 0

Views: 305

Answers (3)

DMC19
DMC19

Reputation: 927

You are not updating the value of totalHealth, you initialize it everytime that attack() method is called, so you always have the same Health.

You can solve it putting out of the attack() method the line:

int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

EDIT:

public void attack(Player somePlayer) {

    somePlayer.totalHealth = somePlayer.totalHealth - this.damage;
    if (somePlayer.totalHealth.equals(0) || somePlayer.totalHealth < 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked " + somePlayer.getName() + " and made " + this.damage + " damage");
        System.out.println("Your remaining health is - " + (somePlayer.totalHealth - somePlayer.getStrength()));
    }

}

Upvotes: 1

Pablo Exp&#243;sito
Pablo Exp&#243;sito

Reputation: 162

You should have a representation of health, defining in Player totalHealth.

You may use a method which calculate the total health (cause your player could get an item that increases strenght or an item that allow heal you).

Then you should use another variable in Player, remainingHealth and initialize it as remainingHealth = totalHealth

    public class Player {

    float totalHealth;
    float remainingHealth;
    ...

    public Player(...) {
        this.remainingHealth = this.totalHealth;
        ...
    }
    ...

}

When you want decrease the health of your player you should use remainingHealth.

    public void attack(Player somePlayer) {

    somePlayer.remainingHealth -= this.damage;
    if (somePlayer.remainingHealth <= 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked ...");
        System.out.println("Your remaining health is - " + somePlayer.remainingHealth);
    }

}

Upvotes: 0

M. Shaw
M. Shaw

Reputation: 1742

On Player create a function updateHealth(int newHealth) that sets the variable returned from getHitPoints() to newHealth. Then just append somePlayer.updateHealth(somePlayer.getHitPoints() - damage); at the end of the attack function.

Upvotes: 0

Related Questions