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 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 7f1901b8b..e7f1f44d5 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -2,6 +2,12 @@ public class Application { public static void main(String[] args) { - //TODO: 숫자 야ꡬ κ²Œμž„ κ΅¬ν˜„ + 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; + } +}