第13章
コード13-01
Hero.java
public class Hero {
int hp;
String name;
Sword sword;
public void bye() {
System.out.println("勇者は別れを告げた");
}
public void die() {
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
int hp;
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-02
Hero.java
public class Hero {
int hp;
String name;
Sword sword;
public void bye() {
System.out.println("勇者は別れを告げた");
}
public void die() {
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Inn.java
public class Inn {
public void checkIn(Hero h) {
h.hp = -100;
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-03
Hero.java
public class Hero {
int hp;
String name;
Sword sword;
public void bye() {
System.out.println("勇者は別れを告げた");
}
public void die() {
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
King.java
public class King {
public void talk(Hero h) {
System.out.println("王様:ようこそ我が国へ、勇者" + h.name + "よ。");
System.out.println("王様:長旅疲れたであろう。");
System.out.println("王様:まずは城下町を見てくるとよい。ではまた会おう。");
h.die();
/* … */
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-04
Hero.java
public class Hero {
private int hp;
String name;
Sword sword;
public void bye() {
System.out.println("勇者は別れを告げた");
}
public void die() {
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Inn.java
public class Inn {
public void checkIn(Hero h) {
h.hp = -100;
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-05
Hero.java
public class Hero {
private int hp;
String name;
Sword sword;
public void bye() {
System.out.println("勇者は別れを告げた");
}
private void die() {
System.out.println(this.name + "は死んでしまった!");
System.out.println("GAME OVERです。");
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-06
Hero.java
public class Hero {
private int hp;
String name;
Sword sword;
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-07
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
King.java
public class King {
public void talk(Hero h) {
System.out.println("王様:ようこそ我が国へ、勇者" + h.name + "よ。");
System.out.println("王様:長旅疲れたであろう。");
System.out.println("王様:まずは城下町を見てくるとよい。ではまた会おう。");
h.bye();
/* … */
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-08
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public String getName(){
return this.name;
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-09
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public String getName(){
return this.name;
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
King.java
public class King {
public void talk(Hero h) {
System.out.println("王様:ようこそ我が国へ、勇者" + h.getName() + "よ。");
System.out.println("王様:長旅疲れたであろう。");
System.out.println("王様:まずは城下町を見てくるとよい。ではまた会おう。");
h.bye();
/* … */
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-10
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public String getName(){
return this.name;
}
public void setName(String name) {
this.name = name;
}
public 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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0 ) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-11
Hero.java
public class Hero {
String name;
/* … */
}
コード13-12
Hero.java
public class Hero {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/* … */
}
コード13-13
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public String getName() {
return this.name;
}
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;
}
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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0) {
this.die();
}
}
/* … */
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード13-14
Hero.java
public class Hero {
private int hp;
private String name;
private Sword sword;
public String getName() {
return this.name;
}
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;
}
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 + "から2ポイントの反撃を受けた");
this.hp -= 2;
if (this.hp <= 0) {
this.die();
}
}
/* … */
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h = new Hero();
h.setName("");
}
}
Matango.java
public class Matango {
final int LEVEL = 10;
char suffix;
public void run() {
System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}