-1

I am trying to manipulate objects in Java. To better explain, I have a superclass that is a called Creature and two subclasses called (Dragon and Warrior). I created two objects one for each the dragon and the warrior to fight each other. How can I set an attack method that does damage from one object and use that number to subtract it from the health of the second object.

please note that warrior and dragon are both subclasses.

public class Creature {

    private int health = 50;
    private int attack;
    private int level = 1;
    private String name;
    private Creature random;
    private Creature random2;

    public String getName() {
        return name;
    }

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

    public int getHealth() {
        if(health >= 3000)
        {
            health = 3000;
        }
        if(health <= 0)
        {
            health = 0;
        }
        return health;
    }

    public void setHealth(int health) {
        this.health = health < 0 ? 0 : health;
    }

    public int getAttack() {
        Random generator = new Random();
        attack = generator.nextInt(10) * level + 1;
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getLevel() {
        if(level >= 60)
        {
            level = 60;
        }
        return level;
    }

    public void setLevel(int level){
        this.level = level;   
    }

    public boolean isAlive() {
        if(getHealth() <= 0)
        {
            return false;
        }
        return true;
    }







    public String getWelcome()
    {
        String welcome = "Hello and welcome to Dragonslayer!";
        return welcome;
    }

    public String getStab()
    {
        String stab = "You choose to stab the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return stab;
    }

    public String getSlash()
    {
        String slash = "You choose to slash the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return slash;
    }

    public String getCut()
    {
        String cut = "You choose to cut the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining.";
        return cut;
    }  



    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("You come across the dragon and you have two options.  Do you run or fight? ");


        Creature kirsta = new Dragon();
        kirsta.setHealth(50);

        Creature brenton = new Warrior();
        brenton.setHealth(50);

        while(in.hasNextLine())
        {
            switch(in.nextLine())
            {
                case "run":
                    System.out.println("I am so sorry, you could not outrun the dragon, you have been killed!");
                    break;
                case "fight":
                    while(kirsta.isAlive() && brenton.isAlive())
                    {
                        System.out.println("Do you want to stab, slash, or cut the dragon? ");
                        switch(in.nextLine())
                        {
                            case "stab":
                                System.out.println("You choose to stab the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            case "slash":
                                System.out.println("You choose to slash the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            case "cut":
                                System.out.println("You choose to cut the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining.");
                                break;
                            default:
                                System.out.println("I am sorry that is not valid, try again. ");
                        }
                    }
                    break;
                default:
                    System.out.println("I am sorry that is not a valid choice. ");
                    break;
            }//end of switch
            if(brenton.isAlive() == false && kirsta.isAlive() == false)
            {
                System.out.println("It is a horrid day today, as both you and the dragon have fallen.");
            }
            else if(brenton.isAlive() == true && kirsta.isAlive() == false)
            {
                System.out.println("Congratulations you have killed the dragon, and survived!");
            }
            else if(brenton.isAlive() == false && kirsta.isAlive() == true)
            {
                System.out.println("You have sadly fallen to the dragon, better luck next time.");
            }
            else
            {
                System.out.println("SOMETHING WENT WRONG!!!");
            }
            break;
        }//end of while loop in.hasNextLine().
    }//end of main
}//end of class
TheDetailer
  • 289
  • 5
  • 16

2 Answers2

3

Assuming that your attack method has form similar to this

public void attack(Creature otherCreature){
    otherCreature.decreaseHitPointsBy(this.attackValue);
}

Usage would look like

warrior.attack(dragon);
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
  • I will give this a try, I was not aware you could use a class as a parameter value. – TheDetailer Sep 02 '15 at 21:23
  • That's actually quite practical. By doing this warriors can attack any creatures, not only single hardcoded dragon :) – Marcin Szymczak Sep 02 '15 at 21:24
  • @Brentonr25: he's not passing in a class at all. He's defining the type of parameter. Please read up on how to use methods and how to pass parameters as this is Java at its most basic. 1+ to the answer btw. – Hovercraft Full Of Eels Sep 02 '15 at 21:25
  • So just to clarify, this is what I am trying to do. But obviously random1 and random2 are not actual objects so i get a compile time error. public void attackResults(Creature random, Creature random2) { this.random = random; this.random2 = random2; random1.getAttack() - random2.getHealth(); } – TheDetailer Sep 02 '15 at 21:33
  • @Brentonr25: Nothing is obvious, especially things that we don't yet understand. Please edit your question and improve it, showing pertinent code and any and all error messages, and helping us to fully understand your current problem. – Hovercraft Full Of Eels Sep 02 '15 at 21:49
1

For example you can create two classess that extends creature and then create instances of them using new operator. They can interract by passing refference to other object as argument of attack method:

class Creature {
    protected int health = 100;
    protected int strength = 10;

    public void attack(Creature other) {
        other.takeDamage(strength);
    }

    public void takeDamage(int amount) {
        health -= amount;
    }
}

class Warrior extends Creature {
    protected int strength = 2;

    //method specific to warrior
    public void eatPotion() {
        health = 100;
    }
}

class Dragon extends Creature {
    //You can simply override values in subclasses
    protected int strength = 20;
    protected int health = 1000;
}

Dragon dragon = new Dragon();
Warrior warrior = new Warrior();

warrior.attack(dragon);
dragon.attack(warrior);
warrior.eatPotion();
warrior.attack(dragon);
Maciej Stępyra
  • 310
  • 1
  • 6