diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..1faf6f2a7e 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,16 @@ package baseball; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + public class Application { + public static void main(String[] args) { // TODO: 프로그램 구현 + BaseballController baseballController = new BaseballController(); + baseballController.gameStart(); } } diff --git a/src/main/java/baseball/BaseballController.java b/src/main/java/baseball/BaseballController.java new file mode 100644 index 0000000000..b8efc76a9d --- /dev/null +++ b/src/main/java/baseball/BaseballController.java @@ -0,0 +1,70 @@ +package baseball; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballController { + private final BaseballModel baseballModel; + private final BaseballView baseballView; + private final List user; + + public BaseballController() { + baseballModel = new BaseballModel(); + baseballView = new BaseballView(); + user = new ArrayList<>(); + } + + public void gameStart() { + System.out.println("숫자 야구 게임을 시작합니다."); + while (true) { + user.clear(); + String userInput = baseballView.getUserInput(); + exception(userInput); + for (int i = 0; i < Constants.NUMBER_SIZE; i++) { + user.add(userInput.charAt(i) - '0'); + } + int[] result = baseballModel.calculateStrikeAndBall(user); + if (result[0] == 3) { //게임 승리 + baseballView.printWinMessage(); + int restart = baseballView.askForRestart(); + if (restart == 1) { + baseballModel.getNewComputerNum(); + } else if (restart == 2) { + return; + } else { + throw new IllegalArgumentException("1,2만 입력하세요"); + } + } else { + baseballView.printResult(result[0], result[1]); + } + } + } + + private static void exception(String input) { + if (input.length() != Constants.NUMBER_SIZE) { + throw new IllegalArgumentException("3개의 값이 들어가야 합니다."); + } + + try { + Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("정수형 변환 불가"); + } + + if (hasDuplicatedDigits(input)) { + throw new IllegalArgumentException("3개의 숫자는 모두 달라야 합니다."); + } + + } + + private static boolean hasDuplicatedDigits(String input) { + for (int i = 0; i < input.length(); i++) { + for (int j = i + 1; j < input.length(); j++) { + if (input.charAt(i) == input.charAt(j)) { + return true; + } + } + } + return false; + } +} diff --git a/src/main/java/baseball/BaseballModel.java b/src/main/java/baseball/BaseballModel.java new file mode 100644 index 0000000000..cb90966372 --- /dev/null +++ b/src/main/java/baseball/BaseballModel.java @@ -0,0 +1,41 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; +import java.util.List; + +public class BaseballModel { + private List computer; + public BaseballModel() { + this.computer = generateRandomNum(); + } + + public List getNewComputerNum() { + return this.computer = generateRandomNum(); + } + + public int[] calculateStrikeAndBall(List user) { + int strike = 0; + int ball = 0; + for (int i = 0; i < Constants.NUMBER_SIZE; i++) { + if (user.get(i).equals(computer.get(i))) { + strike++; + } else if (user.contains(computer.get(i))) { + ball++; + } + } + return new int[]{strike, ball}; + } + + + public List generateRandomNum() { + List number = new ArrayList<>(); + while (number.size() < Constants.NUMBER_SIZE) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!number.contains(randomNumber)) { + number.add(randomNumber); + } + } + return number; + } +} diff --git a/src/main/java/baseball/BaseballView.java b/src/main/java/baseball/BaseballView.java new file mode 100644 index 0000000000..c63a210f54 --- /dev/null +++ b/src/main/java/baseball/BaseballView.java @@ -0,0 +1,39 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; +import java.util.List; + +public class BaseballView { + public String getUserInput() { + System.out.print("숫자를 입력해 주세요: "); + return Console.readLine(); + } + + public void printResult(int strike, int ball) { + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike > 0 && ball > 0) { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } else if (strike > 0) { + System.out.println(strike + "스트라이크"); + } else if (ball > 0) { + System.out.println(ball + "볼"); + } + } + + public void printWinMessage() { + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + public int askForRestart() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + return Integer.parseInt(Console.readLine()); //이것도 예외처리 해야함 + } + + public void printError(String message) { + System.out.println(message); + } +} diff --git a/src/main/java/baseball/Constants.java b/src/main/java/baseball/Constants.java new file mode 100644 index 0000000000..8aadc3c39e --- /dev/null +++ b/src/main/java/baseball/Constants.java @@ -0,0 +1,5 @@ +package baseball; + +public class Constants { + public static final int NUMBER_SIZE = 3; +}