import java.util.Random;
public class Main {
public static void main(String[] args) {
Hero hero = new Hero();
Dragon dragon = new Dragon();
hero.receiveAttack(dragon);
hero.showHp();
}
}
// ③
interface Creature {
void receiveAttack(Creature creature);
}
interface Human extends Creature {
void receiveAttack(Monster m, int damage);
}
// ②
abstract class Character implements Human {
String name;
int hp;
int maxHp;
int mp;
int maxMp;
static Random random = new Random();
public void showHp() {
System.out.println(this.name + "の現在HPは" + this.hp + "だ");
}
@Override
public void receiveAttack(Creature creature) {
if(creature instanceof Dragon &&
random.nextDouble()<1.0/3.0
//↑「1.0/3.0」実数で計算しないと、整数で計算されてバグになる。
){
this.hp = 0;
}
}
@Override
public void receiveAttack(Monster m, int damage) {
receiveAttack(m);
}
}
// ①
class Hero extends Character {
Sword sword;
Shield shield;
public Hero() {
this.name = "NoName";
this.hp = 100;
this.maxHp = 100;
this.mp = 0;
this.maxMp = 0;
this.sword = new Sword();
this.shield = new Shield();
}
public Hero(String name, int hp, int mp, String sName, int sDamage, String shName, int shDefense) {
this.name = name;
this.hp = hp;
this.maxHp = hp;
this.mp = mp;
this.maxMp = mp;
this.sword = new Sword(sName, sDamage);
this.shield = new Shield(shName, shDefense);
}
}
// ⑧
abstract class Monster implements Creature {
String name;
String num;
int hp;
int damage;
public void run() {
System.out.println(this.nameNum() + "は逃げ出した");
}
public String nameNum() {
return (this.name + this.num);
}
}
// ⑨
class Dragon extends Monster {
public Dragon() {
this.name = "";
this.num = "A";
this.hp = 500;
this.damage = 100;
}
public Dragon(String num) {
this.name = "";
this.num = num;
this.hp = 500;
this.damage = 100;
}
@Override
public void receiveAttack(Creature creature) {
}
}
// ⑤
abstract class Weapon {
String type;
String name;
int damage;
}
// ④
class Sword extends Weapon {
final String type = "";
public Sword() {
this.name = "";
this.damage = 0;
}
public Sword(String name, int damage) {
this.name = name;
this.damage = damage;
}
}
// ⑦
abstract class Guard {
String type;
String name;
int defense;
}
// ⑥
class Shield extends Guard {
final String type = "";
public Shield() {
this.name = "";
this.defense = 0;
}
public Shield(String name, int defense) {
this.name = name;
this.defense = defense;
}
}