import java.util.*; class Player6{ protected int maxHp; protected int hp; protected int mp; protected int place; protected int ratio; public Player6(int maxHp, int mp, int ratio) { this.maxHp = maxHp; // 最大HPのセット this.hp = maxHp; // 現在HPを最大HPで初期化 this.mp = mp; // MPのセット this.ratio = ratio; // 回復確率のセット this.place = 0; // 初期位置を0に設定 } public boolean isAlive(){ return hp>=1; } public void work() { if (isAlive()) { // 生存している場合のみ行動する int chance = rnd.nextInt(100); // 0から99のランダムな数を生成 if (chance < ratio) { // 回復を試みる (ratio% の確率) if (mp > 0) { hp = maxHp; // HPを最大値に回復 mp--; // MPを1減らす } else { hp--; // HPを1減らす(回復失敗) } } else { // 前に進む (100 - ratio% の確率) place++; // 位置を1進める hp--; // HPを1減らす } } } public void printStatus() { } } class Game6 { // Player6型のインスタンスを作成(引数例: maxHp=10, mp=3, ratio=50) Player6 p = new Player6(10, 3, 50); play(p); } public static void play(Player6 p) { while (p.isAlive()) { // プレイヤーが生存している間ループ p.work(); // 行動 p.printStatus(); // ステータスを表示 } } }
Standard input is empty
Moved forward. HP: 9, MP: 3, Place: 1 Moved forward. HP: 8, MP: 3, Place: 2 Healing successful! HP restored to max. HP: 10, MP: 2, Place: 2 Healing successful! HP restored to max. HP: 10, MP: 1, Place: 2 Healing successful! HP restored to max. HP: 10, MP: 0, Place: 2 Moved forward. HP: 9, MP: 0, Place: 3 Moved forward. HP: 8, MP: 0, Place: 4 Healing failed! Not enough MP. HP: 7, MP: 0, Place: 4 Healing failed! Not enough MP. HP: 6, MP: 0, Place: 4 Healing failed! Not enough MP. HP: 5, MP: 0, Place: 4 Healing failed! Not enough MP. HP: 4, MP: 0, Place: 4 Moved forward. HP: 3, MP: 0, Place: 5 Healing failed! Not enough MP. HP: 2, MP: 0, Place: 5 Moved forward. HP: 1, MP: 0, Place: 6 Healing failed! Not enough MP. HP: 0, MP: 0, Place: 6 Player has died. Game over.