part2-10
午前中の分のコード(一部未完成)
--->Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
private static int money;
//--------------------------
//このひとらがコンストラクタ
//--------------------------
Hero(){
this("ダミー");
}
Hero(String name){
this.hp = 100;
this.name = name;
}
void bye(){
System.out.println("勇者は別れを告げた");
}
//--------------------------
//このひとらがメソッド
//--------------------------
private void die() {//修正
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
void sleep() {
this.hp = 100;
System.out.println(this.name + "は、眠って回復した!");
}
public void attack(Matango m){
System.out.println(this.name + "の攻撃!");
System.out.println("お化けキノコ" + m.suffix + "に" +
sword.damage + "ポイントのダメージを与えた");
m.hp -= sword.damage;
if(m.hp <= 0) {
System.out.println("お化けキノコ" + m.suffix + "を倒した!");
} else {
System.out.println("お化けキノコ" + m.suffix + "から2ポイントの反撃を受けた");
this.hp -= 2;
if(this.hp <= 0) {
this.die();
}
}
}
//--------------------------
//getter/setter自動生成
//--------------------------
public int getHp() {
return hp;
}
public void setHp(int hp) {
if( hp<0) {//マイナスの値は受け付けない
throw new IllegalArgumentException
("hpの値がマイナスだ!処理を止めますよ");
}
this.hp = hp;
}
public Sword getSword() {
return sword;
}
public void setSword(Sword sword) {
this.sword = sword;
}
public static int getMoney() {
return money;
}
public static void setMoney(int money) {
Hero.money = money;
}
void sit(int sec) {
this.hp += sec;
System.out.println(this.name + "は、" + sec + "秒すわった!");
System.out.println("HPが" + sec + "ポイント回復した.");
}
void slip() {
this.hp -= 5;
System.out.println(this.name + "は転んだ!");
System.out.println("5のダメージ!");
if(this.hp <= 0) {
die();
}
}
void run() {
System.out.println(this.name + "は、逃げ出した!");
System.out.println("GAMEOVER");
System.out.println("最終HPは" + this.hp + "でした");
}
static void setRandomMoney() {
Hero.money = (int)(Math.random() * 1000);
}
//名前を返すだけのメソッド/getter method
public String getName(){
return this.name;
}
//値の妥当性がチェックできる/setter method
public void setName(String name) {
if(name==null) {
throw new IllegalArgumentException
("名前がnullかい!処理を止めますよ");
}
if(name.length()<=1) {
throw new IllegalArgumentException
("名前短すぎやん、処理を止めますよ");
}
if(name.length()>=8) {
throw new IllegalArgumentException
("名前長すぎるわ、処理を止めますよ");
}
//上の非妥当性条件にすべて当てはまらない
this.name=name;
}
}
--->Main.java
public class Hero {
private int hp;
private String name;
private Sword sword;
private static int money;
//--------------------------
//このひとらがコンストラクタ
//--------------------------
Hero(){
this("ダミー");
}
Hero(String name){
this.hp = 100;
this.name = name;
}
void bye(){
System.out.println("勇者は別れを告げた");
}
//--------------------------
//このひとらがメソッド
//--------------------------
private void die() {//修正
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
void sleep() {
this.hp = 100;
System.out.println(this.name + "は、眠って回復した!");
}
public void attack(Matango m){
System.out.println(this.name + "の攻撃!");
System.out.println("お化けキノコ" + m.suffix + "に" +
sword.damage + "ポイントのダメージを与えた");
m.hp -= sword.damage;
if(m.hp <= 0) {
System.out.println("お化けキノコ" + m.suffix + "を倒した!");
} else {
System.out.println("お化けキノコ" + m.suffix + "から2ポイントの反撃を受けた");
this.hp -= 2;
if(this.hp <= 0) {
this.die();
}
}
}
//--------------------------
//getter/setter自動生成
//--------------------------
public int getHp() {
return hp;
}
public void setHp(int hp) {
if( hp<0) {//マイナスの値は受け付けない
throw new IllegalArgumentException
("hpの値がマイナスだ!処理を止めますよ");
}
this.hp = hp;
}
public Sword getSword() {
return sword;
}
public void setSword(Sword sword) {
this.sword = sword;
}
public static int getMoney() {
return money;
}
public static void setMoney(int money) {
Hero.money = money;
}
void sit(int sec) {
this.hp += sec;
System.out.println(this.name + "は、" + sec + "秒すわった!");
System.out.println("HPが" + sec + "ポイント回復した.");
}
void slip() {
this.hp -= 5;
System.out.println(this.name + "は転んだ!");
System.out.println("5のダメージ!");
if(this.hp <= 0) {
die();
}
}
void run() {
System.out.println(this.name + "は、逃げ出した!");
System.out.println("GAMEOVER");
System.out.println("最終HPは" + this.hp + "でした");
}
static void setRandomMoney() {
Hero.money = (int)(Math.random() * 1000);
}
//名前を返すだけのメソッド/getter method
public String getName(){
return this.name;
}
//値の妥当性がチェックできる/setter method
public void setName(String name) {
if(name==null) {
throw new IllegalArgumentException
("名前がnullかい!処理を止めますよ");
}
if(name.length()<=1) {
throw new IllegalArgumentException
("名前短すぎやん、処理を止めますよ");
}
if(name.length()>=8) {
throw new IllegalArgumentException
("名前長すぎるわ、処理を止めますよ");
}
//上の非妥当性条件にすべて当てはまらない
this.name=name;
}
}
--->King.java
public class King {
void talk(Hero h) {
System.out.println("王様:ようこそ我が国へ、勇者" + h.getName() + "よ。");
System.out.println("王様:長旅疲れたであろう、そこで横になって休むがよい");
//h.die(); // ここが不具合の原因。勇者が死ぬ!
h.bye();
}
}
--->Inn.java
public class Inn {
void checkIn(Hero h) {
//h.hp = -100;
h.setHp(-100);//怒られるテスト
}
}
--->Wand.java
public class Wand {
private String name; // 杖の名前
private double power; //杖の魔力
//------------------------------
//自動生成されたgetter/setter
//------------------------------
public String getName() {
return name;
}
public void setName(String name) {//名前をセットするときの正誤チェック
if(name==null) {
throw new IllegalArgumentException
("名前がnull、処理中止");
}
if(name.length()<3) {
throw new IllegalArgumentException
("名前3文字より小、処理中止");
}
this.name = name;
}
public double getPower() {
return power;
}
public void setPower(double power) {//杖の魔力セットの正誤チェック
if(power<0.5||power>100.0 ){
throw new IllegalArgumentException
("魔力設定異常、処理中止");
}
this.power = power;
}
}
--->Wizard.java
public class Wizard {
private int hp;
private int mp;
private String name;
private Wand wand;
//-------------------------------
//メソッドさん
//-------------------------------
void heal(Hero h) {
int basepoint = 10;//基本回復ポイント
int recovPoint = (int)(basepoint * this.wand.getPower());
//wand.getPower:杖による増幅
//なんかばってんダブルクリックしたら勝手にpowerがgetPowerになったがそれでいいのかわからない
h.setHp(h.getHp()+recovPoint);;
System.out.println(h.getName()+"のHPを"+recovPoint+"回復");
}
//-------------------------------
//自動生成されたgetter/setter
//-------------------------------
public int getHp() {
return hp;
}
public void setHp(int hp) {
if( hp<0) {//マイナスの値が設定されそう
hp=0;
}else {//ここはelse条件じゃないとうまくない。上書きされるからな
this.hp = hp;
}
}
public int getMp() {
return mp;
}
public void setMp(int mp) {
if(mp<0) {
throw new IllegalArgumentException
("魔力値設定異常");
}
this.mp = mp;
}
public String getName() {
return name;
}
public void setName(String name) {//名前セットするときの正誤チェック
if(name==null) {
throw new IllegalArgumentException
("名前がnull、処理中止");
}
if(name.length()<3) {
throw new IllegalArgumentException
("名前3文字より小、処理中止");
}
this.name = name;
}
public Wand getWand() {
return wand;
}
public void setWand(Wand wand) {
if(wand==null) {
throw new IllegalArgumentException
("設定したい杖がnull");
}
this.wand = wand;
}
}