Homework Hack

import java.lang.Math;

public static int randInt(int a, int b) {
    return (int)(Math.random() * (b - a + 1)) + a;
}

class Droid {
    public String name;
    public int health;
    public int power;
    private int cooldown;

    public Droid(String name, int health, int power) {
        this.name = name;
        this.health = health;
        this.power = power;
        this.cooldown = 100;
    }

    public void attack(Droid opponent) {
        double rand = randInt(0, 100) / 100;

        if (this.cooldown >= 100 * rand) {
            double dmg = this.power + (5 * rand);
            opponent.health -= dmg;
            this.cooldown = 0;
            System.out.println(this.name + " attacks " + opponent.name + " for " + dmg + " damage!");
        } else {
            System.out.println(this.name + " was unable to attack!");
        }
    }

    public void printStatus() {
        System.out.println(this.name + " - Health: " + this.health + " | Power: " + this.power + " | Cooldown: " + this.cooldown);
    }

    public void deathCheck() {
        if (this.health <= 0) {
            System.out.println(this.name + " has been defeated!");
        }
    }
}

Droid droid1 = new Droid("Carl", 1000, 500);
Droid droid2 = new Droid("Ultimate X925-83", 5, 1);

double fightDuration = 0;
while (droid1.health > 0 && droid2.health > 0) {
    droid1.attack(droid2);
    droid2.attack(droid1);
    droid1.printStatus();
    droid2.printStatus();
    droid1.deathCheck();
    droid2.deathCheck();
    fightDuration++;
}
if (droid1.health <= 0)  {
    System.out.println(droid2.name + " Wins!");
} else {
    System.out.println(droid1.name + " Wins!");
}
System.out.println("Fight Duration: " + fightDuration + " turns");
Carl attacks Ultimate X925-83 for 500.0 damage!
Ultimate X925-83 attacks Carl for 6.0 damage!
Carl - Health: 994 | Power: 500 | Cooldown: 0
Ultimate X925-83 - Health: -495 | Power: 1 | Cooldown: 0
Ultimate X925-83 has been defeated!
Carl Wins!
Fight Duration: 1.0 turns