diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..625b1171c1 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,11 @@ package baseball; +import java.util.*; +import camp.nextstep.edu.missionutils.Randoms; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + GameController gameController = new GameController(); + gameController.startGame(); } } diff --git a/src/main/java/baseball/GameController.java b/src/main/java/baseball/GameController.java new file mode 100644 index 0000000000..2733574fb7 --- /dev/null +++ b/src/main/java/baseball/GameController.java @@ -0,0 +1,44 @@ +package baseball; + +import java.util.*; +import camp.nextstep.edu.missionutils.Console; +import camp.nextstep.edu.missionutils.Randoms; + +public class GameController { + public static void startGame() { + boolean again = true; + System.out.println("숫자 야구 게임을 시작합니다."); + + while (again) { + NumberGenerator numberGenerator = new NumberGenerator(); + List computerNum = numberGenerator.getRandomComputerNumber(); + + boolean baseballGame = true; + while (baseballGame) { + InputHandler inputHandler = new InputHandler(); + List userNum = inputHandler.getUserInput(); + + NumberChecker numberChecker = new NumberChecker(); + baseballGame = numberChecker.checkNumber(computerNum, userNum); + } + + again = askForRestart(); + } + } + + private static boolean askForRestart() { + try { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int restart = Integer.parseInt(Console.readLine()); + if (restart == 1) { + return true; + } else if (restart == 2) { + return false; + } else { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("잘못된 입력입니다. 게임을 종료합니다."); + } + } +} diff --git a/src/main/java/baseball/InputHandler.java b/src/main/java/baseball/InputHandler.java new file mode 100644 index 0000000000..3f522855e0 --- /dev/null +++ b/src/main/java/baseball/InputHandler.java @@ -0,0 +1,28 @@ +package baseball; + +import java.util.*; +import camp.nextstep.edu.missionutils.Randoms; + +public class InputHandler { + public static List getUserInput() { + try { + System.out.print("숫자를 입력해주세요 : "); + String inputUserNum = camp.nextstep.edu.missionutils.Console.readLine(); + if (inputUserNum == null || inputUserNum.length() != 3) { + throw new IllegalArgumentException(); + } + + List userInput = new ArrayList<>(); + for (char digitChar : inputUserNum.toCharArray()) { + int digit = Character.getNumericValue(digitChar); + if (digit < 1 || digit > 9) { + throw new IllegalArgumentException(); + } + userInput.add(digit); + } + return userInput; + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("잘못된 입력입니다. 다시 시도해주세요."); + } + } +} diff --git a/src/main/java/baseball/NumberChecker.java b/src/main/java/baseball/NumberChecker.java new file mode 100644 index 0000000000..0b43b8ba75 --- /dev/null +++ b/src/main/java/baseball/NumberChecker.java @@ -0,0 +1,35 @@ +package baseball; + +import java.util.*; +import camp.nextstep.edu.missionutils.Randoms; + +public class NumberChecker { + public boolean checkNumber(List computerNum, List userNum) { + int strike = 0; + int ball = 0; + + for (int i = 0; i < 3; i++) { + if (userNum.get(i).equals(computerNum.get(i))) { + strike++; + } else if (computerNum.contains(userNum.get(i))) { + ball++; + } + } + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike == 3) { + System.out.println(strike + "스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return false; // 게임 종료 + } else { + if (ball > 0) { + System.out.print(ball + "볼 "); + } + if (strike > 0) { + System.out.print(strike + "스트라이크"); + } + System.out.println(); + } + return true; + } +} diff --git a/src/main/java/baseball/NumberGenerator.java b/src/main/java/baseball/NumberGenerator.java new file mode 100644 index 0000000000..703fb50cde --- /dev/null +++ b/src/main/java/baseball/NumberGenerator.java @@ -0,0 +1,19 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class NumberGenerator { + public static List getRandomComputerNumber() { + List computerNum = new ArrayList<>(); + while (computerNum.size() < 3) { + int randomNumber = Randoms.pickNumberInRange(1, 9); + if (!computerNum.contains(randomNumber)) { + computerNum.add(randomNumber); + } + } + return computerNum; + } +} diff --git a/src/main/java/baseball/ResultPrinter.java b/src/main/java/baseball/ResultPrinter.java new file mode 100644 index 0000000000..7803662f2b --- /dev/null +++ b/src/main/java/baseball/ResultPrinter.java @@ -0,0 +1,22 @@ +package baseball; + +public class ResultPrinter { + public static boolean printResult(int strike, int ball) { + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else if (strike == 3) { + System.out.println(strike + "스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return false; // 게임 종료 + } else { + if (ball > 0) { + System.out.print(ball + "볼 "); + } + if (strike > 0) { + System.out.print(strike + "스트라이크"); + } + System.out.println(); + } + return true; + } +} diff --git a/src/main/java/baseball/Validator.java b/src/main/java/baseball/Validator.java new file mode 100644 index 0000000000..d645bc2277 --- /dev/null +++ b/src/main/java/baseball/Validator.java @@ -0,0 +1,16 @@ +package baseball; + +public class Validator { + public static void validateInput(String input) { + if (input == null || input.length() != 3) { + throw new IllegalArgumentException(); + } + + for (char digitChar : input.toCharArray()) { + int digit = Character.getNumericValue(digitChar); + if (digit < 1 || digit > 9) { + throw new IllegalArgumentException(); + } + } + } +}