public class Warrior {
private String name;
private int health;
public Warrior(String name) {
this.name = name;
this.health = 100;
}
public void attack(Warrior opponent) {
opponent.health -= 20;
System.out.println(this.name + " атаковал, у " + opponent.name + " осталось " + opponent.health + " здоровья.");
}
public boolean isAlive() {
return this.health > 0;
}
public static void main(String[] args) {
Warrior warrior1 = new Warrior("Воин 1");
Warrior warrior2 = new Warrior("Воин 2");
while (true) {
warrior1.attack(warrior2);
if (!warrior2.isAlive()) {
System.out.println(warrior1.name + " одержал победу!");
break;
}
warrior2.attack(warrior1);
if (!warrior1.isAlive()) {
System.out.println(warrior2.name + " одержал победу!");
break;
}
}
}
}