fork download
  1. import java.util.Random;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Hero hero = new Hero();
  6. Dragon dragon = new Dragon();
  7. hero.receiveAttack(dragon);
  8. hero.showHp();
  9. }
  10. }
  11.  
  12. // ③
  13. interface Creature {
  14. void receiveAttack(Creature creature);
  15. }
  16.  
  17. interface Human extends Creature {
  18. void receiveAttack(Monster m, int damage);
  19. }
  20.  
  21. // ②
  22. abstract class Character implements Human {
  23. String name;
  24. int hp;
  25. int maxHp;
  26. int mp;
  27. int maxMp;
  28. static Random random = new Random();
  29.  
  30. public void showHp() {
  31. System.out.println(this.name + "の現在HPは" + this.hp + "だ");
  32. }
  33.  
  34. @Override
  35. public void receiveAttack(Creature creature) {
  36. if(creature instanceof Dragon &&
  37. random.nextDouble()<1.0/3.0
  38. //↑「1.0/3.0」実数で計算しないと、整数で計算されてバグになる。
  39. ){
  40. this.hp = 0;
  41. }
  42. }
  43.  
  44. @Override
  45. public void receiveAttack(Monster m, int damage) {
  46. receiveAttack(m);
  47. }
  48. }
  49.  
  50. // ①
  51. class Hero extends Character {
  52. Sword sword;
  53. Shield shield;
  54.  
  55. public Hero() {
  56. this.name = "NoName";
  57. this.hp = 100;
  58. this.maxHp = 100;
  59. this.mp = 0;
  60. this.maxMp = 0;
  61. this.sword = new Sword();
  62. this.shield = new Shield();
  63. }
  64.  
  65. public Hero(String name, int hp, int mp, String sName, int sDamage, String shName, int shDefense) {
  66. this.name = name;
  67. this.hp = hp;
  68. this.maxHp = hp;
  69. this.mp = mp;
  70. this.maxMp = mp;
  71. this.sword = new Sword(sName, sDamage);
  72. this.shield = new Shield(shName, shDefense);
  73. }
  74. }
  75.  
  76. // ⑧
  77. abstract class Monster implements Creature {
  78. String name;
  79. String num;
  80. int hp;
  81. int damage;
  82.  
  83. public void run() {
  84. System.out.println(this.nameNum() + "は逃げ出した");
  85. }
  86.  
  87. public String nameNum() {
  88. return (this.name + this.num);
  89. }
  90. }
  91.  
  92. // ⑨
  93. class Dragon extends Monster {
  94. public Dragon() {
  95. this.name = "";
  96. this.num = "A";
  97. this.hp = 500;
  98. this.damage = 100;
  99. }
  100.  
  101. public Dragon(String num) {
  102. this.name = "";
  103. this.num = num;
  104. this.hp = 500;
  105. this.damage = 100;
  106. }
  107.  
  108. @Override
  109. public void receiveAttack(Creature creature) {
  110. }
  111. }
  112.  
  113. // ⑤
  114. abstract class Weapon {
  115. String type;
  116. String name;
  117. int damage;
  118. }
  119.  
  120. // ④
  121. class Sword extends Weapon {
  122. final String type = "";
  123.  
  124. public Sword() {
  125. this.name = "";
  126. this.damage = 0;
  127. }
  128.  
  129. public Sword(String name, int damage) {
  130. this.name = name;
  131. this.damage = damage;
  132. }
  133. }
  134.  
  135. // ⑦
  136. abstract class Guard {
  137. String type;
  138. String name;
  139. int defense;
  140. }
  141.  
  142. // ⑥
  143. class Shield extends Guard {
  144. final String type = "";
  145.  
  146. public Shield() {
  147. this.name = "";
  148. this.defense = 0;
  149. }
  150.  
  151. public Shield(String name, int defense) {
  152. this.name = name;
  153. this.defense = defense;
  154. }
  155. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty