課題2 継承完了
aゴブリンと勇者にcharacterを継承させることができたけど、継承のコードかなり短い。。
こんなものなのかな、継承させるものが少なすぎる。
よし、次は
1. モンスターが現れる。
2. 勇者はどのモンスターを攻撃するか選ぶ。
3. 勇者の攻撃が実行される。
4. 全てのモンスターのHPが0になった場合ゲーム終了。
そうでない場合、1に戻る。
の4番をやってみます。
Main
public class Main {
public static void main(String[] args) {
//勇者を作成
Character c = new Character();
c.name = "やっつん";
System.out.println("生物" + c.name + "が第一世界に召喚された。");
Hero h = new Hero();
h.name = "マッサル";
h.hp = 100;
h.power = 20;
System.out.println("勇者" + h.name + "が第一世界に召喚された。HP" + h.hp);
Goblin g1 = new Goblin();
g1.name = "ゴブリン";
g1.hp = 50;
g1.defence = 10;
g1.suffix = 'A';
System.out.println("ゴブリン" + g1.suffix + "が現れた!!");
g1.dance();
g1.die();
h.damage = h.power - g1.defence;
h.sleep(10);
h.attack(g1);
SuperHero sh = new SuperHero();
sh.run();
}
public static void main(String[] args) {
//勇者を作成
Character c = new Character();
c.name = "やっつん";
System.out.println("生物" + c.name + "が第一世界に召喚された。");
Hero h = new Hero();
h.name = "マッサル";
h.hp = 100;
h.power = 20;
System.out.println("勇者" + h.name + "が第一世界に召喚された。HP" + h.hp);
Goblin g1 = new Goblin();
g1.name = "ゴブリン";
g1.hp = 50;
g1.defence = 10;
g1.suffix = 'A';
System.out.println("ゴブリン" + g1.suffix + "が現れた!!");
g1.dance();
g1.die();
h.damage = h.power - g1.defence;
h.sleep(10);
h.attack(g1);
SuperHero sh = new SuperHero();
sh.run();
}
}
Character
public class Character {
String name;
int hp;
int power;
int damage;
int defence;
int recover;
String name;
int hp;
int power;
int damage;
int defence;
int recover;
}
Hero
public class Hero extends Character {
void sleep(int hour){
recover = 10 * hour;
this.hp += recover;
System.out.println(this.name + "は、" + hour + "時間眠って" + recover + "回復した。");
System.out.println(this.name + "の現在のHPは" + this.hp + "です。");
}
void attack(Goblin g1){
System.out.println(this.name + "は、" + g1.name + "に" + damage + "ダメージを与えた");
g1.hp -= damage;
System.out.println(g1.name + "の現在のHPは" + g1.hp + "です。");
}
void sleep(int hour){
recover = 10 * hour;
this.hp += recover;
System.out.println(this.name + "は、" + hour + "時間眠って" + recover + "回復した。");
System.out.println(this.name + "の現在のHPは" + this.hp + "です。");
}
void attack(Goblin g1){
System.out.println(this.name + "は、" + g1.name + "に" + damage + "ダメージを与えた");
g1.hp -= damage;
System.out.println(g1.name + "の現在のHPは" + g1.hp + "です。");
}
}
Goblin
public class Goblin extends Character{
char suffix;
void dance(){
System.out.println("ゴブリンは無我夢中でダンスした");
}
void die(){
if(hp<0){
System.out.println("ゴブリンは動かなくなった。");
}
}
char suffix;
void dance(){
System.out.println("ゴブリンは無我夢中でダンスした");
}
void die(){
if(hp<0){
System.out.println("ゴブリンは動かなくなった。");
}
}
}