From d0d9eb464b27aa1508c012cd49b50fc97b90244e Mon Sep 17 00:00:00 2001 From: "otto.yoon" Date: Thu, 22 Dec 2022 18:04:58 +0900 Subject: [PATCH 1/2] feat: baseball game --- src/main/java/Baseball.java | 50 ++++++++++++++++++++++++++ src/main/java/BaseballValue.java | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/Baseball.java create mode 100644 src/main/java/BaseballValue.java diff --git a/src/main/java/Baseball.java b/src/main/java/Baseball.java new file mode 100644 index 00000000..d0dfa985 --- /dev/null +++ b/src/main/java/Baseball.java @@ -0,0 +1,50 @@ +import java.util.Random; +import java.util.Scanner; + +public class Baseball { + public static int generateNumbers() { + Random random = new Random(); + random.setSeed(System.currentTimeMillis()); + int[] values = new int[3]; + values[0] = random.nextInt(8) + 1; + do { + values[1] = random.nextInt(8) + 1; + } while (values[1] == values[0]); + do { + values[2] = random.nextInt(8) + 1; + } while (values[2] == values[0] || values[2] == values[1]); + return values[0] * 100 + values[1] * 10 + values[2]; + } + + public static void baseballGame() { + + int inputValue; + BaseballValue answer, guess; + Scanner sc = new Scanner(System.in); + answer = new BaseballValue(generateNumbers()); + System.out.print(answer.getValues()[0] + " "); + System.out.print(answer.getValues()[1] + " "); + System.out.print(answer.getValues()[2] + " \n"); + // guess and check + do { + System.out.print("숫자를 입력해 주세요 : "); + inputValue = sc.nextInt(); + guess = new BaseballValue(inputValue); + guess.compare(answer); + guess.printResult(); + } while (guess.getStrike() < 3); + + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int moreGame; + do { + baseballGame(); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + moreGame = sc.nextInt(); + } while (moreGame == 1); + } +} diff --git a/src/main/java/BaseballValue.java b/src/main/java/BaseballValue.java new file mode 100644 index 00000000..31272551 --- /dev/null +++ b/src/main/java/BaseballValue.java @@ -0,0 +1,60 @@ +public class BaseballValue { + private final int[] values = new int[3]; + private final int[] count = new int[10]; + private int strike; + private int ball; + + public BaseballValue(int newValues) { + for (int i = 0; i < 10; i++) count[i] = 0; + for (int i = 2; i >= 0; i--) { + values[i] = newValues % 10; + newValues /= 10; + count[values[i]] = 1; + } + strike = 0; + ball = 0; + } + + public int[] getValues() { + return values; + } + + public int[] getCount() { + return count; + } + + public int getStrike() { + return strike; + } + + private int compareValue(int input1, int input2) { + if (input1 == input2) { + return 1; + } + return 0; + } + + public void compare(BaseballValue comp) { + int[] compValue = comp.getValues(); + int[] compCount = comp.getCount(); + for (int i = 0; i < 3; i++) { + strike += compareValue(values[i], compValue[i]); + ball += compCount[values[i]]; + } + ball -= strike; + } + + public void printResult() { + if (strike > 0) { + System.out.print(strike + "스트라이크 "); + } + if (ball > 0) { + System.out.print(ball + "볼 "); + } + if (strike == 0 && ball == 0) { + System.out.print("낫싱"); + } + System.out.print("\n"); + } +} + From e1344f960aa5ec468cf33629090bc3f364cd294d Mon Sep 17 00:00:00 2001 From: "otto.yoon" Date: Thu, 22 Dec 2022 18:40:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20main=20class=20=EB=B0=8F=20?= =?UTF-8?q?=EC=9E=85=EC=B6=9C=EB=A0=A5=20=ED=95=A8=EC=88=98=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Baseball.java | 41 ++++++++++++++++++------------------- src/main/java/Main.java | 14 +++++++++++++ 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 src/main/java/Main.java diff --git a/src/main/java/Baseball.java b/src/main/java/Baseball.java index d0dfa985..16752537 100644 --- a/src/main/java/Baseball.java +++ b/src/main/java/Baseball.java @@ -2,9 +2,16 @@ import java.util.Scanner; public class Baseball { - public static int generateNumbers() { - Random random = new Random(); + private final Random random; + private final Scanner sc; + + public Baseball() { + random = new Random(); random.setSeed(System.currentTimeMillis()); + sc = new Scanner(System.in); + } + + private int generateNumbers() { int[] values = new int[3]; values[0] = random.nextInt(8) + 1; do { @@ -16,35 +23,27 @@ public static int generateNumbers() { return values[0] * 100 + values[1] * 10 + values[2]; } - public static void baseballGame() { + private int userInput() { + System.out.print("숫자를 입력해 주세요 : "); + return sc.nextInt(); + } + private void clearGame() { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + } + + public void baseballGame() { int inputValue; BaseballValue answer, guess; - Scanner sc = new Scanner(System.in); answer = new BaseballValue(generateNumbers()); - System.out.print(answer.getValues()[0] + " "); - System.out.print(answer.getValues()[1] + " "); - System.out.print(answer.getValues()[2] + " \n"); - // guess and check do { - System.out.print("숫자를 입력해 주세요 : "); - inputValue = sc.nextInt(); + inputValue = userInput(); guess = new BaseballValue(inputValue); guess.compare(answer); guess.printResult(); } while (guess.getStrike() < 3); - - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + clearGame(); } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int moreGame; - do { - baseballGame(); - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - moreGame = sc.nextInt(); - } while (moreGame == 1); - } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..d119e367 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,14 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Baseball baseball = new Baseball(); + int moreGame; + do { + baseball.baseballGame(); + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + moreGame = sc.nextInt(); + } while (moreGame == 1); + } +}