public abstract class Character {
private String name;
private double baseDamage;
private double baseArmor;
private double health;
public Character(String name, double baseDamage, double baseArmor, double health) {
this.name = name;
this.baseDamage = baseDamage;
this.baseArmor = baseArmor;
this.health = health;
}
public abstract void dealDamageTo(Character foe);
public abstract double getDefense();
public String getName() {
return name;
}
public double getBaseArmor() {
return baseArmor;
}
public double getBaseDamage() {
return baseDamage;
}
public double getHealth() {
return health;
}
public void setHealth(double health) {
this.health = health;
}
@Override
public String toString() {
return "Name<" + name + ">, BaseDamage<" + baseDamage +
">, BaseArmor<" + baseArmor +
">, Health<" + health +
'>';
}
}
import java.util.Random;
public class Warrior extends Character {
private int critChance;
public Warrior(String name, double baseDamage, double baseArmor, double health, int critChance) {
super(name, baseDamage, baseArmor, health);
this.critChance = critChance;
}
@Override
public void dealDamageTo(Character foe) {
double foeDefense = foe.getDefense();
double myDamage = new Random().nextInt() > critChance ? getBaseDamage() : getBaseDamage() * 1.25;
foe.setHealth(Math.max(0, foe.getHealth() + Math.min(0, foeDefense - myDamage)));
}
@Override
public double getDefense() {
return getBaseArmor() * 1.2;
}
@Override
public String toString() {
return "Warrior: " + super.toString() + ", Defense<" + getDefense() +
">, CriticalChance<" + critChance + '>';
}
}
public class Bandit extends Character {
private double secondaryWeapon;
public Bandit(String name, double baseDamage, double baseArmor, double health, double secondaryWeapon) {
super(name, baseDamage, baseArmor, health);
this.secondaryWeapon = secondaryWeapon;
}
@Override
public void dealDamageTo(Character foe) {
double foeDefense = foe.getDefense();
double myDamage = getBaseDamage() + secondaryWeapon;
foe.setHealth(Math.max(0, foe.getHealth() + Math.min(0, foeDefense - myDamage)));
}
@Override
public double getDefense() {
return getBaseArmor();
}
@Override
public String toString() {
return "Bandit: " + super.toString() + ", Defense<" + getDefense() +
">, SecondaryWeapon<" + secondaryWeapon + '>';
}
}
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random random = new Random();
Character hero = new Warrior("Tom", 10, 5, 100, 25);
System.out.print("Enter the size of the dungeon: ");
Character[] foes = new Character[Integer.parseInt(in.nextLine())];
for (int i = 0; i < foes.length; i++) {
foes[i] = new Bandit("Bandit" + i, random.nextInt(5) + 1,
random.nextInt(5), 15, random.nextInt(4));
}
int currentFoe = 0;
System.out.println(hero);
while (hero.getHealth() > 0 && currentFoe < foes.length) {
System.out.println("\n-----------------------");
System.out.println("Current foe:");
System.out.println(foes[currentFoe]);
System.out.println("Hero attacked:");
hero.dealDamageTo(foes[currentFoe]);
System.out.println(foes[currentFoe]);
if (foes[currentFoe].getHealth() > 0) {
System.out.println("Foe attacked:");
foes[currentFoe].dealDamageTo(hero);
System.out.println(hero);
} else {
System.out.println("Foe died");
currentFoe++;
}
}
if (hero.getHealth() > 0) {
System.out.println("Hero won.");
} else {
System.out.println("Hero died");
}
}
}
Comments
Leave a comment