From 604e538c8defa1eb1543c4be24d74f275a60a876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Wed, 2 Jul 2025 17:14:26 +0900 Subject: [PATCH 1/7] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC148?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/FightSimulation.java | 185 +++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 ntakanabe/src/FightSimulation.java diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java new file mode 100644 index 0000000..58bbcac --- /dev/null +++ b/ntakanabe/src/FightSimulation.java @@ -0,0 +1,185 @@ +package paiza.src; + +import java.util.Scanner; + +/** + * . C148 異世界転生した勇者のレベル変化をシミュレーションするメインクラス 戦闘回数と初期レベル、そして各戦闘相手のレベルを入力として受け取り、 N回の戦闘後の勇者のレベルを出力する + * + * @author ntakanabe + */ +public class FightSimulation { + + /** + * . 入力メソッド、戦闘シミュレーションメソッド、出力メソッドの呼び出しを行うmainメソッド + * + * @param args コマンドライン引数 + */ + public static void main(final String[] args) { + + final SimulateParams params = getInputSimulation(); + final int finalHeroLevel = simulateCombat(params.getNumOfBattles(), params + .getInitialHeroLevel(), params.getScanner()); + outputResult(finalHeroLevel); + } + + /** + * . 戦闘回数と勇者の初期レベルを読み込むメソッド + * + * @return シミュレーションに必要な入力値を含むSimulationParamsオブジェクト + */ + private static SimulateParams getInputSimulation() { + + final Scanner scan = new Scanner(System.in); + final int numOfBattles = scan.nextInt(); + final int initialHeroLevel = scan.nextInt(); + return new SimulateParams(numOfBattles, initialHeroLevel, scan); + } + + /** + * . 戦闘シミュレーションを実行し、最終的な勇者のレベルを計算するメソッド + * + * @param numOfBattles 戦闘回数 + * @param initialHeroLevel 勇者の初期レベル + * @param scan 敵のレベルを読み込むためのScannerオブジェクト + * @return N回の戦闘後の勇者の最終レベル + */ + private static int simulateCombat(final int numOfBattles, final int initialHeroLevel, + final Scanner scan) { + + final Hero hero = new Hero(initialHeroLevel); + final BattleSimulator simulator = new BattleSimulator(hero, scan); + simulator.simulateBattles(numOfBattles); + + scan.close(); + return hero.getLevel(); + } + + /** + * . シミュレーション結果(勇者の最終レベル)を出力するメソッド + * + * @param finalHeroLevel 勇者の最終レベル + */ + private static void outputResult(final int finalHeroLevel) { + + System.out.println(finalHeroLevel); + } + + /** + * . シミュレーション入力を保持するヘルパークラス + */ + private static class SimulateParams { + + private final int numOfBattles; + private final int initialHeroLevel; + private final Scanner scan; + + public SimulateParams(final int numOfBattles, final int initialHeroLevel, + final Scanner scan) { + + this.numOfBattles = numOfBattles; + this.initialHeroLevel = initialHeroLevel; + this.scan = scan; + } + + public int getNumOfBattles() { + + return numOfBattles; + } + + public int getInitialHeroLevel() { + + return initialHeroLevel; + } + + public Scanner getScanner() { + + return scan; + } + } +} + + +/** + * . 戦闘による勇者のレベル増減の計算を行うクラス + */ +class Hero { + + // 勇者の現在のレベル + private int level; + + /** + * . 勇者オブジェクトを生成するコンストラクタ + * + * @param initialLevel 勇者の初期レベル + */ + public Hero(final int initialLevel) { + + this.level = initialLevel; + } + + /** + * . 勇者の現在のレベルを返すメソッド + * + * @return 勇者の現在のレベル + */ + public int getLevel() { + + return level; + } + + /** + * . 勇者と敵のレベルに基づいて戦闘結果を処理し、勇者のレベルを更新するメソッド + * + * @param enemyLevel 敵のレベル + */ + public void updateLevel(final int enemyLevel) { + + if (this.level > enemyLevel) { + // 勇者の勝利 + final int levelIncrease = enemyLevel / 2; // 相手のレベルの半分だけ上昇 + this.level += levelIncrease; + } else if (this.level < enemyLevel) { + // 勇者の敗北 + this.level /= 2; // 勇者のレベルが半分になる + } else { + // 引き分け(レベルが同じなので、変動なし) + } + } +} + + +/** + * . 勇者と敵のレベルの入力を受け取り、指定された回数の戦闘をシミュレーションするクラス + */ +class BattleSimulator { + + // シミュレーションの対象となる勇者オブジェクト + private final Hero hero; + // 戦闘相手のレベルを読み取るためのScannerオブジェクト + private final Scanner scan; + + /** + * . 新しい戦闘シミュレーターを生成するコンストラクタ + * + * @param hero シミュレーションを行う勇者オブジェクト + * @param scan 入力を読み取るためのScannerオブジェクト + */ + public BattleSimulator(final Hero hero, final Scanner scan) { + + this.hero = hero; + this.scan = scan; + } + + /** + * . 指定された回数だけ、入力された敵のレベルを読み込み、戦闘処理を行うメソッド + * + * @param numOfBattles 戦闘回数 + */ + public void simulateBattles(final int numOfBattles) { + + for (int i = 0; i < numOfBattles; i++) { + final int enemyLevel = scan.nextInt(); // 敵のレベルを読み込む + hero.updateLevel(enemyLevel); // 勇者と敵のレベルに基づいて戦闘を処理 + } + } +} -- GitLab From aeee29d762d0695e4979f453869e276aa7b4ac14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Thu, 3 Jul 2025 15:48:23 +0900 Subject: [PATCH 2/7] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC=5F148?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/FightSimulation.java | 111 ++++++++++++----------------- 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index 58bbcac..fadfb31 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -16,23 +16,35 @@ public class FightSimulation { */ public static void main(final String[] args) { - final SimulateParams params = getInputSimulation(); - final int finalHeroLevel = simulateCombat(params.getNumOfBattles(), params - .getInitialHeroLevel(), params.getScanner()); + final int[] inputValues = getInputSimulation(); + final int numOfBattles = inputValues[0]; + final int initialHeroLevel = inputValues[1]; + final int[] enemyLevels = new int[numOfBattles]; + System.arraycopy(inputValues, 2, enemyLevels, 0, numOfBattles); + + final int finalHeroLevel = simulateCombat(numOfBattles, initialHeroLevel, enemyLevels); outputResult(finalHeroLevel); } /** - * . 戦闘回数と勇者の初期レベルを読み込むメソッド + * . 戦闘回数と勇者の初期レベル、および各敵のレベルを読み込むメソッド * - * @return シミュレーションに必要な入力値を含むSimulationParamsオブジェクト + * @return 読み込んだ値を含むint配列。要素[0]が戦闘回数、[1]が初期レベル、それ以降が敵のレベル */ - private static SimulateParams getInputSimulation() { + private static int[] getInputSimulation() { final Scanner scan = new Scanner(System.in); final int numOfBattles = scan.nextInt(); final int initialHeroLevel = scan.nextInt(); - return new SimulateParams(numOfBattles, initialHeroLevel, scan); + final int[] allInputs = new int[2 + numOfBattles]; + allInputs[0] = numOfBattles; + allInputs[1] = initialHeroLevel; + for (int i = 0; i < numOfBattles; i++) { + allInputs[2 + i] = scan.nextInt(); + } + scan.close(); + + return allInputs; } /** @@ -40,17 +52,15 @@ public class FightSimulation { * * @param numOfBattles 戦闘回数 * @param initialHeroLevel 勇者の初期レベル - * @param scan 敵のレベルを読み込むためのScannerオブジェクト + * @param enemyLevels 敵のレベルの配列 * @return N回の戦闘後の勇者の最終レベル */ private static int simulateCombat(final int numOfBattles, final int initialHeroLevel, - final Scanner scan) { + final int[] enemyLevels) { final Hero hero = new Hero(initialHeroLevel); - final BattleSimulator simulator = new BattleSimulator(hero, scan); - simulator.simulateBattles(numOfBattles); - - scan.close(); + final BattleSimulator battle = new BattleSimulator(hero, enemyLevels); + battle.simulate(numOfBattles); return hero.getLevel(); } @@ -63,39 +73,6 @@ public class FightSimulation { System.out.println(finalHeroLevel); } - - /** - * . シミュレーション入力を保持するヘルパークラス - */ - private static class SimulateParams { - - private final int numOfBattles; - private final int initialHeroLevel; - private final Scanner scan; - - public SimulateParams(final int numOfBattles, final int initialHeroLevel, - final Scanner scan) { - - this.numOfBattles = numOfBattles; - this.initialHeroLevel = initialHeroLevel; - this.scan = scan; - } - - public int getNumOfBattles() { - - return numOfBattles; - } - - public int getInitialHeroLevel() { - - return initialHeroLevel; - } - - public Scanner getScanner() { - - return scan; - } - } } @@ -104,7 +81,6 @@ public class FightSimulation { */ class Hero { - // 勇者の現在のレベル private int level; /** @@ -128,21 +104,26 @@ class Hero { } /** - * . 勇者と敵のレベルに基づいて戦闘結果を処理し、勇者のレベルを更新するメソッド + * . 勇者と敵の戦闘における勝利判定をするメソッド + * + * @return 勇者の勝利 + */ + private boolean isHeroWin(final int heroLevel, final int enemyLevel) { + + return heroLevel > enemyLevel; + } + + /** + * . 勝利判定メソッドにより、勇者のレベルを更新するメソッド * * @param enemyLevel 敵のレベル */ public void updateLevel(final int enemyLevel) { - if (this.level > enemyLevel) { - // 勇者の勝利 - final int levelIncrease = enemyLevel / 2; // 相手のレベルの半分だけ上昇 - this.level += levelIncrease; - } else if (this.level < enemyLevel) { - // 勇者の敗北 - this.level /= 2; // 勇者のレベルが半分になる - } else { - // 引き分け(レベルが同じなので、変動なし) + if (isHeroWin(this.level, enemyLevel)) { + this.level += enemyLevel / 2; + } else if (!isHeroWin(this.level, enemyLevel) && this.level != enemyLevel) { + this.level /= 2; } } } @@ -155,19 +136,19 @@ class BattleSimulator { // シミュレーションの対象となる勇者オブジェクト private final Hero hero; - // 戦闘相手のレベルを読み取るためのScannerオブジェクト - private final Scanner scan; + // 敵のレベルの配列 + private final int[] enemyLevels; /** * . 新しい戦闘シミュレーターを生成するコンストラクタ * * @param hero シミュレーションを行う勇者オブジェクト - * @param scan 入力を読み取るためのScannerオブジェクト + * @param enemyLevels 敵のレベルの配列 */ - public BattleSimulator(final Hero hero, final Scanner scan) { + public BattleSimulator(final Hero hero, final int[] enemyLevels) { this.hero = hero; - this.scan = scan; + this.enemyLevels = enemyLevels; } /** @@ -175,11 +156,11 @@ class BattleSimulator { * * @param numOfBattles 戦闘回数 */ - public void simulateBattles(final int numOfBattles) { + public void simulate(final int numOfBattles) { for (int i = 0; i < numOfBattles; i++) { - final int enemyLevel = scan.nextInt(); // 敵のレベルを読み込む - hero.updateLevel(enemyLevel); // 勇者と敵のレベルに基づいて戦闘を処理 + final int enemyLevel = enemyLevels[i]; + hero.updateLevel(enemyLevel); } } } -- GitLab From f53726cc80a9cf27ee97d4053b84b0e0f32c064f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Thu, 3 Jul 2025 16:13:14 +0900 Subject: [PATCH 3/7] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC=5F148?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/FightSimulation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index fadfb31..29c205f 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -152,7 +152,7 @@ class BattleSimulator { } /** - * . 指定された回数だけ、入力された敵のレベルを読み込み、戦闘処理を行うメソッド + * . 指定された回数だけ入力された敵のレベルを読み込み、戦闘処理を行うメソッド * * @param numOfBattles 戦闘回数 */ -- GitLab From 11557f7632e9c1cbe3aa2bf3833e41f55e225003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Thu, 3 Jul 2025 16:58:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?Revert=20"paiza=E3=81=AE=E5=95=8F=E9=A1=8CC?= =?UTF-8?q?=5F148=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=AE=E6=AD=A3"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f53726cc80a9cf27ee97d4053b84b0e0f32c064f. --- ntakanabe/src/FightSimulation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index 29c205f..fadfb31 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -152,7 +152,7 @@ class BattleSimulator { } /** - * . 指定された回数だけ入力された敵のレベルを読み込み、戦闘処理を行うメソッド + * . 指定された回数だけ、入力された敵のレベルを読み込み、戦闘処理を行うメソッド * * @param numOfBattles 戦闘回数 */ -- GitLab From 500577c44666b9674ebd4a8d4d5c2da8deb67684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Thu, 3 Jul 2025 16:59:58 +0900 Subject: [PATCH 5/7] =?UTF-8?q?Revert=20"paiza=E3=81=AE=E5=95=8F=E9=A1=8CC?= =?UTF-8?q?=5F148=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=AE=E6=AD=A3"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit aeee29d762d0695e4979f453869e276aa7b4ac14. --- ntakanabe/src/FightSimulation.java | 111 +++++++++++++++++------------ 1 file changed, 65 insertions(+), 46 deletions(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index fadfb31..58bbcac 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -16,35 +16,23 @@ public class FightSimulation { */ public static void main(final String[] args) { - final int[] inputValues = getInputSimulation(); - final int numOfBattles = inputValues[0]; - final int initialHeroLevel = inputValues[1]; - final int[] enemyLevels = new int[numOfBattles]; - System.arraycopy(inputValues, 2, enemyLevels, 0, numOfBattles); - - final int finalHeroLevel = simulateCombat(numOfBattles, initialHeroLevel, enemyLevels); + final SimulateParams params = getInputSimulation(); + final int finalHeroLevel = simulateCombat(params.getNumOfBattles(), params + .getInitialHeroLevel(), params.getScanner()); outputResult(finalHeroLevel); } /** - * . 戦闘回数と勇者の初期レベル、および各敵のレベルを読み込むメソッド + * . 戦闘回数と勇者の初期レベルを読み込むメソッド * - * @return 読み込んだ値を含むint配列。要素[0]が戦闘回数、[1]が初期レベル、それ以降が敵のレベル + * @return シミュレーションに必要な入力値を含むSimulationParamsオブジェクト */ - private static int[] getInputSimulation() { + private static SimulateParams getInputSimulation() { final Scanner scan = new Scanner(System.in); final int numOfBattles = scan.nextInt(); final int initialHeroLevel = scan.nextInt(); - final int[] allInputs = new int[2 + numOfBattles]; - allInputs[0] = numOfBattles; - allInputs[1] = initialHeroLevel; - for (int i = 0; i < numOfBattles; i++) { - allInputs[2 + i] = scan.nextInt(); - } - scan.close(); - - return allInputs; + return new SimulateParams(numOfBattles, initialHeroLevel, scan); } /** @@ -52,15 +40,17 @@ public class FightSimulation { * * @param numOfBattles 戦闘回数 * @param initialHeroLevel 勇者の初期レベル - * @param enemyLevels 敵のレベルの配列 + * @param scan 敵のレベルを読み込むためのScannerオブジェクト * @return N回の戦闘後の勇者の最終レベル */ private static int simulateCombat(final int numOfBattles, final int initialHeroLevel, - final int[] enemyLevels) { + final Scanner scan) { final Hero hero = new Hero(initialHeroLevel); - final BattleSimulator battle = new BattleSimulator(hero, enemyLevels); - battle.simulate(numOfBattles); + final BattleSimulator simulator = new BattleSimulator(hero, scan); + simulator.simulateBattles(numOfBattles); + + scan.close(); return hero.getLevel(); } @@ -73,6 +63,39 @@ public class FightSimulation { System.out.println(finalHeroLevel); } + + /** + * . シミュレーション入力を保持するヘルパークラス + */ + private static class SimulateParams { + + private final int numOfBattles; + private final int initialHeroLevel; + private final Scanner scan; + + public SimulateParams(final int numOfBattles, final int initialHeroLevel, + final Scanner scan) { + + this.numOfBattles = numOfBattles; + this.initialHeroLevel = initialHeroLevel; + this.scan = scan; + } + + public int getNumOfBattles() { + + return numOfBattles; + } + + public int getInitialHeroLevel() { + + return initialHeroLevel; + } + + public Scanner getScanner() { + + return scan; + } + } } @@ -81,6 +104,7 @@ public class FightSimulation { */ class Hero { + // 勇者の現在のレベル private int level; /** @@ -104,26 +128,21 @@ class Hero { } /** - * . 勇者と敵の戦闘における勝利判定をするメソッド - * - * @return 勇者の勝利 - */ - private boolean isHeroWin(final int heroLevel, final int enemyLevel) { - - return heroLevel > enemyLevel; - } - - /** - * . 勝利判定メソッドにより、勇者のレベルを更新するメソッド + * . 勇者と敵のレベルに基づいて戦闘結果を処理し、勇者のレベルを更新するメソッド * * @param enemyLevel 敵のレベル */ public void updateLevel(final int enemyLevel) { - if (isHeroWin(this.level, enemyLevel)) { - this.level += enemyLevel / 2; - } else if (!isHeroWin(this.level, enemyLevel) && this.level != enemyLevel) { - this.level /= 2; + if (this.level > enemyLevel) { + // 勇者の勝利 + final int levelIncrease = enemyLevel / 2; // 相手のレベルの半分だけ上昇 + this.level += levelIncrease; + } else if (this.level < enemyLevel) { + // 勇者の敗北 + this.level /= 2; // 勇者のレベルが半分になる + } else { + // 引き分け(レベルが同じなので、変動なし) } } } @@ -136,19 +155,19 @@ class BattleSimulator { // シミュレーションの対象となる勇者オブジェクト private final Hero hero; - // 敵のレベルの配列 - private final int[] enemyLevels; + // 戦闘相手のレベルを読み取るためのScannerオブジェクト + private final Scanner scan; /** * . 新しい戦闘シミュレーターを生成するコンストラクタ * * @param hero シミュレーションを行う勇者オブジェクト - * @param enemyLevels 敵のレベルの配列 + * @param scan 入力を読み取るためのScannerオブジェクト */ - public BattleSimulator(final Hero hero, final int[] enemyLevels) { + public BattleSimulator(final Hero hero, final Scanner scan) { this.hero = hero; - this.enemyLevels = enemyLevels; + this.scan = scan; } /** @@ -156,11 +175,11 @@ class BattleSimulator { * * @param numOfBattles 戦闘回数 */ - public void simulate(final int numOfBattles) { + public void simulateBattles(final int numOfBattles) { for (int i = 0; i < numOfBattles; i++) { - final int enemyLevel = enemyLevels[i]; - hero.updateLevel(enemyLevel); + final int enemyLevel = scan.nextInt(); // 敵のレベルを読み込む + hero.updateLevel(enemyLevel); // 勇者と敵のレベルに基づいて戦闘を処理 } } } -- GitLab From db491296726e5a00fd342df1020a7bb22cc05050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Thu, 3 Jul 2025 17:01:30 +0900 Subject: [PATCH 6/7] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC=5F148?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/FightSimulation.java | 111 ++++++++++++----------------- 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index 58bbcac..fadfb31 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -16,23 +16,35 @@ public class FightSimulation { */ public static void main(final String[] args) { - final SimulateParams params = getInputSimulation(); - final int finalHeroLevel = simulateCombat(params.getNumOfBattles(), params - .getInitialHeroLevel(), params.getScanner()); + final int[] inputValues = getInputSimulation(); + final int numOfBattles = inputValues[0]; + final int initialHeroLevel = inputValues[1]; + final int[] enemyLevels = new int[numOfBattles]; + System.arraycopy(inputValues, 2, enemyLevels, 0, numOfBattles); + + final int finalHeroLevel = simulateCombat(numOfBattles, initialHeroLevel, enemyLevels); outputResult(finalHeroLevel); } /** - * . 戦闘回数と勇者の初期レベルを読み込むメソッド + * . 戦闘回数と勇者の初期レベル、および各敵のレベルを読み込むメソッド * - * @return シミュレーションに必要な入力値を含むSimulationParamsオブジェクト + * @return 読み込んだ値を含むint配列。要素[0]が戦闘回数、[1]が初期レベル、それ以降が敵のレベル */ - private static SimulateParams getInputSimulation() { + private static int[] getInputSimulation() { final Scanner scan = new Scanner(System.in); final int numOfBattles = scan.nextInt(); final int initialHeroLevel = scan.nextInt(); - return new SimulateParams(numOfBattles, initialHeroLevel, scan); + final int[] allInputs = new int[2 + numOfBattles]; + allInputs[0] = numOfBattles; + allInputs[1] = initialHeroLevel; + for (int i = 0; i < numOfBattles; i++) { + allInputs[2 + i] = scan.nextInt(); + } + scan.close(); + + return allInputs; } /** @@ -40,17 +52,15 @@ public class FightSimulation { * * @param numOfBattles 戦闘回数 * @param initialHeroLevel 勇者の初期レベル - * @param scan 敵のレベルを読み込むためのScannerオブジェクト + * @param enemyLevels 敵のレベルの配列 * @return N回の戦闘後の勇者の最終レベル */ private static int simulateCombat(final int numOfBattles, final int initialHeroLevel, - final Scanner scan) { + final int[] enemyLevels) { final Hero hero = new Hero(initialHeroLevel); - final BattleSimulator simulator = new BattleSimulator(hero, scan); - simulator.simulateBattles(numOfBattles); - - scan.close(); + final BattleSimulator battle = new BattleSimulator(hero, enemyLevels); + battle.simulate(numOfBattles); return hero.getLevel(); } @@ -63,39 +73,6 @@ public class FightSimulation { System.out.println(finalHeroLevel); } - - /** - * . シミュレーション入力を保持するヘルパークラス - */ - private static class SimulateParams { - - private final int numOfBattles; - private final int initialHeroLevel; - private final Scanner scan; - - public SimulateParams(final int numOfBattles, final int initialHeroLevel, - final Scanner scan) { - - this.numOfBattles = numOfBattles; - this.initialHeroLevel = initialHeroLevel; - this.scan = scan; - } - - public int getNumOfBattles() { - - return numOfBattles; - } - - public int getInitialHeroLevel() { - - return initialHeroLevel; - } - - public Scanner getScanner() { - - return scan; - } - } } @@ -104,7 +81,6 @@ public class FightSimulation { */ class Hero { - // 勇者の現在のレベル private int level; /** @@ -128,21 +104,26 @@ class Hero { } /** - * . 勇者と敵のレベルに基づいて戦闘結果を処理し、勇者のレベルを更新するメソッド + * . 勇者と敵の戦闘における勝利判定をするメソッド + * + * @return 勇者の勝利 + */ + private boolean isHeroWin(final int heroLevel, final int enemyLevel) { + + return heroLevel > enemyLevel; + } + + /** + * . 勝利判定メソッドにより、勇者のレベルを更新するメソッド * * @param enemyLevel 敵のレベル */ public void updateLevel(final int enemyLevel) { - if (this.level > enemyLevel) { - // 勇者の勝利 - final int levelIncrease = enemyLevel / 2; // 相手のレベルの半分だけ上昇 - this.level += levelIncrease; - } else if (this.level < enemyLevel) { - // 勇者の敗北 - this.level /= 2; // 勇者のレベルが半分になる - } else { - // 引き分け(レベルが同じなので、変動なし) + if (isHeroWin(this.level, enemyLevel)) { + this.level += enemyLevel / 2; + } else if (!isHeroWin(this.level, enemyLevel) && this.level != enemyLevel) { + this.level /= 2; } } } @@ -155,19 +136,19 @@ class BattleSimulator { // シミュレーションの対象となる勇者オブジェクト private final Hero hero; - // 戦闘相手のレベルを読み取るためのScannerオブジェクト - private final Scanner scan; + // 敵のレベルの配列 + private final int[] enemyLevels; /** * . 新しい戦闘シミュレーターを生成するコンストラクタ * * @param hero シミュレーションを行う勇者オブジェクト - * @param scan 入力を読み取るためのScannerオブジェクト + * @param enemyLevels 敵のレベルの配列 */ - public BattleSimulator(final Hero hero, final Scanner scan) { + public BattleSimulator(final Hero hero, final int[] enemyLevels) { this.hero = hero; - this.scan = scan; + this.enemyLevels = enemyLevels; } /** @@ -175,11 +156,11 @@ class BattleSimulator { * * @param numOfBattles 戦闘回数 */ - public void simulateBattles(final int numOfBattles) { + public void simulate(final int numOfBattles) { for (int i = 0; i < numOfBattles; i++) { - final int enemyLevel = scan.nextInt(); // 敵のレベルを読み込む - hero.updateLevel(enemyLevel); // 勇者と敵のレベルに基づいて戦闘を処理 + final int enemyLevel = enemyLevels[i]; + hero.updateLevel(enemyLevel); } } } -- GitLab From 7933aa4941ddedce5aef62b06d4d371186d70f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=99=E9=8D=8B=20=E5=B0=9A=E8=BC=9D?= Date: Fri, 4 Jul 2025 13:57:58 +0900 Subject: [PATCH 7/7] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CC=5F166?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E5=86=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ntakanabe/src/FightSimulation.java | 39 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/ntakanabe/src/FightSimulation.java b/ntakanabe/src/FightSimulation.java index fadfb31..bb081b0 100644 --- a/ntakanabe/src/FightSimulation.java +++ b/ntakanabe/src/FightSimulation.java @@ -1,5 +1,7 @@ package paiza.src; +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; /** @@ -16,11 +18,11 @@ public class FightSimulation { */ public static void main(final String[] args) { - final int[] inputValues = getInputSimulation(); - final int numOfBattles = inputValues[0]; - final int initialHeroLevel = inputValues[1]; - final int[] enemyLevels = new int[numOfBattles]; - System.arraycopy(inputValues, 2, enemyLevels, 0, numOfBattles); + final List inputValues = getInputSimulation(); + final int numOfBattles = inputValues.get(0); + final int initialHeroLevel = inputValues.get(1); + + final List enemyLevels = inputValues.subList(2, 2 + numOfBattles); final int finalHeroLevel = simulateCombat(numOfBattles, initialHeroLevel, enemyLevels); outputResult(finalHeroLevel); @@ -29,18 +31,21 @@ public class FightSimulation { /** * . 戦闘回数と勇者の初期レベル、および各敵のレベルを読み込むメソッド * - * @return 読み込んだ値を含むint配列。要素[0]が戦闘回数、[1]が初期レベル、それ以降が敵のレベル + * @return 入力値を含むInteger型のリスト。要素[0]が戦闘回数、[1]が初期レベル、それ以降が敵のレベル */ - private static int[] getInputSimulation() { + private static List getInputSimulation() { final Scanner scan = new Scanner(System.in); + final List allInputs = new ArrayList<>(); + final int numOfBattles = scan.nextInt(); + allInputs.add(numOfBattles); + final int initialHeroLevel = scan.nextInt(); - final int[] allInputs = new int[2 + numOfBattles]; - allInputs[0] = numOfBattles; - allInputs[1] = initialHeroLevel; + allInputs.add(initialHeroLevel); + for (int i = 0; i < numOfBattles; i++) { - allInputs[2 + i] = scan.nextInt(); + allInputs.add(scan.nextInt()); } scan.close(); @@ -52,11 +57,11 @@ public class FightSimulation { * * @param numOfBattles 戦闘回数 * @param initialHeroLevel 勇者の初期レベル - * @param enemyLevels 敵のレベルの配列 + * @param enemyLevels 敵のレベルのリスト * @return N回の戦闘後の勇者の最終レベル */ private static int simulateCombat(final int numOfBattles, final int initialHeroLevel, - final int[] enemyLevels) { + final List enemyLevels) { final Hero hero = new Hero(initialHeroLevel); final BattleSimulator battle = new BattleSimulator(hero, enemyLevels); @@ -137,15 +142,15 @@ class BattleSimulator { // シミュレーションの対象となる勇者オブジェクト private final Hero hero; // 敵のレベルの配列 - private final int[] enemyLevels; + private final List enemyLevels; /** * . 新しい戦闘シミュレーターを生成するコンストラクタ * * @param hero シミュレーションを行う勇者オブジェクト - * @param enemyLevels 敵のレベルの配列 + * @param enemyLevels 敵のレベルのリスト */ - public BattleSimulator(final Hero hero, final int[] enemyLevels) { + public BattleSimulator(final Hero hero, final List enemyLevels) { this.hero = hero; this.enemyLevels = enemyLevels; @@ -159,7 +164,7 @@ class BattleSimulator { public void simulate(final int numOfBattles) { for (int i = 0; i < numOfBattles; i++) { - final int enemyLevel = enemyLevels[i]; + final int enemyLevel = enemyLevels.get(i); hero.updateLevel(enemyLevel); } } -- GitLab