From 5c3fced1334481d72368375f52fe14c0a60b53a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Mon, 6 Nov 2023 15:15:40 +0900 Subject: [PATCH 01/11] start --- src/main/java/baseball/Application.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..d483f01147 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,6 +2,7 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + // TODO: 프로그램 + } } From 0cb6b364d9db2932eb54b7b82a1a18d3bde8ba00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Thu, 9 Nov 2023 11:51:26 +0900 Subject: [PATCH 02/11] baseball (mvc) --- src/main/java/baseball/Application.java | 157 +++++++++++++++++- .../baseball/Controller/GameController.java | 78 +++++++++ src/main/java/baseball/Model/Game.java | 56 +++++++ src/main/java/baseball/Model/GameResult.java | 43 +++++ src/main/java/baseball/View/GameView.java | 34 ++++ src/test/java/baseball/ApplicationTest.java | 3 +- 6 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 src/main/java/baseball/Controller/GameController.java create mode 100644 src/main/java/baseball/Model/Game.java create mode 100644 src/main/java/baseball/Model/GameResult.java create mode 100644 src/main/java/baseball/View/GameView.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index d483f01147..03628f07ed 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,8 +1,159 @@ package baseball; +import baseball.View.GameView; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + public class Application { + public static void main(String[] args) { - // TODO: 프로그램 - + boolean playAgain = true; + GameView gameview = new GameView(); + + while (playAgain) { + gameview.showGameStart(); + int[] ComputerNumber = CheckComputerNumber(getComputerNumber()); + playGame(ComputerNumber); + playAgain = playAgain(); + + } + } + + private static void playGame(int[] ComputerNumber) { //depth 2 , 게임 실행 메소드 + + boolean win = false; + + while (!win) { + int[] UserNumber = StringToIntArray(getUserNumber()); + CheckUserNumber(UserNumber); + int[] result = checkNum(ComputerNumber, UserNumber); + printResult(result); + System.out.println(); + + if (result[1] == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + win = true; + } + } + } + + private static boolean playAgain() { //depth 0 , 게임 제시작 메소드 + + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + + String num = Console.readLine(); + + return !num.equals("2"); + } + + private static int[] getComputerNumber() { //depth 1 , 컴퓨터 수 생성 메소드 + int[] computerNumber = new int[3]; + + computerNumber[0] = Randoms.pickNumberInRange(1, 9); + + for (int i = 1; i < 3; i++) { + computerNumber[i] = Randoms.pickNumberInRange(1, 9); + } + + return computerNumber; + } + + private static int[] CheckComputerNumber(int[] array) { //depth 3, 컴퓨터가 생성한 수 에 대한 중복 검사 메소드 + int[] newArray = new int[3]; + Set set = new HashSet<>(); + int count = 0; + + for (int num : array) { + if (set.contains(num)) { + int newNumber; + + do { + newNumber = Randoms.pickNumberInRange(1, 9); + } while (set.contains(newNumber)); // 중복된 값을 피하기 위해 반복 + + newArray[count] = newNumber; + } + + if (!set.contains(num)) { + newArray[count] = array[count]; + } + + set.add(num); + count++; + } + + return newArray; + } + + + private static String getUserNumber() {// depth 0, 유저에게 값을 입력받는 메소드 + System.out.print("숫자를 입력해주세요 : "); + String UserNum = Console.readLine(); + return UserNum; + } + + private static void CheckUserNumber(int[] array) { //depth 2, 유저 가 입력한 값에 오류가 있는지 확인하는 메소드 + //서로 다른 3자리 정수 + Set verify = new HashSet<>(); + for (int num : array) { + if (verify.contains(num) || array.length != 3) { + throw new IllegalArgumentException("유효하지 않은 사용자 입력입니다."); + } + verify.add(num); + } + } + + private static int[] StringToIntArray(String input) {//depth 1, 문자열로 입력받은 값을 정수 배열로 변환 + int[] result = new int[input.length()]; + + for (int i = 0; i < input.length(); i++) { + char digit = input.charAt(i); + result[i] = Character.getNumericValue(digit); + } + return result; + } + + private static int[] checkNum(int[] computerNumber, int[] userNum) {//depth 2 , 볼 과 스트라이크 판별 메소드 + int strikes = 0; + int balls = 0; + + for (int i = 0; i < 3; i++) { + int num = userNum[i]; + + if (userNum[i] == computerNumber[i]) { + strikes++; + } + + if (Arrays.stream(computerNumber).anyMatch(x -> x == num) && userNum[i] != computerNumber[i]) { + balls++; + } + } + + return new int[]{balls, strikes}; + } + + private static void printResult(int[] result) { //depth 1 + if (result[0] == 0 && result[1] == 0) {// 볼도 스트라이크도 아닌경우 + System.out.println("낫싱"); + return; + } + + if (result[1] > 0 && result[0] > 0) { // 볼, 스트라이크 둘다 있는 경우 + System.out.println(result[0] + "볼 " + result[1] + "스트라이크"); + return; + } + + if (result[1] > 0 && result[0] == 0) { // 스트라이크 만 있는 경우 + System.out.println(result[1] + "스트라이크"); + return; + } + + if (result[1] == 0 && result[0] > 0) { // 볼 만 있는 경우 + System.out.println(result[0] + "볼 "); + } + } -} +} \ No newline at end of file diff --git a/src/main/java/baseball/Controller/GameController.java b/src/main/java/baseball/Controller/GameController.java new file mode 100644 index 0000000000..d8560eefa8 --- /dev/null +++ b/src/main/java/baseball/Controller/GameController.java @@ -0,0 +1,78 @@ +package baseball.Controller; + +import baseball.Model.Game; +import baseball.View.GameView; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class GameController { + private final GameView gameView = new GameView(); + + GameController() { + gameView.showGameStart(); + } + + public void start() { + Game game = new Game(); + while (game.getGameStatus()) { + gameView.showInputNumbers(); + String strInputNumbers = gameView.getInput(); + validInputNumbers(strInputNumbers); + List inputNumbers = stringToIntList(strInputNumbers); + + String gameResult = game.makeGameResult(inputNumbers); + gameView.showString(gameResult); + } + gameView.showGameEnd(); + + gameView.showInputRestart(); + String strInputRestart = gameView.getInput(); + if (game.isRestart(strInputRestart)) { + start(); + } + } + + private void validInputNumbers(String String) { + checkLength(String); + checkInt(String); + checkDuplicate(String); + } + + private void checkLength(String String) { //입력값이 길이가 3인지 확인 + if (String.length() != 3) { + throw new IllegalArgumentException(); + } + } + + private void checkInt(String String) { //입력 값이 정수형인지 확인 + try { + int inputInt = Integer.parseInt(String); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + } + + private void checkDuplicate(String String) { //입력값이 같은지 판단 ex)333 221 112 + Set charSet = new HashSet<>(); + for (int i = 0; i < String.length(); i++) { + char currentChar = String.charAt(i); + if (charSet.contains(currentChar)) { + throw new IllegalArgumentException(); + } + charSet.add(currentChar); + } + } + + private List stringToIntList(String String) { //입력값을 List로 변경 + List intList = new ArrayList<>(); + for (int i = 0; i < String.length(); i++) { + char digitChar = String.charAt(i); + int digitInt = Character.getNumericValue(digitChar); + intList.add(digitInt); + } + return intList; + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Model/Game.java b/src/main/java/baseball/Model/Game.java new file mode 100644 index 0000000000..60f4dce09a --- /dev/null +++ b/src/main/java/baseball/Model/Game.java @@ -0,0 +1,56 @@ +package baseball.Model; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class Game { + private final int NUMBER_LENGTH = 3; + private final List answerNumbers = new ArrayList<>(); + private final boolean PLAYING = true; + private final boolean END = false; + private boolean gameStatus; + private final String RESTART = "1"; + private final String QUIT = "2"; + + public Game() { + this.setAnswerNumbers(); + this.gameStatus = PLAYING; + } + + private void setAnswerNumbers() { + while (this.answerNumbers.size() < this.NUMBER_LENGTH) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!this.answerNumbers.contains(randomNumber)) { + this.answerNumbers.add(randomNumber); + } + } + } + + public String makeGameResult(List inputNumbers) { + GameResult gameResult = new GameResult(this.answerNumbers, inputNumbers); + applyResult(gameResult.getStrikes()); + return gameResult.resultToString(); + } + + public void applyResult(int strikes) { + if (strikes == NUMBER_LENGTH) { + this.gameStatus = END; + } + } + + public boolean getGameStatus() { + return this.gameStatus; + } + + public boolean isRestart(String input) { + if (input.equals(RESTART)) { + return true; + } else if (input.equals(QUIT)) { + return false; + } else { + throw new IllegalArgumentException(); + } + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Model/GameResult.java b/src/main/java/baseball/Model/GameResult.java new file mode 100644 index 0000000000..57f601911b --- /dev/null +++ b/src/main/java/baseball/Model/GameResult.java @@ -0,0 +1,43 @@ +package baseball.Model; + +import java.util.List; + +public class GameResult { + private int balls = 0; + private int strikes = 0; + + GameResult(List answerNumbers, List inputNumbers) { + for (int i = 0; i < answerNumbers.size(); i++) { + int answerDigit = answerNumbers.get(i); + int inputDigit = inputNumbers.get(i); + + if (answerDigit == inputDigit) { + this.strikes++; + } else if (answerNumbers.contains(inputDigit)) { + this.balls++; + } + } + } + + public String resultToString() { + String result = ""; + if (this.balls != 0) { + result += this.balls + "볼"; + } + if (this.strikes != 0) { + if (this.balls != 0) { + result += " "; + } + result += this.strikes + "스트라이크"; + } + if (this.balls == 0 && this.strikes == 0) { + result = "낫싱"; + } + return result; + } + + public int getStrikes() { + return this.strikes; + } + +} \ No newline at end of file diff --git a/src/main/java/baseball/View/GameView.java b/src/main/java/baseball/View/GameView.java new file mode 100644 index 0000000000..c46193e0a8 --- /dev/null +++ b/src/main/java/baseball/View/GameView.java @@ -0,0 +1,34 @@ +package baseball.View; + +import camp.nextstep.edu.missionutils.Console; + +public class GameView { + private final static String GAME_START = "숫자 야구 게임을 시작합니다."; + private final static String INPUT_NUMBERS = "숫자를 입력해주세요 : "; + private final static String GAME_END = "3개의 숫자를 모두 맞히셨습니다! 게임 종료"; + private final static String INPUT_RESTART = "게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."; + + public void showGameStart() { + System.out.println(GAME_START); + } + + public void showInputNumbers() { + System.out.print(INPUT_NUMBERS); + } + + public void showGameEnd() { + System.out.println(GAME_END); + } + + public void showInputRestart() { + System.out.println(INPUT_RESTART); + } + + public void showString(String string) { + System.out.println(string); + } + + public String getInput() { + return Console.readLine(); + } +} \ No newline at end of file diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 3fa29fa67b..f9c4121d02 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -7,6 +7,7 @@ import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; class ApplicationTest extends NsTest { @Test @@ -32,4 +33,4 @@ class ApplicationTest extends NsTest { public void runMain() { Application.main(new String[]{}); } -} +} \ No newline at end of file From 548dca310da25e4111b1dd9a85364864739414fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Thu, 9 Nov 2023 12:04:23 +0900 Subject: [PATCH 03/11] baseball (mvc) --- 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 03628f07ed..2d91d6a41c 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -89,13 +89,13 @@ private static int[] CheckComputerNumber(int[] array) { //depth 3, 컴퓨터가 } - private static String getUserNumber() {// depth 0, 유저에게 값을 입력받는 메소드 + private static String getUserNumber() {//유저에게 값을 입력 System.out.print("숫자를 입력해주세요 : "); String UserNum = Console.readLine(); return UserNum; } - private static void CheckUserNumber(int[] array) { //depth 2, 유저 가 입력한 값에 오류가 있는지 확인하는 메소드 + private static void CheckUserNumber(int[] array) { //유저가 입력한 값에 오류가 있는지 확인 //서로 다른 3자리 정수 Set verify = new HashSet<>(); for (int num : array) { @@ -106,7 +106,7 @@ private static void CheckUserNumber(int[] array) { //depth 2, 유저 가 입력 } } - private static int[] StringToIntArray(String input) {//depth 1, 문자열로 입력받은 값을 정수 배열로 변환 + private static int[] StringToIntArray(String input) {//문자열로 입력받은 값을 정수 배열로 변환 int[] result = new int[input.length()]; for (int i = 0; i < input.length(); i++) { @@ -116,7 +116,7 @@ private static int[] StringToIntArray(String input) {//depth 1, 문자열로 입 return result; } - private static int[] checkNum(int[] computerNumber, int[] userNum) {//depth 2 , 볼 과 스트라이크 판별 메소드 + private static int[] checkNum(int[] computerNumber, int[] userNum) {//볼과 스트라이크 판별 int strikes = 0; int balls = 0; @@ -135,7 +135,7 @@ private static int[] checkNum(int[] computerNumber, int[] userNum) {//depth 2 , return new int[]{balls, strikes}; } - private static void printResult(int[] result) { //depth 1 + private static void printResult(int[] result) { if (result[0] == 0 && result[1] == 0) {// 볼도 스트라이크도 아닌경우 System.out.println("낫싱"); return; From 261df119cd4f4bdab46b6c11c9c32f37b8ea211e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Sun, 12 Nov 2023 15:23:57 +0900 Subject: [PATCH 04/11] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=85?= =?UTF-8?q?=EC=84=B8=20=EA=B5=AC=EC=B2=B4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29bb2..a58fe65207 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,63 @@ +# ⚾ BaseBall Game +## 요구 사항 분석 +### 게임 규칙 +>같은 수가 같은 자리에 있으면 Strike +>
+>같은 수가 다른 자리에 있으면 Ball +>
+>같은 수가 하나도 없으면 Nothing +>
+>Strike, Ball, Nothing의 힌트를 가지고 Computer의 수를 맞추면 승리 +>
+> 예시)Compuer의 수 : 829일때 +>
+> 123을 제시 : 1스트라이크 +>
+> 456을 제시 : 낫싱 +>
+> 189을 제시 : 1볼 1스트라이크 + +
+ +### 게임 진행 과정 +> 게임 플레이어는 서로 다른 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 결과물 출력 + +
+ +### 게임 종료 조건 +> 컴퓨터의 숫자 3개를 모두 맞추면 게임 종료 + +
+ +### 게임 종료 후 처리 방법 +> 게임을 다시 시작하거나 종료 + +
+ +### 예외 조건 +1. 입력값이 길이가 3이 초과가 되는지 +2. 입력값이 정수형이 아닌지 +3. 입력값이 서로 다른 수가 아니라 중복되는 수인지 +- 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException()을 발생시킨 후 애플리케이션 종료 + + + +### 기능 +1. PlayGame +- UserNumber 배열에 값을 입력 받는다. +- ComputerNumber값과 UserNumber 값을 비교 +- 답을 맞추면 종료 + +
+ +2. PlayAgain +- 게임을 다시할건지 안할건지 판단 + +
+ +3. getComputerNumber +- 컴퓨터의 3개의 랜덤 숫자 생성 메소드 + +
+ +4. CheckComputerNumber \ No newline at end of file From b161f01c8f57dec691f75bcea73be105e6cf2582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Sun, 12 Nov 2023 15:25:26 +0900 Subject: [PATCH 05/11] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EB=AA=85=EC=84=B8=20=EA=B5=AC=EC=B2=B4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index a58fe65207..9a255fbfad 100644 --- a/docs/README.md +++ b/docs/README.md @@ -40,9 +40,10 @@ 3. 입력값이 서로 다른 수가 아니라 중복되는 수인지 - 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException()을 발생시킨 후 애플리케이션 종료 +
+## 구현 목록 -### 기능 1. PlayGame - UserNumber 배열에 값을 입력 받는다. - ComputerNumber값과 UserNumber 값을 비교 From f3bea6a0d9d7618a74f1de71f913c666f02e7e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Mon, 13 Nov 2023 21:39:45 +0900 Subject: [PATCH 06/11] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EB=AA=85=EC=84=B8=20=EA=B5=AC=EC=B2=B4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 9a255fbfad..aa8bc019a2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -61,4 +61,8 @@
-4. CheckComputerNumber \ No newline at end of file +4. CheckComputerNumber +- 컴퓨터가 생성한 수 에 대한 중복 검사 메소드 + +5. CheckUserNumber +- 유저가 입력한 값에 오류가 있는지 확인 \ No newline at end of file From 3a7ace9407afd338fff64d238a7b8be339622e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Tue, 14 Nov 2023 03:01:53 +0900 Subject: [PATCH 07/11] =?UTF-8?q?refacotr=20:=20Application=20=EA=B0=84?= =?UTF-8?q?=EB=8B=A8=ED=95=98=EA=B2=8C=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8E=99=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/Application.java | 148 +----------------------- 1 file changed, 3 insertions(+), 145 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 2d91d6a41c..8fb21daf19 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,5 +1,6 @@ package baseball; +import baseball.Controller.GameController; import baseball.View.GameView; import camp.nextstep.edu.missionutils.Console; import camp.nextstep.edu.missionutils.Randoms; @@ -10,150 +11,7 @@ public class Application { public static void main(String[] args) { - boolean playAgain = true; - GameView gameview = new GameView(); - - while (playAgain) { - gameview.showGameStart(); - int[] ComputerNumber = CheckComputerNumber(getComputerNumber()); - playGame(ComputerNumber); - playAgain = playAgain(); - - } - } - - private static void playGame(int[] ComputerNumber) { //depth 2 , 게임 실행 메소드 - - boolean win = false; - - while (!win) { - int[] UserNumber = StringToIntArray(getUserNumber()); - CheckUserNumber(UserNumber); - int[] result = checkNum(ComputerNumber, UserNumber); - printResult(result); - System.out.println(); - - if (result[1] == 3) { - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - win = true; - } - } - } - - private static boolean playAgain() { //depth 0 , 게임 제시작 메소드 - - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - - String num = Console.readLine(); - - return !num.equals("2"); - } - - private static int[] getComputerNumber() { //depth 1 , 컴퓨터 수 생성 메소드 - int[] computerNumber = new int[3]; - - computerNumber[0] = Randoms.pickNumberInRange(1, 9); - - for (int i = 1; i < 3; i++) { - computerNumber[i] = Randoms.pickNumberInRange(1, 9); - } - - return computerNumber; - } - - private static int[] CheckComputerNumber(int[] array) { //depth 3, 컴퓨터가 생성한 수 에 대한 중복 검사 메소드 - int[] newArray = new int[3]; - Set set = new HashSet<>(); - int count = 0; - - for (int num : array) { - if (set.contains(num)) { - int newNumber; - - do { - newNumber = Randoms.pickNumberInRange(1, 9); - } while (set.contains(newNumber)); // 중복된 값을 피하기 위해 반복 - - newArray[count] = newNumber; - } - - if (!set.contains(num)) { - newArray[count] = array[count]; - } - - set.add(num); - count++; - } - - return newArray; - } - - - private static String getUserNumber() {//유저에게 값을 입력 - System.out.print("숫자를 입력해주세요 : "); - String UserNum = Console.readLine(); - return UserNum; - } - - private static void CheckUserNumber(int[] array) { //유저가 입력한 값에 오류가 있는지 확인 - //서로 다른 3자리 정수 - Set verify = new HashSet<>(); - for (int num : array) { - if (verify.contains(num) || array.length != 3) { - throw new IllegalArgumentException("유효하지 않은 사용자 입력입니다."); - } - verify.add(num); - } - } - - private static int[] StringToIntArray(String input) {//문자열로 입력받은 값을 정수 배열로 변환 - int[] result = new int[input.length()]; - - for (int i = 0; i < input.length(); i++) { - char digit = input.charAt(i); - result[i] = Character.getNumericValue(digit); - } - return result; - } - - private static int[] checkNum(int[] computerNumber, int[] userNum) {//볼과 스트라이크 판별 - int strikes = 0; - int balls = 0; - - for (int i = 0; i < 3; i++) { - int num = userNum[i]; - - if (userNum[i] == computerNumber[i]) { - strikes++; - } - - if (Arrays.stream(computerNumber).anyMatch(x -> x == num) && userNum[i] != computerNumber[i]) { - balls++; - } - } - - return new int[]{balls, strikes}; - } - - private static void printResult(int[] result) { - if (result[0] == 0 && result[1] == 0) {// 볼도 스트라이크도 아닌경우 - System.out.println("낫싱"); - return; - } - - if (result[1] > 0 && result[0] > 0) { // 볼, 스트라이크 둘다 있는 경우 - System.out.println(result[0] + "볼 " + result[1] + "스트라이크"); - return; - } - - if (result[1] > 0 && result[0] == 0) { // 스트라이크 만 있는 경우 - System.out.println(result[1] + "스트라이크"); - return; - } - - if (result[1] == 0 && result[0] > 0) { // 볼 만 있는 경우 - System.out.println(result[0] + "볼 "); - } - + GameController gameController = new GameController(); + gameController.GameStart(); } } \ No newline at end of file From 3c22766d7cdccc9f7181c10d586c4f4a3feb2060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Tue, 14 Nov 2023 03:02:16 +0900 Subject: [PATCH 08/11] =?UTF-8?q?feat=20:=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20?= =?UTF-8?q?=EA=B2=80=EC=82=AC=20=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 --- .../baseball/Service/CheckUserNumber.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/baseball/Service/CheckUserNumber.java diff --git a/src/main/java/baseball/Service/CheckUserNumber.java b/src/main/java/baseball/Service/CheckUserNumber.java new file mode 100644 index 0000000000..bce08bc45a --- /dev/null +++ b/src/main/java/baseball/Service/CheckUserNumber.java @@ -0,0 +1,38 @@ +package baseball.Service; + +import java.util.HashSet; +import java.util.Set; + +public class CheckUserNumber { + + public CheckUserNumber(String String){ + CheckLength(String); + CheckInt(String); + CheckDuplicate(String); + } + + private void CheckLength(String String){ + if(String.length() != 3) + throw new IllegalArgumentException(); + + } + + private void CheckInt(String String){ + try { + int inputInt = Integer.parseInt(String); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(); + } + } + + private void CheckDuplicate(String String){ + Set charSet = new HashSet<>(); + for (int i = 0; i < String.length(); i++) { + char currentChar = String.charAt(i); + if (charSet.contains(currentChar)) { + throw new IllegalArgumentException(); + } + charSet.add(currentChar); + } + } +} From f80c16b22abbfcaf462c47cd95bb9681f7bbcee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Tue, 14 Nov 2023 03:02:29 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor=20:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8E=99=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/Model/Game.java | 41 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/baseball/Model/Game.java b/src/main/java/baseball/Model/Game.java index 60f4dce09a..001febff6d 100644 --- a/src/main/java/baseball/Model/Game.java +++ b/src/main/java/baseball/Model/Game.java @@ -6,21 +6,20 @@ import java.util.List; public class Game { - private final int NUMBER_LENGTH = 3; + private final List answerNumbers = new ArrayList<>(); - private final boolean PLAYING = true; - private final boolean END = false; - private boolean gameStatus; + private boolean GameStatus; + private final boolean Playing = true; + private final boolean End = false; private final String RESTART = "1"; private final String QUIT = "2"; - - public Game() { + public Game(){ this.setAnswerNumbers(); - this.gameStatus = PLAYING; + this.GameStatus = Playing; } - private void setAnswerNumbers() { - while (this.answerNumbers.size() < this.NUMBER_LENGTH) { + public void setAnswerNumbers(){ + while (this.answerNumbers.size() < 3) { int randomNumber = Randoms.pickNumberInRange(1, 9); if (!this.answerNumbers.contains(randomNumber)) { this.answerNumbers.add(randomNumber); @@ -34,16 +33,17 @@ public String makeGameResult(List inputNumbers) { return gameResult.resultToString(); } - public void applyResult(int strikes) { - if (strikes == NUMBER_LENGTH) { - this.gameStatus = END; + public void applyResult(int strike) { + if (strike == 3) { + this.GameStatus = End; } } - public boolean getGameStatus() { - return this.gameStatus; + public boolean gamestatus() { + return this.GameStatus; } + public boolean isRestart(String input) { if (input.equals(RESTART)) { return true; @@ -53,4 +53,15 @@ public boolean isRestart(String input) { throw new IllegalArgumentException(); } } -} \ No newline at end of file + + + public List stringToIntList(String String) { //입력값을 List로 변경 + List intList = new ArrayList<>(); + for (int i = 0; i < String.length(); i++) { + char digitChar = String.charAt(i); + int digitInt = Character.getNumericValue(digitChar); + intList.add(digitInt); + } + return intList; + } +} From 99293fb19d12239a3dd17f12448e3ea2ca64b21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B5=AC=EB=8F=99=EC=97=BD?= Date: Tue, 14 Nov 2023 03:02:40 +0900 Subject: [PATCH 10/11] =?UTF-8?q?refactor=20:=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=A6=AC=ED=8E=99=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baseball/Controller/GameController.java | 68 ++++--------------- src/main/java/baseball/Model/GameResult.java | 39 +++++------ src/main/java/baseball/View/GameView.java | 2 +- 3 files changed, 35 insertions(+), 74 deletions(-) diff --git a/src/main/java/baseball/Controller/GameController.java b/src/main/java/baseball/Controller/GameController.java index d8560eefa8..7175469b29 100644 --- a/src/main/java/baseball/Controller/GameController.java +++ b/src/main/java/baseball/Controller/GameController.java @@ -1,78 +1,38 @@ package baseball.Controller; +import baseball.Service.CheckUserNumber; import baseball.Model.Game; import baseball.View.GameView; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class GameController { + private final GameView gameView = new GameView(); - GameController() { + public GameController(){ gameView.showGameStart(); } - public void start() { + public void GameStart(){ Game game = new Game(); - while (game.getGameStatus()) { - gameView.showInputNumbers(); - String strInputNumbers = gameView.getInput(); - validInputNumbers(strInputNumbers); - List inputNumbers = stringToIntList(strInputNumbers); - - String gameResult = game.makeGameResult(inputNumbers); - gameView.showString(gameResult); + while(game.gamestatus()){ + gameView.showInputNumbers(); //숫자를 입력해 주세요: + String strInputNumbers = gameView.getInput(); //입력받아 string형에 저장 + CheckUserNumber check = new CheckUserNumber(strInputNumbers); //오류 검사 + List UserNumbers = game.stringToIntList(strInputNumbers); //console로 입력 받으면 string형 이기 때문에 비교과 쉽게 List로 변환 + + String Gameresult = game.makeGameResult(UserNumbers); + gameView.showString(Gameresult); } gameView.showGameEnd(); gameView.showInputRestart(); String strInputRestart = gameView.getInput(); if (game.isRestart(strInputRestart)) { - start(); - } - } - - private void validInputNumbers(String String) { - checkLength(String); - checkInt(String); - checkDuplicate(String); - } - - private void checkLength(String String) { //입력값이 길이가 3인지 확인 - if (String.length() != 3) { - throw new IllegalArgumentException(); + GameStart(); } } - private void checkInt(String String) { //입력 값이 정수형인지 확인 - try { - int inputInt = Integer.parseInt(String); - } catch (NumberFormatException e) { - throw new IllegalArgumentException(); - } - } - private void checkDuplicate(String String) { //입력값이 같은지 판단 ex)333 221 112 - Set charSet = new HashSet<>(); - for (int i = 0; i < String.length(); i++) { - char currentChar = String.charAt(i); - if (charSet.contains(currentChar)) { - throw new IllegalArgumentException(); - } - charSet.add(currentChar); - } - } - - private List stringToIntList(String String) { //입력값을 List로 변경 - List intList = new ArrayList<>(); - for (int i = 0; i < String.length(); i++) { - char digitChar = String.charAt(i); - int digitInt = Character.getNumericValue(digitChar); - intList.add(digitInt); - } - return intList; - } -} \ No newline at end of file +} diff --git a/src/main/java/baseball/Model/GameResult.java b/src/main/java/baseball/Model/GameResult.java index 57f601911b..372ff48a82 100644 --- a/src/main/java/baseball/Model/GameResult.java +++ b/src/main/java/baseball/Model/GameResult.java @@ -3,41 +3,42 @@ import java.util.List; public class GameResult { - private int balls = 0; - private int strikes = 0; - GameResult(List answerNumbers, List inputNumbers) { - for (int i = 0; i < answerNumbers.size(); i++) { - int answerDigit = answerNumbers.get(i); - int inputDigit = inputNumbers.get(i); + private int ball = 0; + private int strike = 0; - if (answerDigit == inputDigit) { - this.strikes++; - } else if (answerNumbers.contains(inputDigit)) { - this.balls++; + GameResult(List answerNumbers, List UserNumbers ){ + for(int i=0; i Date: Tue, 14 Nov 2023 03:03:11 +0900 Subject: [PATCH 11/11] =?UTF-8?q?docs=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD,=20=EA=B5=AC=ED=98=84=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EA=B5=AC=EC=B2=B4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index aa8bc019a2..1ffc0e4046 100644 --- a/docs/README.md +++ b/docs/README.md @@ -32,37 +32,54 @@ ### 게임 종료 후 처리 방법 > 게임을 다시 시작하거나 종료 + +
### 예외 조건 1. 입력값이 길이가 3이 초과가 되는지 2. 입력값이 정수형이 아닌지 3. 입력값이 서로 다른 수가 아니라 중복되는 수인지 -- 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException()을 발생시킨 후 애플리케이션 종료 +4. 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException()을 발생시킨 후 애플리케이션 종료
## 구현 목록 -1. PlayGame -- UserNumber 배열에 값을 입력 받는다. -- ComputerNumber값과 UserNumber 값을 비교 -- 답을 맞추면 종료 +### Controller +- GameController + - 시작 기능 -
-2. PlayAgain -- 게임을 다시할건지 안할건지 판단 -
+### Model +- Game + - 컴퓨터 정답 생성 + - 결과값 생성 + - 현재 상태 (이겼는지 / 재시작 할건지 / 종료할건지) -3. getComputerNumber -- 컴퓨터의 3개의 랜덤 숫자 생성 메소드 +- GameResult + - 컴퓨터 vs 유저 값 판단하여 스트라이크, 볼 개수 판단 + - 결과를 string형으로 변환 + +### View +- GameView + - 출력 인터페이스 + +### Service +- CheckUserNumber + - 예외 처리 기능 + - 입력 길이 3 이상인지 + - 입력 값이 정수인지 + - 입력 값이 중복되는지
-4. CheckComputerNumber -- 컴퓨터가 생성한 수 에 대한 중복 검사 메소드 +## 커밋 방법 [깃 배쉬] +1. git clone --bare [forked한 레포지 주소] +2. cd [forked한 레포지 주소] +3. git push --mirror [새 레포지 주소] +4. cd .. +5. rm -rf [forked한 레포지 주소] -5. CheckUserNumber -- 유저가 입력한 값에 오류가 있는지 확인 \ No newline at end of file +