From 74ce91e37419b8065242a7c083f4772a8da5cd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:37:06 +0900 Subject: [PATCH 01/40] =?UTF-8?q?feat:=20=EC=BD=98=EC=86=94=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=EC=97=90=EC=84=9C=20=EC=9C=A0=EC=A0=80=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=ED=95=B8=EB=93=A4=EB=9F=AC=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/InputHandler.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/io/InputHandler.java diff --git a/src/main/java/io/InputHandler.java b/src/main/java/io/InputHandler.java new file mode 100644 index 0000000..75ba1fd --- /dev/null +++ b/src/main/java/io/InputHandler.java @@ -0,0 +1,12 @@ +package io; + +import java.util.Scanner; + +public class InputHandler { + + private static final Scanner SCANNER = new Scanner(System.in); + + public String getUserInput() { + return SCANNER.nextLine(); + } +} From dd102b04ea4806634ee8d5aa13798bb8fb5b604a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:37:30 +0900 Subject: [PATCH 02/40] =?UTF-8?q?feat:=20=EC=BD=98=EC=86=94=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=EC=97=90=EC=84=9C=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EB=82=B4=EC=9A=A9=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=ED=95=B8=EB=93=A4=EB=9F=AC=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/OutputHandler.java | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/io/OutputHandler.java diff --git a/src/main/java/io/OutputHandler.java b/src/main/java/io/OutputHandler.java new file mode 100644 index 0000000..3ea2c79 --- /dev/null +++ b/src/main/java/io/OutputHandler.java @@ -0,0 +1,36 @@ +package io; + +import core.AppException; + +public class OutputHandler { + + public static void printAppExceptionMessage(AppException e) { + System.out.println(e.getMessage()); + } + + public static void printMessage(String message) { + System.out.println(message); + } + + public static void printDefaultExceptionMessage(Exception e) { + printMessage("알 수 없는 오류로 인해 시스템이 종료 됩니다."); + printMessage("############# [DEBUG] Traceback Logging ############"); + System.out.println(e.getMessage()); + } + + public void gameStartCommentPrint() { + System.out.println("##################################"); + System.out.println("#\t\t\t\t숫자 야구 게임 시작!\t\t\t\t#"); + System.out.println("##################################"); + System.out.println(); + } + + public void questionToUserAboutInputNumber() { + System.out.print("숫자를 입력 해주세요: "); + } + + public void gameEndCommentPrint() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + System.out.print("> "); + } +} From 226ab873bfd72a69dcf6eab3fdc637857aa68731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:38:43 +0900 Subject: [PATCH 03/40] =?UTF-8?q?chore:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EB=A0=88=EB=B2=A8=20=EC=BB=A4?= =?UTF-8?q?=EC=8A=A4=ED=85=80=20Exception=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/core/AppException.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/core/AppException.java diff --git a/src/main/java/core/AppException.java b/src/main/java/core/AppException.java new file mode 100644 index 0000000..b61440c --- /dev/null +++ b/src/main/java/core/AppException.java @@ -0,0 +1,8 @@ +package core; + +public class AppException extends RuntimeException { + + public AppException(String message) { + super(message); + } +} From 2c9309b7f82efd2f355eddfbd25557f78ba1f738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:39:24 +0900 Subject: [PATCH 04/40] =?UTF-8?q?chore:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=A7=84=ED=96=89=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EB=A7=A4=EC=A7=81=20=EB=84=98=EB=B2=84,?= =?UTF-8?q?=20=EC=8A=A4=ED=8A=B8=EB=A7=81=20=EC=83=81=EC=88=98=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/core/SystemConstant.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/core/SystemConstant.java diff --git a/src/main/java/core/SystemConstant.java b/src/main/java/core/SystemConstant.java new file mode 100644 index 0000000..3998e7b --- /dev/null +++ b/src/main/java/core/SystemConstant.java @@ -0,0 +1,9 @@ +package core; + +public class SystemConstant { + public static final int INPUT_LIMIT_LENGTH = 3; + public static final int WINNING_STRIKE_COUNT = 3; + public static final String GAME_STOP_FLAG = "2"; + public static final String GAME_RESTART_FLAG = "1"; + +} From 588ad94155ac23ea38b37f16bcf9c7f2899aa511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:39:46 +0900 Subject: [PATCH 05/40] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=20=EC=97=AD=ED=95=A0=EC=9D=98=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/User.java | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/domain/User.java diff --git a/src/main/java/domain/User.java b/src/main/java/domain/User.java new file mode 100644 index 0000000..c137fb2 --- /dev/null +++ b/src/main/java/domain/User.java @@ -0,0 +1,56 @@ +package domain; + + +import static core.SystemConstant.*; + +import java.util.ArrayList; +import java.util.Arrays; +import core.AppException; + +public class User { + + private final static int NUMBER_LIMIT_LENGTH = INPUT_LIMIT_LENGTH; + private final ArrayList userInputNumbers; + + public User(String userInputNumber) { + if (invalidLengthUserInputNumber(userInputNumber)) { + throw new AppException("입력한 숫자의 길이가 올바르지 않습니다."); + } + + if (doesNotNumeric(userInputNumber)) { + throw new AppException("지정 되지 않은 타입의 입력입니다."); + } + + this.userInputNumbers = convertStringToArrayList(userInputNumber); + + if (hasZeroNumber()) { + throw new AppException("0은 입력 할 수 없습니다."); + } + + } + + public ArrayList getUserInputNumbers() { + return userInputNumbers; + } + + private static ArrayList convertStringToArrayList(String userInput) { + return new ArrayList<>(Arrays.asList(userInput.split(""))); + } + + private boolean hasZeroNumber() { + return this.userInputNumbers.contains("0"); + } + + private boolean doesNotNumeric(String userInput) { + try { + Integer.valueOf(userInput); + return false; + } catch (Exception e) { + return true; + } + } + + private boolean invalidLengthUserInputNumber(String userInput) { + return !(userInput.length() == NUMBER_LIMIT_LENGTH); + } +} From af248eb89bb2df379087eb47d80a916a3ebcf598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:40:30 +0900 Subject: [PATCH 06/40] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=9D=98=20=EC=9E=85=EB=A0=A5=20=EA=B0=92=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EC=8B=9C=20=EC=A0=90=EC=88=98=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=8A=A4=EC=BD=94=EC=96=B4=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Score.java | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/domain/Score.java diff --git a/src/main/java/domain/Score.java b/src/main/java/domain/Score.java new file mode 100644 index 0000000..9629159 --- /dev/null +++ b/src/main/java/domain/Score.java @@ -0,0 +1,49 @@ +package domain; + +import static core.SystemConstant.*; + +import io.OutputHandler; + +public class Score { + + private int strikeCount; + private int ballCount; + + + void clean() { + this.strikeCount = 0; + this.ballCount = 0; + } + + public void strikeIncrement() { + this.strikeCount += 1; + } + + public void ballIncrement() { + this.ballCount += 1; + } + + public void printScoreRecordResult() { + if (ballCount > 0 && strikeCount > 0) { + OutputHandler.printMessage(ballCount + "볼 " + strikeCount + "스트라이크"); + return; + } + + if (ballCount > 0) { + OutputHandler.printMessage(ballCount + "볼"); + return; + } + + if (strikeCount > 0) { + OutputHandler.printMessage(strikeCount + "스트라이크"); + return; + } + + OutputHandler.printMessage("낫싱"); + } + + public boolean isStrikeCountEqualToWinningStrikeCount() { + return strikeCount == WINNING_STRIKE_COUNT; + } + +} From 2a7ad63879d06b1fd750009b538a608965ae5706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:40:54 +0900 Subject: [PATCH 07/40] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=9D=98=20=EC=A0=95=EB=8B=B5=EA=B3=BC=20=EB=B9=84?= =?UTF-8?q?=EA=B5=90=20=ED=95=A0=20=EB=8C=80=EC=83=81=20=EC=BB=B4=ED=93=A8?= =?UTF-8?q?=ED=84=B0=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Computer.java | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/domain/Computer.java diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java new file mode 100644 index 0000000..2f6ade4 --- /dev/null +++ b/src/main/java/domain/Computer.java @@ -0,0 +1,31 @@ +package domain; + +import static core.SystemConstant.*; +import java.util.ArrayList; +import java.util.Random; + +public class Computer { + + private final Random random = new Random(); + private final ArrayList randomNumbers = new ArrayList<>(); + + public ArrayList generateRandomNumber() { + if (randomNumbers.size() == INPUT_LIMIT_LENGTH) { + return randomNumbers; + } + String number = getRandomNumberToString(); + + if (doesNotDuplicate(number)) { + randomNumbers.add(number); + } + return generateRandomNumber(); + } + + private String getRandomNumberToString() { + return String.valueOf(random.nextInt(9) + 1); + } + + private boolean doesNotDuplicate(String compareInteger) { + return !randomNumbers.contains(compareInteger); + } +} From 38017b4fac53455925b07c9abc58090163d58aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:41:40 +0900 Subject: [PATCH 08/40] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EC=9D=98=20=ED=96=89=EB=8F=99=EC=9D=84=20=EC=A0=90?= =?UTF-8?q?=EC=88=98=EB=A1=9C=20=EA=B8=B0=EB=A1=9D=20=ED=95=A0=20=EC=8B=AC?= =?UTF-8?q?=ED=8C=90=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Judgment.java | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/domain/Judgment.java diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java new file mode 100644 index 0000000..8ee55f6 --- /dev/null +++ b/src/main/java/domain/Judgment.java @@ -0,0 +1,44 @@ +package domain; + +import java.util.ArrayList; + +public class Judgment { + + private final Score score = new Score(); + private final ArrayList computerRandomNumbers; + + + public Judgment(Computer computer) { + this.computerRandomNumbers = computer.generateRandomNumber(); + System.out.println("computerRandomNumbers = " + computerRandomNumbers); + } + + public Score judge(ArrayList userInputNumbers) { + score.clean(); + + for (int seq = 0; seq < userInputNumbers.size(); seq++) { + scoreRecord(userInputNumbers, seq); + } + score.printScoreRecordResult(); + return score; + } + + private void scoreRecord(ArrayList userInputNumbers, int i) { + if (isStrike(userInputNumbers, i)) { + score.strikeIncrement(); + return; + } + + if (isBall(userInputNumbers, i)) { + score.ballIncrement(); + } + } + + private boolean isBall(ArrayList userInputNumbers, int i) { + return computerRandomNumbers.contains(userInputNumbers.get(i)); + } + + private boolean isStrike(ArrayList userInputNumbers, int i) { + return computerRandomNumbers.get(i).equals(userInputNumbers.get(i)); + } +} From de9e1101fb6879fe4a49651345def7eaad02e3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:42:02 +0900 Subject: [PATCH 09/40] =?UTF-8?q?feat:=20=EB=A9=94=EC=9D=B8=20=EC=96=B4?= =?UTF-8?q?=ED=94=8C=EB=A6=AC=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/base/Application.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/base/Application.java diff --git a/src/main/java/base/Application.java b/src/main/java/base/Application.java new file mode 100644 index 0000000..7430507 --- /dev/null +++ b/src/main/java/base/Application.java @@ -0,0 +1,13 @@ +package base; + +public interface Application { + + boolean isGameRestart(String gameFlag); + + boolean isGameStop(String gameFlag); + + String selectGameRestartOrStop(); + + void run(); + +} From 413519c75c7afca107607a24c2700dfc93a6a81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:42:43 +0900 Subject: [PATCH 10/40] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=20=EC=95=BC?= =?UTF-8?q?=EA=B5=AC=EA=B2=8C=EC=9E=84=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/java/app/BaseballApplication.java diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java new file mode 100644 index 0000000..73fe345 --- /dev/null +++ b/src/main/java/app/BaseballApplication.java @@ -0,0 +1,83 @@ +package app; + +import base.Application; +import domain.Computer; +import domain.Judgment; +import domain.Score; +import domain.User; +import io.InputHandler; +import io.OutputHandler; +import java.util.ArrayList; +import core.AppException; + +import static core.SystemConstant.*; + + +public class BaseballApplication implements Application { + + private final InputHandler inputHandler = new InputHandler(); + private final OutputHandler outputHandler = new OutputHandler(); + + public void run() { + Judgment judgment = readyToGame(); + outputHandler.gameStartCommentPrint(); + + applicationToGameStart(judgment); + } + + public String selectGameRestartOrStop() { + outputHandler.gameEndCommentPrint(); + return inputHandler.getUserInput(); + } + + public boolean isGameRestart(String gameFlag) { + return gameFlag.equals(GAME_RESTART_FLAG); + } + + public boolean isGameStop(String gameFlag) { + return gameFlag.equals(GAME_STOP_FLAG); + } + + private void applicationToGameStart(Judgment judgment) { + try { + actionToGameStartByJudgement(judgment); + + } catch (AppException e) { + OutputHandler.printAppExceptionMessage(e); + + applicationToGameStart(judgment); + } + } + + private void actionToGameStartByJudgement(Judgment judgment) { + boolean isGameSet = false; + + while (hasGameRunning(isGameSet)) { + String userInputNumber = userAction(); + ArrayList userActionResult = userReadyComplete(userInputNumber); + Score score = judgment.judge(userActionResult); + isGameSet = score.isStrikeCountEqualToWinningStrikeCount(); + } + } + + private static boolean hasGameRunning(boolean isGameSet) { + return !isGameSet; + } + + private ArrayList userReadyComplete(String userInputNumber) { + User user = new User(userInputNumber); + return user.getUserInputNumbers(); + } + + private String userAction() { + outputHandler.questionToUserAboutInputNumber(); + return inputHandler.getUserInput(); + } + + private Judgment readyToGame() { + Computer computer = new Computer(); + return new Judgment(computer); + } + + +} From c61f9a7ab19204baf513978e5fa5130546d41dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:43:16 +0900 Subject: [PATCH 11/40] =?UTF-8?q?feat:=20=EB=A9=94=EC=9D=B8=20=EC=96=B4?= =?UTF-8?q?=ED=94=8C=EB=A6=AC=EC=BC=80=EC=9D=B4=EC=85=98=20=EB=A0=88?= =?UTF-8?q?=EB=B2=A8=20=EC=8B=A4=ED=96=89=20=ED=95=A8=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/Main.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..8230731 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,47 @@ +import app.BaseballApplication; +import base.Application; +import core.AppException; +import io.OutputHandler; + +public class Main { + + public static void main(String[] args) { + try { + startGame(); + } catch (Exception e) { + OutputHandler.printDefaultExceptionMessage(e); + } + } + + private static void startGame() { + boolean isGameRunning = true; + + while (isGameRunning) { + Application baseballApplication = new BaseballApplication(); + baseballApplication.run(); + + isGameRunning = getGameStatusOption(baseballApplication); + } + } + + private static boolean getGameStatusOption(Application baseballApplication) { + + try { + String gameFlag = baseballApplication.selectGameRestartOrStop(); + + if (baseballApplication.isGameRestart(gameFlag)) { + return true; + } + + if (baseballApplication.isGameStop(gameFlag)) { + return false; + } + + throw new AppException("게임 시작 명령어를 잘못 입력 하였습니다."); + + } catch (AppException e) { + OutputHandler.printAppExceptionMessage(e); + return getGameStatusOption(baseballApplication); + } + } +} From 8bd1ec91d50917ba7276cfb12b22018f5e749ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:56:40 +0900 Subject: [PATCH 12/40] update: change multi line to one-line --- src/main/java/app/BaseballApplication.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 73fe345..1207de9 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -75,8 +75,7 @@ private String userAction() { } private Judgment readyToGame() { - Computer computer = new Computer(); - return new Judgment(computer); + return new Judgment(new Computer()); } From 67ac759c656042162a17728cf177b5196a260511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:58:56 +0900 Subject: [PATCH 13/40] update: change the method names --- src/main/java/app/BaseballApplication.java | 4 ++-- src/main/java/domain/Computer.java | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 1207de9..472d31d 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -52,7 +52,7 @@ private void applicationToGameStart(Judgment judgment) { private void actionToGameStartByJudgement(Judgment judgment) { boolean isGameSet = false; - while (hasGameRunning(isGameSet)) { + while (hasGameRun(isGameSet)) { String userInputNumber = userAction(); ArrayList userActionResult = userReadyComplete(userInputNumber); Score score = judgment.judge(userActionResult); @@ -60,7 +60,7 @@ private void actionToGameStartByJudgement(Judgment judgment) { } } - private static boolean hasGameRunning(boolean isGameSet) { + private static boolean hasGameRun(boolean isGameSet) { return !isGameSet; } diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java index 2f6ade4..07a1795 100644 --- a/src/main/java/domain/Computer.java +++ b/src/main/java/domain/Computer.java @@ -10,7 +10,7 @@ public class Computer { private final ArrayList randomNumbers = new ArrayList<>(); public ArrayList generateRandomNumber() { - if (randomNumbers.size() == INPUT_LIMIT_LENGTH) { + if (isGenerateRandomNumberSizeEqualToLimit()) { return randomNumbers; } String number = getRandomNumberToString(); @@ -21,6 +21,10 @@ public ArrayList generateRandomNumber() { return generateRandomNumber(); } + private boolean isGenerateRandomNumberSizeEqualToLimit() { + return randomNumbers.size() == INPUT_LIMIT_LENGTH; + } + private String getRandomNumberToString() { return String.valueOf(random.nextInt(9) + 1); } From 44f0badf9d7c22ef1381aed21cb234947a87eabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:52:09 +0900 Subject: [PATCH 14/40] =?UTF-8?q?refactor:=20=EC=8A=A4=EC=BD=94=EC=96=B4?= =?UTF-8?q?=20=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EB=A7=A4=EC=A7=81=20=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=A7=81=20=EC=83=81=EC=88=98=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Score.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/domain/Score.java b/src/main/java/domain/Score.java index 9629159..d805358 100644 --- a/src/main/java/domain/Score.java +++ b/src/main/java/domain/Score.java @@ -9,6 +9,9 @@ public class Score { private int strikeCount; private int ballCount; + private static final String BALL_MESSAGE = "볼"; + private static final String STRIKE_MESSAGE = "스트라이크"; + private static final String UN_HANDLE_MESSAGE = "낫싱"; void clean() { this.strikeCount = 0; @@ -27,12 +30,17 @@ public void printScoreRecordResult() { if (ballCount > 0 && strikeCount > 0) { OutputHandler.printMessage(ballCount + "볼 " + strikeCount + "스트라이크"); return; + return ballCount + BALL_MESSAGE + " " + strikeCount + STRIKE_MESSAGE; + return ballCount + BALL_MESSAGE; + return strikeCount + STRIKE_MESSAGE; } - if (ballCount > 0) { - OutputHandler.printMessage(ballCount + "볼"); - return; - } + return UN_HANDLE_MESSAGE; + } + + private boolean isOnlyStrike() { + return strikeCount > 0; + } if (strikeCount > 0) { OutputHandler.printMessage(strikeCount + "스트라이크"); From db0f07703370f2e8082637017ebe89a4dd9cb97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:53:28 +0900 Subject: [PATCH 15/40] =?UTF-8?q?refactor:=20=EC=8A=A4=EC=BD=94=EC=96=B4?= =?UTF-8?q?=20=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EA=B2=B0=EA=B3=BC=20=EA=B0=92?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=EB=8C=80=EC=8B=A0=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Score.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/domain/Score.java b/src/main/java/domain/Score.java index d805358..0be6523 100644 --- a/src/main/java/domain/Score.java +++ b/src/main/java/domain/Score.java @@ -2,8 +2,6 @@ import static core.SystemConstant.*; -import io.OutputHandler; - public class Score { private int strikeCount; @@ -26,12 +24,16 @@ public void ballIncrement() { this.ballCount += 1; } - public void printScoreRecordResult() { - if (ballCount > 0 && strikeCount > 0) { - OutputHandler.printMessage(ballCount + "볼 " + strikeCount + "스트라이크"); - return; + public String getScoreRecordResult() { + if (isBallAndStrike()) { return ballCount + BALL_MESSAGE + " " + strikeCount + STRIKE_MESSAGE; + } + + if (isOnlyBall()) { return ballCount + BALL_MESSAGE; + } + + if (isOnlyStrike()) { return strikeCount + STRIKE_MESSAGE; } @@ -42,12 +44,12 @@ private boolean isOnlyStrike() { return strikeCount > 0; } - if (strikeCount > 0) { - OutputHandler.printMessage(strikeCount + "스트라이크"); - return; - } + private boolean isOnlyBall() { + return ballCount > 0; + } - OutputHandler.printMessage("낫싱"); + private boolean isBallAndStrike() { + return ballCount > 0 && strikeCount > 0; } public boolean isStrikeCountEqualToWinningStrikeCount() { From f0eccc565dc7438c89bc4b523666162bf5eb4f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:54:03 +0900 Subject: [PATCH 16/40] =?UTF-8?q?update:=20=EC=B6=9C=EB=A0=A5=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=9F=AC=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EB=A9=94?= =?UTF-8?q?=EC=84=B8=EC=A7=80=20=EC=9D=B8=EC=8A=A4=ED=84=B4=EC=8A=A4=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/OutputHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/OutputHandler.java b/src/main/java/io/OutputHandler.java index 3ea2c79..f1d9e7b 100644 --- a/src/main/java/io/OutputHandler.java +++ b/src/main/java/io/OutputHandler.java @@ -8,13 +8,13 @@ public static void printAppExceptionMessage(AppException e) { System.out.println(e.getMessage()); } - public static void printMessage(String message) { + public void printMessage(String message) { System.out.println(message); } public static void printDefaultExceptionMessage(Exception e) { - printMessage("알 수 없는 오류로 인해 시스템이 종료 됩니다."); - printMessage("############# [DEBUG] Traceback Logging ############"); + System.out.println("알 수 없는 오류로 인해 시스템이 종료 됩니다."); + System.out.println("############# [DEBUG] Traceback Logging ############"); System.out.println(e.getMessage()); } From 853a13d55c95d8bf0b40b19f30b6962a74b942a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:55:02 +0900 Subject: [PATCH 17/40] =?UTF-8?q?update:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EB=82=9C=EC=88=98=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=EA=B3=BC=20=EA=B4=80=EB=A0=A8=EB=90=9C=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=AA=85=EC=B9=AD=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Computer.java | 4 ++-- src/main/java/domain/Judgment.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java index 07a1795..7721838 100644 --- a/src/main/java/domain/Computer.java +++ b/src/main/java/domain/Computer.java @@ -9,7 +9,7 @@ public class Computer { private final Random random = new Random(); private final ArrayList randomNumbers = new ArrayList<>(); - public ArrayList generateRandomNumber() { + public ArrayList readyToGameStart() { if (isGenerateRandomNumberSizeEqualToLimit()) { return randomNumbers; } @@ -18,7 +18,7 @@ public ArrayList generateRandomNumber() { if (doesNotDuplicate(number)) { randomNumbers.add(number); } - return generateRandomNumber(); + return readyToGameStart(); } private boolean isGenerateRandomNumberSizeEqualToLimit() { diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java index 8ee55f6..1f64599 100644 --- a/src/main/java/domain/Judgment.java +++ b/src/main/java/domain/Judgment.java @@ -9,7 +9,7 @@ public class Judgment { public Judgment(Computer computer) { - this.computerRandomNumbers = computer.generateRandomNumber(); + this.computerRandomNumbers = computer.readyToGameStart(); System.out.println("computerRandomNumbers = " + computerRandomNumbers); } From cf30776ad84a4abf13f28a25ea38795da41c980e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:56:16 +0900 Subject: [PATCH 18/40] =?UTF-8?q?refactor:=20=EC=8B=AC=ED=8C=90=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EA=B2=B0=EA=B3=BC=20=ED=8C=90?= =?UTF-8?q?=EC=A0=95=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 5 +++++ src/main/java/domain/Judgment.java | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 472d31d..3cb5b05 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -55,7 +55,12 @@ private void actionToGameStartByJudgement(Judgment judgment) { while (hasGameRun(isGameSet)) { String userInputNumber = userAction(); ArrayList userActionResult = userReadyComplete(userInputNumber); + Score score = judgment.judge(userActionResult); + + String scoreResultMessage = score.getScoreRecordResult(); + outputHandler.printMessage(scoreResultMessage); + isGameSet = score.isStrikeCountEqualToWinningStrikeCount(); } } diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java index 1f64599..a342f59 100644 --- a/src/main/java/domain/Judgment.java +++ b/src/main/java/domain/Judgment.java @@ -19,7 +19,6 @@ public Score judge(ArrayList userInputNumbers) { for (int seq = 0; seq < userInputNumbers.size(); seq++) { scoreRecord(userInputNumbers, seq); } - score.printScoreRecordResult(); return score; } From c682dc0802229d6d3f6f0db8df7cac48fa299333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:57:38 +0900 Subject: [PATCH 19/40] =?UTF-8?q?update:=20=EC=8A=A4=EC=BD=94=EC=96=B4=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EC=B4=88=EA=B8=B0=ED=99=94=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=9E=98=ED=95=91=20=EB=B0=8F=20=EB=AA=85?= =?UTF-8?q?=EC=B9=AD=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Judgment.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java index a342f59..f9ed0f3 100644 --- a/src/main/java/domain/Judgment.java +++ b/src/main/java/domain/Judgment.java @@ -14,7 +14,7 @@ public Judgment(Computer computer) { } public Score judge(ArrayList userInputNumbers) { - score.clean(); + newScore(); for (int seq = 0; seq < userInputNumbers.size(); seq++) { scoreRecord(userInputNumbers, seq); @@ -22,6 +22,10 @@ public Score judge(ArrayList userInputNumbers) { return score; } + private void newScore() { + score.clean(); + } + private void scoreRecord(ArrayList userInputNumbers, int i) { if (isStrike(userInputNumbers, i)) { score.strikeIncrement(); From 66882e13c5bbe14732d778b5fc0cce57ac4b6424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:58:04 +0900 Subject: [PATCH 20/40] =?UTF-8?q?refactor:=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=8B=A4=ED=98=95=EC=84=B1=20?= =?UTF-8?q?=EC=9C=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 8230731..d4d0768 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,34 +6,35 @@ public class Main { public static void main(String[] args) { + Application baseballApplication = new BaseballApplication(); + try { - startGame(); + startGame(baseballApplication); } catch (Exception e) { OutputHandler.printDefaultExceptionMessage(e); } } - private static void startGame() { + private static void startGame(Application gameApplication) { boolean isGameRunning = true; while (isGameRunning) { - Application baseballApplication = new BaseballApplication(); - baseballApplication.run(); + gameApplication.run(); - isGameRunning = getGameStatusOption(baseballApplication); + isGameRunning = getGameStatusOption(gameApplication); } } - private static boolean getGameStatusOption(Application baseballApplication) { + private static boolean getGameStatusOption(Application gameApplication) { try { - String gameFlag = baseballApplication.selectGameRestartOrStop(); + String gameFlag = gameApplication.selectGameRestartOrStop(); - if (baseballApplication.isGameRestart(gameFlag)) { + if (gameApplication.isGameRestart(gameFlag)) { return true; } - if (baseballApplication.isGameStop(gameFlag)) { + if (gameApplication.isGameStop(gameFlag)) { return false; } @@ -41,7 +42,7 @@ private static boolean getGameStatusOption(Application baseballApplication) { } catch (AppException e) { OutputHandler.printAppExceptionMessage(e); - return getGameStatusOption(baseballApplication); + return getGameStatusOption(gameApplication); } } } From 8f00b979ef45e91032a33168686f26bb8d74275c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Fri, 23 Aug 2024 13:33:17 +0900 Subject: [PATCH 21/40] =?UTF-8?q?update:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=8B=9C=EA=B7=B8=EB=8B=88=EC=B2=98=EC=9D=98=20=EB=B6=88?= =?UTF-8?q?=EB=B6=84=EB=AA=85=ED=95=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8?= =?UTF-8?q?=ED=84=B0=20=EB=AA=85=EC=B9=AD=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Judgment.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java index f9ed0f3..793ff36 100644 --- a/src/main/java/domain/Judgment.java +++ b/src/main/java/domain/Judgment.java @@ -26,22 +26,22 @@ private void newScore() { score.clean(); } - private void scoreRecord(ArrayList userInputNumbers, int i) { - if (isStrike(userInputNumbers, i)) { + private void scoreRecord(ArrayList userInputNumbers, int arrayInPosition) { + if (isStrike(userInputNumbers, arrayInPosition)) { score.strikeIncrement(); return; } - if (isBall(userInputNumbers, i)) { + if (isBall(userInputNumbers, arrayInPosition)) { score.ballIncrement(); } } - private boolean isBall(ArrayList userInputNumbers, int i) { - return computerRandomNumbers.contains(userInputNumbers.get(i)); + private boolean isBall(ArrayList userInputNumbers, int arrayInPosition) { + return computerRandomNumbers.contains(userInputNumbers.get(arrayInPosition)); } - private boolean isStrike(ArrayList userInputNumbers, int i) { - return computerRandomNumbers.get(i).equals(userInputNumbers.get(i)); + private boolean isStrike(ArrayList userInputNumbers, int arrayInPosition) { + return computerRandomNumbers.get(arrayInPosition).equals(userInputNumbers.get(arrayInPosition)); } } From 133bcdfd3c330aeaf392996b91b3b7c69c13d76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Sat, 24 Aug 2024 01:59:20 +0900 Subject: [PATCH 22/40] =?UTF-8?q?refactor:=20=EB=A9=94=EC=9D=B8=20?= =?UTF-8?q?=EC=96=B4=ED=94=8C=EB=A6=AC=EC=BC=80=EC=9D=B4=EC=85=98=20?= =?UTF-8?q?=EB=A0=88=EB=B2=A8=EC=97=90=EC=84=9C=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EB=8F=84=EC=9A=B0=EB=AF=B8=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index d4d0768..d30d00c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,48 +1,15 @@ import app.BaseballApplication; import base.Application; -import core.AppException; import io.OutputHandler; public class Main { public static void main(String[] args) { Application baseballApplication = new BaseballApplication(); - try { - startGame(baseballApplication); + baseballApplication.startGame(); } catch (Exception e) { OutputHandler.printDefaultExceptionMessage(e); } } - - private static void startGame(Application gameApplication) { - boolean isGameRunning = true; - - while (isGameRunning) { - gameApplication.run(); - - isGameRunning = getGameStatusOption(gameApplication); - } - } - - private static boolean getGameStatusOption(Application gameApplication) { - - try { - String gameFlag = gameApplication.selectGameRestartOrStop(); - - if (gameApplication.isGameRestart(gameFlag)) { - return true; - } - - if (gameApplication.isGameStop(gameFlag)) { - return false; - } - - throw new AppException("게임 시작 명령어를 잘못 입력 하였습니다."); - - } catch (AppException e) { - OutputHandler.printAppExceptionMessage(e); - return getGameStatusOption(gameApplication); - } - } } From 6c0ed041b68609093fa2e610e4297afa31da4b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Sat, 24 Aug 2024 01:59:43 +0900 Subject: [PATCH 23/40] =?UTF-8?q?update:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=95?= =?UTF-8?q?=EC=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/base/Application.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/base/Application.java b/src/main/java/base/Application.java index 7430507..55eadd0 100644 --- a/src/main/java/base/Application.java +++ b/src/main/java/base/Application.java @@ -2,12 +2,5 @@ public interface Application { - boolean isGameRestart(String gameFlag); - - boolean isGameStop(String gameFlag); - - String selectGameRestartOrStop(); - - void run(); - + void startGame(); } From 03da346964c86bfb54899a215b3b701c9421e92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:01:06 +0900 Subject: [PATCH 24/40] =?UTF-8?q?update:=20=EC=A7=81=EA=B4=80=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=EB=84=A4=EC=9D=B4=EB=B0=8D=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 59 +++++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 3cb5b05..e4accbe 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -18,35 +18,65 @@ public class BaseballApplication implements Application { private final InputHandler inputHandler = new InputHandler(); private final OutputHandler outputHandler = new OutputHandler(); - public void run() { + + public void startGame() { Judgment judgment = readyToGame(); outputHandler.gameStartCommentPrint(); - applicationToGameStart(judgment); + boolean isGameRunning = true; + while (isGameRunning) { + run(judgment); + + isGameRunning = pauseForUserGameRunOptionSelect(); + } } - public String selectGameRestartOrStop() { + private String userSelectGameRestartOrStop() { outputHandler.gameEndCommentPrint(); return inputHandler.getUserInput(); } - public boolean isGameRestart(String gameFlag) { - return gameFlag.equals(GAME_RESTART_FLAG); + private boolean isGameRestart(String gameFlag) { + return GAME_RESTART_FLAG.equals(gameFlag); } - public boolean isGameStop(String gameFlag) { - return gameFlag.equals(GAME_STOP_FLAG); + private boolean isGameStop(String gameFlag) { + return GAME_STOP_FLAG.equals(gameFlag); } - private void applicationToGameStart(Judgment judgment) { + private void run(Judgment judgment) { try { actionToGameStartByJudgement(judgment); } catch (AppException e) { - OutputHandler.printAppExceptionMessage(e); + outputHandler.printAppExceptionMessage(e); + + run(judgment); + } + } + + private boolean pauseForUserGameRunOptionSelect() { + try { + String gameFlag = userSelectGameRestartOrStop(); + + return isGameContinue(gameFlag); + + } catch (AppException e) { + outputHandler.printAppExceptionMessage(e); + return pauseForUserGameRunOptionSelect(); + } + } + + private boolean isGameContinue(String gameFlag) { + if (isGameRestart(gameFlag)) { + return true; + } - applicationToGameStart(judgment); + if (isGameStop(gameFlag)) { + return false; } + + throw new AppException("게임 시작 명령어를 잘못 입력 하였습니다."); } private void actionToGameStartByJudgement(Judgment judgment) { @@ -61,11 +91,14 @@ private void actionToGameStartByJudgement(Judgment judgment) { String scoreResultMessage = score.getScoreRecordResult(); outputHandler.printMessage(scoreResultMessage); - isGameSet = score.isStrikeCountEqualToWinningStrikeCount(); + boolean isGameSet = score.isStrikeCountEqualToWinningStrikeCount(); + + if (stillGameRunning(isGameSet)) { + actionToGameStartByJudgement(judgment); } } - private static boolean hasGameRun(boolean isGameSet) { + private boolean stillGameRunning(boolean isGameSet) { return !isGameSet; } @@ -82,6 +115,4 @@ private String userAction() { private Judgment readyToGame() { return new Judgment(new Computer()); } - - } From 9850d248d6c47a87c87b068d1ab86f98d33b2f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:01:23 +0900 Subject: [PATCH 25/40] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=B0=98=EB=B3=B5=20=EA=B5=AC=EC=A1=B0=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index e4accbe..05413d0 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -80,16 +80,13 @@ private boolean isGameContinue(String gameFlag) { } private void actionToGameStartByJudgement(Judgment judgment) { - boolean isGameSet = false; + String userInputNumber = userAction(); + ArrayList userActionResult = userReadyComplete(userInputNumber); - while (hasGameRun(isGameSet)) { - String userInputNumber = userAction(); - ArrayList userActionResult = userReadyComplete(userInputNumber); + Score score = judgment.judge(userActionResult); - Score score = judgment.judge(userActionResult); - - String scoreResultMessage = score.getScoreRecordResult(); - outputHandler.printMessage(scoreResultMessage); + String scoreResultMessage = score.getScoreRecordResult(); + outputHandler.printMessage(scoreResultMessage); boolean isGameSet = score.isStrikeCountEqualToWinningStrikeCount(); From c6f365897a94b401040102d0b3e4110ed91cb193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:02:15 +0900 Subject: [PATCH 26/40] =?UTF-8?q?chore:=20=EC=B6=9C=EB=A0=A5=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=9F=AC=20=EC=A0=95=EC=A0=81=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=95=EC=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/OutputHandler.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/OutputHandler.java b/src/main/java/io/OutputHandler.java index f1d9e7b..f78df45 100644 --- a/src/main/java/io/OutputHandler.java +++ b/src/main/java/io/OutputHandler.java @@ -4,7 +4,9 @@ public class OutputHandler { - public static void printAppExceptionMessage(AppException e) { + public static void printDefaultExceptionMessage(Exception e) { + System.out.println("알 수 없는 오류로 인해 시스템이 종료 됩니다."); + System.out.println("############# [DEBUG] Traceback Logging ############"); System.out.println(e.getMessage()); } @@ -12,9 +14,7 @@ public void printMessage(String message) { System.out.println(message); } - public static void printDefaultExceptionMessage(Exception e) { - System.out.println("알 수 없는 오류로 인해 시스템이 종료 됩니다."); - System.out.println("############# [DEBUG] Traceback Logging ############"); + public void printAppExceptionMessage(AppException e) { System.out.println(e.getMessage()); } From 5d9f09ed996dcb4c28dd890ffb240133de7d70fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:04:11 +0900 Subject: [PATCH 27/40] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=EC=9D=98=20=EC=B9=B4=EC=9A=B4=ED=8A=B8=20=EC=A6=9D?= =?UTF-8?q?=EA=B0=80=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Score.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/domain/Score.java b/src/main/java/domain/Score.java index 0be6523..f92ce9f 100644 --- a/src/main/java/domain/Score.java +++ b/src/main/java/domain/Score.java @@ -2,6 +2,8 @@ import static core.SystemConstant.*; +import core.AppException; + public class Score { private int strikeCount; @@ -17,10 +19,17 @@ void clean() { } public void strikeIncrement() { + if (strikeCount + 1 > WINNING_STRIKE_COUNT) { + throw new AppException("스트라이크 카운트는 최대 " + WINNING_STRIKE_COUNT + "까지 증가할 수 있습니다."); + } this.strikeCount += 1; } public void ballIncrement() { + if (ballCount + 1 > WINNING_STRIKE_COUNT) { + throw new AppException("볼 카운트는 최대 " + WINNING_STRIKE_COUNT + "까지 증가할 수 있습니다."); + } + this.ballCount += 1; } From 1ee19ae27eece9bccaf9bca4e4bc871286d60ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:04:37 +0900 Subject: [PATCH 28/40] =?UTF-8?q?feat:=20=EB=9E=9C=EB=8D=A4=20=EB=82=9C?= =?UTF-8?q?=EC=88=98=20=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EA=B0=92=20=EC=83=81=EC=88=98=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Computer.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java index 7721838..fed1078 100644 --- a/src/main/java/domain/Computer.java +++ b/src/main/java/domain/Computer.java @@ -6,10 +6,12 @@ public class Computer { - private final Random random = new Random(); + public static final int BOUND = 9; + private static final Random random = new Random(); private final ArrayList randomNumbers = new ArrayList<>(); public ArrayList readyToGameStart() { + if (isGenerateRandomNumberSizeEqualToLimit()) { return randomNumbers; } @@ -26,7 +28,7 @@ private boolean isGenerateRandomNumberSizeEqualToLimit() { } private String getRandomNumberToString() { - return String.valueOf(random.nextInt(9) + 1); + return String.valueOf(random.nextInt(BOUND) + 1); } private boolean doesNotDuplicate(String compareInteger) { From d7a47a12eaba9e211f40dd4ca29d947b7cb25035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:06:19 +0900 Subject: [PATCH 29/40] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A5=BC=20=EA=B3=A0=EB=A0=A4=ED=95=98=EC=97=AC=20=EC=8B=AC?= =?UTF-8?q?=ED=8C=90=20=EA=B0=9D=EC=B2=B4=EC=9D=98=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=8B=9C=EA=B7=B8=EB=8B=88=EC=B2=98=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 2 +- src/main/java/domain/Judgment.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 05413d0..5fc20c6 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -110,6 +110,6 @@ private String userAction() { } private Judgment readyToGame() { - return new Judgment(new Computer()); + return new Judgment(computer.readyToGameStart()); } } diff --git a/src/main/java/domain/Judgment.java b/src/main/java/domain/Judgment.java index 793ff36..070c552 100644 --- a/src/main/java/domain/Judgment.java +++ b/src/main/java/domain/Judgment.java @@ -8,9 +8,8 @@ public class Judgment { private final ArrayList computerRandomNumbers; - public Judgment(Computer computer) { - this.computerRandomNumbers = computer.readyToGameStart(); - System.out.println("computerRandomNumbers = " + computerRandomNumbers); + public Judgment(ArrayList computerRandomNumbers) { + this.computerRandomNumbers = computerRandomNumbers; } public Score judge(ArrayList userInputNumbers) { From d15062c67a4e6c1a0e52b8807e0c612a66f51380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:06:52 +0900 Subject: [PATCH 30/40] =?UTF-8?q?update:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=B3=80?= =?UTF-8?q?=EC=88=98=20=EB=A0=88=EB=B2=A8=EB=A1=9C=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 5fc20c6..6f8a3df 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -15,6 +15,7 @@ public class BaseballApplication implements Application { + private final Computer computer = new Computer(); private final InputHandler inputHandler = new InputHandler(); private final OutputHandler outputHandler = new OutputHandler(); From 48df0ef1294f5f5e96469168b688d7a4a48fac29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:07:21 +0900 Subject: [PATCH 31/40] =?UTF-8?q?fix:=20=EA=B2=8C=EC=9E=84=20=EC=9E=AC?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EC=8B=9C=20=EB=AC=B8=EA=B5=AC=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=20=ED=8F=AC=ED=95=A8=20=EB=B0=8F=20=EB=82=9C=EC=88=98?= =?UTF-8?q?=20=EC=9E=AC=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 6f8a3df..0103b27 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -21,11 +21,11 @@ public class BaseballApplication implements Application { public void startGame() { - Judgment judgment = readyToGame(); - outputHandler.gameStartCommentPrint(); - boolean isGameRunning = true; + while (isGameRunning) { + Judgment judgment = readyToGame(); + outputHandler.gameStartCommentPrint(); run(judgment); isGameRunning = pauseForUserGameRunOptionSelect(); From 56f785793409d4a4a46c384bd4532f0d58cf2a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:07:36 +0900 Subject: [PATCH 32/40] =?UTF-8?q?test:=20=EC=8B=AC=ED=8C=90=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/domain/JudgmentTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/test/java/baseball/domain/JudgmentTest.java diff --git a/src/test/java/baseball/domain/JudgmentTest.java b/src/test/java/baseball/domain/JudgmentTest.java new file mode 100644 index 0000000..c7fa1ae --- /dev/null +++ b/src/test/java/baseball/domain/JudgmentTest.java @@ -0,0 +1,75 @@ +package baseball.domain; + +import static org.assertj.core.api.Assertions.*; + +import domain.Judgment; +import domain.Score; +import domain.User; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class JudgmentTest { + + private static ArrayList randomNumbers; + + @BeforeAll + static void setUp() { + randomNumbers = new ArrayList<>(Arrays.asList("1", "2", "3")); + } + + @DisplayName("같은 위치에 숫자는 다르지만 다른 위치에 속해 있는 경우 볼로 판정한다.") + @ParameterizedTest + @CsvSource({"345,1볼", "314,2볼", "312,3볼"}) + void 심판_볼_판정(String numberToString, String result) { + Judgment judgmentFixture = new Judgment(randomNumbers); + User userFixture = new User(numberToString); + + // Given + ArrayList userInputNumbers = userFixture.getUserInputNumbers(); + + // When + Score score = judgmentFixture.judge(userInputNumbers); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo(result); + } + + @DisplayName("같은 위치에 같은 숫자가 있는 경우 스트라이크로 판정한다.") + @ParameterizedTest + @CsvSource({"145,1스트라이크", "125,2스트라이크", "123,3스트라이크"}) + void 심판_스트라이크_판정(String numberToString, String result) { + Judgment judgmentFixture = new Judgment(randomNumbers); + User userFixture = new User(numberToString); + + // Given + ArrayList userInputNumbers = userFixture.getUserInputNumbers(); + + // When + Score score = judgmentFixture.judge(userInputNumbers); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo(result); + } + + @DisplayName("같은 위치에 같은 숫자가 있는 경우 스트라이크로 판정한다.") + @Test + void 심판_낫싱_판정() { + Judgment judgmentFixture = new Judgment(randomNumbers); + User userFixture = new User("789"); + + // Given + ArrayList userInputNumbers = userFixture.getUserInputNumbers(); + + // When + Score score = judgmentFixture.judge(userInputNumbers); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo("낫싱"); + } + +} \ No newline at end of file From 01ec9183db7602b2e17e3a6dbe6ff5e37e4e3deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:07:51 +0900 Subject: [PATCH 33/40] =?UTF-8?q?test:=20=EC=A0=90=EC=88=98=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/baseball/domain/ScoreTest.java | 136 +++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/test/java/baseball/domain/ScoreTest.java diff --git a/src/test/java/baseball/domain/ScoreTest.java b/src/test/java/baseball/domain/ScoreTest.java new file mode 100644 index 0000000..46f75a5 --- /dev/null +++ b/src/test/java/baseball/domain/ScoreTest.java @@ -0,0 +1,136 @@ +package baseball.domain; + +import static org.assertj.core.api.Assertions.*; + +import core.AppException; +import domain.Score; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + + +public class ScoreTest { + + @DisplayName("스코어의 볼을 증가 시키면 변경 된 볼 카운트를 반환한다") + @Test + void 볼_증가() { + // Given + Score score = new Score(); + + // When + score.ballIncrement(); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo("1볼"); + } + + @DisplayName("스코어의 스트라이크를 증가 시키면 변경 된 스트라이크 카운트를 반환한다") + @Test + void 스트라이크_증가() { + // Given + Score score = new Score(); + + // When + score.strikeIncrement(); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo("1스트라이크"); + } + + @DisplayName("볼 카운트는 최대 3까지 증가 할 수 있다") + @Test + void 볼_카운트_최대값() { + // Given + Score score = new Score(); + + // When + score.ballIncrement(); + score.ballIncrement(); + score.ballIncrement(); + assertThatThrownBy(() -> { + score.ballIncrement(); + }) + // Then + .isInstanceOf(AppException.class) + .hasMessageContaining("볼 카운트는 최대 3까지 증가할 수 있습니다"); + } + + @DisplayName("스트라이크 카운트는 최대 3까지 증가 할 수 있다") + @Test + void 스트라이크_카운트_최대값() { + // Given + Score score = new Score(); + + // When + score.strikeIncrement(); + score.strikeIncrement(); + score.strikeIncrement(); + assertThatThrownBy(() -> { + score.strikeIncrement(); + }) + // Then + .isInstanceOf(AppException.class) + .hasMessageContaining("스트라이크 카운트는 최대 3까지 증가할 수 있습니다"); + } + + + @DisplayName("스코어의 스트라이크와 볼을 동시에 증가 시키면 볼과 스트라이크의 결과를 반환한다") + @Test + void 볼_스트라이크_반환() { + // Given + Score score = new Score(); + + // When + score.strikeIncrement(); + score.ballIncrement(); + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo("1볼 1스트라이크"); + } + + @DisplayName("스코어에서 스트라이크 카운트가 3이라면 아웃 상태 값을 반환한다") + @Test + void 스트라이크3_아웃_반환() { + // Given + Score score = new Score(); + + // When + score.strikeIncrement(); + score.strikeIncrement(); + score.strikeIncrement(); + + System.out.println("score.getScoreRecordResult() = " + score.getScoreRecordResult()); + + // Then + assertThat(score.isStrikeCountEqualToWinningStrikeCount()).isTrue(); + } + + @DisplayName("스코어에서 스트라이크 카운트가 3 미만이라면 아웃 상태가 아닌 값을 반환한다") + @Test + void 스트라이크3_아웃_아님_반환() { + // Given + Score score = new Score(); + + // When + score.strikeIncrement(); + score.strikeIncrement(); + + // Then + assertThat(score.isStrikeCountEqualToWinningStrikeCount()).isFalse(); + } + + @DisplayName("스코어는 볼과 스트라이크 카운트가 증가하지 않으면 낫싱을 반환한다") + @Test + void 스코어_낫싱_반환() { + // Given + Score score = new Score(); + + // When + // Pass + + // Then + assertThat(score.getScoreRecordResult()).isEqualTo("낫싱"); + + } +} From 2782287375c17d5c8e5c0885e9a16a0c366b9c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 23:49:15 +0900 Subject: [PATCH 34/40] =?UTF-8?q?update:=20=EB=B6=88=EB=AA=85=ED=99=95?= =?UTF-8?q?=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 0103b27..7c43061 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -81,10 +81,11 @@ private boolean isGameContinue(String gameFlag) { } private void actionToGameStartByJudgement(Judgment judgment) { - String userInputNumber = userAction(); - ArrayList userActionResult = userReadyComplete(userInputNumber); + String userInputValue = userInput(); + User user = new User(userInputValue); + ArrayList getUserInputArrayStringNumbers = user.getUserInputNumbers(); - Score score = judgment.judge(userActionResult); + Score score = judgment.judge(getUserInputArrayStringNumbers); String scoreResultMessage = score.getScoreRecordResult(); outputHandler.printMessage(scoreResultMessage); @@ -100,12 +101,7 @@ private boolean stillGameRunning(boolean isGameSet) { return !isGameSet; } - private ArrayList userReadyComplete(String userInputNumber) { - User user = new User(userInputNumber); - return user.getUserInputNumbers(); - } - - private String userAction() { + private String userInput() { outputHandler.questionToUserAboutInputNumber(); return inputHandler.getUserInput(); } From 0655846e7cc2636e33dc1da08c8f0f3b3f730a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Mon, 26 Aug 2024 23:49:28 +0900 Subject: [PATCH 35/40] =?UTF-8?q?test:=20=EC=9C=A0=EC=A0=80=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/domain/UserPlayerTest.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/test/java/baseball/domain/UserPlayerTest.java diff --git a/src/test/java/baseball/domain/UserPlayerTest.java b/src/test/java/baseball/domain/UserPlayerTest.java new file mode 100644 index 0000000..4632f1f --- /dev/null +++ b/src/test/java/baseball/domain/UserPlayerTest.java @@ -0,0 +1,73 @@ +package baseball.domain; + +import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.*; + +import domain.User; +import core.AppException; +import java.util.ArrayList; +import java.util.Arrays; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class UserPlayerTest { + + @DisplayName("플레이어는 3자리 숫자만 입력 할 수 있다") + @Test + void 유저_입력_자리수_검증() { + // Given + String inputFixture = "1234"; + + // When + assertThatThrownBy(() -> { + User user = new User(inputFixture); + }) + // Then + .isInstanceOf(AppException.class) + .hasMessageContaining("입력한 숫자의 길이가 올바르지 않습니다."); + } + + @DisplayName("유저는 문자를 입력 할 수 없다") + @Test + void 유저_입력_자료형_검증() { + // Given + String userInputFixture = "asd"; + + // When + assertThatThrownBy(() -> { + User user = new User(userInputFixture); + }) + // Then + .isInstanceOf(AppException.class) + .hasMessageContaining("지정 되지 않은 타입의 입력입니다."); + } + + @DisplayName("유저는 0을 입력 할 수 없다") + @Test + void 유저_입력_0_검증() { + // Given + String userInputFixture = "120"; + + // When + assertThatThrownBy(() -> { + User user = new User(userInputFixture); + }) + // Then + .isInstanceOf(AppException.class) + .hasMessageContaining("0은 입력 할 수 없습니다."); + } + + @DisplayName("유저가 정상적으로 입력한 숫자를 문자 타입의 배열로 반환한다") + @Test + void 유저_정상입력_반환() { + // Given + String userInputFixture = "123"; + + // When + User user = new User(userInputFixture); + + // Then + assertThat(user.getUserInputNumbers()).isInstanceOf(ArrayList.class); + assertThat(user.getUserInputNumbers()).isEqualTo(new ArrayList<>(Arrays.asList("1", "2", "3"))); + } +} \ No newline at end of file From 82b962b831a467e0a3022f84a7b70a660d0cba19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:34:16 +0900 Subject: [PATCH 36/40] =?UTF-8?q?chore:=20mocking=20dependency=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 8172fb7..6b428de 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ repositories { dependencies { testImplementation "org.junit.jupiter:junit-jupiter:5.7.2" testImplementation "org.assertj:assertj-core:3.19.0" + testImplementation "org.mockito:mockito-junit-jupiter:5.2.0" } test { From aed9f79ba4ce6a48770747927050541d49541add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:35:18 +0900 Subject: [PATCH 37/40] =?UTF-8?q?update:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=A3=BC=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/BaseballApplication.java | 3 ++- src/main/java/domain/Computer.java | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/app/BaseballApplication.java b/src/main/java/app/BaseballApplication.java index 7c43061..ec4f08a 100644 --- a/src/main/java/app/BaseballApplication.java +++ b/src/main/java/app/BaseballApplication.java @@ -9,13 +9,14 @@ import io.OutputHandler; import java.util.ArrayList; import core.AppException; +import util.RandomGenerator; import static core.SystemConstant.*; public class BaseballApplication implements Application { - private final Computer computer = new Computer(); + private final Computer computer = new Computer(new RandomGenerator()); private final InputHandler inputHandler = new InputHandler(); private final OutputHandler outputHandler = new OutputHandler(); diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java index fed1078..9632fc7 100644 --- a/src/main/java/domain/Computer.java +++ b/src/main/java/domain/Computer.java @@ -2,20 +2,24 @@ import static core.SystemConstant.*; import java.util.ArrayList; -import java.util.Random; +import util.RandomGenerator; public class Computer { public static final int BOUND = 9; - private static final Random random = new Random(); + private final RandomGenerator randomGenerator; private final ArrayList randomNumbers = new ArrayList<>(); + public Computer(RandomGenerator randomGenerator) { + this.randomGenerator = randomGenerator; + } + public ArrayList readyToGameStart() { if (isGenerateRandomNumberSizeEqualToLimit()) { return randomNumbers; } - String number = getRandomNumberToString(); + String number = randomGenerator.getRandomNumberToString(BOUND); if (doesNotDuplicate(number)) { randomNumbers.add(number); @@ -27,10 +31,6 @@ private boolean isGenerateRandomNumberSizeEqualToLimit() { return randomNumbers.size() == INPUT_LIMIT_LENGTH; } - private String getRandomNumberToString() { - return String.valueOf(random.nextInt(BOUND) + 1); - } - private boolean doesNotDuplicate(String compareInteger) { return !randomNumbers.contains(compareInteger); } From 09c884f8363af8857bb795dbddf3becc2693dd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:36:07 +0900 Subject: [PATCH 38/40] =?UTF-8?q?update:=20=EB=B0=B0=EC=97=B4=EC=97=90=20?= =?UTF-8?q?=EA=B0=92=20=EC=B6=94=EA=B0=80=ED=95=98=EB=8A=94=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/domain/Computer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/Computer.java b/src/main/java/domain/Computer.java index 9632fc7..cf6c231 100644 --- a/src/main/java/domain/Computer.java +++ b/src/main/java/domain/Computer.java @@ -22,11 +22,15 @@ public ArrayList readyToGameStart() { String number = randomGenerator.getRandomNumberToString(BOUND); if (doesNotDuplicate(number)) { - randomNumbers.add(number); + append(number); } return readyToGameStart(); } + private void append(String number) { + randomNumbers.add(number); + } + private boolean isGenerateRandomNumberSizeEqualToLimit() { return randomNumbers.size() == INPUT_LIMIT_LENGTH; } From dd1fd378581ca7f7c67c5ad9183857e80ed50820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:36:37 +0900 Subject: [PATCH 39/40] =?UTF-8?q?feat:=20=EB=9E=9C=EB=8D=A4=20=EC=9C=A0?= =?UTF-8?q?=ED=8B=B8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/util/RandomGenerator.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/util/RandomGenerator.java diff --git a/src/main/java/util/RandomGenerator.java b/src/main/java/util/RandomGenerator.java new file mode 100644 index 0000000..ca4e866 --- /dev/null +++ b/src/main/java/util/RandomGenerator.java @@ -0,0 +1,12 @@ +package util; + +import java.util.Random; + +public class RandomGenerator { + + private final Random randomGenerator = new Random(); + + public String getRandomNumberToString(int bound) { + return String.valueOf(randomGenerator.nextInt(bound) + 1); + } +} From c5653fb39b4a1a7d428f21bb07126c08e6023e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A3=BC=ED=99=98?= <118493627+LeeJuHwan@users.noreply.github.com> Date: Wed, 28 Aug 2024 01:36:50 +0900 Subject: [PATCH 40/40] =?UTF-8?q?test:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EB=8B=A8=EC=9C=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/domain/ComputerTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/baseball/domain/ComputerTest.java diff --git a/src/test/java/baseball/domain/ComputerTest.java b/src/test/java/baseball/domain/ComputerTest.java new file mode 100644 index 0000000..aef4381 --- /dev/null +++ b/src/test/java/baseball/domain/ComputerTest.java @@ -0,0 +1,49 @@ +package baseball.domain; + +import static org.assertj.core.api.Assertions.*; + +import domain.Computer; +import java.util.ArrayList; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import util.RandomGenerator; + +class ComputerTest { + + @DisplayName("컴퓨터 플레이어의 난수 생성 메서드는 3개여야한다") + @Test + void 길이_검증() { + // Given + Computer computer = new Computer(new RandomGenerator()); + + // When + int randomNumberGeneratorSize = computer.readyToGameStart().size(); + + // Then + assertThat(randomNumberGeneratorSize).isEqualTo(3); + } + + @DisplayName("컴퓨터 플레이어의 난수 생성 메서드는 중복을 포함할 수 없다") + @Test + void 요소_중복_검사() { + // Given + RandomGenerator mockRandom = Mockito.mock(RandomGenerator.class); + Mockito.when(mockRandom.getRandomNumberToString(9)) + .thenReturn("1", "1", "2", "3"); + + Computer computer = new Computer(mockRandom); + + // When + ArrayList fixture = computer.readyToGameStart(); + System.out.println("fixture = " + fixture); + int randomNumberGeneratorSize = (int) fixture.stream() + .distinct() + .count(); + + // Then + assertThat(randomNumberGeneratorSize).isEqualTo(3); + assertThat(fixture).containsExactly("1", "2", "3"); + } + +} \ No newline at end of file