From cdb9f0a9d4bd0ca3bbd5c28f798eece67caf33fd Mon Sep 17 00:00:00 2001 From: Jihyun Date: Mon, 8 Jul 2024 12:50:30 +0900 Subject: [PATCH] initial commit --- build.gradle | 1 + src/main/java/baseball/Application.java | 44 +++++++++++++++++- src/main/java/baseball/Count.java | 49 ++++++++++++++++++++ src/main/java/baseball/ExceptionHandler.java | 15 ++++++ src/main/java/baseball/RandomNumber.java | 22 +++++++++ 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/main/java/baseball/Count.java create mode 100644 src/main/java/baseball/ExceptionHandler.java create mode 100644 src/main/java/baseball/RandomNumber.java diff --git a/build.gradle b/build.gradle index 080ed8c12e..526501b8f9 100644 --- a/build.gradle +++ b/build.gradle @@ -23,3 +23,4 @@ java { test { useJUnitPlatform() } + diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index dd95a34214..a2859b46a7 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,49 @@ package baseball; +import camp.nextstep.edu.missionutils.Randoms; +import camp.nextstep.edu.missionutils.Console; + +import java.util.Scanner; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + Scanner scanner = new Scanner(System.in); + + System.out.println("숫자 야구 게임을 시작합니다."); + int[] randoms = new int[3]; + RandomNumber random = new RandomNumber(); + random.randomNumber(randoms); + + while (true) { + try { + System.out.print("숫자를 입력해주세요 : "); + + String answer = Console.readLine(); + ExceptionHandler exceptionHandler = new ExceptionHandler(); + exceptionHandler.checkValid(answer); + + String[] answers = new String[3]; + for (int i = 0; i < 3; i++) { + answers[i] = answer.substring(i, i + 1); + } + + Count count = new Count(); + count.countStrike(randoms, answers); + count.countBall(randoms, answers); + count.printCount(); + + if (count.getStrikes() == 3) { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int option = scanner.nextInt(); + if (option == 2) { + return; + } else { + random.randomNumber(randoms); + } + } + } catch (IllegalArgumentException e){ + throw e; + } + } } } diff --git a/src/main/java/baseball/Count.java b/src/main/java/baseball/Count.java new file mode 100644 index 0000000000..ac5f2df528 --- /dev/null +++ b/src/main/java/baseball/Count.java @@ -0,0 +1,49 @@ +package baseball; + +public class Count { + int strikes = 0; + int balls = 0; + + int countStrike(int[] r, String[] str) { + for (int i = 0; i < 3; i++) { + if (r[i] == Integer.parseInt(str[i])) { + strikes++; + } + } + return strikes; + } + + int countBall(int[] r, String[] str) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (i != j && r[i] == Integer.parseInt(str[j])) { + balls++; + } + } + } + return balls; + } + + void printCount() { + if(strikes == 3){ + System.out.println("3스트라이크"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임종료"); + } + else if(strikes == 0 && balls != 0){ + System.out.println(balls + "볼"); + } + else if(strikes != 0 && balls == 0){ + System.out.println(strikes + "스트라이크"); + } + else if(strikes != 0 && balls != 0){ + System.out.println(balls + "볼 " + strikes + "스트라이크"); + } + else{ + System.out.println("낫싱"); + } + } + + int getStrikes() { + return strikes; + } +} diff --git a/src/main/java/baseball/ExceptionHandler.java b/src/main/java/baseball/ExceptionHandler.java new file mode 100644 index 0000000000..813113f745 --- /dev/null +++ b/src/main/java/baseball/ExceptionHandler.java @@ -0,0 +1,15 @@ +package baseball; + +public class ExceptionHandler { + void checkValid(String answer){ + if(answer.length() != 3){ + throw new IllegalArgumentException("3자리 수를 입력합니다."); + } + for (int i = 0; i < 3; i++) { + String a = answer.substring(i, i + 1); + if(a.equals("0")){ + throw new IllegalArgumentException("잘못된 값을 입력했습니다."); + } + } + } +} diff --git a/src/main/java/baseball/RandomNumber.java b/src/main/java/baseball/RandomNumber.java new file mode 100644 index 0000000000..2d8fd5e9af --- /dev/null +++ b/src/main/java/baseball/RandomNumber.java @@ -0,0 +1,22 @@ +package baseball; + +import camp.nextstep.edu.missionutils.Randoms; + +public class RandomNumber { + void randomNumber(int[] r){ + boolean duplicate; + + for (int i = 0; i < 3; i++) { + do { + duplicate = false; + r[i] = Randoms.pickNumberInRange(1, 9); + for (int j = 0; j < i; j++) { + if (r[i] == r[j]) { + duplicate = true; + break; + } + } + } while (duplicate); + } + } +}