Skip to content

Instantly share code, notes, and snippets.

@raccy
Last active January 8, 2017 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raccy/9591eabbe79231ac6f691b390726b8ee to your computer and use it in GitHub Desktop.
Save raccy/9591eabbe79231ac6f691b390726b8ee to your computer and use it in GitHub Desktop.
抽象クラスを説明するためのRPGプログラム

抽象クラスを説明するためのRPGプログラム

はたしてそれはRPGなのだろうか…。

使い方

$ mkdir rpg
$ mv *.java rpg
$ javac rpg/*.java
$ java rpg/RPG

ライセンス

NYSL Version 0.9982

  1. 本ソフトウェアは Everyone'sWare です。このソフトを手にした一人一人が、 ご自分の作ったものを扱うのと同じように、自由に利用することが出来ます。

    1. フリーウェアです。作者からは使用料等を要求しません。
    2. 有料無料や媒体の如何を問わず、自由に転載・再配布できます。
    3. いかなる種類の 改変・他プログラムでの利用 を行っても構いません。
    4. 変更したものや部分的に使用したものは、あなたのものになります。 公開する場合は、あなたの名前の下で行って下さい。
  2. このソフトを利用することによって生じた損害等について、作者は 責任を負わないものとします。各自の責任においてご利用下さい。

  3. 著作者人格権は raccy に帰属します。著作権は放棄します。

  4. 以上の3項は、ソース・実行バイナリの双方に適用されます。

package rpg;
public abstract class AbstractCreature implements Battlable {
private final String name;
private final int maxHP;
private int currentHP;
protected AbstractCreature(String name, int hp) {
this.name = name;
this.maxHP = hp;
this.currentHP = hp;
}
public String getName() { return this.name; }
public boolean isAlive() { return this.getHP() > 0; }
public int getHP() { return this.currentHP; }
public int attack(Battlable other) {
return other.recieveAttack(this.getAttackPower());
}
public int recieveAttack(int attackPower) {
int damagePoint = attackPower - this.getDefencePower();
if (damagePoint < 0) damagePoint = 0;
this.changeHP(-damagePoint);
return damagePoint;
}
public void changeHP(int changePoint) {
if (changePoint == 0) return;
this.currentHP += changePoint;
if (this.currentHP > this.maxHP) {
this.currentHP = this.maxHP;
} else if (this.currentHP < 0) {
this.currentHP = 0;
}
}
public abstract int getAttackPower();
public abstract int getDefencePower();
}
package rpg;
public interface Battlable {
public String getName();
public boolean isAlive();
public int attack(Battlable other);
public int recieveAttack(int attackPower);
}
package rpg;
import java.io.PrintStream;
public class BattleField {
private boolean finished = false;
private final Battlable first;
private final Battlable second;
public BattleField(Battlable first, Battlable second) {
this.first = first;
this.second = second;
}
public boolean AttackAndDamage(Battlable b1, Battlable b2) {
System.out.println(b1.getName() + "の攻撃!");
int firstAttackDamage = b1.attack(b2);
if (firstAttackDamage == 0) {
System.out.println("ミス、ダメージは与えられない!!!");
} else {
System.out.println(b2.getName() + " に " + firstAttackDamage +
" のダメージ!!!");
}
boolean ended = false;
if (!b1.isAlive()) {
System.out.println(b1.getName() + " は倒れた…");
ended = true;
}
if (!b2.isAlive()) {
System.out.println(b2.getName() + " は倒れた…");
ended = true;
}
return ended;
}
public void start() {
if (this.finished) {
System.out.println("戦闘は既に終了しています。");
return;
}
System.out.println(first.getName() + " vs " + second.getName() +
" の戦闘を開始します。");
int maxTurn = 256;
for (int i = 0; i < maxTurn; i++) {
if (AttackAndDamage(first, second)) break;
if (AttackAndDamage(second, first)) break;
}
if (first.isAlive()) {
if (second.isAlive()) {
System.out.println("決着は付かなかった。");
} else {
System.out.println(first.getName() + "の勝ち。");
}
} else {
if (second.isAlive()) {
System.out.println(second.getName() + "の勝ち。");
} else {
System.out.println("相打ち。");
}
}
this.finished = true;
}
}
package rpg;
import java.util.Random;
public class Human extends AbstractCreature {
private int level;
private int strength;
private int vitality;
final private Random random;
public Human(String name, int level, int hp, int strength, int vitality) {
super(name, hp);
this.level = level;
this.strength = strength;
this.vitality = vitality;
this.random = new Random();
}
public int getAttackPower() {
return this.level * this.strength + random.nextInt(8);
}
public int getDefencePower() {
return this.level * this.vitality + random.nextInt(8);
}
@Override
public int attack(Battlable other) {
if (random.nextInt(8) > 6) {
System.out.println(
this.getName() +
"「必殺・ゼロディメンションエターナルストリームアタック!!!」");
this.changeHP(-100);
return other.recieveAttack(3 * this.getAttackPower());
} else {
return super.attack(other);
}
}
}
package rpg;
public class Monster extends AbstractCreature {
private final int attackPower;
private final int defencePower;
public Monster(String name, int hp, int attackPower, int defencePower) {
super(name, hp);
this.attackPower = attackPower;
this.defencePower = defencePower;
}
public int getAttackPower() { return this.attackPower; }
public int getDefencePower() { return this.defencePower; }
}
package rpg;
public class RPG {
public static void main(String[] args) {
Battlable player = new Human("勇者", 5, 150, 5, 5);
Battlable enemy = new Monster("スライム", 100, 30, 20);
BattleField baddleField = new BattleField(player, enemy);
baddleField.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment