From fb6f312cc3641e0da61b17a8aa8008bab5c209c8 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:06:17 +0900 Subject: [PATCH 01/21] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2..aae6ad3ee0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,37 @@ +# πŸ’ͺ ν”„λ‘œμ νŠΈ κ°œμš” + +컴퓨터가 μƒμ„±ν•œ 숫자λ₯Ό λ§žμΆ”κΈ° μœ„ν•΄ ν”Œλ ˆμ΄μ–΄κ°€ 숫자λ₯Ό μž…λ ₯ν•œλ‹€. +μ»΄ν“¨ν„°λŠ” ν”Œλ ˆμ΄μ–΄μ˜ μˆ«μžμ— λŒ€ν•΄ 슀트라이크, λ³Ό, λ‚«μ‹±μœΌλ‘œ 힌트λ₯Ό μ œκ³΅ν•œλ‹€. + +# πŸ“ κ΅¬ν˜„ κΈ°λŠ₯ λͺ©λ‘ + +### 컴퓨터가 숫자λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ + +- [ ] `숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.`λ₯Ό 좜λ ₯ν•œλ‹€. +- [ ] 1λΆ€ν„° 9κΉŒμ§€μ˜ 숫자 μ„Έ 개λ₯Ό μƒμ„±ν•œλ‹€. + +### ν”Œλ ˆμ΄μ–΄κ°€ 숫자λ₯Ό μž…λ ₯ν•˜λŠ” κΈ°λŠ₯ + +- [ ] `숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : `λ₯Ό 좜λ ₯ν•œλ‹€. +- [ ] 숫자λ₯Ό μž…λ ₯λ°›λŠ”λ‹€. + - [ ] 빈 λ¬Έμžμ—΄μ΄ μ•„λ‹˜μ„ κ²€μ¦ν•œλ‹€. + - [ ] 길이가 3인 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. + - [ ] 숫자둜 이루어진 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. + - [ ] μˆ«μžκ°€ 1 이상, 9 μ΄ν•˜μž„μ„ κ²€μ¦ν•œλ‹€. + - [ ] μ„œλ‘œ λ‹€λ₯Έ μˆ«μžμž„μ„ κ²€μ¦ν•œλ‹€. + +### 힌트λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ + +- [ ] 컴퓨터에 μ €μž₯ν•œ μˆ«μžμ™€ μž…λ ₯된 숫자λ₯Ό λΉ„κ΅ν•œλ‹€. +- [ ] μŠ€νŠΈλΌμ΄ν¬μ™€ λ³Ό 개수λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. + +### 힌트λ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ + +- [ ] `1λ³Ό 1슀트라이크`와 같이 λ³Ό κ°œμˆ˜μ™€ 슀트라이크 개수λ₯Ό 좜λ ₯ν•œλ‹€. + +### κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•˜λŠ” κΈ°λŠ₯ + +- [ ] μŠ€νŠΈλΌμ΄ν¬κ°€ 3인 경우 κ²Œμž„μ„ μ’…λ£Œν•œλ‹€. + - [ ] `3개의 숫자λ₯Ό λͺ¨λ‘ λ§žνžˆμ…¨μŠ΅λ‹ˆλ‹€! κ²Œμž„ μ’…λ£Œ \n κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”.`λ₯Ό 좜λ ₯ν•œλ‹€. + - [ ] 1 ν˜Ήμ€ 2의 μž…λ ₯을 λ°›λŠ”λ‹€. +- [ ] μž…λ ₯에 따라 κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•œλ‹€. From c4397fbb293631cf00fe23a80a9b4cf0791ce23c Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:06:42 +0900 Subject: [PATCH 02/21] =?UTF-8?q?feat:=20=EC=BD=98=EC=86=94=EC=97=90=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=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 --- .../baseball/view/console/ConsoleReader.java | 24 +++++++++++++++++++ .../baseball/view/console/ConsoleWriter.java | 15 ++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/baseball/view/console/ConsoleReader.java create mode 100644 src/main/java/baseball/view/console/ConsoleWriter.java diff --git a/src/main/java/baseball/view/console/ConsoleReader.java b/src/main/java/baseball/view/console/ConsoleReader.java new file mode 100644 index 0000000000..eecad34e25 --- /dev/null +++ b/src/main/java/baseball/view/console/ConsoleReader.java @@ -0,0 +1,24 @@ +package baseball.view.console; + +import baseball.global.exception.CustomException; +import baseball.global.exception.ErrorMessage; +import camp.nextstep.edu.missionutils.Console; + +public final class ConsoleReader { + public static String enterMessage() { + return Validator.validate(Console.readLine()); + } + + private static class Validator { + public static String validate(String message) { + validateBlankInput(message); + return message; + } + + private static void validateBlankInput(String message) { + if (message.isBlank()) { + throw CustomException.from(ErrorMessage.BLANK_INPUT_ERROR); + } + } + } +} diff --git a/src/main/java/baseball/view/console/ConsoleWriter.java b/src/main/java/baseball/view/console/ConsoleWriter.java new file mode 100644 index 0000000000..4e695bd82b --- /dev/null +++ b/src/main/java/baseball/view/console/ConsoleWriter.java @@ -0,0 +1,15 @@ +package baseball.view.console; + +public final class ConsoleWriter { + public static void printMessage(String message) { + System.out.print(message); + } + + public static void printlnMessage(String message) { + System.out.println(message); + } + + public static void printlnFormat(String message, Object... args) { + printlnMessage(String.format(message, args)); + } +} From d504a069b46fca8d313b47b7de4bc76b0527292c Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:07:02 +0900 Subject: [PATCH 03/21] =?UTF-8?q?feat:=20=EC=BB=A4=EC=8A=A4=ED=85=80=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=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 --- .../global/exception/CustomException.java | 13 +++++++++++++ .../baseball/global/exception/ErrorMessage.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/java/baseball/global/exception/CustomException.java create mode 100644 src/main/java/baseball/global/exception/ErrorMessage.java diff --git a/src/main/java/baseball/global/exception/CustomException.java b/src/main/java/baseball/global/exception/CustomException.java new file mode 100644 index 0000000000..138a9b9ee2 --- /dev/null +++ b/src/main/java/baseball/global/exception/CustomException.java @@ -0,0 +1,13 @@ +package baseball.global.exception; + +public class CustomException extends IllegalArgumentException { + private static final String PREFIX = "[ERROR] "; + + private CustomException(ErrorMessage errorMessage) { + super(PREFIX + errorMessage.getMessage()); + } + + public static CustomException from(ErrorMessage errorMessage) { + return new CustomException(errorMessage); + } +} diff --git a/src/main/java/baseball/global/exception/ErrorMessage.java b/src/main/java/baseball/global/exception/ErrorMessage.java new file mode 100644 index 0000000000..d1bff88deb --- /dev/null +++ b/src/main/java/baseball/global/exception/ErrorMessage.java @@ -0,0 +1,15 @@ +package baseball.global.exception; + +public enum ErrorMessage { + BLANK_INPUT_ERROR("빈 λ¬Έμžμ—΄μ΄ μž…λ ₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€."), + + private final String message; + + ErrorMessage(String message) { + this.message = message; + } + + public String getMessage() { + return this.message; + } +} From b8c8715abe99a826ffd67bbdebf14c02adda4838 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:08:10 +0900 Subject: [PATCH 04/21] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=EC=9D=84=20?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- .../java/baseball/controller/GameManager.java | 19 +++++++++++++++++++ src/main/java/baseball/view/OutputView.java | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/controller/GameManager.java create mode 100644 src/main/java/baseball/view/OutputView.java diff --git a/docs/README.md b/docs/README.md index aae6ad3ee0..b7b7718785 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@ ### 컴퓨터가 숫자λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ -- [ ] `숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.`λ₯Ό 좜λ ₯ν•œλ‹€. +- [x] `숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.`λ₯Ό 좜λ ₯ν•œλ‹€. - [ ] 1λΆ€ν„° 9κΉŒμ§€μ˜ 숫자 μ„Έ 개λ₯Ό μƒμ„±ν•œλ‹€. ### ν”Œλ ˆμ΄μ–΄κ°€ 숫자λ₯Ό μž…λ ₯ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/java/baseball/controller/GameManager.java b/src/main/java/baseball/controller/GameManager.java new file mode 100644 index 0000000000..57d57ac7d5 --- /dev/null +++ b/src/main/java/baseball/controller/GameManager.java @@ -0,0 +1,19 @@ +package baseball.controller; + +import baseball.domain.Computer; +import baseball.view.InputView; +import baseball.view.OutputView; + +public class GameManager { + private final InputView inputView; + private final OutputView outputView; + private final Computer computer; + + public GameManager() { + this.inputView = new InputView(); + this.outputView = new OutputView(); + this.outputView.start(); + this.computer = new Computer(); + } + +} diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java new file mode 100644 index 0000000000..2c3ae9ba9d --- /dev/null +++ b/src/main/java/baseball/view/OutputView.java @@ -0,0 +1,15 @@ +package baseball.view; + +import baseball.view.console.ConsoleWriter; + +public class OutputView { + private static final String BALL = "%dλ³Ό"; + private static final String STRIKE = "%d슀트라이크"; + private static final String BOTH = "%s %s"; + private static final String NOTHING = "λ‚«μ‹±"; + + public void start() { + ConsoleWriter.printlnMessage("숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€."); + } + +} From 6cf7c04b7cd4b8e1d0d2bfd47568290daf854991 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:09:09 +0900 Subject: [PATCH 05/21] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=EB=A5=BC=20?= =?UTF-8?q?=EC=84=B8=20=EA=B0=9C=20=EC=83=9D=EC=84=B1=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=EC=BB=B4=ED=93=A8=ED=84=B0=EC=97=90=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/baseball/domain/Computer.java | 13 +++++++++++++ .../global/util/RandomNumberGenerator.java | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/domain/Computer.java create mode 100644 src/main/java/baseball/global/util/RandomNumberGenerator.java diff --git a/docs/README.md b/docs/README.md index b7b7718785..70bb920690 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,7 @@ ### 컴퓨터가 숫자λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ - [x] `숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.`λ₯Ό 좜λ ₯ν•œλ‹€. -- [ ] 1λΆ€ν„° 9κΉŒμ§€μ˜ 숫자 μ„Έ 개λ₯Ό μƒμ„±ν•œλ‹€. +- [x] 1λΆ€ν„° 9κΉŒμ§€μ˜ 숫자 μ„Έ 개λ₯Ό μƒμ„±ν•œλ‹€. ### ν”Œλ ˆμ΄μ–΄κ°€ 숫자λ₯Ό μž…λ ₯ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/java/baseball/domain/Computer.java b/src/main/java/baseball/domain/Computer.java new file mode 100644 index 0000000000..565dcd5756 --- /dev/null +++ b/src/main/java/baseball/domain/Computer.java @@ -0,0 +1,13 @@ +package baseball.domain; + +import baseball.global.util.RandomNumberGenerator; +import java.util.List; + +public class Computer { + private final List numbers; + + public Computer() { + this.numbers = RandomNumberGenerator.generateRandomNumber(); + } + +} diff --git a/src/main/java/baseball/global/util/RandomNumberGenerator.java b/src/main/java/baseball/global/util/RandomNumberGenerator.java new file mode 100644 index 0000000000..8b9c48956e --- /dev/null +++ b/src/main/java/baseball/global/util/RandomNumberGenerator.java @@ -0,0 +1,18 @@ +package baseball.global.util; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; + +public class RandomNumberGenerator { + public static List generateRandomNumber() { + List computer = new ArrayList<>(); + while (computer.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + return computer; + } +} From 376503a86e80f29a2e647c656819753239724ddd Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:09:57 +0900 Subject: [PATCH 06/21] =?UTF-8?q?feat:=20=ED=94=8C=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EA=B0=80=20=EC=88=AB=EC=9E=90=EB=A5=BC=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EA=B3=A0=20=EA=B2=80=EC=A6=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 14 ++--- src/main/java/baseball/view/InputView.java | 66 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/main/java/baseball/view/InputView.java diff --git a/docs/README.md b/docs/README.md index 70bb920690..354eda4eae 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,13 +12,13 @@ ### ν”Œλ ˆμ΄μ–΄κ°€ 숫자λ₯Ό μž…λ ₯ν•˜λŠ” κΈ°λŠ₯ -- [ ] `숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : `λ₯Ό 좜λ ₯ν•œλ‹€. -- [ ] 숫자λ₯Ό μž…λ ₯λ°›λŠ”λ‹€. - - [ ] 빈 λ¬Έμžμ—΄μ΄ μ•„λ‹˜μ„ κ²€μ¦ν•œλ‹€. - - [ ] 길이가 3인 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. - - [ ] 숫자둜 이루어진 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. - - [ ] μˆ«μžκ°€ 1 이상, 9 μ΄ν•˜μž„μ„ κ²€μ¦ν•œλ‹€. - - [ ] μ„œλ‘œ λ‹€λ₯Έ μˆ«μžμž„μ„ κ²€μ¦ν•œλ‹€. +- [x] `숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : `λ₯Ό 좜λ ₯ν•œλ‹€. +- [x] 숫자λ₯Ό μž…λ ₯λ°›λŠ”λ‹€. + - [x] 빈 λ¬Έμžμ—΄μ΄ μ•„λ‹˜μ„ κ²€μ¦ν•œλ‹€. + - [x] 길이가 3인 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. + - [x] 숫자둜 이루어진 λ¬Έμžμ—΄μž„μ„ κ²€μ¦ν•œλ‹€. + - [x] μˆ«μžκ°€ 1 이상, 9 μ΄ν•˜μž„μ„ κ²€μ¦ν•œλ‹€. + - [x] μ„œλ‘œ λ‹€λ₯Έ μˆ«μžμž„μ„ κ²€μ¦ν•œλ‹€. ### 힌트λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java new file mode 100644 index 0000000000..1c5cde3903 --- /dev/null +++ b/src/main/java/baseball/view/InputView.java @@ -0,0 +1,66 @@ +package baseball.view; + +import baseball.domain.GameCommand; +import baseball.global.exception.CustomException; +import baseball.global.exception.ErrorMessage; +import baseball.global.validator.Validator; +import baseball.view.console.ConsoleReader; +import baseball.view.console.ConsoleWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class InputView { + public List enterNumbers() { + ConsoleWriter.printMessage("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : "); + return validateNumbers(ConsoleReader.enterMessage()); + } + + private List validateNumbers(String message) { + validateLength(message); + List numbers = parseStringToList(message, ""); + validateDuplicated(numbers); + List result = new ArrayList<>(); + for (String number : numbers) { + int value = Validator.validateNumber(message); + Validator.validateRange(value, 1, 9); + result.add(value); + } + return result; + } + + private void validateLength(String message) { + if (message.length() != 3) { + throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); + } + } + + private static List parseStringToList(String message, String separator) { + return Arrays.stream(split(message, separator)).toList(); + } + + private static String[] split(String message, String separator) { + return message.split(separator, -1); + } + + private static void validateDuplicated(List items) { + if (hasDuplicatedItem(items)) { + throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); + } + } + + private static boolean hasDuplicatedItem(List items) { + return items.size() != calculateUniqueItemsCount(items); + } + + private static int calculateUniqueItemsCount(List items) { + return (int) items.stream() + .distinct() + .count(); + } + + public GameCommand enterRestartOrQuit() { + ConsoleWriter.printlnMessage("κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”."); + return GameCommand.from(ConsoleReader.enterMessage()); + } +} From bbb317b94a4edc4c1f188e6464152ed9a110fc36 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:10:16 +0900 Subject: [PATCH 07/21] =?UTF-8?q?feat:=20=ED=9E=8C=ED=8A=B8=EB=A5=BC=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 4 ++-- src/main/java/baseball/domain/Computer.java | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 354eda4eae..de7e7656ca 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,8 +22,8 @@ ### 힌트λ₯Ό μƒμ„±ν•˜λŠ” κΈ°λŠ₯ -- [ ] 컴퓨터에 μ €μž₯ν•œ μˆ«μžμ™€ μž…λ ₯된 숫자λ₯Ό λΉ„κ΅ν•œλ‹€. -- [ ] μŠ€νŠΈλΌμ΄ν¬μ™€ λ³Ό 개수λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. +- [x] 컴퓨터에 μ €μž₯ν•œ μˆ«μžμ™€ μž…λ ₯된 숫자λ₯Ό λΉ„κ΅ν•œλ‹€. +- [x] μŠ€νŠΈλΌμ΄ν¬μ™€ λ³Ό 개수λ₯Ό κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. ### 힌트λ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/java/baseball/domain/Computer.java b/src/main/java/baseball/domain/Computer.java index 565dcd5756..409666d211 100644 --- a/src/main/java/baseball/domain/Computer.java +++ b/src/main/java/baseball/domain/Computer.java @@ -10,4 +10,24 @@ public Computer() { this.numbers = RandomNumberGenerator.generateRandomNumber(); } + public HintResult generateHintResult(List givenNumbers) { + int strike = 0; + int ball = 0; + for (int i = 0; i < numbers.size(); i++) { + if (numbers.get(i) == givenNumbers.get(i)) { + strike++; + } + ball += isBallIndex(i, givenNumbers); + } + return new HintResult(strike, ball); + } + + private int isBallIndex(int i, List givenNumbers) { + for (int j = 0; j < givenNumbers.size(); j++) { + if (i != j && numbers.get(i) == givenNumbers.get(j)) { + return 1; + } + } + return 0; + } } From df056dd1e95ad308b6abe24fd103be1c8eb09f1c Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:10:40 +0900 Subject: [PATCH 08/21] =?UTF-8?q?feat:=20=ED=9E=8C=ED=8A=B8=EB=A5=BC=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- src/main/java/baseball/view/OutputView.java | 28 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index de7e7656ca..241d3d4b16 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,7 +27,7 @@ ### 힌트λ₯Ό 좜λ ₯ν•˜λŠ” κΈ°λŠ₯ -- [ ] `1λ³Ό 1슀트라이크`와 같이 λ³Ό κ°œμˆ˜μ™€ 슀트라이크 개수λ₯Ό 좜λ ₯ν•œλ‹€. +- [x] `1λ³Ό 1슀트라이크`와 같이 λ³Ό κ°œμˆ˜μ™€ 슀트라이크 개수λ₯Ό 좜λ ₯ν•œλ‹€. ### κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•˜λŠ” κΈ°λŠ₯ diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index 2c3ae9ba9d..8d132e9cec 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -1,5 +1,6 @@ package baseball.view; +import baseball.domain.HintResult; import baseball.view.console.ConsoleWriter; public class OutputView { @@ -12,4 +13,31 @@ public void start() { ConsoleWriter.printlnMessage("숫자 야ꡬ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€."); } + public void printHintResult(HintResult hintResult) { + int strike = hintResult.strike(); + int ball = hintResult.ball(); + + ConsoleWriter.printlnMessage(generateResultMessage(strike, ball)); + } + + public String generateResultMessage(int strike, int ball) { + if (strike != 0 && ball != 0) { + return String.format( + BOTH, + String.format(BALL, ball), + String.format(STRIKE, strike) + ); + } + if (ball != 0) { + return String.format(BALL, ball); + } + if (strike != 0) { + return String.format(STRIKE, strike); + } + return NOTHING; + } + + public void printGameOver() { + ConsoleWriter.printlnMessage("3개의 숫자λ₯Ό λͺ¨λ‘ λ§žνžˆμ…¨μŠ΅λ‹ˆλ‹€! κ²Œμž„ μ’…λ£Œ"); + } } From 233a282591bac576dfa2402eefb855ea088d5aab Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:11:09 +0900 Subject: [PATCH 09/21] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=EC=9D=84=20?= =?UTF-8?q?=EA=B3=84=EC=86=8D=20=EC=A7=84=ED=96=89=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EB=B0=98=EB=B3=B5=EB=AC=B8=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/controller/GameManager.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/baseball/controller/GameManager.java b/src/main/java/baseball/controller/GameManager.java index 57d57ac7d5..17df6b8a84 100644 --- a/src/main/java/baseball/controller/GameManager.java +++ b/src/main/java/baseball/controller/GameManager.java @@ -1,8 +1,10 @@ package baseball.controller; import baseball.domain.Computer; +import baseball.domain.HintResult; import baseball.view.InputView; import baseball.view.OutputView; +import java.util.List; public class GameManager { private final InputView inputView; @@ -16,4 +18,20 @@ public GameManager() { this.computer = new Computer(); } + public void run() { + boolean isRunning = true; + while (isRunning) { + List numbers = inputView.enterNumbers(); + HintResult hintResult = computer.generateHintResult(numbers); + outputView.printHintResult(hintResult); + if (isSuccess(hintResult)) { + outputView.printGameOver(); + isRunning = inputView.enterRestartOrQuit().isRunning(); + } + } + } + + private boolean isSuccess(HintResult hintResult) { + return hintResult.strike() == 3; + } } From f0ac6e92bc74353fe38b0af0b290d59f25876ffe Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:11:30 +0900 Subject: [PATCH 10/21] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=EC=9D=98=20?= =?UTF-8?q?=EC=9E=AC=EC=8B=9C=EC=9E=91=20=ED=98=B9=EC=9D=80=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=EC=97=AC=EB=B6=80=EB=A5=BC=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=ED=95=A0=20Enum=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/domain/GameCommand.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/baseball/domain/GameCommand.java diff --git a/src/main/java/baseball/domain/GameCommand.java b/src/main/java/baseball/domain/GameCommand.java new file mode 100644 index 0000000000..dbf9e1c678 --- /dev/null +++ b/src/main/java/baseball/domain/GameCommand.java @@ -0,0 +1,29 @@ +package baseball.domain; + +import baseball.global.exception.CustomException; +import baseball.global.exception.ErrorMessage; +import java.util.Arrays; + +public enum GameCommand { + RESTART("1", true), + QUIT("2", false); + + private final String command; + private final boolean isRunning; + + GameCommand(String command, boolean isRunning) { + this.command = command; + this.isRunning = isRunning; + } + + public static GameCommand from(String command) { + return Arrays.stream(GameCommand.values()) + .filter(element -> element.command.equals(command)) + .findFirst() + .orElseThrow(() -> CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR)); + } + + public boolean isRunning() { + return isRunning; + } +} From 0308d4f5237a4ebe838a8eac6b2444964ea789de Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:11:57 +0900 Subject: [PATCH 11/21] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=EC=9D=98=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=99=80=20=EB=B2=94=EC=9C=84=EB=A5=BC=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=ED=95=98=EB=8A=94=20Validator=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/global/validator/Validator.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/baseball/global/validator/Validator.java diff --git a/src/main/java/baseball/global/validator/Validator.java b/src/main/java/baseball/global/validator/Validator.java new file mode 100644 index 0000000000..529e236c6e --- /dev/null +++ b/src/main/java/baseball/global/validator/Validator.java @@ -0,0 +1,27 @@ +package baseball.global.validator; + +import baseball.global.exception.CustomException; +import baseball.global.exception.ErrorMessage; + +public class Validator { + public static int validateNumber(String message) { + if (isNotNumber(message)) { + throw CustomException.from(ErrorMessage.NOT_NUMBER_ERROR); + } + return Integer.parseInt(message); + } + + private static boolean isNotNumber(String str) { + return !str.matches("-?\\d+"); + } + + public static void validateRange(int number, int start, int end) { + if (isInvalidRange(number, start, end)) { + throw CustomException.from(ErrorMessage.INVALID_RANGE_ERROR); + } + } + + private static boolean isInvalidRange(int number, int start, int end) { + return number < start || number > end; + } +} From 2ef10d1bcddc57b4ccb9a10d88ab651f95643725 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:12:12 +0900 Subject: [PATCH 12/21] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=EB=A5=BC=20=EC=A0=80=EC=9E=A5=ED=95=98?= =?UTF-8?q?=EB=8A=94=20Enum=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/global/exception/ErrorMessage.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/baseball/global/exception/ErrorMessage.java b/src/main/java/baseball/global/exception/ErrorMessage.java index d1bff88deb..c683d8af32 100644 --- a/src/main/java/baseball/global/exception/ErrorMessage.java +++ b/src/main/java/baseball/global/exception/ErrorMessage.java @@ -2,6 +2,9 @@ public enum ErrorMessage { BLANK_INPUT_ERROR("빈 λ¬Έμžμ—΄μ΄ μž…λ ₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€."), + NOT_NUMBER_ERROR("μˆ«μžλ§Œμ„ μž…λ ₯ν•΄μ£Όμ„Έμš”."), + INVALID_NUMBER_ERROR("숫자λ₯Ό λ‹€μ‹œ μž…λ ₯ν•΄μ£Όμ„Έμš”."), + INVALID_RANGE_ERROR("μ˜¬λ°”λ₯Έ λ²”μœ„μ˜ 숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”."); private final String message; From f3a1f4373ee86c558fe0715feb0cf1616d212f16 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:12:25 +0900 Subject: [PATCH 13/21] =?UTF-8?q?feat:=20=EC=96=B4=ED=94=8C=EB=A6=AC?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=85=98=20=EC=8B=A4=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 7 ++++++- src/main/java/baseball/domain/HintResult.java | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/domain/HintResult.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..bc41af4f62 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,12 @@ package baseball; +import baseball.controller.GameManager; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO: ν”„λ‘œκ·Έλž¨ κ΅¬ν˜„ + GameManager gameManager = new GameManager(); + gameManager.run(); + Console.close(); } } diff --git a/src/main/java/baseball/domain/HintResult.java b/src/main/java/baseball/domain/HintResult.java new file mode 100644 index 0000000000..f7ace8a492 --- /dev/null +++ b/src/main/java/baseball/domain/HintResult.java @@ -0,0 +1,7 @@ +package baseball.domain; + +public record HintResult( + int strike, + int ball +) { +} From 7cf4bece5d4b888dc072ea3ac8d7a8ccd6d52297 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:12:42 +0900 Subject: [PATCH 14/21] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/README.md b/docs/README.md index 241d3d4b16..9e5fed66b2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -31,7 +31,7 @@ ### κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•˜λŠ” κΈ°λŠ₯ -- [ ] μŠ€νŠΈλΌμ΄ν¬κ°€ 3인 경우 κ²Œμž„μ„ μ’…λ£Œν•œλ‹€. - - [ ] `3개의 숫자λ₯Ό λͺ¨λ‘ λ§žνžˆμ…¨μŠ΅λ‹ˆλ‹€! κ²Œμž„ μ’…λ£Œ \n κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”.`λ₯Ό 좜λ ₯ν•œλ‹€. - - [ ] 1 ν˜Ήμ€ 2의 μž…λ ₯을 λ°›λŠ”λ‹€. -- [ ] μž…λ ₯에 따라 κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•œλ‹€. +- [x] μŠ€νŠΈλΌμ΄ν¬κ°€ 3인 경우 κ²Œμž„μ„ μ’…λ£Œν•œλ‹€. + - [x] `3개의 숫자λ₯Ό λͺ¨λ‘ λ§žνžˆμ…¨μŠ΅λ‹ˆλ‹€! κ²Œμž„ μ’…λ£Œ \n κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”.`λ₯Ό 좜λ ₯ν•œλ‹€. + - [x] 1 ν˜Ήμ€ 2의 μž…λ ₯을 λ°›λŠ”λ‹€. +- [x] μž…λ ₯에 따라 κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜κ±°λ‚˜ μ’…λ£Œν•œλ‹€. From ccd28543c392cb2cba797cd134852e6583a0baa3 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:22:20 +0900 Subject: [PATCH 15/21] =?UTF-8?q?refactor:=20=EA=B2=8C=EC=9E=84=EC=9D=84?= =?UTF-8?q?=20=EB=8B=A4=EC=8B=9C=20=EC=8B=9C=EC=9E=91=ED=95=A0=20=EB=95=8C?= =?UTF-8?q?=20=EB=9E=9C=EB=8D=A4=20=EC=88=AB=EC=9E=90=EB=A5=BC=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/controller/GameManager.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/controller/GameManager.java b/src/main/java/baseball/controller/GameManager.java index 17df6b8a84..54ebfa14b9 100644 --- a/src/main/java/baseball/controller/GameManager.java +++ b/src/main/java/baseball/controller/GameManager.java @@ -21,14 +21,22 @@ public GameManager() { public void run() { boolean isRunning = true; while (isRunning) { + computer.generate(); + isRunning = play(); + } + } + + private boolean play() { + while (true) { List numbers = inputView.enterNumbers(); HintResult hintResult = computer.generateHintResult(numbers); outputView.printHintResult(hintResult); if (isSuccess(hintResult)) { - outputView.printGameOver(); - isRunning = inputView.enterRestartOrQuit().isRunning(); + break; } } + outputView.printGameOver(); + return inputView.enterRestartOrQuit().isRunning(); } private boolean isSuccess(HintResult hintResult) { From 88c62a8b955c44064ebd48866fecbec33dfeb687 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:22:39 +0900 Subject: [PATCH 16/21] =?UTF-8?q?refactor:=20=EB=A7=88=EC=A7=80=EB=A7=89?= =?UTF-8?q?=20=EA=B3=B5=EB=B0=B1=EA=B9=8C=EC=A7=80=20=EC=9D=B8=EC=8B=9D?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=ED=98=84=EC=83=81=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/baseball/view/InputView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 1c5cde3903..836c0c9fcd 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -22,7 +22,7 @@ private List validateNumbers(String message) { validateDuplicated(numbers); List result = new ArrayList<>(); for (String number : numbers) { - int value = Validator.validateNumber(message); + int value = Validator.validateNumber(number); Validator.validateRange(value, 1, 9); result.add(value); } @@ -40,7 +40,7 @@ private static List parseStringToList(String message, String separator) } private static String[] split(String message, String separator) { - return message.split(separator, -1); + return message.split(separator); } private static void validateDuplicated(List items) { From 8f2f600155e77eb4e4a02265592e826b3686ac2d Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 04:22:59 +0900 Subject: [PATCH 17/21] =?UTF-8?q?refactor:=20=EB=9E=9C=EB=8D=A4=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EC=83=9D=EC=84=B1=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=EB=A5=BC=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/domain/Computer.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/domain/Computer.java b/src/main/java/baseball/domain/Computer.java index 409666d211..a96ddbf9fa 100644 --- a/src/main/java/baseball/domain/Computer.java +++ b/src/main/java/baseball/domain/Computer.java @@ -1,13 +1,18 @@ package baseball.domain; import baseball.global.util.RandomNumberGenerator; +import java.util.ArrayList; import java.util.List; public class Computer { - private final List numbers; + private List numbers; public Computer() { - this.numbers = RandomNumberGenerator.generateRandomNumber(); + this.numbers = new ArrayList<>(); + } + + public void generate() { + numbers = RandomNumberGenerator.generateRandomNumber(); } public HintResult generateHintResult(List givenNumbers) { From 0b6626463a7de9e1d7ab79e96bd35c50e273b7b8 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 12:08:51 +0900 Subject: [PATCH 18/21] =?UTF-8?q?feat:=20=EC=97=90=EB=9F=AC=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=20=EC=8B=9C=20=EC=9E=AC=EC=9E=85=EB=A0=A5=EB=B0=9B?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/controller/GameManager.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/controller/GameManager.java b/src/main/java/baseball/controller/GameManager.java index 54ebfa14b9..9cac62f294 100644 --- a/src/main/java/baseball/controller/GameManager.java +++ b/src/main/java/baseball/controller/GameManager.java @@ -2,9 +2,11 @@ import baseball.domain.Computer; import baseball.domain.HintResult; +import baseball.domain.Numbers; import baseball.view.InputView; import baseball.view.OutputView; -import java.util.List; +import baseball.view.console.ConsoleWriter; +import java.util.function.Supplier; public class GameManager { private final InputView inputView; @@ -28,7 +30,9 @@ public void run() { private boolean play() { while (true) { - List numbers = inputView.enterNumbers(); + Numbers numbers = retry(() -> { + return new Numbers(inputView.enterNumbers()); + }); HintResult hintResult = computer.generateHintResult(numbers); outputView.printHintResult(hintResult); if (isSuccess(hintResult)) { @@ -36,10 +40,22 @@ private boolean play() { } } outputView.printGameOver(); - return inputView.enterRestartOrQuit().isRunning(); + return retry(() -> { + return inputView.enterRestartOrQuit().isRunning(); + }); } private boolean isSuccess(HintResult hintResult) { return hintResult.strike() == 3; } + + private static T retry(Supplier supplier) { + while (true) { + try { + return supplier.get(); + } catch (IllegalArgumentException e) { + ConsoleWriter.printlnMessage(e.getMessage()); + } + } + } } From 18c84a81c5556927fbf484fbbec0b11b29c95a16 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 12:09:12 +0900 Subject: [PATCH 19/21] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90=EB=A5=BC=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20=EB=9E=98=ED=8D=BC=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EC=99=80=20=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=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/baseball/domain/Number.java | 24 ++++++++++ src/main/java/baseball/domain/Numbers.java | 54 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/main/java/baseball/domain/Number.java create mode 100644 src/main/java/baseball/domain/Numbers.java diff --git a/src/main/java/baseball/domain/Number.java b/src/main/java/baseball/domain/Number.java new file mode 100644 index 0000000000..dfdc1272cd --- /dev/null +++ b/src/main/java/baseball/domain/Number.java @@ -0,0 +1,24 @@ +package baseball.domain; + +import static baseball.global.validator.Validator.validateRange; + +public class Number { + private static final int START = 1; + private static final int END = 9; + private Integer value; + + public Number(Integer value) { + Validator.validate(value); + this.value = value; + } + + public int getValue() { + return value; + } + + private static class Validator { + public static void validate(int value) { + validateRange(value, 1, 9); + } + } +} diff --git a/src/main/java/baseball/domain/Numbers.java b/src/main/java/baseball/domain/Numbers.java new file mode 100644 index 0000000000..0a2e89fe64 --- /dev/null +++ b/src/main/java/baseball/domain/Numbers.java @@ -0,0 +1,54 @@ +package baseball.domain; + +import baseball.global.exception.CustomException; +import baseball.global.exception.ErrorMessage; +import java.util.List; + +public class Numbers { + private static final int COUNT = 3; + private List numbers; + + public Numbers(List numbers) { + this.numbers = Validator.validate(numbers); + } + + public int get(int index) { + return numbers.get(index).getValue(); + } + + public int size() { + return numbers.size(); + } + + private static class Validator { + private static List validate(List numbers) { + validateLength(numbers); + validateDuplicated(numbers); + return numbers.stream() + .map(Number::new) + .toList(); + } + + private static void validateLength(List numbers) { + if (numbers.size() != COUNT) { + throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); + } + } + + private static void validateDuplicated(List items) { + if (hasDuplicatedItem(items)) { + throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); + } + } + + private static boolean hasDuplicatedItem(List items) { + return items.size() != calculateUniqueItemsCount(items); + } + + private static int calculateUniqueItemsCount(List items) { + return (int) items.stream() + .distinct() + .count(); + } + } +} From 071004d061f2e2ca5d7254d0fb6b657160f21128 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 12:09:35 +0900 Subject: [PATCH 20/21] =?UTF-8?q?refactor:=20=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=EC=9D=84=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EC=9D=B8=EC=9E=90=20=EC=A0=84=EB=8B=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/domain/Computer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/baseball/domain/Computer.java b/src/main/java/baseball/domain/Computer.java index a96ddbf9fa..b882906403 100644 --- a/src/main/java/baseball/domain/Computer.java +++ b/src/main/java/baseball/domain/Computer.java @@ -15,7 +15,7 @@ public void generate() { numbers = RandomNumberGenerator.generateRandomNumber(); } - public HintResult generateHintResult(List givenNumbers) { + public HintResult generateHintResult(Numbers givenNumbers) { int strike = 0; int ball = 0; for (int i = 0; i < numbers.size(); i++) { @@ -27,7 +27,7 @@ public HintResult generateHintResult(List givenNumbers) { return new HintResult(strike, ball); } - private int isBallIndex(int i, List givenNumbers) { + private int isBallIndex(int i, Numbers givenNumbers) { for (int j = 0; j < givenNumbers.size(); j++) { if (i != j && numbers.get(i) == givenNumbers.get(j)) { return 1; From b3b4d1a7d7ebd7e913698903bbbc54a39f347255 Mon Sep 17 00:00:00 2001 From: MingyeomKim Date: Thu, 14 Dec 2023 12:09:53 +0900 Subject: [PATCH 21/21] =?UTF-8?q?feat:=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=EC=9D=84=20=EB=8F=84=EB=A9=94=EC=9D=B8=EA=B3=BC=20?= =?UTF-8?q?=EB=B6=84=EC=82=B0=ED=95=98=EB=8A=94=20=EA=B2=83=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/view/InputView.java | 61 ++++++---------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 836c0c9fcd..b75239d841 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -1,66 +1,37 @@ package baseball.view; import baseball.domain.GameCommand; -import baseball.global.exception.CustomException; -import baseball.global.exception.ErrorMessage; -import baseball.global.validator.Validator; import baseball.view.console.ConsoleReader; import baseball.view.console.ConsoleWriter; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class InputView { public List enterNumbers() { ConsoleWriter.printMessage("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” : "); - return validateNumbers(ConsoleReader.enterMessage()); + return Validator.validateNumbers(ConsoleReader.enterMessage()); } - private List validateNumbers(String message) { - validateLength(message); - List numbers = parseStringToList(message, ""); - validateDuplicated(numbers); - List result = new ArrayList<>(); - for (String number : numbers) { - int value = Validator.validateNumber(number); - Validator.validateRange(value, 1, 9); - result.add(value); - } - return result; - } - - private void validateLength(String message) { - if (message.length() != 3) { - throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); - } - } - - private static List parseStringToList(String message, String separator) { - return Arrays.stream(split(message, separator)).toList(); + public GameCommand enterRestartOrQuit() { + ConsoleWriter.printlnMessage("κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”."); + return GameCommand.from(ConsoleReader.enterMessage()); } - private static String[] split(String message, String separator) { - return message.split(separator); - } + private static class Validator { + private static List validateNumbers(String message) { + List numbers = parseStringToList(message, ""); - private static void validateDuplicated(List items) { - if (hasDuplicatedItem(items)) { - throw CustomException.from(ErrorMessage.INVALID_NUMBER_ERROR); + return numbers.stream() + .map(baseball.global.validator.Validator::validateNumber) + .toList(); } - } - - private static boolean hasDuplicatedItem(List items) { - return items.size() != calculateUniqueItemsCount(items); - } - private static int calculateUniqueItemsCount(List items) { - return (int) items.stream() - .distinct() - .count(); - } + private static List parseStringToList(String message, String separator) { + return Arrays.stream(split(message, separator)).toList(); + } - public GameCommand enterRestartOrQuit() { - ConsoleWriter.printlnMessage("κ²Œμž„μ„ μƒˆλ‘œ μ‹œμž‘ν•˜λ €λ©΄ 1, μ’…λ£Œν•˜λ €λ©΄ 2λ₯Ό μž…λ ₯ν•˜μ„Έμš”."); - return GameCommand.from(ConsoleReader.enterMessage()); + private static String[] split(String message, String separator) { + return message.split(separator); + } } }