Home > Blockchain >  Inheritance, setters and getters (Game Class Output)
Inheritance, setters and getters (Game Class Output)

Time:03-06

How am I supposed to decrease its armor first and next the health? I want not to print the negative of armor, and I want to print if the armor gets 0, the health should be the next to decrease.

For Instance in Testing Class, I use wizard and cast a spell to fireball 3 times to knight. Since knight should decrease the armor first, and health after the armor gets to 0.

Output:

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now 6 armor left, and 80 health left.

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 80 health left.

Wizard use a 'Fireball' and will deal 24 damage to Knight
Knight has now -18 armor left, and 56 health left.

As you can see in the Second Print, the armor gets -18, I want that to print 0 and 62 health left. since wizard deal 24 damage

enter image description here

This is the Parent class:

    public class Human {
    
   String name;
   int strength;
   int stealth;
   int intelligence;
   int health;

   public Human() {
       name = "Human";
       strength=3;
       stealth=3;
       intelligence=3;
       health=100;
   }
   
  
   public int getStrength() 
   {
       return strength;
   }

   public int getStealth() 
   {
       return stealth;
   }

   public int getIntelligence() 
   {
       return intelligence;
   }

   public int getHealth() 
   {
       return health;
   }

  
   public String getName()
   {
       return name;
   }


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


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


   public void setStealth(int stealth) 
   {
    this.stealth = stealth;
   }


   public void setIntelligence(int intelligence)
   {
    this.intelligence = intelligence;
   }


   public void setHealth(int health) 
   {
    this.health = health;
   }

   public void attack(Human human)
   {
       int damagetaken = this.getStrength();
        human.health = human.health - this.getStrength();
        System.out.println(this.getName()  " attack "   human.getName()   " and will deal "  damagetaken  " damage.");
        System.out.println(human.getName()  " has now " human.getHealth() " health left");
        System.out.println();
   }


}

Child Class Knight:

public class Knight extends Human
{
   int armor=30;
   
   public Knight() {
       this.name="Knight";
       this.health=80;  
   }
   public void Reinforce(){
       this.armor=this.armor 5;
       System.out.println(this.getName()  " use a 'Reinforce' and will increase its armor to 5 ");
       System.out.println(this.getName()  " has now "  this.armor  " armor left.");
       System.out.println();

   }
   public void HolyStrike(Human human)
   {   
       int damagetaken = (int)(human.health-10-(.20*this.armor));
       human.health= human.getHealth() - damagetaken;
       System.out.println(this.getName()  " use a 'Holy Strike' and will deal " damagetaken  " damage to "  human.getName());
       System.out.println(human.getName() " has now "  human.getHealth() " health left.");
       System.out.println();
   }
}

Child Class Wizard:

public class Wizard extends Human{

   public Wizard() 
   {
       this.name="Wizard";
       this.health=50;
       this.intelligence=8;  
   }
   public void heal(Human human)
   {
       int healtake = this.getIntelligence();
       human.health=human.health this.intelligence;
       System.out.println(this.getName() " use a 'Heal' to "   human.getName()   " he/she will receive "   healtake   " additional health to his/her HP");
       System.out.println(human.getName() " has now "  human.getHealth() " health left.");
       System.out.println();
   }
   public void fireball(Human human)
   {
       int damagetaken = this.intelligence*3;
       human.health= human.health-(this.intelligence*3);
       System.out.println(this.getName()  " use a 'Fireball' and will deal " damagetaken  " damage to "  human.getName());
       System.out.println(human.getName() " has now "  human.getHealth() " health left.");
       System.out.println();
   }
   
   public void fireball(Knight human)
   {
       
       if (human.armor > 1)
       {
           int damagetaken = this.intelligence*3;
           human.armor= human.armor-(this.intelligence*3);
           System.out.println(this.getName()  " use a 'Fireball' and will deal " damagetaken  " damage to "  human.getName());
           System.out.println(human.getName() " has now "  human.armor " armor left, and " human.getHealth() " health left.");
           System.out.println();
       }
       
       else
       {
           int damagetaken = this.intelligence*3;
           human.health= human.health-(this.intelligence*3);
           System.out.println(this.getName()  " use a 'Fireball' and will deal " damagetaken  " damage to "  human.getName());
           System.out.println(human.getName() " has now "  human.armor " armor left, and " human.getHealth() " health left.");
           System.out.println();
       }
   }
   
}

Testing Class:

public class HumanTest {
   public static void main(String Args[]){
       
       Human h1 = new Human();
       Wizard w1=  new Wizard();
       Ninja n1 = new Ninja();
       Samurai s1 = new Samurai();
       Knight k1 = new Knight();
       
   
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
      w1.fireball(k1);
   }
}

Can you help me to solve this?

you can ask me about the source code.

CodePudding user response:

You are checking if armor is greater than 1 (human.armor > 1). You must reduce as much armor as possible and then the rest from health.

public void fireball(Knight knight) { //I've renamed the variable as knight
    int damageToTake = this.intelligence * 3;
    if (knight.armor >= damageToTake) {
        knight.armor -= damageToTake;
    } else {
        int remaining = damageToTake - knight.armor;
        knight.armor = 0;
        // reduce remaining from health
        knight.health -= remaining;
    }
    System.out.println(this.getName()  " use a 'Fireball' and will deal " damagetaken  " damage to "  knight.getName());
    System.out.println(knight.getName() " has now "  knight.armor " armor left, and " knight.getHealth() " health left.");
    System.out.println();

}

I guess you'll also have to handle when there isn't enough health as well. I'll leave that to you.

CodePudding user response:

Your should should add an additional check:

  1. If armor less than 0
  2. Then health = armor
  3. Then set armor to 0.

Example armor = -10, health = 30, then health = 20, armor = 0. Other algorithms are possible as well.

As additional note having method for dealing damage to knight and method for dealing damage to human is bad design. If you have more classes with armor, would you make additional method for each of them?

It's better for example to have all humans have armor, but with default value of 0 for them, and assign correct armor to extending classes.

Or even better have an interface for dealing with armor and health, your classes should implement it, and damage dealing methods should accept it.

Or best, have a method accepting damage done for each class. Like this the class itself will handle health left according to different modifiers, armor in your case, but you can add more. You really should not handle your knight's armor and health in the wizard class.

  • Related