From 27ce39fa9381612979045af97509343218362aa5 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sat, 10 Feb 2024 00:19:25 +0900 Subject: [PATCH 01/18] feat(Boss): make Boss class with status field, status controlling method and attacking method --- src/main/java/bossmonster/domain/Boss.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/java/bossmonster/domain/Boss.java diff --git a/src/main/java/bossmonster/domain/Boss.java b/src/main/java/bossmonster/domain/Boss.java new file mode 100644 index 00000000..bdeb3174 --- /dev/null +++ b/src/main/java/bossmonster/domain/Boss.java @@ -0,0 +1,62 @@ +package bossmonster.domain; + +import java.util.Random; + +public class Boss { + private Integer maxHp; + private Integer currentHp; + + public Boss() { + selectRandomHp(); + printFigure(true); + } + + private void selectRandomHp() { + Random random = new Random(); + random.setSeed(System.currentTimeMillis()); + this.maxHp = random.nextInt(200) + 100; //100~200 사이 난수값 + this.currentHp = this.maxHp; + } + + public void printFigure(final Boolean isInitial) { + System.out.println("BOSS HP [" + this.currentHp + "/" + this.maxHp + "]"); + System.out.println("____________________________"); + + if(isInitial) { + System.out.println( + " ^-^\n" + + " / 0 0 \\\n" + + "( \" )\n" + + " \\ - /\n" + + " - ^ - " + ); + System.out.println("____________________________"); + return; + } + + System.out.println( + " ^-^\n" + + " / X X \\\n" + + "( \"\\ )\n" + + " \\ - /\n" + + " - ^ - " + ); + System.out.println("____________________________"); + } + + public Integer getHp() { return this.currentHp; } + public void decreaseHp(final Integer hp) { + if(this.currentHp - hp < 0) { + this.currentHp = 0; + return; + } + + this.currentHp -= hp; + } + + public Integer attack() { + Random random = new Random(); + random.setSeed(System.currentTimeMillis()); + return random.nextInt(20); + } +} \ No newline at end of file From 6ed3ab53affcdf61e26314cdf8ac4c8523f64e52 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sat, 10 Feb 2024 00:19:45 +0900 Subject: [PATCH 02/18] feat(Player): make Player class with status field, status controlling method and attacking method --- src/main/java/bossmonster/domain/Player.java | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/bossmonster/domain/Player.java diff --git a/src/main/java/bossmonster/domain/Player.java b/src/main/java/bossmonster/domain/Player.java new file mode 100644 index 00000000..7b82496b --- /dev/null +++ b/src/main/java/bossmonster/domain/Player.java @@ -0,0 +1,59 @@ +package bossmonster.domain; + +import bossmonster.exception.NoMPException; + +public class Player { + private final String name; + private final Integer maxHp; + private Integer currentHp; + private final Integer maxMp; + private Integer currentMp; + + public Player(String name, Integer hp, Integer mp) { + this.name = name; + this.maxHp = hp; + this.currentHp = hp; + this.maxMp = mp; + this.currentMp = mp; + } + + public String getName() { return name; } + + public Integer getHp() { return currentHp; } + + public Integer getMp() { return currentMp; } + + public void decreaseHp(final Integer hp) { + if(this.currentHp - hp < 0) { + this.currentHp = 0; + return; + } + + this.currentHp -= hp; + } + + public void decreaseMp(final Integer mp) { + if(this.currentMp - mp < 0) { + this.currentMp = 0; + return; + } + + this.currentMp -= mp; + } + + public Integer physicalAttack() { + if(this.currentMp + 10 > this.maxMp) + this.currentMp = this.maxMp; + else this.currentMp += 10; + + return 10; + } + + public Integer magicalAttack() throws NoMPException { + if(currentMp < 30) + throw new NoMPException(); + + this.currentMp -= 30; + return 20; + } +} \ No newline at end of file From b61dbb4af7b98bfc21423cf4fe6e35893d93c4f2 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sat, 10 Feb 2024 00:28:12 +0900 Subject: [PATCH 03/18] refactor: delete getRandomHp method to make users set the HP of boss manually --- src/main/java/bossmonster/domain/Boss.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/java/bossmonster/domain/Boss.java b/src/main/java/bossmonster/domain/Boss.java index bdeb3174..21cdaa49 100644 --- a/src/main/java/bossmonster/domain/Boss.java +++ b/src/main/java/bossmonster/domain/Boss.java @@ -6,18 +6,12 @@ public class Boss { private Integer maxHp; private Integer currentHp; - public Boss() { - selectRandomHp(); + public Boss(Integer hp) { + this.maxHp = hp; + this.currentHp = hp; printFigure(true); } - private void selectRandomHp() { - Random random = new Random(); - random.setSeed(System.currentTimeMillis()); - this.maxHp = random.nextInt(200) + 100; //100~200 사이 난수값 - this.currentHp = this.maxHp; - } - public void printFigure(final Boolean isInitial) { System.out.println("BOSS HP [" + this.currentHp + "/" + this.maxHp + "]"); System.out.println("____________________________"); From 7d17113cccc32b5e9f56b49cbf90acd4f9c4976b Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sat, 10 Feb 2024 00:44:16 +0900 Subject: [PATCH 04/18] feat(main): link main method to execute GameUI --- src/main/java/bossmonster/Main.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/bossmonster/Main.java b/src/main/java/bossmonster/Main.java index d9488c65..c6f70311 100644 --- a/src/main/java/bossmonster/Main.java +++ b/src/main/java/bossmonster/Main.java @@ -1,7 +1,10 @@ package bossmonster; +import bossmonster.controller.GameUI; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + GameUI gameUI = new GameUI(); + gameUI.execute(); } } From 513b513ca674ff8105437932f9cf29beafbcc842 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sat, 10 Feb 2024 18:21:32 +0900 Subject: [PATCH 05/18] refactor: fix printFigure to print separation line at the top --- src/main/java/bossmonster/domain/Boss.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/bossmonster/domain/Boss.java b/src/main/java/bossmonster/domain/Boss.java index 21cdaa49..d257cdbd 100644 --- a/src/main/java/bossmonster/domain/Boss.java +++ b/src/main/java/bossmonster/domain/Boss.java @@ -13,6 +13,7 @@ public Boss(Integer hp) { } public void printFigure(final Boolean isInitial) { + System.out.println("============================"); System.out.println("BOSS HP [" + this.currentHp + "/" + this.maxHp + "]"); System.out.println("____________________________"); From 40389511e80cea08d7d19aa4590bc36988d900a4 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:26:10 +0900 Subject: [PATCH 06/18] refactor: fix printStat, printFigure method to change console gui --- src/main/java/bossmonster/domain/Boss.java | 43 +++++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/bossmonster/domain/Boss.java b/src/main/java/bossmonster/domain/Boss.java index d257cdbd..325e5d6f 100644 --- a/src/main/java/bossmonster/domain/Boss.java +++ b/src/main/java/bossmonster/domain/Boss.java @@ -3,21 +3,26 @@ import java.util.Random; public class Boss { - private Integer maxHp; + private final Integer maxHp; private Integer currentHp; public Boss(Integer hp) { this.maxHp = hp; this.currentHp = hp; - printFigure(true); + printStat(); + printFigure(1); } - public void printFigure(final Boolean isInitial) { - System.out.println("============================"); + public Integer getHp() { return this.currentHp; } + + public void printStat() { + System.out.println("\n======================================"); System.out.println("BOSS HP [" + this.currentHp + "/" + this.maxHp + "]"); - System.out.println("____________________________"); + System.out.println("______________________________________"); + } - if(isInitial) { + public void printFigure(final Integer figureType) { + if(figureType == 1) { //initial System.out.println( " ^-^\n" + " / 0 0 \\\n" @@ -25,21 +30,31 @@ public void printFigure(final Boolean isInitial) { + " \\ - /\n" + " - ^ - " ); - System.out.println("____________________________"); + System.out.println("______________________________________"); + return; + } + if(figureType == 0) { //damaged + System.out.println( + " ^-^\n" + + " / X X \\\n" + + "( \"\\ )\n" + + " \\ - /\n" + + " - ^ - " + ); + System.out.println("______________________________________"); return; } - System.out.println( + System.out.println( //lose " ^-^\n" - + " / X X \\\n" - + "( \"\\ )\n" - + " \\ - /\n" - + " - ^ - " + + " / ^ ^ \\\n" + + "( \"\\ )\n" + + " \\ 3 /\n" + + " - ^ - " ); - System.out.println("____________________________"); + System.out.println("______________________________________"); } - public Integer getHp() { return this.currentHp; } public void decreaseHp(final Integer hp) { if(this.currentHp - hp < 0) { this.currentHp = 0; From 1231b77760cc2ef956c5c615fd20269f05b59f3b Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:26:41 +0900 Subject: [PATCH 07/18] refactor: fix printStat method to change console gui --- src/main/java/bossmonster/domain/Player.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/bossmonster/domain/Player.java b/src/main/java/bossmonster/domain/Player.java index 7b82496b..f001b51b 100644 --- a/src/main/java/bossmonster/domain/Player.java +++ b/src/main/java/bossmonster/domain/Player.java @@ -15,13 +15,18 @@ public Player(String name, Integer hp, Integer mp) { this.currentHp = hp; this.maxMp = mp; this.currentMp = mp; + printStat(); } public String getName() { return name; } public Integer getHp() { return currentHp; } - public Integer getMp() { return currentMp; } + public void printStat() { + System.out.println(this.name + " HP [" + this.currentHp + "/" + this.maxHp + "]" + + " MP [" + this.currentMp + "/" + this.maxMp + "]"); + System.out.println("======================================\n"); + } public void decreaseHp(final Integer hp) { if(this.currentHp - hp < 0) { @@ -32,15 +37,6 @@ public void decreaseHp(final Integer hp) { this.currentHp -= hp; } - public void decreaseMp(final Integer mp) { - if(this.currentMp - mp < 0) { - this.currentMp = 0; - return; - } - - this.currentMp -= mp; - } - public Integer physicalAttack() { if(this.currentMp + 10 > this.maxMp) this.currentMp = this.maxMp; From 59953f928d380521b156c235d0b95ada943ef0d8 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:27:44 +0900 Subject: [PATCH 08/18] add: exception class concerning unusual mp status --- src/main/java/bossmonster/exception/NoMPException.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/bossmonster/exception/NoMPException.java diff --git a/src/main/java/bossmonster/exception/NoMPException.java b/src/main/java/bossmonster/exception/NoMPException.java new file mode 100644 index 00000000..5790c4ce --- /dev/null +++ b/src/main/java/bossmonster/exception/NoMPException.java @@ -0,0 +1,7 @@ +package bossmonster.exception; + +public class NoMPException extends RuntimeException { + public NoMPException() { + System.out.println("\n[ERROR] MP가 부족합니다. 다른 공격 방법을 선택해주세요."); + } +} From ec9de044c646735830e1402a93cce593d5f8f0df Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:29:01 +0900 Subject: [PATCH 09/18] refactor: change main method to start the program by executing method of gameUI --- src/main/java/bossmonster/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/bossmonster/Main.java b/src/main/java/bossmonster/Main.java index c6f70311..3e8edfa8 100644 --- a/src/main/java/bossmonster/Main.java +++ b/src/main/java/bossmonster/Main.java @@ -3,7 +3,7 @@ import bossmonster.controller.GameUI; public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { GameUI gameUI = new GameUI(); gameUI.execute(); } From f1b8971482f5411e8d91d9bfc3fbf717dff2a35c Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:30:29 +0900 Subject: [PATCH 10/18] add: service class controlling core logic of the program including attack, damage, win/lose determination --- .../java/bossmonster/service/GameService.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/main/java/bossmonster/service/GameService.java diff --git a/src/main/java/bossmonster/service/GameService.java b/src/main/java/bossmonster/service/GameService.java new file mode 100644 index 00000000..f6b0d10c --- /dev/null +++ b/src/main/java/bossmonster/service/GameService.java @@ -0,0 +1,82 @@ +package bossmonster.service; + +import bossmonster.domain.Boss; +import bossmonster.domain.Player; +import java.util.Scanner; + +public class GameService { + private final Player player; + private final Boss boss; + private final Scanner sc = new Scanner(System.in); + + private int attackNum = 0; + + public GameService(final Integer bossHp, final String playerName, final Integer playerHp, final Integer playerMp) { + System.out.println("\n보스 레이드를 시작합니다.\n"); + this.boss = new Boss(bossHp); + this.player = new Player(playerName, playerHp, playerMp); + } + + public void play() throws Exception { + while(true) { + boss.decreaseHp(playerAttack()); + player.decreaseHp(bossAttack()); + attackNum += 1; + + if(boss.getHp() <= 0) { + this.win(); + break; + } + if(player.getHp() <= 0) { + this.lose(); + break; + } + + boss.printStat(); + boss.printFigure(0); + player.printStat(); + } + } + + public Integer playerAttack() { + Integer attackType, damage; + + System.out.println("어떤 공격을 하시겠습니까? 공격 번호를 입력해주세요."); + System.out.println("1. 물리 공격\n2. 마법 공격"); + attackType = sc.nextInt(); + + if(attackType == 1) { + damage = player.physicalAttack(); + System.out.println("\n물리 공격을 했습니다. (입힌 데미지: " + damage + ")"); + return damage; + } + + if(attackType == 2) { + damage = player.magicalAttack(); + System.out.println("\n마법 공격을 했습니다. (입힌 데미지: " + damage + ")"); + return damage; + } + + return -1; + } + + public Integer bossAttack() { + Integer damage; + + damage = boss.attack(); + System.out.println("보스가 공격했습니다. (입힌 데미지: " + damage + ")"); + return damage; + } + + public void win() { + System.out.println("\n" + player.getName() + " 님이 " + attackNum + "번의 전투 끝에 보스 몬스터를 잡았습니다."); + } + + public void lose() { + boss.printStat(); + boss.printFigure(-1); + player.printStat(); + System.out.println(player.getName() + "의 HP가 0이 되었습니다."); + System.out.println("보스 레이드에 실패하셨습니다."); + } +} From 1090f16f4370b6dc670fd453002a582955c633cb Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Sun, 11 Feb 2024 00:31:17 +0900 Subject: [PATCH 11/18] add: controller class managing user input & console output --- .../java/bossmonster/controller/GameUI.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/bossmonster/controller/GameUI.java diff --git a/src/main/java/bossmonster/controller/GameUI.java b/src/main/java/bossmonster/controller/GameUI.java new file mode 100644 index 00000000..814d74d0 --- /dev/null +++ b/src/main/java/bossmonster/controller/GameUI.java @@ -0,0 +1,34 @@ +package bossmonster.controller; + +import bossmonster.service.GameService; +import java.util.Scanner; + +public class GameUI { + private final Scanner sc; + private Integer bossHp, playerHp, playerMp; + private String playerName; + + public GameUI() { + this.sc = new Scanner(System.in); + setPlayerData(); + } + + private void setPlayerData() { + System.out.println("! Welcome to Boss Monster Game !"); + System.out.println("보스 몬스터의 HP를 입력해주세요."); + this.bossHp = this.sc.nextInt(); + + System.out.println("플레이어의 이름을 입력해주세요."); + this.playerName = this.sc.next(); + + System.out.println("플레이어의 HP와 MP를 입력해주세요.(,로 구분 / HP와 MP의 합 최대 200 이내)"); + String[] playerStat = this.sc.next().split(","); + this.playerHp = Integer.parseInt(playerStat[0]); + this.playerMp = Integer.parseInt(playerStat[1]); + } + + public void execute() throws Exception { + GameService gameService = new GameService(this.bossHp, this.playerName, this.playerHp, this.playerMp); + gameService.play(); + } +} From cbc5ee839e801cda6037a1658e6f359224bef17b Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 00:13:24 +0900 Subject: [PATCH 12/18] fix: remove unnecessary print statement --- src/main/java/bossmonster/exception/NoMPException.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/bossmonster/exception/NoMPException.java b/src/main/java/bossmonster/exception/NoMPException.java index 5790c4ce..71af9ebb 100644 --- a/src/main/java/bossmonster/exception/NoMPException.java +++ b/src/main/java/bossmonster/exception/NoMPException.java @@ -1,7 +1,5 @@ package bossmonster.exception; public class NoMPException extends RuntimeException { - public NoMPException() { - System.out.println("\n[ERROR] MP가 부족합니다. 다른 공격 방법을 선택해주세요."); - } + public NoMPException() { } } From 131b1ab805cd7b558d470cfbb93677cf7495d9a9 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 00:13:59 +0900 Subject: [PATCH 13/18] refactor: add NoMPException handling logic --- .../java/bossmonster/service/GameService.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/bossmonster/service/GameService.java b/src/main/java/bossmonster/service/GameService.java index f6b0d10c..1c9cc389 100644 --- a/src/main/java/bossmonster/service/GameService.java +++ b/src/main/java/bossmonster/service/GameService.java @@ -2,12 +2,13 @@ import bossmonster.domain.Boss; import bossmonster.domain.Player; +import bossmonster.exception.NoMPException; import java.util.Scanner; public class GameService { + private final Scanner sc = new Scanner(System.in); private final Player player; private final Boss boss; - private final Scanner sc = new Scanner(System.in); private int attackNum = 0; @@ -17,7 +18,7 @@ public GameService(final Integer bossHp, final String playerName, final Integer this.player = new Player(playerName, playerHp, playerMp); } - public void play() throws Exception { + public void play() { while(true) { boss.decreaseHp(playerAttack()); player.decreaseHp(bossAttack()); @@ -41,23 +42,27 @@ public void play() throws Exception { public Integer playerAttack() { Integer attackType, damage; - System.out.println("어떤 공격을 하시겠습니까? 공격 번호를 입력해주세요."); - System.out.println("1. 물리 공격\n2. 마법 공격"); - attackType = sc.nextInt(); - - if(attackType == 1) { - damage = player.physicalAttack(); - System.out.println("\n물리 공격을 했습니다. (입힌 데미지: " + damage + ")"); - return damage; - } - - if(attackType == 2) { - damage = player.magicalAttack(); - System.out.println("\n마법 공격을 했습니다. (입힌 데미지: " + damage + ")"); - return damage; + while(true) { + try { + System.out.println("어떤 공격을 하시겠습니까? 공격 번호를 입력해주세요."); + System.out.println("1. 물리 공격\n2. 마법 공격"); + attackType = sc.nextInt(); + + if(attackType == 1) { + damage = player.physicalAttack(); + System.out.println("\n물리 공격을 했습니다. (입힌 데미지: " + damage + ")"); + return damage; + } + + if(attackType == 2) { + damage = player.magicalAttack(); + System.out.println("\n마법 공격을 했습니다. (입힌 데미지: " + damage + ")"); + return damage; + } + } catch (NoMPException e) { + System.out.println("\n[ERROR] MP가 부족합니다. 다른 공격 방법을 선택해주세요."); + } } - - return -1; } public Integer bossAttack() { From 9214352b72e6d884f46ab6c0402ccf4d019b57e5 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 00:58:14 +0900 Subject: [PATCH 14/18] feat: add exception class for invalid boss hp input --- .../java/bossmonster/exception/InvalidBossHPException.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/bossmonster/exception/InvalidBossHPException.java diff --git a/src/main/java/bossmonster/exception/InvalidBossHPException.java b/src/main/java/bossmonster/exception/InvalidBossHPException.java new file mode 100644 index 00000000..a6ea0526 --- /dev/null +++ b/src/main/java/bossmonster/exception/InvalidBossHPException.java @@ -0,0 +1,5 @@ +package bossmonster.exception; + +public class InvalidBossHPException extends RuntimeException { + public InvalidBossHPException() { } +} From a58e74cc2b677bcf6bc84bbb14eca6f1e36101d3 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 00:58:26 +0900 Subject: [PATCH 15/18] feat: add exception class for invalid player hp, mp input --- .../bossmonster/exception/InvalidPlayerHpMpException.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/bossmonster/exception/InvalidPlayerHpMpException.java diff --git a/src/main/java/bossmonster/exception/InvalidPlayerHpMpException.java b/src/main/java/bossmonster/exception/InvalidPlayerHpMpException.java new file mode 100644 index 00000000..a0ee864d --- /dev/null +++ b/src/main/java/bossmonster/exception/InvalidPlayerHpMpException.java @@ -0,0 +1,5 @@ +package bossmonster.exception; + +public class InvalidPlayerHpMpException extends RuntimeException { + public InvalidPlayerHpMpException() { } +} From b49093ecfa6c25f4a5c3a4bd55ed3bab77cbb753 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 00:58:33 +0900 Subject: [PATCH 16/18] feat: add exception class for invalid player name input --- .../bossmonster/exception/InvalidPlayerNameException.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/bossmonster/exception/InvalidPlayerNameException.java diff --git a/src/main/java/bossmonster/exception/InvalidPlayerNameException.java b/src/main/java/bossmonster/exception/InvalidPlayerNameException.java new file mode 100644 index 00000000..84546efe --- /dev/null +++ b/src/main/java/bossmonster/exception/InvalidPlayerNameException.java @@ -0,0 +1,5 @@ +package bossmonster.exception; + +public class InvalidPlayerNameException extends RuntimeException { + public InvalidPlayerNameException() { } +} From 211caf47651fb5a3f041e8728925505cd37661d4 Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 16:03:59 +0900 Subject: [PATCH 17/18] add: add readme file for checking implementation details --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 93db812c..5f766d40 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,27 @@ # 미션 - 보스 몬스터 잡기 +## ✔️ 구현사항 + +- 플레이어 클래스 구현 + - [X] HP, MP + - [X] 물리 공격, 마법 공격 + - [X] 상태 업데이트 메서드 +- 보스몬스터 클래스 구현 + - [X] HP, MP + - [X] 랜덤 데미지 공격 + - [X] 상태 업데이트 메서드 + - [X] 몬스터 그림 GUI 구현 +- 입력 예외처리 구현 + - [X] Exception Class + - [X] 게임 초기 설정 예외처리 + - [X] 게임 중간 공격 입력 예외처리 +- 로직 구현 + - [X] 게임 core 로직 구현 + - [X] 디버깅&테스트 +- 인터페이스 구현 + - [X] 게임 진행상태 안내 메시지 UI + + ## 🔍 진행방식 - 미션은 **기능 요구사항, 프로그래밍 요구사항, 과제 진행 요구사항** 세 가지로 구성되어 있습니다. From 1d6576e79d5bc246790ad8dc807685054ecc79af Mon Sep 17 00:00:00 2001 From: Seonho Kim <5061ksh@naver.com> Date: Mon, 12 Feb 2024 16:05:56 +0900 Subject: [PATCH 18/18] refactor: add exception handling code for user input process --- .../java/bossmonster/controller/GameUI.java | 81 ++++++++++++++++--- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/src/main/java/bossmonster/controller/GameUI.java b/src/main/java/bossmonster/controller/GameUI.java index 814d74d0..475f2f1e 100644 --- a/src/main/java/bossmonster/controller/GameUI.java +++ b/src/main/java/bossmonster/controller/GameUI.java @@ -1,5 +1,8 @@ package bossmonster.controller; +import bossmonster.exception.InvalidBossHPException; +import bossmonster.exception.InvalidPlayerHpMpException; +import bossmonster.exception.InvalidPlayerNameException; import bossmonster.service.GameService; import java.util.Scanner; @@ -10,25 +13,79 @@ public class GameUI { public GameUI() { this.sc = new Scanner(System.in); - setPlayerData(); + this.setBossHp(); + this.setPlayerName(); + this.setPlayerHpMp(); } - private void setPlayerData() { + private void setBossHp() { System.out.println("! Welcome to Boss Monster Game !"); - System.out.println("보스 몬스터의 HP를 입력해주세요."); - this.bossHp = this.sc.nextInt(); + while(true) { + try { + System.out.println("보스 몬스터의 HP를 입력해주세요. (100이상 300이하)"); + this.bossHp = this.sc.nextInt(); - System.out.println("플레이어의 이름을 입력해주세요."); - this.playerName = this.sc.next(); + if(this.bossHp < 100 || this.bossHp > 300) + throw new InvalidBossHPException(); - System.out.println("플레이어의 HP와 MP를 입력해주세요.(,로 구분 / HP와 MP의 합 최대 200 이내)"); - String[] playerStat = this.sc.next().split(","); - this.playerHp = Integer.parseInt(playerStat[0]); - this.playerMp = Integer.parseInt(playerStat[1]); + return; + } catch (InvalidBossHPException e) { + System.out.println("\n[ERROR] 유효한 보스 HP 값을 입력해주세요."); + } + } } - public void execute() throws Exception { + private void setPlayerName() { + while(true) { + try { + System.out.println("플레이어의 이름을 입력해주세요. (5자 이내)"); + this.playerName = this.sc.next(); + + if(this.playerName.length() > 5) + throw new InvalidPlayerNameException(); + + this.sc.nextLine(); + return; + } catch (InvalidPlayerNameException e) { + System.out.println("\n[ERROR] 올바른 플레이어 이름을 입력해주세요."); + } + } + } + + private void setPlayerHpMp() { + while(true) { + String playerStatInput; + try { + System.out.println("플레이어의 HP와 MP를 입력해주세요.(,로 구분 / HP와 MP의 합 최대 200 이내)"); + playerStatInput = this.sc.nextLine(); + + if(!playerStatInput.matches("^[0-9]{1,3},[0-9]{1,3}$")) // 콤마 앞뒤로 숫자 3자리 이내 + throw new IllegalArgumentException(); + + } catch (IllegalArgumentException e) { + System.out.println("\n[ERROR] 유효한 형태의 HP, MP 값을 입력해주세요 (Ex. 100,100)"); + continue; + } + + try { + String[] playerStat = playerStatInput.split(","); + this.playerHp = Integer.parseInt(playerStat[0]); + this.playerMp = Integer.parseInt(playerStat[1]); + + if(this.playerHp + this.playerMp > 200) + throw new InvalidPlayerHpMpException(); + } catch (InvalidPlayerHpMpException e) { + e.getStackTrace(); + System.out.println("\n[ERROR] 유효한 형태의 HP, MP 값을 입력해주세요 (Ex. 100,100)"); + continue; + } + + return; + } + } + + public void execute() { GameService gameService = new GameService(this.bossHp, this.playerName, this.playerHp, this.playerMp); gameService.play(); } -} +} \ No newline at end of file