From d3d2b6572f26d4d6d08d2605caf87eccef83cc0b Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 20 Jan 2024 07:25:25 +0900 Subject: [PATCH 1/8] =?UTF-8?q?feat=20:=20=EA=B5=AC=ED=98=84=ED=95=A0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=20=EB=AA=A9=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 8cf0c69..9a98f1e 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,19 @@
--- +## 구현할 기능 목록 +- 컴퓨터가 임의의 수 3개 선택하도록 구현 +- 게임 진행 + - 스트라이크일 경우 + - 볼일 경우 + - 낫싱일 경우 +- 3개의 숫자를 모두 맞힐시에 대한 결과 출력 +- 게임이 끝났을 경우에 대한 처리 +- 입력에 대한 에러처리 +
+ +--- ## 📝 License This project is [MIT](https://github.com/woowacourse/java-baseball-precourse/blob/master/LICENSE) licensed. From 296688c993e92eb284ea7e485e9fa5ea7735b76e Mon Sep 17 00:00:00 2001 From: Ojimin Date: Wed, 24 Jan 2024 03:30:00 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat=20:=20=EC=BB=B4=ED=93=A8=ED=84=B0=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=20=EC=88=AB=EC=9E=90=20=EC=83=9D=EC=84=B1=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 --- README.md | 3 +++ src/main/java/baseball/Application.java | 8 ++++++++ .../java/baseball/controller/BaseballGame.java | 11 +++++++++++ src/main/java/baseball/model/RandomNum.java | 15 +++++++++++++++ src/main/java/baseball/view/InputView.java | 6 ++++++ src/main/java/baseball/view/OutputView.java | 3 +++ 6 files changed, 46 insertions(+) create mode 100644 src/main/java/baseball/controller/BaseballGame.java create mode 100644 src/main/java/baseball/model/RandomNum.java diff --git a/README.md b/README.md index 9a98f1e..67b477f 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,9 @@ - 3개의 숫자를 모두 맞힐시에 대한 결과 출력 - 게임이 끝났을 경우에 대한 처리 - 입력에 대한 에러처리 + - 3자리의 수가 아닌경우 + - 게임이 끝난 경우 재시작/종료를 구분하는 1과 2가 아닌 다른 + 숫자를 입력한 경우
diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b..870b1ab 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,15 @@ package baseball; +import baseball.controller.BaseballGame; +import baseball.view.InputView; +import baseball.view.OutputView; + public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 + OutputView.outputNumber(); + InputView.inputNumber(); + BaseballGame baseballGame = new BaseballGame(); + baseballGame.start(); } } diff --git a/src/main/java/baseball/controller/BaseballGame.java b/src/main/java/baseball/controller/BaseballGame.java new file mode 100644 index 0000000..38c84d9 --- /dev/null +++ b/src/main/java/baseball/controller/BaseballGame.java @@ -0,0 +1,11 @@ +package baseball.controller; + +import baseball.model.RandomNum; + +public class BaseballGame { + + public void start(){ + RandomNum randomNum = new RandomNum(); + System.out.println(randomNum.getRandomNum()); + } +} diff --git a/src/main/java/baseball/model/RandomNum.java b/src/main/java/baseball/model/RandomNum.java new file mode 100644 index 0000000..63629af --- /dev/null +++ b/src/main/java/baseball/model/RandomNum.java @@ -0,0 +1,15 @@ +package baseball.model; + +import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange; + +public class RandomNum { + private int randomNum; + + public RandomNum() { + this.randomNum = pickNumberInRange(100, 999); + } + + public int getRandomNum(){ + return this.randomNum; + } +} diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index 6c04755..a4ea403 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -1,5 +1,11 @@ package baseball.view; +import camp.nextstep.edu.missionutils.Console; + public class InputView { + public static void inputNumber(){ + String inputNumber = Console.readLine(); + } + } diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index dceccc5..803a82b 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -2,4 +2,7 @@ public class OutputView { + public static void outputNumber() { + System.out.print("숫자를 입력해주세요 : "); + } } From 6de10bf9eed19502ee6222d0e4342bda3b399ba5 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Wed, 24 Jan 2024 10:21:40 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat=20:=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=EC=97=90=20=EB=8C=80=ED=95=9C=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/main/java/baseball/Application.java | 13 +++++---- .../baseball/controller/BaseballGame.java | 7 +++-- .../java/baseball/model/ComputerNumber.java | 29 +++++++++++++++++++ src/main/java/baseball/model/RandomNum.java | 15 ---------- src/main/java/baseball/model/UserNumber.java | 29 +++++++++++++++++++ src/main/java/baseball/view/InputView.java | 6 ++-- src/main/java/baseball/view/OutputView.java | 2 +- 8 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 src/main/java/baseball/model/ComputerNumber.java delete mode 100644 src/main/java/baseball/model/RandomNum.java create mode 100644 src/main/java/baseball/model/UserNumber.java diff --git a/README.md b/README.md index 67b477f..a868909 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,8 @@ - 게임이 끝났을 경우에 대한 처리 - 입력에 대한 에러처리 - 3자리의 수가 아닌경우 + - 숫자가 아닌 입력을 받은 경우 + - 입력받은 수가 서로 다른 자릿수가 아닐 경우 - 게임이 끝난 경우 재시작/종료를 구분하는 1과 2가 아닌 다른 숫자를 입력한 경우 diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 870b1ab..c7ce2c7 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,15 +1,16 @@ package baseball; -import baseball.controller.BaseballGame; +import baseball.model.ComputerNumber; +import baseball.model.UserNumber; import baseball.view.InputView; -import baseball.view.OutputView; public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 - OutputView.outputNumber(); - InputView.inputNumber(); - BaseballGame baseballGame = new BaseballGame(); - baseballGame.start(); +// BaseballGame baseballGame = new BaseballGame(); +// baseballGame.start(); + UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); + ComputerNumber computerNumber = new ComputerNumber(); + System.out.println(computerNumber.getRandomNum()); } } diff --git a/src/main/java/baseball/controller/BaseballGame.java b/src/main/java/baseball/controller/BaseballGame.java index 38c84d9..5a5e915 100644 --- a/src/main/java/baseball/controller/BaseballGame.java +++ b/src/main/java/baseball/controller/BaseballGame.java @@ -1,11 +1,12 @@ package baseball.controller; -import baseball.model.RandomNum; +import baseball.model.ComputerNumber; +import baseball.view.InputView; public class BaseballGame { public void start(){ - RandomNum randomNum = new RandomNum(); - System.out.println(randomNum.getRandomNum()); + InputView.inputUserNumber(); + ComputerNumber computerNumber = new ComputerNumber(); } } diff --git a/src/main/java/baseball/model/ComputerNumber.java b/src/main/java/baseball/model/ComputerNumber.java new file mode 100644 index 0000000..65b19db --- /dev/null +++ b/src/main/java/baseball/model/ComputerNumber.java @@ -0,0 +1,29 @@ +package baseball.model; + +import java.util.ArrayList; +import java.util.List; + +import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange; + +public class ComputerNumber { + private List randomNum; + + public ComputerNumber() { + this.randomNum = makeRandomNum(); + } + + public List makeRandomNum() { + List randomNum = new ArrayList<>(); + while (randomNum.size() < 3) { + int random = pickNumberInRange(1, 9); + if (!randomNum.contains(random)) { + randomNum.add(random); + } + } + return randomNum; + } + + public List getRandomNum() { + return this.randomNum; + } +} diff --git a/src/main/java/baseball/model/RandomNum.java b/src/main/java/baseball/model/RandomNum.java deleted file mode 100644 index 63629af..0000000 --- a/src/main/java/baseball/model/RandomNum.java +++ /dev/null @@ -1,15 +0,0 @@ -package baseball.model; - -import static camp.nextstep.edu.missionutils.Randoms.pickNumberInRange; - -public class RandomNum { - private int randomNum; - - public RandomNum() { - this.randomNum = pickNumberInRange(100, 999); - } - - public int getRandomNum(){ - return this.randomNum; - } -} diff --git a/src/main/java/baseball/model/UserNumber.java b/src/main/java/baseball/model/UserNumber.java new file mode 100644 index 0000000..7e5073f --- /dev/null +++ b/src/main/java/baseball/model/UserNumber.java @@ -0,0 +1,29 @@ +package baseball.model; + +public class UserNumber { + private String userNum; + + public UserNumber(String userNum){ + isNumber(userNum); + is3DigitNumber(userNum); + this.userNum = userNum; + } + + public String getUserNum(){ + return this.userNum; + } + + public void isNumber(String userNumber) { + try { + int toNumber = Integer.parseInt(userNumber); + } catch (NumberFormatException e) { + System.out.println("숫자로 입력해주세요"); + } + } + + public void is3DigitNumber(String userNumber) { + if(userNumber.length() != 3) { + throw new IllegalArgumentException("3자리 숫자로 입력해주세요"); + } + } +} diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index a4ea403..a2d2e18 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -4,8 +4,8 @@ public class InputView { - public static void inputNumber(){ - String inputNumber = Console.readLine(); + public static String inputUserNumber() { + System.out.print("숫자를 입력해주세요 : "); + return Console.readLine(); } - } diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index 803a82b..cc8b938 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -3,6 +3,6 @@ public class OutputView { public static void outputNumber() { - System.out.print("숫자를 입력해주세요 : "); + } } From 2d5a6aab7e1be8c84d3abd1be7fcca4a94881615 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Fri, 26 Jan 2024 10:05:04 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=20=ED=9B=84=20=EC=B2=98=EB=A6=AC=EB=B6=80=EB=B6=84=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 --- src/main/java/baseball/Application.java | 37 +++++++++++++++++++-- src/main/java/baseball/view/InputView.java | 4 +++ src/main/java/baseball/view/OutputView.java | 6 +++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index c7ce2c7..b7b87be 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,16 +1,49 @@ package baseball; +import baseball.controller.BaseballGame; import baseball.model.ComputerNumber; import baseball.model.UserNumber; import baseball.view.InputView; +import baseball.view.OutputView; + +import java.awt.*; public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 // BaseballGame baseballGame = new BaseballGame(); // baseballGame.start(); - UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); ComputerNumber computerNumber = new ComputerNumber(); - System.out.println(computerNumber.getRandomNum()); +// while (true) { +// //스트라이크, 볼 처리 +// UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); +// +// //만약 다 맞추면 반복문 종료 +// +// +// } + //1 or 2 입력 통해서 다시 게임 진행할지 종료할지 선택 + OutputView.outputSuccessMessage(); + OutputView.outputRestartMessage(); + String inputUserRestartNumber = InputView.inputUserRestartNumber(); + if(!inputUserRestartNumber.equals("1") && !inputUserRestartNumber.equals("2")) { + throw new IllegalArgumentException("잘못된 입력입니다"); + } + if(inputUserRestartNumber.equals("1")){ + InputView.inputUserNumber(); + ComputerNumber newComputerNumber = new ComputerNumber(); + while (true) { + //스트라이크, 볼 처리 + UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); + + //만약 다 맞추면 반복문 종료 + + + } + + } + if(inputUserRestartNumber.equals("2")){ + return ; + } } } diff --git a/src/main/java/baseball/view/InputView.java b/src/main/java/baseball/view/InputView.java index a2d2e18..08fc9e7 100644 --- a/src/main/java/baseball/view/InputView.java +++ b/src/main/java/baseball/view/InputView.java @@ -8,4 +8,8 @@ public static String inputUserNumber() { System.out.print("숫자를 입력해주세요 : "); return Console.readLine(); } + + public static String inputUserRestartNumber() { + return Console.readLine(); + } } diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index cc8b938..cbc78ac 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -2,7 +2,11 @@ public class OutputView { - public static void outputNumber() { + public static void outputSuccessMessage() { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + public static void outputRestartMessage() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); } } From febd3847ddc854aed09b7e12c6a9676a737be385 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 27 Jan 2024 20:48:51 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=A7=84?= =?UTF-8?q?=ED=96=89=20=EB=B0=8F=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=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 | 36 ++++++++++------ src/main/java/baseball/model/Result.java | 39 +++++++++++++++++ src/main/java/baseball/model/UserNumber.java | 37 ++++++++++++++-- src/main/java/baseball/view/OutputView.java | 45 +++++++++++++++++++- 4 files changed, 141 insertions(+), 16 deletions(-) create mode 100644 src/main/java/baseball/model/Result.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index b7b87be..9393398 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,11 +2,13 @@ import baseball.controller.BaseballGame; import baseball.model.ComputerNumber; +import baseball.model.Result; import baseball.model.UserNumber; import baseball.view.InputView; import baseball.view.OutputView; import java.awt.*; +import java.util.List; public class Application { public static void main(String[] args) { @@ -14,22 +16,32 @@ public static void main(String[] args) { // BaseballGame baseballGame = new BaseballGame(); // baseballGame.start(); ComputerNumber computerNumber = new ComputerNumber(); -// while (true) { -// //스트라이크, 볼 처리 -// UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); -// -// //만약 다 맞추면 반복문 종료 -// -// -// } + List computerRandomNumber = computerNumber.getRandomNum(); + while (true) { + //스트라이크, 볼, 낫싱 처리 + UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); + String userNumber = inputUserNumber.getUserNum(); + int ball = 0; + int strike = 0; + ball = inputUserNumber.countBall(computerRandomNumber, ball); + strike = inputUserNumber.countStrike(computerRandomNumber, strike); + Result result = new Result(ball, strike); + result.updateNothing(); + //결과 출력 + OutputView.printResult(result); + //만약 다 맞추면 반복문 종료 + if (result.isSuccess()) { + break; + } + } //1 or 2 입력 통해서 다시 게임 진행할지 종료할지 선택 OutputView.outputSuccessMessage(); OutputView.outputRestartMessage(); String inputUserRestartNumber = InputView.inputUserRestartNumber(); - if(!inputUserRestartNumber.equals("1") && !inputUserRestartNumber.equals("2")) { + if (!inputUserRestartNumber.equals("1") && !inputUserRestartNumber.equals("2")) { throw new IllegalArgumentException("잘못된 입력입니다"); } - if(inputUserRestartNumber.equals("1")){ + if (inputUserRestartNumber.equals("1")) { InputView.inputUserNumber(); ComputerNumber newComputerNumber = new ComputerNumber(); while (true) { @@ -42,8 +54,8 @@ public static void main(String[] args) { } } - if(inputUserRestartNumber.equals("2")){ - return ; + if (inputUserRestartNumber.equals("2")) { + return; } } } diff --git a/src/main/java/baseball/model/Result.java b/src/main/java/baseball/model/Result.java new file mode 100644 index 0000000..b9d5e5b --- /dev/null +++ b/src/main/java/baseball/model/Result.java @@ -0,0 +1,39 @@ +package baseball.model; + +/** + * 온전히 결과에 대한 책임만 + */ +public class Result { + private int ball; + private int strike; + private boolean isNothing; + + public Result(int ball, int strike) { + this.ball = ball; + this.strike = strike; + this.isNothing = false; + + } + + public int getBall() { + return ball; + } + + public int getStrike() { + return strike; + } + + public boolean isNothing() { + return isNothing; + } + + public void updateNothing() { + if (this.ball == 0 && this.strike == 0) { + this.isNothing = true; + } + } + + public boolean isSuccess() { + return this.strike == 3; + } +} diff --git a/src/main/java/baseball/model/UserNumber.java b/src/main/java/baseball/model/UserNumber.java index 7e5073f..71108df 100644 --- a/src/main/java/baseball/model/UserNumber.java +++ b/src/main/java/baseball/model/UserNumber.java @@ -1,15 +1,20 @@ package baseball.model; +import java.util.List; + +/** + * 여기서 strike, ball 계산완료할것 + */ public class UserNumber { private String userNum; - public UserNumber(String userNum){ + public UserNumber(String userNum) { isNumber(userNum); is3DigitNumber(userNum); this.userNum = userNum; } - public String getUserNum(){ + public String getUserNum() { return this.userNum; } @@ -22,8 +27,34 @@ public void isNumber(String userNumber) { } public void is3DigitNumber(String userNumber) { - if(userNumber.length() != 3) { + if (userNumber.length() != 3) { throw new IllegalArgumentException("3자리 숫자로 입력해주세요"); } } + + /** + * 숫자는 같지만 자릿수가 다른경우 + */ + public int countBall(List computerNumber, int ball) { + for (int i = 0; i < computerNumber.size(); i++) { + for (int j = 0; j < 3; j++) { + if (i != j && computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(j))) { + ball += 1; + } + } + } + return ball; + + } + + public int countStrike(List computerNumber, int strike) { + for (int i = 0; i < computerNumber.size(); i++) { + if(computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(i))){ + strike += 1; + } + } + return strike; + } + + } diff --git a/src/main/java/baseball/view/OutputView.java b/src/main/java/baseball/view/OutputView.java index cbc78ac..168ecde 100644 --- a/src/main/java/baseball/view/OutputView.java +++ b/src/main/java/baseball/view/OutputView.java @@ -1,9 +1,52 @@ package baseball.view; +import baseball.model.Result; + public class OutputView { + public static void printResult(Result result) { + if (result.isSuccess()) { + outputSuccessMessage(); + } + if (!result.isSuccess()) { + printNotSuccessResult(result); + } + } + + public static void printNotSuccessResult(Result result) { + outputNothingMessage(result); + outputBallMessage(result); + outputStrikeMessage(result); + outputBallStrikeMessage(result); + } + + + public static void outputNothingMessage(Result result) { + if (result.isNothing()) { + System.out.println("낫싱"); + } + } + + public static void outputBallMessage(Result result) { + if (result.getBall() > 0 && result.getStrike() == 0) { + System.out.println(result.getBall() + "볼"); + } + } + + public static void outputStrikeMessage(Result result) { + if (result.getStrike() > 0 && result.getBall() == 0) { + System.out.println(result.getStrike() + "스트라이크"); + } + } + + public static void outputBallStrikeMessage(Result result) { + if (result.getBall() > 0 && result.getStrike() > 0) { + System.out.println(result.getBall() + "볼 " + result.getStrike() + "스트라이크"); + } + } + public static void outputSuccessMessage() { - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("3스트라이크\n3개의 숫자를 모두 맞히셨습니다! 게임 종료"); } public static void outputRestartMessage() { From 0924dbd29e974c4e6e46f14bec1faee387ea5d98 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 27 Jan 2024 22:18:03 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20mvc=20=ED=8C=A8=ED=84=B4?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=EC=A1=B0=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/Application.java | 46 +------------------ .../baseball/controller/BaseballGame.java | 44 +++++++++++++++++- src/main/java/baseball/model/Result.java | 17 +++++-- src/main/java/baseball/model/UserNumber.java | 11 ++--- .../baseball/validate/ValidateRestart.java | 10 ++++ 5 files changed, 70 insertions(+), 58 deletions(-) create mode 100644 src/main/java/baseball/validate/ValidateRestart.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 9393398..97dcc0c 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -13,49 +13,7 @@ public class Application { public static void main(String[] args) { //TODO: 숫자 야구 게임 구현 -// BaseballGame baseballGame = new BaseballGame(); -// baseballGame.start(); - ComputerNumber computerNumber = new ComputerNumber(); - List computerRandomNumber = computerNumber.getRandomNum(); - while (true) { - //스트라이크, 볼, 낫싱 처리 - UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); - String userNumber = inputUserNumber.getUserNum(); - int ball = 0; - int strike = 0; - ball = inputUserNumber.countBall(computerRandomNumber, ball); - strike = inputUserNumber.countStrike(computerRandomNumber, strike); - Result result = new Result(ball, strike); - result.updateNothing(); - //결과 출력 - OutputView.printResult(result); - //만약 다 맞추면 반복문 종료 - if (result.isSuccess()) { - break; - } - } - //1 or 2 입력 통해서 다시 게임 진행할지 종료할지 선택 - OutputView.outputSuccessMessage(); - OutputView.outputRestartMessage(); - String inputUserRestartNumber = InputView.inputUserRestartNumber(); - if (!inputUserRestartNumber.equals("1") && !inputUserRestartNumber.equals("2")) { - throw new IllegalArgumentException("잘못된 입력입니다"); - } - if (inputUserRestartNumber.equals("1")) { - InputView.inputUserNumber(); - ComputerNumber newComputerNumber = new ComputerNumber(); - while (true) { - //스트라이크, 볼 처리 - UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); - - //만약 다 맞추면 반복문 종료 - - - } - - } - if (inputUserRestartNumber.equals("2")) { - return; - } + BaseballGame baseballGame = new BaseballGame(); + baseballGame.start(); } } diff --git a/src/main/java/baseball/controller/BaseballGame.java b/src/main/java/baseball/controller/BaseballGame.java index 5a5e915..3875aae 100644 --- a/src/main/java/baseball/controller/BaseballGame.java +++ b/src/main/java/baseball/controller/BaseballGame.java @@ -1,12 +1,52 @@ package baseball.controller; import baseball.model.ComputerNumber; +import baseball.model.Result; +import baseball.model.UserNumber; +import baseball.validate.ValidateRestart; import baseball.view.InputView; +import baseball.view.OutputView; + +import java.util.List; public class BaseballGame { - public void start(){ - InputView.inputUserNumber(); + public void start() { + do { + List computerRandomNumber = makeComputerNumber(); + playGame(computerRandomNumber); + } while (isRestart()); + } + + public List makeComputerNumber() { ComputerNumber computerNumber = new ComputerNumber(); + return computerNumber.getRandomNum(); + } + + public void playGame(List computerRandomNumber) { + while (true) { + UserNumber inputUserNumber = new UserNumber(InputView.inputUserNumber()); + Result result = makeResult(inputUserNumber, computerRandomNumber); + OutputView.printResult(result); + if (result.isSuccess()) { + break; + } + } } + + public Result makeResult(UserNumber inputUserNumber, List computerRandomNumber) { + Result result = new Result(); + inputUserNumber.countBall(computerRandomNumber, result); + inputUserNumber.countStrike(computerRandomNumber, result); + result.updateIsNothing(); + return result; + } + + public boolean isRestart() { + OutputView.outputRestartMessage(); + String inputUserRestartNumber = InputView.inputUserRestartNumber(); + ValidateRestart.validateInputRestartNumber(inputUserRestartNumber); + return inputUserRestartNumber.equals("1"); + } + } diff --git a/src/main/java/baseball/model/Result.java b/src/main/java/baseball/model/Result.java index b9d5e5b..3c4a238 100644 --- a/src/main/java/baseball/model/Result.java +++ b/src/main/java/baseball/model/Result.java @@ -8,11 +8,10 @@ public class Result { private int strike; private boolean isNothing; - public Result(int ball, int strike) { - this.ball = ball; - this.strike = strike; + public Result() { + this.ball = 0; + this.strike = 0; this.isNothing = false; - } public int getBall() { @@ -27,7 +26,15 @@ public boolean isNothing() { return isNothing; } - public void updateNothing() { + public void updateBall() { + this.ball+=1; + } + + public void updateStrike() { + this.strike+=1; + } + + public void updateIsNothing() { if (this.ball == 0 && this.strike == 0) { this.isNothing = true; } diff --git a/src/main/java/baseball/model/UserNumber.java b/src/main/java/baseball/model/UserNumber.java index 71108df..1b6f5d8 100644 --- a/src/main/java/baseball/model/UserNumber.java +++ b/src/main/java/baseball/model/UserNumber.java @@ -35,25 +35,22 @@ public void is3DigitNumber(String userNumber) { /** * 숫자는 같지만 자릿수가 다른경우 */ - public int countBall(List computerNumber, int ball) { + public void countBall(List computerNumber, Result result) { for (int i = 0; i < computerNumber.size(); i++) { for (int j = 0; j < 3; j++) { if (i != j && computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(j))) { - ball += 1; + result.updateBall(); } } } - return ball; - } - public int countStrike(List computerNumber, int strike) { + public void countStrike(List computerNumber, Result result) { for (int i = 0; i < computerNumber.size(); i++) { if(computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(i))){ - strike += 1; + result.updateStrike(); } } - return strike; } diff --git a/src/main/java/baseball/validate/ValidateRestart.java b/src/main/java/baseball/validate/ValidateRestart.java new file mode 100644 index 0000000..6b1c810 --- /dev/null +++ b/src/main/java/baseball/validate/ValidateRestart.java @@ -0,0 +1,10 @@ +package baseball.validate; + +public class ValidateRestart { + + public static void validateInputRestartNumber(String inputRestartNumber){ + if (!inputRestartNumber.equals("1") && !inputRestartNumber.equals("2")) { + throw new IllegalArgumentException("잘못된 입력입니다"); + } + } +} From 1a1b813a21f81e49b7abba2009672c1c180777ab Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 27 Jan 2024 22:28:09 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20=EC=9D=B8=EB=8D=B4=ED=8A=B8=203?= =?UTF-8?q?=20=EC=9D=B4=EC=83=81=EC=9D=B8=20=EB=B6=80=EB=B6=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20validator=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=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/model/UserNumber.java | 35 +++++++------------ .../baseball/validate/ValidateUserNumber.java | 18 ++++++++++ 2 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/main/java/baseball/validate/ValidateUserNumber.java diff --git a/src/main/java/baseball/model/UserNumber.java b/src/main/java/baseball/model/UserNumber.java index 1b6f5d8..ef5b429 100644 --- a/src/main/java/baseball/model/UserNumber.java +++ b/src/main/java/baseball/model/UserNumber.java @@ -1,5 +1,7 @@ package baseball.model; +import baseball.validate.ValidateUserNumber; + import java.util.List; /** @@ -9,8 +11,8 @@ public class UserNumber { private String userNum; public UserNumber(String userNum) { - isNumber(userNum); - is3DigitNumber(userNum); + ValidateUserNumber.isNumber(userNum); + ValidateUserNumber.is3DigitNumber(userNum); this.userNum = userNum; } @@ -18,36 +20,23 @@ public String getUserNum() { return this.userNum; } - public void isNumber(String userNumber) { - try { - int toNumber = Integer.parseInt(userNumber); - } catch (NumberFormatException e) { - System.out.println("숫자로 입력해주세요"); - } - } - - public void is3DigitNumber(String userNumber) { - if (userNumber.length() != 3) { - throw new IllegalArgumentException("3자리 숫자로 입력해주세요"); + public void countBall(List computerNumber, Result result) { + for (int i = 0; i < computerNumber.size(); i++) { + checkBallByUserNumber(i, computerNumber, result); } } - /** - * 숫자는 같지만 자릿수가 다른경우 - */ - public void countBall(List computerNumber, Result result) { - for (int i = 0; i < computerNumber.size(); i++) { - for (int j = 0; j < 3; j++) { - if (i != j && computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(j))) { - result.updateBall(); - } + public void checkBallByUserNumber(int i, List computerNumber, Result result) { + for (int j = 0; j < this.getUserNum().length(); j++) { + if (i != j && computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(j))) { + result.updateBall(); } } } public void countStrike(List computerNumber, Result result) { for (int i = 0; i < computerNumber.size(); i++) { - if(computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(i))){ + if (computerNumber.get(i) == Character.getNumericValue(this.userNum.charAt(i))) { result.updateStrike(); } } diff --git a/src/main/java/baseball/validate/ValidateUserNumber.java b/src/main/java/baseball/validate/ValidateUserNumber.java new file mode 100644 index 0000000..dd770ca --- /dev/null +++ b/src/main/java/baseball/validate/ValidateUserNumber.java @@ -0,0 +1,18 @@ +package baseball.validate; + +public class ValidateUserNumber { + + public static void isNumber(String userNumber) { + try { + int toNumber = Integer.parseInt(userNumber); + } catch (NumberFormatException e) { + System.out.println("숫자로 입력해주세요"); + } + } + + public static void is3DigitNumber(String userNumber) { + if (userNumber.length() != 3) { + throw new IllegalArgumentException("3자리 숫자로 입력해주세요"); + } + } +} From f8340c915a522ce5e700ebfe540ffbf3642c4404 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 27 Jan 2024 22:42:06 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=A4=91=EB=B3=B5=EB=90=9C=20=EC=9E=90=EB=A6=BF?= =?UTF-8?q?=EC=88=98=20=EC=9E=85=EB=A0=A5=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/model/UserNumber.java | 4 +--- .../java/baseball/validate/ValidateUserNumber.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/model/UserNumber.java b/src/main/java/baseball/model/UserNumber.java index ef5b429..0e62318 100644 --- a/src/main/java/baseball/model/UserNumber.java +++ b/src/main/java/baseball/model/UserNumber.java @@ -4,15 +4,13 @@ import java.util.List; -/** - * 여기서 strike, ball 계산완료할것 - */ public class UserNumber { private String userNum; public UserNumber(String userNum) { ValidateUserNumber.isNumber(userNum); ValidateUserNumber.is3DigitNumber(userNum); + ValidateUserNumber.isDulplicateDigit(userNum); this.userNum = userNum; } diff --git a/src/main/java/baseball/validate/ValidateUserNumber.java b/src/main/java/baseball/validate/ValidateUserNumber.java index dd770ca..c06f753 100644 --- a/src/main/java/baseball/validate/ValidateUserNumber.java +++ b/src/main/java/baseball/validate/ValidateUserNumber.java @@ -1,5 +1,8 @@ package baseball.validate; +import java.util.HashSet; +import java.util.Set; + public class ValidateUserNumber { public static void isNumber(String userNumber) { @@ -15,4 +18,13 @@ public static void is3DigitNumber(String userNumber) { throw new IllegalArgumentException("3자리 숫자로 입력해주세요"); } } + + public static void isDulplicateDigit(String userNumber) { + Set digitSet = new HashSet<>(); + for (char digit : userNumber.toCharArray()) { + if (!digitSet.add(digit)) { + throw new IllegalArgumentException("각 자리 숫자가 중복되지 않게 입력해주세요."); + } + } + } }