From ea2ef905b68c0e71c104f33e3c9b8929473f2567 Mon Sep 17 00:00:00 2001 From: oeunji Date: Mon, 19 Aug 2024 15:53:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=BB=A4=EB=B0=8B=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baseball/Application.java | 85 ++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 7f1901b8b..3e0b68a11 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,90 @@ package baseball; +import java.util.Scanner; + public class Application { + + int numArr[] = new int[3]; // 크기가 3인 배열. 세 자리의 난수 저장. + int inputArr[] = new int[3]; // 사용자 입력 배열 + int strike = 0; // 스트라이크 개수 + int ball = 0; // 볼 개수 + + public void createRandomNumber() { // 난수 생성 메서드 + boolean isDuplicate; + do { + isDuplicate = false; + for (int i = 0; i < numArr.length; i++) { + numArr[i] = (int) (Math.random() * 9) + 1; // 1 ~ 9 난수 생성 + } + + // 중복 체크 + if (numArr[0] == numArr[1] || numArr[1] == numArr[2] || numArr[0] == numArr[2]) { + isDuplicate = true; + } + } while (isDuplicate); + } + + public void inputUserAnswer() { // 사용자 입력 메서드 + Scanner scanner = new Scanner(System.in); + createRandomNumber(); + + while (true) { + System.out.println("숫자를 입력해주세요 :"); + for (int i = 0; i < inputArr.length; i++) { + inputArr[i] = scanner.nextInt(); + for (int j = 0; j < i; j++) { + if (inputArr[i] == inputArr[j]) { + System.out.println("중복된 숫자가 있습니다. 다시 입력해주세요."); + i--; + break; + } + } + } + + strike = 0; + ball = 0; + + for (int i = 0; i < inputArr.length; i++) { // 스트라이크, 볼 개수 + for (int j = 0; j < numArr.length; j++) { + if (inputArr[i] == numArr[j]) { + if (i == j) { + strike++; + } else { + ball++; + } + } + } + } + + if (strike == 0 && ball == 0) { // 숫자 비교 + System.out.println("낫싱"); + } else { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } + + if (strike == 3) { + System.out.println(strike + "스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + + int choice = scanner.nextInt(); + if (choice == 1) { + // 상태 초기화 + createRandomNumber(); + strike = 0; + ball = 0; + } else if (choice == 2) { + break; + } else { + System.out.println("잘못된 값이 입력되었습니다. 종료합니다."); + break; + } + } + } + } + public static void main(String[] args) { - //TODO: 숫자 야구 게임 구현 + Application application = new Application(); + application.inputUserAnswer(); } } From 06302eb95077b8ff0db7a2a9cb73c10816ea5fa4 Mon Sep 17 00:00:00 2001 From: oeunji Date: Mon, 26 Aug 2024 00:13:36 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Feat=20:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 1bee39036..74535df4c 100644 --- a/README.md +++ b/README.md @@ -128,3 +128,23 @@ ## 📝 License This project is [MIT](https://github.com/woowacourse/java-baseball-precourse/blob/master/LICENSE) licensed. + +## 구현할 기능 목록 +Computer +- 3자리 난수 생성 + +Player +- 사용자 3자리수 숫자 input +- 게임 재시작 여부 input + +CompareNumber +- 사용자 숫자 입력 후 정답과 비교 +- 힌트 출력 +- 3 Strike인 경우 게임 종료 + +Game +- 게임 실행 +- 게임 재시작 및 종료 + +InputException +- 예외 관리(플레이어가 세자리 숫자를 입력하지 않은 경우, 동일한 숫자를 입력한 경우, 1~9 범위 외 숫자를 입력하는 경우, 게임 종료 시 1 또는 2를 입력하지 않았을 경우) \ No newline at end of file From c27e116acb68aa5759e2408d5c7fa739acde8c6b Mon Sep 17 00:00:00 2001 From: oeunji Date: Mon, 26 Aug 2024 00:17:58 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Feat=20:=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=9E=91=EC=84=B1=20Feat=20:=20=EB=82=9C=EC=88=98?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20Feat=20:=20=EC=9E=85=EB=A0=A5=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84=20Feat=20:=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EA=B4=80=EB=A6=AC=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20Feat=20:=20=EC=88=AB=EC=9E=90=20=EB=B9=84=EA=B5=90?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC=ED=98=84=20Feat=20:?= =?UTF-8?q?=20=EA=B2=8C=EC=9E=84=20=EC=A7=84=ED=96=89=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle/wrapper/gradle-wrapper.properties | 3 +- src/main/java/baseball/Application.java | 91 ++-------------------- src/main/java/baseball/CompareNumber.java | 59 ++++++++++++++ src/main/java/baseball/Computer.java | 46 +++++++++++ src/main/java/baseball/Game.java | 38 +++++++++ src/main/java/baseball/InputException.java | 56 +++++++++++++ src/main/java/baseball/Player.java | 36 +++++++++ 7 files changed, 244 insertions(+), 85 deletions(-) create mode 100644 src/main/java/baseball/CompareNumber.java create mode 100644 src/main/java/baseball/Computer.java create mode 100644 src/main/java/baseball/Game.java create mode 100644 src/main/java/baseball/InputException.java create mode 100644 src/main/java/baseball/Player.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bb8b2fc26..13197b0de 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Sun Aug 25 23:56:59 KST 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 3e0b68a11..e7f1f44d5 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,90 +1,13 @@ package baseball; -import java.util.Scanner; - public class Application { - - int numArr[] = new int[3]; // 크기가 3인 배열. 세 자리의 난수 저장. - int inputArr[] = new int[3]; // 사용자 입력 배열 - int strike = 0; // 스트라이크 개수 - int ball = 0; // 볼 개수 - - public void createRandomNumber() { // 난수 생성 메서드 - boolean isDuplicate; - do { - isDuplicate = false; - for (int i = 0; i < numArr.length; i++) { - numArr[i] = (int) (Math.random() * 9) + 1; // 1 ~ 9 난수 생성 - } - - // 중복 체크 - if (numArr[0] == numArr[1] || numArr[1] == numArr[2] || numArr[0] == numArr[2]) { - isDuplicate = true; - } - } while (isDuplicate); - } - - public void inputUserAnswer() { // 사용자 입력 메서드 - Scanner scanner = new Scanner(System.in); - createRandomNumber(); - - while (true) { - System.out.println("숫자를 입력해주세요 :"); - for (int i = 0; i < inputArr.length; i++) { - inputArr[i] = scanner.nextInt(); - for (int j = 0; j < i; j++) { - if (inputArr[i] == inputArr[j]) { - System.out.println("중복된 숫자가 있습니다. 다시 입력해주세요."); - i--; - break; - } - } - } - - strike = 0; - ball = 0; - - for (int i = 0; i < inputArr.length; i++) { // 스트라이크, 볼 개수 - for (int j = 0; j < numArr.length; j++) { - if (inputArr[i] == numArr[j]) { - if (i == j) { - strike++; - } else { - ball++; - } - } - } - } - - if (strike == 0 && ball == 0) { // 숫자 비교 - System.out.println("낫싱"); - } else { - System.out.println(ball + "볼 " + strike + "스트라이크"); - } - - if (strike == 3) { - System.out.println(strike + "스트라이크"); - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - - int choice = scanner.nextInt(); - if (choice == 1) { - // 상태 초기화 - createRandomNumber(); - strike = 0; - ball = 0; - } else if (choice == 2) { - break; - } else { - System.out.println("잘못된 값이 입력되었습니다. 종료합니다."); - break; - } - } - } - } - public static void main(String[] args) { - Application application = new Application(); - application.inputUserAnswer(); + try { + Game game = new Game(); + game.play(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + System.exit(1); // 프로그램 종료 + } } } diff --git a/src/main/java/baseball/CompareNumber.java b/src/main/java/baseball/CompareNumber.java new file mode 100644 index 000000000..8d43bb493 --- /dev/null +++ b/src/main/java/baseball/CompareNumber.java @@ -0,0 +1,59 @@ +package baseball; + +import java.util.List; + +public class CompareNumber { + private boolean gameOver; + + public void compareNumber(Player player, Computer computer) { + List randomNumber = computer.getRandomNumber(); + player.playerInputNumber(); // 숫자 입력 + List playerNumber = player.getInputNumber(); + + int strike = calculateStrikes(randomNumber, playerNumber); + int ball = calculateBalls(randomNumber, playerNumber); + + // 결과 출력 + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else { + System.out.println(ball + "볼 " + strike + "스트라이크"); + } + + // 3 스트라이크인 경우 게임 종료 + if (strike == 3) { + printGameOverMessage(); + gameOver = true; + } else { + gameOver = false; + } + } + + private int calculateStrikes(List randomNumber, List playerNumber) { + int strike = 0; + for (int i = 0; i < randomNumber.size(); i++) { + if (randomNumber.get(i).equals(playerNumber.get(i))) { + strike++; + } + } + return strike; + } + + private int calculateBalls(List randomNumber, List playerNumber) { + int ball = 0; + for (int i = 0; i < randomNumber.size(); i++) { + if (randomNumber.contains(playerNumber.get(i)) && !randomNumber.get(i).equals(playerNumber.get(i))) { + ball++; + } + } + return ball; + } + + public boolean isGameOver() { + return gameOver; + } + + private void printGameOverMessage() { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } +} diff --git a/src/main/java/baseball/Computer.java b/src/main/java/baseball/Computer.java new file mode 100644 index 000000000..80a470cdc --- /dev/null +++ b/src/main/java/baseball/Computer.java @@ -0,0 +1,46 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class Computer { + private List randomNumber; + + public Computer() { + this.randomNumber = generateRandomNumber(); + } + + private List generateRandomNumber() { + List numbers = new ArrayList<>(); + boolean isDuplicate; + + do { + isDuplicate = false; + for (int i = 0; i < 3; i++) { + int num = Randoms.pickNumberInRange(1, 9); // 1 ~ 9 난수 생성 + if (!numbers.contains(num)) { + numbers.add(num); + } else { + isDuplicate = true; + break; // 중복이 발생하면 break -> 다시 시도 + } + } + + if (isDuplicate) { + numbers.clear(); + } + } while (isDuplicate || numbers.size() < 3); + + return numbers; + } + + public List getRandomNumber() { + return this.randomNumber; + } + + public void resetRandomNumber() { + this.randomNumber = generateRandomNumber(); + } +} diff --git a/src/main/java/baseball/Game.java b/src/main/java/baseball/Game.java new file mode 100644 index 000000000..99beb0dd3 --- /dev/null +++ b/src/main/java/baseball/Game.java @@ -0,0 +1,38 @@ +package baseball; + +public class Game { + private final Computer computer; + private final Player player; + private final CompareNumber compareNumber; + + public Game() { + computer = new Computer(); + player = new Player(); + compareNumber = new CompareNumber(); + } + + public void play() { + boolean continuePlaying = true; + + while (continuePlaying) { + compareNumber.compareNumber(player, computer); + + if (compareNumber.isGameOver()) { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + String replayInput = player.inputReplay(); + + if ("1".equals(replayInput)) { + // 게임 재시작 + computer.resetRandomNumber(); // 새로운 난수 생성 + } else if ("2".equals(replayInput)) { + // 게임 종료 + continuePlaying = false; + } else { + // 잘못된 입력 처리 + System.out.println("잘못된 입력입니다. 게임을 종료합니다."); + continuePlaying = false; + } + } + } + } +} diff --git a/src/main/java/baseball/InputException.java b/src/main/java/baseball/InputException.java new file mode 100644 index 000000000..602a91bc3 --- /dev/null +++ b/src/main/java/baseball/InputException.java @@ -0,0 +1,56 @@ +package baseball; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +public class InputException { + private static final String NOT_NUMBER = "숫자가 아닙니다."; + private static final String NOT_NUMBER_LENGTH = "숫자의 길이가 맞지 않습니다."; + private static final String DUPLICATE = "중복된 숫자가 있습니다."; + private static final String OUT_OF_RANGE = "숫자가 1~9 범위를 벗어났습니다."; + private static final String NUMBER_REGEX = "^[0-9]*$"; + + private InputException() { + } + + public static void validatesNumber(String inputNumbers) { + isNumber(inputNumbers); + isLength(inputNumbers); + isDuplicate(inputNumbers); + isInRange(inputNumbers); + } + + public static void isNumber(String inputNumbers) { + if (!Pattern.matches(NUMBER_REGEX, inputNumbers)) { + exception(NOT_NUMBER); + } + } + + public static void isLength(String inputNumbers) { + if (inputNumbers.length() != 3) { + exception(NOT_NUMBER_LENGTH); + } + } + + public static void isDuplicate(String inputNumbers) { + List inputNumberList = Arrays.asList(inputNumbers.split("")); + if (inputNumberList.stream().distinct().count() < 3) { + exception(DUPLICATE); + } + } + + public static void isInRange(String inputNumbers) { + List inputNumberList = Arrays.asList(inputNumbers.split("")); + for (String num : inputNumberList) { + int number = Integer.parseInt(num); + if (number < 1 || number > 9) { + exception(OUT_OF_RANGE); + } + } + } + + private static void exception(String message) { + throw new IllegalArgumentException(message); + } +} diff --git a/src/main/java/baseball/Player.java b/src/main/java/baseball/Player.java new file mode 100644 index 000000000..2b3e1ae45 --- /dev/null +++ b/src/main/java/baseball/Player.java @@ -0,0 +1,36 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Console; +import java.util.ArrayList; +import java.util.List; + +import static baseball.InputException.validatesNumber; + +public class Player { + private final List inputNumber; + + public Player() { + this.inputNumber = new ArrayList<>(); + } + + public void playerInputNumber() { // 사용자 입력 메서드 + System.out.println("숫자를 입력해주세요 : "); + String input = Console.readLine(); + validatesNumber(input); + + inputNumber.clear(); // 이전 입력값 지우기 + for (char ch : input.toCharArray()) { // 입력받은 숫자를 리스트에 저장 + inputNumber.add(Character.getNumericValue(ch)); + } + } + + public List getInputNumber(){ + return this.inputNumber; + } + + // 게임 재시작 여부 + public String inputReplay(){ + String inputReplay = Console.readLine(); + return inputReplay; + } +}