0

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);
}

}

4
  • 1
    keep that as an instance variable as well, on construction, set that to hitpoints + strength, and lower that value when he's hit Commented Nov 16, 2017 at 11:33
  • Put totalHealth out of the function and keep it updated Commented Nov 16, 2017 at 11:35
  • totalHealth is instanciated every time you call attack - so if it doesn´t change the value (only once), have a look at the player-values. Add logging for totalHealth and the player-values. print actual and expected. Commented Nov 16, 2017 at 11:36
  • @canillas if the player-Values was changed between the attack()-calls it will be updated and right Commented Nov 16, 2017 at 11:37

3 Answers 3

1

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()));
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes but I can't do it with "somePlayer" because it's something I passed in the method.
@TornikeShelia Your Player class has an health attribute?Can you share with us your Player class?
@TornikeShelia As I can see, you just have an health attribute into your player, so you don't need to calculate the totalHealth again because it's declared in your player class. Check my edit.
I checked it , but somehow it doesn't calculate the right amount of health, I've set the damage of the "Monster" to 6 and the health of "Player" to 100 but every time I run the attack method the "Player" dies, I'll try to find the problem
I figured it out, problem was that I wasn't passing the totalHealth to the constructor, Thank you so much , It works now and I figured out how to make it work in the future . :)
|
0

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.

Comments

0

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);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.