diff --git a/docs/README.md b/docs/README.md index e69de29bb2..52c3d810a3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,33 @@ +랜덤 숫자 생성 + +- 범위는 1~9 +- 3개의 숫자를 받음 +- 중복되는 숫자가 있으면 안됨 +- camp.nextstep.edu.missionutils.Randoms을 통해 생성 + +사용자 입력 + +- 1~9사이의 숫자 +- 3개를 입력받음 +- 예외 처리 ( IllegalArgumentException 발생 후 종료 ) + - 3개가 아닌 경우 + - 중복되는 숫자가 있을 경우 + - 1~9가 아닐 경우 + +입력받은 수와 랜덤 수 비교 + +- 스트라이크 : 같은 자리 && 같은 수 +- 볼 : 다른 자리 && 같은 수 +- 낫싱 : 다른 자리 && 다른 수 + +결과 출력 + +- 볼, 스트라이크 횟수 차례로 출력 +- 3스트라이크가 아닌 경우 다시 사용자 입력으로 돌아가기 +- 3 스트라이크면 종료 입력으로 이동 + +종료 입력 + +- ‘1’ 입력될 경우 재시작 ( 랜덤 숫자 단계로 다시 돌아감(= 재시작) ) +- ‘2’ 입력될 경우 게임 종료 +- ‘1’ or ‘2’ 가 아닌 숫자 입력될 경우 IllegalArgumentException 발생 후 종료 diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..2c7a7fac49 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,10 @@ package baseball; +import baseball.Controller.Play_Controller; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + Play_Controller controller = new Play_Controller(); + controller.runGame(); } -} +} \ No newline at end of file diff --git a/src/main/java/baseball/Controller/Play_Controller.java b/src/main/java/baseball/Controller/Play_Controller.java new file mode 100644 index 0000000000..7c6cd2764b --- /dev/null +++ b/src/main/java/baseball/Controller/Play_Controller.java @@ -0,0 +1,51 @@ +package baseball.Controller; + +import baseball.Service.Game_Service; +import baseball.View.RequestMessage; +import baseball.View.SystemMessage; +import camp.nextstep.edu.missionutils.Console; + +public class Play_Controller { + private static final int NUM_SIZE = 3; + private static final int START = 1; + private static final int END = 9; + private static final int RETRY = 1; + private static final int GAME_OVER = 2; + + Game_Service gameService = new Game_Service(); + + public void runGame() throws IllegalArgumentException { + setGame(); + startGame(); + endGame(); + askRetry(); + } + + private void setGame() { + gameService.setGame(NUM_SIZE, START, END); + } + + private void startGame() throws IllegalArgumentException { + gameService.playGame(); + } + + private void endGame() { + SystemMessage.printGameOverMessage(); + } + + private void askRetry() throws IllegalArgumentException { + RequestMessage.printRetryMessage(); + if (getInputNum() == RETRY) { + runGame(); + } + } + + private int getInputNum() throws IllegalArgumentException { + int inputNum = Integer.parseInt(Console.readLine()); + + if (inputNum == 0 || inputNum > GAME_OVER) { + throw new IllegalArgumentException(); + } + return inputNum; + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Controller/User_Controller.java b/src/main/java/baseball/Controller/User_Controller.java new file mode 100644 index 0000000000..93f1d87c91 --- /dev/null +++ b/src/main/java/baseball/Controller/User_Controller.java @@ -0,0 +1,42 @@ +package baseball.Controller; + +import java.util.HashSet; +import java.util.Set; + +public class User_Controller { + + public int[] checkUserInput(String input, int size) throws IllegalArgumentException { + checkSize(input, size); + checkSame(input); + return getNum(input, size); + } + + private void checkSize(String input, int size) throws IllegalArgumentException { + if (input.length() != size) { + throw new IllegalArgumentException(); + } + } + + private void checkSame(String input) throws IllegalArgumentException{ + Set charSet = new HashSet<>(); + for (int i = 0; i < input.length(); i++) { + char currentChar = input.charAt(i); + if (charSet.contains(currentChar)) { + throw new IllegalArgumentException(); + } + charSet.add(currentChar); + } + } + + private int[] getNum(String input, int size) throws IllegalArgumentException { + int[] parseInt = new int[size]; + + for (int i = 0; i < input.length(); i++) { + if (!('0' <= input.charAt(i) && input.charAt(i) <= '9')) { + throw new IllegalArgumentException(); + } + parseInt[i] = input.charAt(i) - '0'; + } + return parseInt; + } +} diff --git a/src/main/java/baseball/Domain/Game_Domain.java b/src/main/java/baseball/Domain/Game_Domain.java new file mode 100644 index 0000000000..7481ba8bf5 --- /dev/null +++ b/src/main/java/baseball/Domain/Game_Domain.java @@ -0,0 +1,35 @@ +package baseball.Domain; + +public class Game_Domain { + int strikeCount; + int ballCount; + int[] gameNumbers; + public Game_Domain(int[] numbers) { + gameNumbers = numbers; + } + + public void initBaseBall() { + strikeCount = 0; + ballCount = 0; + } + + public int getStrikeCount() { + return strikeCount; + } + + public int getBallCount() { + return ballCount; + } + + public int[] getGameNumbers() { + return gameNumbers; + } + + public void incStrikeCount() { + strikeCount += 1; + } + + public void incBallCount() { + ballCount += 1; + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Model/Computer.java b/src/main/java/baseball/Model/Computer.java new file mode 100644 index 0000000000..2070934478 --- /dev/null +++ b/src/main/java/baseball/Model/Computer.java @@ -0,0 +1,31 @@ +package baseball.Model; + +import camp.nextstep.edu.missionutils.Randoms; + +public class Computer { + + public static int[] getComputerNum(int size, int start, int end) { + int[] numbers = new int[3]; + for (int i = 0; i < size; i++) { + numbers[i] = RandomNumber(numbers, start, end, i); + } + return numbers; + } + + private static int RandomNumber(int[] numbers, int start, int end, int i) { + int randomNumber = Randoms.pickNumberInRange(start, end); + while (!isSame(numbers, i, randomNumber)) { + randomNumber = Randoms.pickNumberInRange(start, end); + } + return randomNumber; + } + + private static Boolean isSame(int[] numbers, int i, int randomNumber) { + for (int j = 0; j < i; j++) { + if (numbers[j] == randomNumber) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Model/User.java b/src/main/java/baseball/Model/User.java new file mode 100644 index 0000000000..be751f28e7 --- /dev/null +++ b/src/main/java/baseball/Model/User.java @@ -0,0 +1,11 @@ +package baseball.Model; + +public class User { + int[] userNumbers; + public int[] getUserNumbers() { + return userNumbers; + } + public void setUserNumbers(int[] userNumbers) { + this.userNumbers = userNumbers; + } +} \ No newline at end of file diff --git a/src/main/java/baseball/Service/Game_Service.java b/src/main/java/baseball/Service/Game_Service.java new file mode 100644 index 0000000000..2a0374889a --- /dev/null +++ b/src/main/java/baseball/Service/Game_Service.java @@ -0,0 +1,71 @@ +package baseball.Service; + +import baseball.Controller.User_Controller; +import baseball.Domain.Game_Domain; +import baseball.Model.User; +import baseball.Model.Computer; +import baseball.View.SystemMessage; +import baseball.View.RequestMessage; + +import camp.nextstep.edu.missionutils.Console; + +public class Game_Service { + + int size; + Game_Domain game; + User user = new User(); + User_Controller parser = new User_Controller(); + SystemMessage systemMessage = new SystemMessage(); + + public void setGame(int size, int start, int end) { + this.size = size; + game = new Game_Domain(Computer.getComputerNum(size, start, end)); + } + + public void playGame() { + int strike = 0; + while (strike != 3) { + play(); + systemMessage.printScoreMessage(game.getBallCount(), game.getStrikeCount()); + strike = game.getStrikeCount(); + } + } + + private void play() { + game.initBaseBall(); + user.setUserNumbers(getUserNumber()); + computeScore(); + } + + private int[] getUserNumber() throws IllegalArgumentException { + RequestMessage.requestInputData(); + String input = Console.readLine(); + return parser.checkUserInput(input, size); + } + + private void computeScore() { + for (int i = 0; i < size; i++) { + compute(game.getGameNumbers(), user.getUserNumbers(), i); + } + } + + private void compute(int[] gameNumber, int[] userNumber, int index) { + int temp = -1; + for (int i = 0; i < gameNumber.length; i++) { + if (gameNumber[i] == userNumber[index]) { + temp = i; + break; + } + } + incCount(index, temp); + } + + private void incCount(int index, int temp) { + if (temp != index && temp != -1) { + game.incBallCount(); + } + if (temp == index) { + game.incStrikeCount(); + } + } +} \ No newline at end of file diff --git a/src/main/java/baseball/View/RequestMessage.java b/src/main/java/baseball/View/RequestMessage.java new file mode 100644 index 0000000000..8d216fe63c --- /dev/null +++ b/src/main/java/baseball/View/RequestMessage.java @@ -0,0 +1,10 @@ +package baseball.View; + +public class RequestMessage { + public static void requestInputData() { + System.out.print("숫자를 입력해 주세요 : "); + } + public static void printRetryMessage() { + System.out.println("게임을 다시 시작하려면 1, 종료하려면 2를 입력하세요."); + } +} diff --git a/src/main/java/baseball/View/SystemMessage.java b/src/main/java/baseball/View/SystemMessage.java new file mode 100644 index 0000000000..39959f002b --- /dev/null +++ b/src/main/java/baseball/View/SystemMessage.java @@ -0,0 +1,21 @@ +package baseball.View; + +public class SystemMessage { + public void printScoreMessage(int ball, int strike) { + if (ball == 0 && strike == 0) { + System.out.println("낫싱"); + } + if (ball == 0 && strike != 0) { + System.out.println(strike + "스트라이크"); + } + if (ball != 0 && strike == 0) { + System.out.println(ball + "볼"); + } + if (ball != 0 && strike != 0) { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } + } + public static void printGameOverMessage() { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } +} \ No newline at end of file diff --git a/src/test/java/baseball/ApplicationTest.java b/src/test/java/baseball/ApplicationTest.java index 3fa29fa67b..f8ec5c3607 100644 --- a/src/test/java/baseball/ApplicationTest.java +++ b/src/test/java/baseball/ApplicationTest.java @@ -22,8 +22,8 @@ class ApplicationTest extends NsTest { @Test void 예외_테스트() { - assertSimpleTest(() -> - assertThatThrownBy(() -> runException("1234")) + assertSimpleTest(() -> + assertThatThrownBy(() -> runException("1234")) .isInstanceOf(IllegalArgumentException.class) ); }