From 68f468ccc6ad4c4c6dcebea2b51f89c4fa970ba4 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 14:15:30 +0900 Subject: [PATCH 01/29] =?UTF-8?q?docs(README):=20README=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..cea05f1 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# 기능 목록 + +- 목표값 생성 연산 + + - `Random` 클래스를 이용한 임의의 수 3개 생성 (1~9) + +- 야구 게임 시작 안내 문구 출력 + + - 잘못된 값에 대한 예외 처리 `IllegalArgumentException` 호출 후 종료 + +- 숫자 입력 요청 + +- 입력값과 목표값 비교 연산 + + - 입력과 목표 간 값 과 위치가 같다면 스트라이크 + - 값만 같다면 볼 + - 둘 다 다르다면 미스 + +- 연산 결과(힌트) 출력 + +- 실패 시 `숫자 입력 요청`부터 반복 + +- 성공 시 입력 요청 + +- 성공 시 입력값에 따른 반복 + + - 1의 경우 프로그램 처음부터 반복 + - 2의 경우 프로그램 종료 + From db22f39a3eaa3580bcce8da0cacaa81b0709f97a Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 14:42:58 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat():=20=EB=AA=A9=ED=91=9C=EA=B0=92=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=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 - Random 클래스의 ints 메서드 사용 - 추후 연산을 위해 목표값을 배열로 생성 --- src/main/java/baseball/Application.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index b4f76f0..abdc8e4 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,26 @@ package baseball; +import java.util.Random; +import java.util.stream.IntStream; + public class Application { + + int[] targetNumber; + public static void main(String[] args) { - // TODO: 코드 구현 + + Application application = new Application(); + + application.createTargetNumber(1, 9, 3); + + } + + private void createTargetNumber(int start, int end, int size) { + + Random random = new Random(); + + IntStream intStream = random.ints(start, end + 1).limit(size); + + targetNumber = intStream.toArray(); } } \ No newline at end of file From 4fd920ab4de93566f2df52f47d38087660f09699 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 14:49:57 +0900 Subject: [PATCH 03/29] =?UTF-8?q?feat():=20=EC=95=BC=EA=B5=AC=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=8B=9C=EC=9E=91=20=EC=95=88=EB=82=B4=20=EB=AC=B8?= =?UTF-8?q?=EA=B5=AC=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=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/Application.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index abdc8e4..e44d9b1 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,5 +1,6 @@ package baseball; +import java.util.Arrays; import java.util.Random; import java.util.stream.IntStream; @@ -13,6 +14,8 @@ public static void main(String[] args) { application.createTargetNumber(1, 9, 3); + System.out.println("숫자 야구 게임을 시작합니다."); + } private void createTargetNumber(int start, int end, int size) { From 4ade51f009e3981adc58ae3060b4248366b09314 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:00:53 +0900 Subject: [PATCH 04/29] =?UTF-8?q?docs(README):=20README=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 - 잘못된 예외 처리 위치 수정 - 스타일 수정 (`-` -> `##`) --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cea05f1..a743265 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,29 @@ # 기능 목록 -- 목표값 생성 연산 +## 목표값 생성 연산 - `Random` 클래스를 이용한 임의의 수 3개 생성 (1~9) + - 추후 연산을 위해 int형 배열로 생성 -- 야구 게임 시작 안내 문구 출력 +## 야구 게임 시작 안내 문구 출력 + +## 숫자 입력 요청 - 잘못된 값에 대한 예외 처리 `IllegalArgumentException` 호출 후 종료 -- 숫자 입력 요청 - -- 입력값과 목표값 비교 연산 +## 입력값과 목표값 비교 연산 - 입력과 목표 간 값 과 위치가 같다면 스트라이크 - 값만 같다면 볼 - 둘 다 다르다면 미스 -- 연산 결과(힌트) 출력 +## 연산 결과(힌트) 출력 -- 실패 시 `숫자 입력 요청`부터 반복 +## 실패 시 `숫자 입력 요청`부터 반복 -- 성공 시 입력 요청 +## 성공 시 입력 요청 -- 성공 시 입력값에 따른 반복 +## 성공 시 입력값에 따른 반복 - 1의 경우 프로그램 처음부터 반복 - 2의 경우 프로그램 종료 From b5152862d84150086b7eb730ffb5e036cd93c407 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:35:58 +0900 Subject: [PATCH 05/29] =?UTF-8?q?docs(README):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EA=B0=92=EC=97=90=20=EB=8C=80=ED=95=9C=20=EA=B2=80=EC=82=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a743265..42d31b7 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,12 @@ - 잘못된 값에 대한 예외 처리 `IllegalArgumentException` 호출 후 종료 +## 입력값에 대한 검사 + + - 공백 검사 -> 처음 입력값 저장 시 모든 공백 제거 // 4 5 6 가능 // 456 가능 // 4 56 가능 + - 타입 검사 -> 예외 + - 사이즈 검사 -> 예외 + ## 입력값과 목표값 비교 연산 - 입력과 목표 간 값 과 위치가 같다면 스트라이크 From a7bad90fb3d0bb13be062b2487438cadf053ad1b Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:37:57 +0900 Subject: [PATCH 06/29] =?UTF-8?q?refactor():=20NUM=5FSIZE=EB=A1=9C=20?= =?UTF-8?q?=EA=B8=B0=EC=A1=B4=20size=20=EB=8C=80=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index e44d9b1..fba56c9 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,10 +1,10 @@ package baseball; -import java.util.Arrays; import java.util.Random; import java.util.stream.IntStream; public class Application { + static final int NUM_SIZE = 3; int[] targetNumber; @@ -12,17 +12,17 @@ public static void main(String[] args) { Application application = new Application(); - application.createTargetNumber(1, 9, 3); + application.createTargetNumber(1, 9); System.out.println("숫자 야구 게임을 시작합니다."); } - private void createTargetNumber(int start, int end, int size) { + private void createTargetNumber(int start, int end) { Random random = new Random(); - IntStream intStream = random.ints(start, end + 1).limit(size); + IntStream intStream = random.ints(start, end + 1).limit(NUM_SIZE); targetNumber = intStream.toArray(); } From 54f0d213a345d3547dbe4ddfa6de1b9bbdf55b1f Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:42:55 +0900 Subject: [PATCH 07/29] =?UTF-8?q?docs(README):=20README=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 - 예외 처리는 검사 기능에서 --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 42d31b7..aea7b6c 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,26 @@ ## 목표값 생성 연산 - - `Random` 클래스를 이용한 임의의 수 3개 생성 (1~9) - - 추후 연산을 위해 int형 배열로 생성 +- `Random` 클래스를 이용한 임의의 수 3개 생성 (1~9) +- 추후 연산을 위해 int형 배열로 생성 ## 야구 게임 시작 안내 문구 출력 ## 숫자 입력 요청 - - - 잘못된 값에 대한 예외 처리 `IllegalArgumentException` 호출 후 종료 ## 입력값에 대한 검사 +- 잘못된 값에 대한 예외 처리 `IllegalArgumentException` 호출 후 종료 + - 공백 검사 -> 처음 입력값 저장 시 모든 공백 제거 // 4 5 6 가능 // 456 가능 // 4 56 가능 - 타입 검사 -> 예외 - 사이즈 검사 -> 예외 ## 입력값과 목표값 비교 연산 - - 입력과 목표 간 값 과 위치가 같다면 스트라이크 - - 값만 같다면 볼 - - 둘 다 다르다면 미스 +- 입력과 목표 간 값 과 위치가 같다면 스트라이크 +- 값만 같다면 볼 +- 둘 다 다르다면 미스 ## 연산 결과(힌트) 출력 @@ -31,6 +31,6 @@ ## 성공 시 입력값에 따른 반복 - - 1의 경우 프로그램 처음부터 반복 - - 2의 경우 프로그램 종료 +- 1의 경우 프로그램 처음부터 반복 +- 2의 경우 프로그램 종료 From 0dd8c6722bc2f6196ec29ff22508df71bcc74119 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:43:29 +0900 Subject: [PATCH 08/29] =?UTF-8?q?feat():=20=EC=88=AB=EC=9E=90=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EC=9A=94=EC=B2=AD=20=EA=B8=B0=EB=8A=A5=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/Application.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index fba56c9..736a743 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,12 +1,14 @@ package baseball; import java.util.Random; +import java.util.Scanner; import java.util.stream.IntStream; public class Application { static final int NUM_SIZE = 3; int[] targetNumber; + int[] userNumber; public static void main(String[] args) { @@ -16,6 +18,8 @@ public static void main(String[] args) { System.out.println("숫자 야구 게임을 시작합니다."); + application.getUserNumber(); + } private void createTargetNumber(int start, int end) { @@ -26,4 +30,17 @@ private void createTargetNumber(int start, int end) { targetNumber = intStream.toArray(); } + + private void getUserNumber() { + + System.out.print("숫자를 입력해주세요 : "); + + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + + userNumber = new int[input.length()]; + for (int i = 0 ; i < input.length() ; i++) { + userNumber[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 + } + } } \ No newline at end of file From 9b48255584ea00cf50f33dcf9d1c9d23fffc91d0 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:45:39 +0900 Subject: [PATCH 09/29] =?UTF-8?q?feat():=20=EC=9E=85=EB=A0=A5=EA=B0=92=20?= =?UTF-8?q?=EA=B2=80=EC=82=AC=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 --- src/main/java/baseball/Application.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 736a743..c558019 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -36,11 +36,28 @@ private void getUserNumber() { System.out.print("숫자를 입력해주세요 : "); Scanner scanner = new Scanner(System.in); - String input = scanner.nextLine(); + String input = scanner.nextLine().replaceAll("\\s+", ""); + + validateInput(input); userNumber = new int[input.length()]; for (int i = 0 ; i < input.length() ; i++) { userNumber[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 } } + + private void validateInput(String input) { + try { + Integer.parseInt(input); + } catch (NumberFormatException e) { + System.out.println("숫자를 입력해주세요 !!"); + throw new IllegalArgumentException(); + } + + + if (input.length() != NUM_SIZE) { + System.out.println("1~9 사이 3개의 숫자를 입력해주세요 !!"); + throw new IllegalArgumentException(); + } + } } \ No newline at end of file From c33df8157c53585e1c54ce4e3af248e25c5e174a Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 15:50:17 +0900 Subject: [PATCH 10/29] =?UTF-8?q?refactor():=20=EB=B0=B0=EC=97=B4=EB=AA=85?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20(=EB=8B=A8=EC=88=98=ED=98=95=20->=20?= =?UTF-8?q?=EB=B3=B5=EC=88=98=ED=98=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index c558019..5f5e0d1 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -7,8 +7,8 @@ public class Application { static final int NUM_SIZE = 3; - int[] targetNumber; - int[] userNumber; + int[] targetNumbers; + int[] userNumbers; public static void main(String[] args) { @@ -28,7 +28,7 @@ private void createTargetNumber(int start, int end) { IntStream intStream = random.ints(start, end + 1).limit(NUM_SIZE); - targetNumber = intStream.toArray(); + targetNumbers = intStream.toArray(); } private void getUserNumber() { @@ -40,9 +40,9 @@ private void getUserNumber() { validateInput(input); - userNumber = new int[input.length()]; + userNumbers = new int[input.length()]; for (int i = 0 ; i < input.length() ; i++) { - userNumber[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 + userNumbers[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 } } From b95ad3da3a1c998590ae3a76c5d2c56135a48401 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Wed, 16 Apr 2025 23:57:59 +0900 Subject: [PATCH 11/29] =?UTF-8?q?feat():=20=EC=9E=85=EB=A0=A5=EA=B0=92?= =?UTF-8?q?=EA=B3=BC=20=EB=AA=A9=ED=91=9C=EA=B0=92=20=EB=B9=84=EA=B5=90=20?= =?UTF-8?q?=EC=97=B0=EC=82=B0=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 --- src/main/java/baseball/Application.java | 62 ++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 5f5e0d1..2b6432d 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,5 +1,6 @@ package baseball; +import java.util.ArrayList; import java.util.Random; import java.util.Scanner; import java.util.stream.IntStream; @@ -10,6 +11,9 @@ public class Application { int[] targetNumbers; int[] userNumbers; + int strike; + int ball; + public static void main(String[] args) { Application application = new Application(); @@ -20,6 +24,8 @@ public static void main(String[] args) { application.getUserNumber(); + application.match(); + } private void createTargetNumber(int start, int end) { @@ -41,7 +47,7 @@ private void getUserNumber() { validateInput(input); userNumbers = new int[input.length()]; - for (int i = 0 ; i < input.length() ; i++) { + for (int i = 0; i < input.length(); i++) { userNumbers[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 } } @@ -60,4 +66,58 @@ private void validateInput(String input) { throw new IllegalArgumentException(); } } + + private void match() { + targetNumbers = new int[]{1, 2, 3}; + + ArrayList redundants = new ArrayList<>(NUM_SIZE); + + strike = matchStrike(redundants); + + ball = matchBall(redundants); + } + + private int matchStrike(ArrayList redundants) { + int strike = 0; + + for (int i = 0; i < NUM_SIZE; i++) { + if (redundants.contains(i)) continue; + + for (int j = 0; j < NUM_SIZE; j++) { + if (redundants.contains(j)) continue; + + if (targetNumbers[i] == userNumbers[j] && (i == j)) { + strike++; + redundants.add(j); + + break; + } + } + } + + return strike; + } + + private int matchBall(ArrayList redundants) { + int ball = 0; + ArrayList ballRedundants = new ArrayList<>(NUM_SIZE); + + for (int i = 0; i < NUM_SIZE; i++) { + if (redundants.contains(i)) continue; + + for (int j = 0; j < NUM_SIZE; j++) { + if (redundants.contains(j)) continue; + if (ballRedundants.contains(j)) continue; + ; + + if (targetNumbers[i] == userNumbers[j]) { + ball++; + ballRedundants.add(j); + + break; + } + } + } + return ball; + } } \ No newline at end of file From 48911352919fff225c61d202f3c5adaaa48b9e27 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 00:07:01 +0900 Subject: [PATCH 12/29] =?UTF-8?q?feat():=20=EC=97=B0=EC=82=B0=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=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/Application.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 2b6432d..7bb4fca 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,6 +1,7 @@ package baseball; import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.util.stream.IntStream; @@ -26,6 +27,8 @@ public static void main(String[] args) { application.match(); + application.printResult(); + } private void createTargetNumber(int start, int end) { @@ -120,4 +123,19 @@ private int matchBall(ArrayList redundants) { } return ball; } + + private void printResult() { + if (ball == 0 && (strike == 0)) { + System.out.println("미스"); + } else if (ball == 0) { + System.out.println(strike + "스트라이크"); + if (strike == NUM_SIZE) { + System.out.println(NUM_SIZE + "개의 숫자를 모두 맞히셨습니다!"); + } + } else if (strike == 0) { + System.out.println(ball + "볼"); + } else { + System.out.println(ball + "볼" + " " + strike + "스트라이크"); + } + } } \ No newline at end of file From f965b4ca3bebe1931de2edb7bed627718bac32cf Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 00:08:13 +0900 Subject: [PATCH 13/29] =?UTF-8?q?fix():=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EB=B3=80=EC=88=98=20=EA=B3=A0?= =?UTF-8?q?=EC=A0=95=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7bb4fca..967d072 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -71,8 +71,6 @@ private void validateInput(String input) { } private void match() { - targetNumbers = new int[]{1, 2, 3}; - ArrayList redundants = new ArrayList<>(NUM_SIZE); strike = matchStrike(redundants); From d5574962bec4af6f6f3951163afaf3b2371b594c Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 00:16:25 +0900 Subject: [PATCH 14/29] =?UTF-8?q?feat():=20=EC=8B=A4=ED=8C=A8=20=EC=8B=9C?= =?UTF-8?q?=20`=EC=88=AB=EC=9E=90=20=EC=9E=85=EB=A0=A5=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD`=EB=B6=80=ED=84=B0=20=EB=B0=98=EB=B3=B5=20=EA=B8=B0?= =?UTF-8?q?=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 --- src/main/java/baseball/Application.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 967d072..882b8c0 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -8,6 +8,8 @@ public class Application { static final int NUM_SIZE = 3; + static final int STATUS_SUCCESS = 1; + static final int STATUS_FAIL = 0; int[] targetNumbers; int[] userNumbers; @@ -15,6 +17,8 @@ public class Application { int strike; int ball; + int status; + public static void main(String[] args) { Application application = new Application(); @@ -29,6 +33,8 @@ public static void main(String[] args) { application.printResult(); + application.checkStatus(application); + } private void createTargetNumber(int start, int end) { @@ -124,16 +130,32 @@ private int matchBall(ArrayList redundants) { private void printResult() { if (ball == 0 && (strike == 0)) { + status = STATUS_FAIL; System.out.println("미스"); } else if (ball == 0) { + status = STATUS_FAIL; System.out.println(strike + "스트라이크"); if (strike == NUM_SIZE) { System.out.println(NUM_SIZE + "개의 숫자를 모두 맞히셨습니다!"); } } else if (strike == 0) { + status = STATUS_FAIL; System.out.println(ball + "볼"); } else { + status = STATUS_FAIL; System.out.println(ball + "볼" + " " + strike + "스트라이크"); } } + + private void checkStatus(Application application) { + if (status == STATUS_FAIL) { + application.getUserNumber(); + + application.match(); + + application.printResult(); + + application.checkStatus(application); + } + } } \ No newline at end of file From 3e69c01fe921a21146c92aa1dce8650d3f055514 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 00:39:59 +0900 Subject: [PATCH 15/29] =?UTF-8?q?feat():=20=EC=84=B1=EA=B3=B5=20=EC=8B=9C?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EC=9A=94=EC=B2=AD=20=EA=B8=B0=EB=8A=A5?= =?UTF-8?q?=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/baseball/Application.java | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 882b8c0..5898afb 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -10,6 +10,7 @@ public class Application { static final int NUM_SIZE = 3; static final int STATUS_SUCCESS = 1; static final int STATUS_FAIL = 0; + static final int STATUS_RESTART = 2; int[] targetNumbers; int[] userNumbers; @@ -136,6 +137,7 @@ private void printResult() { status = STATUS_FAIL; System.out.println(strike + "스트라이크"); if (strike == NUM_SIZE) { + status = STATUS_SUCCESS; System.out.println(NUM_SIZE + "개의 숫자를 모두 맞히셨습니다!"); } } else if (strike == 0) { @@ -157,5 +159,30 @@ private void checkStatus(Application application) { application.checkStatus(application); } + if (status == STATUS_SUCCESS) { + getGameOption(application); + } + } + + private void getGameOption(Application application) { + int gameOption = 0; + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + Scanner scanner = new Scanner(System.in); + try { + gameOption = scanner.nextInt(); + + if (gameOption == 1) { + status = STATUS_RESTART; + checkStatus(application); + + } else if (gameOption == 2) { + return; + } else { + throw new IllegalArgumentException(); + } + } catch (Exception e) { + System.out.println("1 또는 2를 입력하세요 !!"); + throw new IllegalArgumentException(); + } } } \ No newline at end of file From 33e7d9dd820653220d69f65020f6b860623c6911 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 00:55:37 +0900 Subject: [PATCH 16/29] =?UTF-8?q?feat():=20=EC=84=B1=EA=B3=B5=20=EC=8B=9C?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=EA=B0=92=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EB=B0=98=EB=B3=B5=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 --- src/main/java/baseball/Application.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 5898afb..d984772 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -162,6 +162,19 @@ private void checkStatus(Application application) { if (status == STATUS_SUCCESS) { getGameOption(application); } + if (status == STATUS_RESTART) { + application.createTargetNumber(1, 9); + + System.out.println("숫자 야구 게임을 시작합니다."); + + application.getUserNumber(); + + application.match(); + + application.printResult(); + + application.checkStatus(application); + } } private void getGameOption(Application application) { From 04ccc5d5823d44b78bd89a0e0983a9d386630c28 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 10:50:56 +0900 Subject: [PATCH 17/29] =?UTF-8?q?refactor():=20=EA=B8=B0=EC=A1=B4=20int?= =?UTF-8?q?=EA=B0=92=20=EC=88=AB=EC=9E=90=20->=20BaseballNumber=EA=B0=92?= =?UTF-8?q?=20=EC=88=AB=EC=9E=90=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 94 +++++++++---------- .../java/baseball/model/BaseballNumber.java | 33 +++++++ 2 files changed, 77 insertions(+), 50 deletions(-) create mode 100644 src/main/java/baseball/model/BaseballNumber.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index d984772..006c6c6 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,10 +1,11 @@ package baseball; +import baseball.model.BaseballNumber; + import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.Scanner; -import java.util.stream.IntStream; public class Application { static final int NUM_SIZE = 3; @@ -12,11 +13,8 @@ public class Application { static final int STATUS_FAIL = 0; static final int STATUS_RESTART = 2; - int[] targetNumbers; - int[] userNumbers; - - int strike; - int ball; + BaseballNumber[] targetNumbers; + BaseballNumber[] userNumbers; int status; @@ -39,12 +37,15 @@ public static void main(String[] args) { } private void createTargetNumber(int start, int end) { + targetNumbers = new BaseballNumber[NUM_SIZE]; - Random random = new Random(); - - IntStream intStream = random.ints(start, end + 1).limit(NUM_SIZE); + for (int i = 0; i < NUM_SIZE; i++) { + targetNumbers[i] = new BaseballNumber(new Random().nextInt(end) + start); + } - targetNumbers = intStream.toArray(); + for (BaseballNumber targetNumber: targetNumbers) { // todo: 디버그용 + System.out.println(targetNumber.getValue()); + } } private void getUserNumber() { @@ -56,9 +57,11 @@ private void getUserNumber() { validateInput(input); - userNumbers = new int[input.length()]; - for (int i = 0; i < input.length(); i++) { - userNumbers[i] = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 + userNumbers = new BaseballNumber[NUM_SIZE]; + int inputNumber; + for (int i = 0; i < NUM_SIZE; i++) { + inputNumber = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 + userNumbers[i] = new BaseballNumber(inputNumber); } } @@ -78,58 +81,49 @@ private void validateInput(String input) { } private void match() { - ArrayList redundants = new ArrayList<>(NUM_SIZE); - - strike = matchStrike(redundants); - - ball = matchBall(redundants); + checkStrike(); + checkBall(); } - private int matchStrike(ArrayList redundants) { - int strike = 0; - - for (int i = 0; i < NUM_SIZE; i++) { - if (redundants.contains(i)) continue; - - for (int j = 0; j < NUM_SIZE; j++) { - if (redundants.contains(j)) continue; + private void checkStrike() { + for (int index = 0; index < NUM_SIZE; index++) { + if ( userNumbers[index].getValue() == targetNumbers[index].getValue()) { + userNumbers[index].setStrike(true); + targetNumbers[index].setStrike(true); + } + } + } - if (targetNumbers[i] == userNumbers[j] && (i == j)) { - strike++; - redundants.add(j); + private void checkBall() { + for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { + if (userNumbers[userIndex].isStrike()) continue; + for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { + if (targetNumbers[targetIndex].isStrike()) continue; + if (userNumbers[userIndex].getValue() == targetNumbers[targetIndex].getValue()) { + userNumbers[userIndex].setBall(true); + targetNumbers[targetIndex].setBall(true); break; } } } - - return strike; } - private int matchBall(ArrayList redundants) { + private void printResult() { + int strike = 0; int ball = 0; - ArrayList ballRedundants = new ArrayList<>(NUM_SIZE); - - for (int i = 0; i < NUM_SIZE; i++) { - if (redundants.contains(i)) continue; - - for (int j = 0; j < NUM_SIZE; j++) { - if (redundants.contains(j)) continue; - if (ballRedundants.contains(j)) continue; - ; - if (targetNumbers[i] == userNumbers[j]) { - ball++; - ballRedundants.add(j); - - break; - } + for (BaseballNumber targetNumber: targetNumbers) { // todo: strike와 ball 계산 책임 // 분리 필요 + if (targetNumber.isStrike()) { + strike++; + continue; + } + if (targetNumber.isBall()) { + ball++; } } - return ball; - } - private void printResult() { + if (ball == 0 && (strike == 0)) { status = STATUS_FAIL; System.out.println("미스"); diff --git a/src/main/java/baseball/model/BaseballNumber.java b/src/main/java/baseball/model/BaseballNumber.java new file mode 100644 index 0000000..7643b4c --- /dev/null +++ b/src/main/java/baseball/model/BaseballNumber.java @@ -0,0 +1,33 @@ +package baseball.model; + +// 시스템이 생성하는 랜덤한 숫자 1개 // vo class +public class BaseballNumber { + private final int value; + + private boolean strike = false; + private boolean ball = false; + + public BaseballNumber(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public boolean isStrike() { + return strike; + } + + public boolean isBall() { + return ball; + } + + public void setStrike(boolean strike) { + this.strike = strike; + } + + public void setBall(boolean ball) { + this.ball = ball; + } +} From d5751a7aa7975b31419ac5ce13970718734d1199 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 10:56:50 +0900 Subject: [PATCH 18/29] =?UTF-8?q?docs(README):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EA=B0=92=20=EA=B2=80=EC=82=AC=20=EC=A4=91=20=EB=88=84=EB=9D=BD?= =?UTF-8?q?=EB=90=9C=200=20=EA=B2=80=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aea7b6c..675bd4d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ - 공백 검사 -> 처음 입력값 저장 시 모든 공백 제거 // 4 5 6 가능 // 456 가능 // 4 56 가능 - 타입 검사 -> 예외 - 사이즈 검사 -> 예외 + - 0 검사 (범위 검사) -> 예외 ## 입력값과 목표값 비교 연산 From 4c2b2dd2cc5b1f6261a66853a11c9cac164ff3d3 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 10:57:17 +0900 Subject: [PATCH 19/29] =?UTF-8?q?fix():=20=EC=9E=85=EB=A0=A5=EA=B0=92=20?= =?UTF-8?q?=EA=B2=80=EC=82=AC=20=EC=A4=91=20=EB=88=84=EB=9D=BD=EB=90=9C=20?= =?UTF-8?q?0=20=EA=B2=80=EC=82=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/baseball/Application.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 006c6c6..fbc1eda 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -73,6 +73,11 @@ private void validateInput(String input) { throw new IllegalArgumentException(); } + if (input.contains("0")) { + System.out.println("1~9 사이 3개의 숫자를 입력해주세요 !!"); + throw new IllegalArgumentException(); + } + if (input.length() != NUM_SIZE) { System.out.println("1~9 사이 3개의 숫자를 입력해주세요 !!"); From 112c522a44e79edb807160ccad35fec488c0c0b4 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 11:01:57 +0900 Subject: [PATCH 20/29] =?UTF-8?q?chore():=20=EB=B6=88=ED=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EB=B0=8F=20import=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/Application.java | 2 -- src/main/java/baseball/model/BaseballNumber.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index fbc1eda..a9ae786 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,8 +2,6 @@ import baseball.model.BaseballNumber; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Random; import java.util.Scanner; diff --git a/src/main/java/baseball/model/BaseballNumber.java b/src/main/java/baseball/model/BaseballNumber.java index 7643b4c..dc6de78 100644 --- a/src/main/java/baseball/model/BaseballNumber.java +++ b/src/main/java/baseball/model/BaseballNumber.java @@ -1,6 +1,6 @@ package baseball.model; -// 시스템이 생성하는 랜덤한 숫자 1개 // vo class +// vo class public class BaseballNumber { private final int value; From a7b339ff054cd407c35c12df91f3fe7a2e0a34c7 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 11:25:48 +0900 Subject: [PATCH 21/29] =?UTF-8?q?refactor():=20=EA=B8=B0=EC=A1=B4=20int?= =?UTF-8?q?=EA=B0=92=EC=9C=BC=EB=A1=9C=20=EB=8B=A4=EB=A3=A8=EB=8D=98=20sta?= =?UTF-8?q?tus=EB=A5=BC=20enum=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 24 +++++++++----------- src/main/java/baseball/model/GameStatus.java | 7 ++++++ 2 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 src/main/java/baseball/model/GameStatus.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index a9ae786..1f2b719 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,20 +1,18 @@ package baseball; import baseball.model.BaseballNumber; +import baseball.model.GameStatus; import java.util.Random; import java.util.Scanner; public class Application { static final int NUM_SIZE = 3; - static final int STATUS_SUCCESS = 1; - static final int STATUS_FAIL = 0; - static final int STATUS_RESTART = 2; BaseballNumber[] targetNumbers; BaseballNumber[] userNumbers; - int status; + GameStatus gameStatus; public static void main(String[] args) { @@ -128,26 +126,26 @@ private void printResult() { if (ball == 0 && (strike == 0)) { - status = STATUS_FAIL; + gameStatus = GameStatus.FAIL; System.out.println("미스"); } else if (ball == 0) { - status = STATUS_FAIL; + gameStatus = GameStatus.FAIL; System.out.println(strike + "스트라이크"); if (strike == NUM_SIZE) { - status = STATUS_SUCCESS; + gameStatus = GameStatus.SUCCESS; System.out.println(NUM_SIZE + "개의 숫자를 모두 맞히셨습니다!"); } } else if (strike == 0) { - status = STATUS_FAIL; + gameStatus = GameStatus.FAIL; System.out.println(ball + "볼"); } else { - status = STATUS_FAIL; + gameStatus = GameStatus.FAIL; System.out.println(ball + "볼" + " " + strike + "스트라이크"); } } private void checkStatus(Application application) { - if (status == STATUS_FAIL) { + if (gameStatus == GameStatus.FAIL) { application.getUserNumber(); application.match(); @@ -156,10 +154,10 @@ private void checkStatus(Application application) { application.checkStatus(application); } - if (status == STATUS_SUCCESS) { + if (gameStatus == GameStatus.SUCCESS) { getGameOption(application); } - if (status == STATUS_RESTART) { + if (gameStatus == GameStatus.RESTART) { application.createTargetNumber(1, 9); System.out.println("숫자 야구 게임을 시작합니다."); @@ -182,7 +180,7 @@ private void getGameOption(Application application) { gameOption = scanner.nextInt(); if (gameOption == 1) { - status = STATUS_RESTART; + gameStatus = GameStatus.RESTART; checkStatus(application); } else if (gameOption == 2) { diff --git a/src/main/java/baseball/model/GameStatus.java b/src/main/java/baseball/model/GameStatus.java new file mode 100644 index 0000000..205bf27 --- /dev/null +++ b/src/main/java/baseball/model/GameStatus.java @@ -0,0 +1,7 @@ +package baseball.model; + +public enum GameStatus { + FAIL, + SUCCESS, + RESTART +} From 77d0740ff8d0dc1b43d61ceda245590d3598564f Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 11:31:08 +0900 Subject: [PATCH 22/29] =?UTF-8?q?fix():=20=EB=88=84=EB=9D=BD=EB=90=9C=20st?= =?UTF-8?q?rike/ball=20=EC=83=81=ED=83=9C=20=EC=B4=88=EA=B8=B0=ED=99=94=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 이전 스트라이크 기록이 그대로 남아있는 에러에 대한 fix --- src/main/java/baseball/Application.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 1f2b719..f68783f 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -146,6 +146,11 @@ private void printResult() { private void checkStatus(Application application) { if (gameStatus == GameStatus.FAIL) { + for (BaseballNumber targetNumber: application.targetNumbers) { + targetNumber.setStrike(false); + targetNumber.setBall(false); + } + application.getUserNumber(); application.match(); From fda9895675b1dbbcfb33a64962a2953e6ae56c7d Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 11:41:21 +0900 Subject: [PATCH 23/29] =?UTF-8?q?refactor():=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=20=EC=88=AB=EC=9E=90=20=EC=9E=AC=EC=9E=85=EB=A0=A5=20=EC=8B=9C?= =?UTF-8?q?=20=EA=B8=B0=EC=A1=B4=EC=97=90=20=EC=83=9D=EC=84=B1=EB=90=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=88=AB=EC=9E=90=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=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/Application.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index f68783f..b65e8bb 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -150,6 +150,7 @@ private void checkStatus(Application application) { targetNumber.setStrike(false); targetNumber.setBall(false); } + userNumbers = null; application.getUserNumber(); From b6682d01e204e90578856189bc3771cd33ad065b Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Thu, 17 Apr 2025 13:44:10 +0900 Subject: [PATCH 24/29] =?UTF-8?q?refactor():=20MVC=20=ED=8C=A8=ED=84=B4?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20&?= =?UTF-8?q?=20=EA=B7=B8=20=EC=99=B8=20Util=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 문자열은 Message 클래스로 분리 - 검사 기능은 Validator 클래스로 분리 --- src/main/java/baseball/Application.java | 197 +----------------- .../controller/BaseballGameController.java | 31 +++ .../java/baseball/model/BaseballGame.java | 81 +++++++ src/main/java/baseball/util/Message.java | 14 ++ src/main/java/baseball/util/Validator.java | 17 ++ src/main/java/baseball/view/InputView.java | 19 ++ src/main/java/baseball/view/OutputView.java | 64 ++++++ 7 files changed, 228 insertions(+), 195 deletions(-) create mode 100644 src/main/java/baseball/controller/BaseballGameController.java create mode 100644 src/main/java/baseball/model/BaseballGame.java create mode 100644 src/main/java/baseball/util/Message.java create mode 100644 src/main/java/baseball/util/Validator.java create mode 100644 src/main/java/baseball/view/InputView.java create mode 100644 src/main/java/baseball/view/OutputView.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index b65e8bb..28ee7fb 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,202 +1,9 @@ package baseball; -import baseball.model.BaseballNumber; -import baseball.model.GameStatus; - -import java.util.Random; -import java.util.Scanner; +import baseball.controller.BaseballGameController; public class Application { - static final int NUM_SIZE = 3; - - BaseballNumber[] targetNumbers; - BaseballNumber[] userNumbers; - - GameStatus gameStatus; - public static void main(String[] args) { - - Application application = new Application(); - - application.createTargetNumber(1, 9); - - System.out.println("숫자 야구 게임을 시작합니다."); - - application.getUserNumber(); - - application.match(); - - application.printResult(); - - application.checkStatus(application); - - } - - private void createTargetNumber(int start, int end) { - targetNumbers = new BaseballNumber[NUM_SIZE]; - - for (int i = 0; i < NUM_SIZE; i++) { - targetNumbers[i] = new BaseballNumber(new Random().nextInt(end) + start); - } - - for (BaseballNumber targetNumber: targetNumbers) { // todo: 디버그용 - System.out.println(targetNumber.getValue()); - } - } - - private void getUserNumber() { - - System.out.print("숫자를 입력해주세요 : "); - - Scanner scanner = new Scanner(System.in); - String input = scanner.nextLine().replaceAll("\\s+", ""); - - validateInput(input); - - userNumbers = new BaseballNumber[NUM_SIZE]; - int inputNumber; - for (int i = 0; i < NUM_SIZE; i++) { - inputNumber = Integer.parseInt(String.valueOf(input.charAt(i))); // todo: 리팩토링 - userNumbers[i] = new BaseballNumber(inputNumber); - } - } - - private void validateInput(String input) { - try { - Integer.parseInt(input); - } catch (NumberFormatException e) { - System.out.println("숫자를 입력해주세요 !!"); - throw new IllegalArgumentException(); - } - - if (input.contains("0")) { - System.out.println("1~9 사이 3개의 숫자를 입력해주세요 !!"); - throw new IllegalArgumentException(); - } - - - if (input.length() != NUM_SIZE) { - System.out.println("1~9 사이 3개의 숫자를 입력해주세요 !!"); - throw new IllegalArgumentException(); - } - } - - private void match() { - checkStrike(); - checkBall(); - } - - private void checkStrike() { - for (int index = 0; index < NUM_SIZE; index++) { - if ( userNumbers[index].getValue() == targetNumbers[index].getValue()) { - userNumbers[index].setStrike(true); - targetNumbers[index].setStrike(true); - } - } - } - - private void checkBall() { - for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { - if (userNumbers[userIndex].isStrike()) continue; - for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { - if (targetNumbers[targetIndex].isStrike()) continue; - - if (userNumbers[userIndex].getValue() == targetNumbers[targetIndex].getValue()) { - userNumbers[userIndex].setBall(true); - targetNumbers[targetIndex].setBall(true); - break; - } - } - } - } - - private void printResult() { - int strike = 0; - int ball = 0; - - for (BaseballNumber targetNumber: targetNumbers) { // todo: strike와 ball 계산 책임 // 분리 필요 - if (targetNumber.isStrike()) { - strike++; - continue; - } - if (targetNumber.isBall()) { - ball++; - } - } - - - if (ball == 0 && (strike == 0)) { - gameStatus = GameStatus.FAIL; - System.out.println("미스"); - } else if (ball == 0) { - gameStatus = GameStatus.FAIL; - System.out.println(strike + "스트라이크"); - if (strike == NUM_SIZE) { - gameStatus = GameStatus.SUCCESS; - System.out.println(NUM_SIZE + "개의 숫자를 모두 맞히셨습니다!"); - } - } else if (strike == 0) { - gameStatus = GameStatus.FAIL; - System.out.println(ball + "볼"); - } else { - gameStatus = GameStatus.FAIL; - System.out.println(ball + "볼" + " " + strike + "스트라이크"); - } - } - - private void checkStatus(Application application) { - if (gameStatus == GameStatus.FAIL) { - for (BaseballNumber targetNumber: application.targetNumbers) { - targetNumber.setStrike(false); - targetNumber.setBall(false); - } - userNumbers = null; - - application.getUserNumber(); - - application.match(); - - application.printResult(); - - application.checkStatus(application); - } - if (gameStatus == GameStatus.SUCCESS) { - getGameOption(application); - } - if (gameStatus == GameStatus.RESTART) { - application.createTargetNumber(1, 9); - - System.out.println("숫자 야구 게임을 시작합니다."); - - application.getUserNumber(); - - application.match(); - - application.printResult(); - - application.checkStatus(application); - } - } - - private void getGameOption(Application application) { - int gameOption = 0; - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - Scanner scanner = new Scanner(System.in); - try { - gameOption = scanner.nextInt(); - - if (gameOption == 1) { - gameStatus = GameStatus.RESTART; - checkStatus(application); - - } else if (gameOption == 2) { - return; - } else { - throw new IllegalArgumentException(); - } - } catch (Exception e) { - System.out.println("1 또는 2를 입력하세요 !!"); - throw new IllegalArgumentException(); - } + new BaseballGameController().run(); } } \ No newline at end of file diff --git a/src/main/java/baseball/controller/BaseballGameController.java b/src/main/java/baseball/controller/BaseballGameController.java new file mode 100644 index 0000000..4f561eb --- /dev/null +++ b/src/main/java/baseball/controller/BaseballGameController.java @@ -0,0 +1,31 @@ +package baseball.controller; + +import baseball.model.BaseballGame; +import baseball.view.InputView; +import baseball.view.OutputView; + +public class BaseballGameController { + private final BaseballGame game = new BaseballGame(); + private final InputView inputView = new InputView(); + private final OutputView outputView = new OutputView(); + + private static final int START = 1; + private static final int END = 9; + + public void run() { + game.createTargetNumber(START, END); + outputView.printStartMessage(); + + while (true) { + game.reset(); // targetNumber의 상태 초기화 및 이전 userNumber free + inputView.getUserNumber(game); + game.match(); + outputView.printResult(game); + + if (game.getStatus() == baseball.model.GameStatus.SUCCESS) { + if (outputView.isRestart(game)) continue; // todo: 클래스 위치가 view? + break; + } + } + } +} diff --git a/src/main/java/baseball/model/BaseballGame.java b/src/main/java/baseball/model/BaseballGame.java new file mode 100644 index 0000000..4abf5cf --- /dev/null +++ b/src/main/java/baseball/model/BaseballGame.java @@ -0,0 +1,81 @@ +package baseball.model; + +import java.util.Random; + +public class BaseballGame { + private static final int NUM_SIZE = 3; + + BaseballNumber[] targetNumbers; + BaseballNumber[] userNumbers; + + GameStatus status; + + public void createTargetNumber(int start, int end) { + targetNumbers = new BaseballNumber[NUM_SIZE]; + for (int i = 0; i < NUM_SIZE; i++) { + targetNumbers[i] = new BaseballNumber(new Random().nextInt(end) + start); + } + } + + public void createUserNumbers(String input) { + userNumbers = new BaseballNumber[NUM_SIZE]; + for (int i = 0; i < NUM_SIZE; i++) { + int inputNumber = Integer.parseInt(String.valueOf(input.charAt(i))); + userNumbers[i] = new BaseballNumber(inputNumber); + } + } + + + public void match() { + checkStrike(); + checkBall(); + } + + private void checkStrike() { + for (int index = 0; index < NUM_SIZE; index++) { + if (userNumbers[index].getValue() == targetNumbers[index].getValue()) { + userNumbers[index].setStrike(true); + targetNumbers[index].setStrike(true); + } + } + } + + private void checkBall() { + for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { + if (userNumbers[userIndex].isStrike()) continue; + for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { + if (targetNumbers[targetIndex].isStrike()) continue; + if (userNumbers[userIndex].getValue() == targetNumbers[targetIndex].getValue()) { + userNumbers[userIndex].setBall(true); + targetNumbers[targetIndex].setBall(true); + break; + } + } + } + } + + public void reset() { + for (BaseballNumber targetNumber: targetNumbers) { + targetNumber.setStrike(false); + targetNumber.setBall(false); + } + + userNumbers = null; + } + + public BaseballNumber[] getTargetNumbers() { + return targetNumbers; + } + + public GameStatus getStatus() { + return status; + } + + public void setStatus(GameStatus status) { + this.status = status; + } + + public static int getNumSize() { + return NUM_SIZE; + } +} diff --git a/src/main/java/baseball/util/Message.java b/src/main/java/baseball/util/Message.java new file mode 100644 index 0000000..7703445 --- /dev/null +++ b/src/main/java/baseball/util/Message.java @@ -0,0 +1,14 @@ +package baseball.util; + +public class Message { + public static final String MESSAGE_START = "숫자 야구 게임을 시작합니다."; + public static final String MESSAGE_INPUT_PROMPT = "숫자를 입력해주세요 : "; + public static final String MESSAGE_INPUT_ERROR = "1~9 사이 3개의 숫자를 입력해주세요 !!"; + public static final String MESSAGE_MISS = "미스"; + public static final String MESSAGE_STRIKE_ONLY_FORMATTED = "%d스트라이크"; + public static final String MESSAGE_BALL_ONLY_FORMATTED = "%d볼"; + public static final String MESSAGE_BALL_STRIKE_FORMATTED = "%d볼 %d스트라이크"; + public static final String MESSAGE_SUCCESS_FORMATTED = "%d개의 숫자를 모두 맞히셨습니다!"; + public static final String MESSAGE_RESTART_PROMPT = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."; + public static final String MESSAGE_RESTART_ERROR = "1 또는 2를 입력하세요 !!"; +} \ No newline at end of file diff --git a/src/main/java/baseball/util/Validator.java b/src/main/java/baseball/util/Validator.java new file mode 100644 index 0000000..6f8c9d1 --- /dev/null +++ b/src/main/java/baseball/util/Validator.java @@ -0,0 +1,17 @@ +package baseball.util; + +import baseball.model.BaseballGame; + +public class Validator { + public static void inputValidate(String input) { + try { + Integer.parseInt(input); + if (input.contains("0") || input.length() != BaseballGame.getNumSize()) { + throw new IllegalArgumentException(); + } + } catch (Exception e) { + System.out.println(Message.MESSAGE_INPUT_ERROR); + throw new IllegalArgumentException(); + } + } +} diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java new file mode 100644 index 0000000..ed79887 --- /dev/null +++ b/src/main/java/baseball/view/InputView.java @@ -0,0 +1,19 @@ +package baseball.view; + +import baseball.model.BaseballGame; +import baseball.util.Message; +import baseball.util.Validator; + +import java.util.Scanner; + +public class InputView { + public void getUserNumber(BaseballGame baseballGame) { + System.out.print(Message.MESSAGE_INPUT_PROMPT); + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine().replaceAll("\\s+", ""); + + Validator.inputValidate(input); + + baseballGame.createUserNumbers(input); + } +} diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java new file mode 100644 index 0000000..e9c5c56 --- /dev/null +++ b/src/main/java/baseball/view/OutputView.java @@ -0,0 +1,64 @@ +package baseball.view; + +import baseball.model.BaseballGame; +import baseball.model.BaseballNumber; +import baseball.model.GameStatus; +import baseball.util.Message; + +import java.util.Scanner; + +public class OutputView { + public void printStartMessage() { + System.out.println(Message.MESSAGE_START); + } + + public void printResult(BaseballGame game) { + int strike = 0; + int ball = 0; + + for (BaseballNumber targetNumber : game.getTargetNumbers()) { + if (targetNumber.isStrike()) { + strike++; + continue; + } + if (targetNumber.isBall()) { + ball++; + } + } + + if (ball == 0 && strike == 0) { + game.setStatus(GameStatus.FAIL); + System.out.println(Message.MESSAGE_MISS); + } else if (ball == 0) { + game.setStatus(GameStatus.FAIL); + System.out.println(String.format(Message.MESSAGE_STRIKE_ONLY_FORMATTED, strike)); + if (strike == game.getNumSize()) { + game.setStatus(GameStatus.SUCCESS); + System.out.println(String.format(Message.MESSAGE_SUCCESS_FORMATTED, BaseballGame.getNumSize())); + } + } else if (strike == 0) { + game.setStatus(GameStatus.FAIL); + System.out.println(String.format(Message.MESSAGE_BALL_ONLY_FORMATTED, ball)); + } else { + game.setStatus(GameStatus.FAIL); + System.out.println(String.format(Message.MESSAGE_BALL_STRIKE_FORMATTED, ball, strike)); + } + } + + public boolean isRestart(BaseballGame game) { // todo: 출력뿐 아니라 조건 분기도 하고 있음.. + System.out.println(Message.MESSAGE_RESTART_PROMPT); + Scanner scanner = new Scanner(System.in); + try { + int option = scanner.nextInt(); + if (option == 1) { + game.createTargetNumber(1, 9); + game.setStatus(GameStatus.RESTART); // 의미가 있나? + return true; + } + if (option == 2) return false; + } catch (Exception e) { + System.out.println(Message.MESSAGE_RESTART_ERROR); + } + throw new IllegalArgumentException(); + } +} From c14b790873ea8724770d3c5bf8d812dc666fa22a Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Sat, 26 Apr 2025 16:30:37 +0900 Subject: [PATCH 25/29] =?UTF-8?q?fix(checkBall()):=20ball=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EC=A4=91=20=EC=9E=98=EB=AA=BB=EB=90=9C=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit isStrike() -> isball() --- src/main/java/baseball/model/BaseballGame.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baseball/model/BaseballGame.java b/src/main/java/baseball/model/BaseballGame.java index 4abf5cf..3341946 100644 --- a/src/main/java/baseball/model/BaseballGame.java +++ b/src/main/java/baseball/model/BaseballGame.java @@ -44,7 +44,7 @@ private void checkBall() { for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { if (userNumbers[userIndex].isStrike()) continue; for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { - if (targetNumbers[targetIndex].isStrike()) continue; + if (targetNumbers[targetIndex].isBall()) continue; if (userNumbers[userIndex].getValue() == targetNumbers[targetIndex].getValue()) { userNumbers[userIndex].setBall(true); targetNumbers[targetIndex].setBall(true); From 6cce9fca72171be56324e37c1168661340a2ca24 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Sat, 26 Apr 2025 16:33:22 +0900 Subject: [PATCH 26/29] =?UTF-8?q?refactor(checkBall()):=20=EA=B0=80?= =?UTF-8?q?=EB=8F=85=EC=84=B1=EC=9D=84=20=EA=B3=A0=EB=A0=A4=ED=95=B4=20?= =?UTF-8?q?=EC=A1=B0=EA=B1=B4=EB=AC=B8=20=EB=82=B4=20=EB=B3=80=EC=88=98=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/baseball/model/BaseballGame.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/baseball/model/BaseballGame.java b/src/main/java/baseball/model/BaseballGame.java index 3341946..de4a15a 100644 --- a/src/main/java/baseball/model/BaseballGame.java +++ b/src/main/java/baseball/model/BaseballGame.java @@ -41,13 +41,13 @@ private void checkStrike() { } private void checkBall() { - for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { - if (userNumbers[userIndex].isStrike()) continue; - for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { - if (targetNumbers[targetIndex].isBall()) continue; - if (userNumbers[userIndex].getValue() == targetNumbers[targetIndex].getValue()) { + for (int targetIndex = 0; targetIndex < NUM_SIZE; targetIndex++) { + if (targetNumbers[targetIndex].isStrike()) continue; + for (int userIndex = 0; userIndex < NUM_SIZE; userIndex++) { + if (userNumbers[userIndex].isBall()) continue; + if (targetNumbers[targetIndex].getValue() == userNumbers[userIndex].getValue()) { + targetNumbers[userIndex].setBall(true); userNumbers[userIndex].setBall(true); - targetNumbers[targetIndex].setBall(true); break; } } From a492d6b3dad14477a20d559c7c96a110d799b757 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Sat, 26 Apr 2025 17:16:41 +0900 Subject: [PATCH 27/29] =?UTF-8?q?refactor(isRestart()):=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=A9=EB=B2=95=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/view/OutputView.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index e9c5c56..419dbee 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -54,11 +54,15 @@ public boolean isRestart(BaseballGame game) { // todo: 출력뿐 아니라 조 game.createTargetNumber(1, 9); game.setStatus(GameStatus.RESTART); // 의미가 있나? return true; + } else if (option == 2) return false; + else { + // 1,2 외의 숫자 + System.out.println(Message.MESSAGE_RESTART_ERROR); + throw new IllegalArgumentException(); } - if (option == 2) return false; - } catch (Exception e) { + } catch (Exception e) { // 잘못된 타입 System.out.println(Message.MESSAGE_RESTART_ERROR); + throw e; } - throw new IllegalArgumentException(); } } From fc1b2f7b1775b710d6d014623f7be93c9a801207 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Sat, 26 Apr 2025 17:18:18 +0900 Subject: [PATCH 28/29] =?UTF-8?q?docs():=20todo=20=EC=A3=BC=EC=84=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baseball/controller/BaseballGameController.java | 8 +++++--- src/main/java/baseball/view/OutputView.java | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/baseball/controller/BaseballGameController.java b/src/main/java/baseball/controller/BaseballGameController.java index 4f561eb..3903605 100644 --- a/src/main/java/baseball/controller/BaseballGameController.java +++ b/src/main/java/baseball/controller/BaseballGameController.java @@ -4,6 +4,8 @@ import baseball.view.InputView; import baseball.view.OutputView; +import java.util.Scanner; + public class BaseballGameController { private final BaseballGame game = new BaseballGame(); private final InputView inputView = new InputView(); @@ -20,10 +22,10 @@ public void run() { game.reset(); // targetNumber의 상태 초기화 및 이전 userNumber free inputView.getUserNumber(game); game.match(); - outputView.printResult(game); + outputView.printResult(game); //todo: ball, strike 연산을 view클래스에서? - if (game.getStatus() == baseball.model.GameStatus.SUCCESS) { - if (outputView.isRestart(game)) continue; // todo: 클래스 위치가 view? + if (game.getStatus() == baseball.model.GameStatus.SUCCESS) { // todo: 이것도 따로 함수로 빼야될 듯 + if (outputView.isRestart(game)) continue; // todo: restart 연산을 view클래스에서? break; } } diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index 419dbee..5d26267 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -26,7 +26,7 @@ public void printResult(BaseballGame game) { } } - if (ball == 0 && strike == 0) { + if (ball == 0 && strike == 0) { // todo: 연산을 view클래스에서? game.setStatus(GameStatus.FAIL); System.out.println(Message.MESSAGE_MISS); } else if (ball == 0) { From edd9b2ec03ade4c9a1fd5eff92761c5c9ea2e1a5 Mon Sep 17 00:00:00 2001 From: HyunjeLee Date: Sat, 26 Apr 2025 17:40:00 +0900 Subject: [PATCH 29/29] =?UTF-8?q?test():=20=EC=BD=94=EB=93=9C=EC=97=90=20?= =?UTF-8?q?=EB=A7=9E=EA=B2=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/baseball/ApplicationTest.java | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index d7242f5..8a88584 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -6,31 +6,31 @@ import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.List; +import baseball.model.BaseballGame; +import baseball.model.BaseballNumber; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class ApplicationTest { - // TODO: 클래스명과 함수명 변경 가능 (RandomNumberGenerator.generate()) @Test public void 랜덤_숫자_생성_함수는_1부터_9까지의_숫자_3개를_생성한다() { - // TODO: 실제 구현에서는 RandomNumberGenerator.generate() 등 실제 함수를 호출할 것 - List numbers = List.of(); // 임시 코드. 구현 후 아래 코드로 변경해주세요. - // 예: List numbers = RandomNumberGenerator.generate(); + BaseballGame baseballGame = new BaseballGame(); + baseballGame.createTargetNumber(1,9); + BaseballNumber[] numbers = baseballGame.getTargetNumbers(); - assertThat(numbers.size()).isEqualTo(3); + assertThat(numbers.length).isEqualTo(3); - for (Integer number : numbers) { - assertThat(number).isBetween(1, 9); + for (BaseballNumber number : numbers) { + assertThat(number.getValue()).isBetween(1, 9); } - long distinctCount = numbers.stream().distinct().count(); - assertThat(distinctCount).isEqualTo(3); +// long distinctCount = numbers.stream().distinct().count(); +// assertThat(distinctCount).isEqualTo(3); } - - // TODO: 필요에 따라 추가 테스트 코드 작성 가능 }