多態性を利用して引数でモンスターを受け取り、「モンスターのダメージ - 盾の防御力」の分のダメージを受ける処理に修正。また、引数のモンスターがドラゴンだった場合Heroの体力、盾の防御力に関わらず1/3の確率で即死する処理を作成したいのです。
しかし、コンソールしてみると、「NoNameの現在HPは100だ」のみとなってしまい、処理ができていないのです。
ヒントとして、下記のコードで「1/3の確率で即死する処理と通常時の処理を付け足せば
できる」と言われても上手くできません。
どなたかヒントやご回答をお願い致します。
public void receiveAttack(Monster m, int damage) {
System.out.println(m.nameNum() + "の攻撃!");
if (m instanceof Dragon) {//
Hero.java
1class Hero extends Character { 2 Sword sword; 3 Shield shield; 4 5 public Hero() { 6 this.name = "NoName"; 7 this.hp = 100; 8 this.maxHp = 100; 9 this.mp = 0; 10 this.maxMp = 0; 11 this.sword = new Sword(); 12 this.shield = new Shield(); 13 } 14 15 public Hero(String name, int hp, int mp, String sName, int sDamage, String shName, int shDefense) { 16 this.name = name; 17 this.hp = hp; 18 this.maxHp = hp; 19 this.mp = mp; 20 this.maxMp = mp; 21 this.sword = new Sword(sName, sDamage); 22 this.shield = new Shield(shName, shDefense); 23 } 24 25 public void receiveAttack(Monster monster) { 26 // TODO 自動生成されたメソッド・スタブ 27 } 28}
Character.java
1import java.util.Random; 2 3abstract class Character implements Human { 4 String name; 5 int hp; 6 int maxHp; 7 int mp; 8 int maxMp; 9 static Random random = new Random(); 10 11 public void showHp() { 12 System.out.println(this.name + "の現在HPは" + this.hp + "だ"); 13 } 14 15 @Override 16 public void receiveAttack(Creature creature) { 17 if(creature instanceof Dragon && 18 random.nextDouble()<1.0/3.0 19 //↑「1.0/3.0」実数で計算しないと、整数で計算されてバグになる。 20 ){ 21 this.hp = 0; 22 } 23 } 24 25 @Override 26 public void receiveAttack(Monster m, int damage) { 27 receiveAttack(m); 28 } 29}
Human.java
1interface Human extends Creature { 2 void receiveAttack(Monster m, int damage); 3}
Creature.java
1interface Creature { 2 void receiveAttack(Creature creature); 3}
Monster.java
1abstract class Monster implements Creature { 2 String name; 3 String num; 4 int hp; 5 int damage; 6 7 public void run() { 8 System.out.println(this.nameNum() + "は逃げ出した"); 9 } 10 11 public String nameNum() { 12 return (this.name + this.num); 13 } 14}
Doragon.java
1class Dragon extends Monster { 2 public Dragon() { 3 this.name = ""; 4 this.num = "A"; 5 this.hp = 500; 6 this.damage = 100; 7 } 8 9 public Dragon(String num) { 10 this.name = ""; 11 this.num = num; 12 this.hp = 500; 13 this.damage = 100; 14 } 15 16 @Override 17 public void receiveAttack(Creature creature) { 18 } 19}
Shield.java
1class Shield extends Guard { 2 final String type = ""; 3 4 public Shield() { 5 this.name = ""; 6 this.defense = 0; 7 } 8 9 public Shield(String name, int defense) { 10 this.name = name; 11 this.defense = defense; 12 } 13}
Guard.java
1abstract class Guard { 2 String type; 3 String name; 4 int defense; 5}
Sword.java
1class Sword extends Weapon { 2 final String type = ""; 3 4 public Sword() { 5 this.name = ""; 6 this.damage = 0; 7 } 8 9 public Sword(String name, int damage) { 10 this.name = name; 11 this.damage = damage; 12 } 13}
Weapon.java
1abstract class Weapon { 2 String type; 3 String name; 4 int damage; 5}
Main.java
1public class Main { 2 public static void main(String[] args) { 3 Hero hero = new Hero(); 4 Dragon dragon = new Dragon(); 5 hero.receiveAttack(dragon); 6 hero.showHp(); 7 } 8}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/11 13:31