Skip to content

Instantly share code, notes, and snippets.

@shiracamus
Last active May 12, 2022 18:24
Show Gist options
  • Save shiracamus/c33d59581d5b8dc35624245f2964478f to your computer and use it in GitHub Desktop.
Save shiracamus/c33d59581d5b8dc35624245f2964478f to your computer and use it in GitHub Desktop.
import java.util.Random;
public class RPG {
public static void main(String[] args) {
Hero hero = new Hero();
Monster monster = new Dragon();
Character attacker = hero;
Character defender = monster;
while (attacker.isAlive()) {
attacker.attack(defender);
defender.showHp();
Character nextAttacker = defender;
Character nextDefender = attacker;
attacker = nextAttacker;
defender = nextDefender;
}
if (monster.isDead()) {
System.out.println(hero.name + "は" + monster.name + "をたおした");
} else {
System.out.println(hero.name + "はちからつきた");
}
}
}
interface Attacker {
public void attack(Defender defender);
}
interface Defender {
public void defend(int damage);
}
abstract class Character implements Attacker, Defender {
public final String name;
protected int hp;
protected int maxHp;
protected int mp;
protected int maxMp;
protected final static Random random = new Random();
public Character(String name, int hp, int mp) {
this.name = name;
this.hp = hp;
this.maxHp = hp;
this.mp = mp;
this.maxMp = mp;
}
public boolean isAlive() {
return this.hp > 0;
}
public boolean isDead() {
return !isAlive();
}
public void showHp() {
System.out.println(this.name + "の現在HPは" + this.hp + "だ");
}
protected int attackPower() {
return random.nextInt(maxHp + 1);
}
public void attack(Defender defender) {
System.out.println(name + "の攻撃!");
defender.defend(attackPower());
}
public void defend(int damage) {
if (damage == 0) {
System.out.println(name + "は攻撃をかわした");
} else {
System.out.println(name + "は" + damage + "のダメージを受けた");
hp = Math.max(0, hp - damage);
}
}
}
class Hero extends Character {
private Sword sword;
private Shield shield;
public Hero() {
this("勇者", 100, 0, new Sword(), new Shield());
}
public Hero(String name, int hp, int mp, Sword sword, Shield shield) {
super(name, hp, mp);
this.sword = sword;
this.shield = shield;
}
@Override
protected int attackPower() {
return sword.power();
}
@Override
public void defend(int damage) {
super.defend(shield.protect(damage));
}
}
class Monster extends Character {
public Monster(String name, int hp, int mp) {
super(name, hp, mp);
}
protected void run() {
System.out.println(name + "は逃げ出した");
this.hp = 0;
}
@Override
public void defend(int damage) {
if (random.nextDouble() < 0.1) {
run();
} else {
super.defend(damage);
}
}
}
class Dragon extends Monster {
public Dragon() {
super("ドラゴン", 500, 100);
}
@Override
protected int attackPower() {
if (random.nextDouble() < 0.3) {
System.out.println("快心の一撃!");
return maxHp;
} else {
return random.nextInt(maxHp / 10);
}
}
}
abstract class Equipment {
public final String name;
protected final int power;
protected final static Random random = new Random();
public Equipment(String name, int power) {
this.name = name;
this.power = power;
}
}
class Sword extends Equipment {
public Sword() {
this("刀", 100);
}
public Sword(String name, int power) {
super(name, power);
}
public int power() {
return random.nextInt(power) + 1;
}
}
class Shield extends Equipment {
public Shield() {
this("盾", 10);
}
public Shield(String name, int power) {
super(name, power);
}
public int protect(int damage) {
return Math.max(0, damage - random.nextInt(power));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment