fork download
  1. import java.util.*;
  2. class Player6{
  3. protected int maxHp;
  4. protected int hp;
  5. protected int mp;
  6. protected int place;
  7. protected int ratio;
  8. protected Random rnd;
  9. public Player6(int maxHp, int mp, int ratio) {
  10. this.maxHp = maxHp; // 最大HPのセット
  11. this.hp = maxHp; // 現在HPを最大HPで初期化
  12. this.mp = mp; // MPのセット
  13. this.ratio = ratio; // 回復確率のセット
  14. this.rnd = new Random(); // ランダムオブジェクトの生成
  15. this.place = 0; // 初期位置を0に設定
  16. }
  17.  
  18. public boolean isAlive(){
  19. return hp>=1;
  20. }
  21.  
  22. public void work() {
  23. if (isAlive()) { // 生存している場合のみ行動する
  24. int chance = rnd.nextInt(100); // 0から99のランダムな数を生成
  25. if (chance < ratio) { // 回復を試みる (ratio% の確率)
  26. if (mp > 0) {
  27. hp = maxHp; // HPを最大値に回復
  28. mp--; // MPを1減らす
  29. System.out.println("Healing successful! HP restored to max.");
  30. } else {
  31. hp--; // HPを1減らす(回復失敗)
  32. System.out.println("Healing failed! Not enough MP.");
  33. }
  34. } else { // 前に進む (100 - ratio% の確率)
  35. place++; // 位置を1進める
  36. hp--; // HPを1減らす
  37. System.out.println("Moved forward.");
  38. }
  39. }
  40. }
  41. public void printStatus() {
  42. System.out.println("HP: " + hp + ", MP: " + mp + ", Place: " + place);
  43. }
  44.  
  45. }
  46. class Game6 {
  47. public static void main(String[] args) {
  48. // Player6型のインスタンスを作成(引数例: maxHp=10, mp=3, ratio=50)
  49. Player6 p = new Player6(10, 3, 50);
  50. play(p);
  51. }
  52. public static void play(Player6 p) {
  53. while (p.isAlive()) { // プレイヤーが生存している間ループ
  54. p.work(); // 行動
  55. p.printStatus(); // ステータスを表示
  56. }
  57. System.out.println("Player has died. Game over.");
  58. }
  59. }
  60.  
Success #stdin #stdout 0.13s 58048KB
stdin
Standard input is empty
stdout
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.