From 3e9895f5608e5a46bde004dd3f060b7074532d99 Mon Sep 17 00:00:00 2001 From: jinhwa-park <--global> Date: Mon, 6 May 2024 12:40:59 +0900 Subject: [PATCH 1/4] main update --- README.md | 11 ++- src/main/baseball/baseballGame.java | 113 ++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/main/baseball/baseballGame.java diff --git a/README.md b/README.md index 8d7e8aee..bd5cd6e5 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse + +- 기능 요구 사항 + - 1~9까지 서로 다른 수로 이루어진 3자리 수를 컴퓨터가 제시한다. + - 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱 + - 컴퓨터가 제시한 3개의 숫자를 플레이어(유저)가 모두 맞히면 게임 종료 + - 그 전까지는 게임이 지속되어야 함. + - 게임을 종료했을 때, 다시 시작하거나 완전히 종료 가능 + - 플레이어(유저)가 잘못된 값을 입력하면 ```IllegalArgumentException```을 발생시킨 후 애플리케이션 종료 + \ No newline at end of file diff --git a/src/main/baseball/baseballGame.java b/src/main/baseball/baseballGame.java new file mode 100644 index 00000000..98018bea --- /dev/null +++ b/src/main/baseball/baseballGame.java @@ -0,0 +1,113 @@ +package baseball; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Random; +import java.util.Scanner; + +public class baseballGame { + public static void main(String[] args) { + System.out.println("숫자 야구 게임을 시작합니다."); + boolean newGame; + do { + newGame = new GameController().playGame(); + } while (newGame); + } +} + +class GameController { + public boolean playGame() { + List comList = RandomNumberGenerator.generateRandomNumberList(); + while (true) { + System.out.print("숫자를 입력해주세요: "); + int ball = 0; + int strike = 0; + int num = InputHandler.getInputNumber(); + checkValid(num); + List myList = NumberUtil.getInts(num); + for (int i = 0; i < 3; i++) { + if (!Objects.equals(myList.get(i), comList.get(i))) { + boolean contains = comList.contains(myList.get(i)); + if (contains) { + ball++; + } + } + if (Objects.equals(myList.get(i), comList.get(i))) { + strike++; + } + } + GameOutput.printResult(ball, strike); + if (strike == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return InputHandler.askForNewGame(); + } + } + } + + private static void checkValid(int num) { + if (num < 100 || num > 999) { + throw new IllegalArgumentException("입력 범위를 초과했습니다."); + } + } +} + +class RandomNumberGenerator { + public static List generateRandomNumberList() { + Random random = new Random(); + List comList = new ArrayList<>(); + while (comList.size() < 3) { + int randomNumber = random.nextInt(9) + 1; + if (!comList.contains(randomNumber)) { + comList.add(randomNumber); + } + } + return comList; + } +} + +class InputHandler { + public static int getInputNumber() { + Scanner scanner = new Scanner(System.in); + int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환 + return number; + } + + public static boolean askForNewGame() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int value = getInputNumber(); + if (value == 1) { + return true; + } else if (value == 2) { + return false; + } else { + throw new IllegalArgumentException("올바른 입력이 아닙니다."); + } + } +} + +class GameOutput { + public static void printResult(int ball, int strike) { + if (ball == 0 && strike == 0) { + System.out.println("낫싱"); + } else { + if (ball != 0) { + System.out.print(ball + "볼 "); + } + if (strike != 0) { + System.out.print(strike + "스트라이크"); + } + System.out.println(); + } + } +} + +class NumberUtil { + public static List getInts(int num) { + List myList = new ArrayList<>(); + myList.add(num / 100); + myList.add((num / 10) % 10); + myList.add(num % 10); + return myList; + } +} \ No newline at end of file From e414e082425d6f1d2dd1d011920368ce32d5fd96 Mon Sep 17 00:00:00 2001 From: jinhwa-park <--global> Date: Mon, 6 May 2024 12:49:56 +0900 Subject: [PATCH 2/4] branch changed --- src/main/baseball/{baseballGame.java => baseball.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/baseball/{baseballGame.java => baseball.java} (100%) diff --git a/src/main/baseball/baseballGame.java b/src/main/baseball/baseball.java similarity index 100% rename from src/main/baseball/baseballGame.java rename to src/main/baseball/baseball.java From 4abe5f3686ed80a8dc652866bc9952e4fa56dbe0 Mon Sep 17 00:00:00 2001 From: jinhwa-park <--global> Date: Mon, 6 May 2024 12:56:42 +0900 Subject: [PATCH 3/4] test code --- src/test/baseballGameTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/test/baseballGameTest.java diff --git a/src/test/baseballGameTest.java b/src/test/baseballGameTest.java new file mode 100644 index 00000000..9337df1f --- /dev/null +++ b/src/test/baseballGameTest.java @@ -0,0 +1,14 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.io.ByteArrayInputStream; + +class baseballGameTest { + @Test + void testPlayGameWithInvalidInput() { + ByteArrayInputStream in = new ByteArrayInputStream("9999\n".getBytes()); + System.setIn(in); + + GameController gameController = new GameController(); + assertThrows(IllegalArgumentException.class, gameController::playGame); + } +} From 3178c83e6cd5ace13a801eea950af2adadfa60e2 Mon Sep 17 00:00:00 2001 From: jinhwa-park <--global> Date: Wed, 8 May 2024 20:10:06 +0900 Subject: [PATCH 4/4] last modified --- settings.gradle | 2 + src/main/baseball/baseball.java | 226 +++++++++--------- src/main/java/Application.java | 113 +++++++++ .../ApplicationTest.java} | 7 +- 4 files changed, 233 insertions(+), 115 deletions(-) create mode 100644 src/main/java/Application.java rename src/test/{baseballGameTest.java => java/ApplicationTest.java} (93%) diff --git a/settings.gradle b/settings.gradle index 11c425be..b6e65347 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,3 +2,5 @@ plugins { id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' } rootProject.name = 'java-baseball' +include 'baseballModule' + diff --git a/src/main/baseball/baseball.java b/src/main/baseball/baseball.java index 98018bea..ddfdfd26 100644 --- a/src/main/baseball/baseball.java +++ b/src/main/baseball/baseball.java @@ -1,113 +1,113 @@ -package baseball; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Random; -import java.util.Scanner; - -public class baseballGame { - public static void main(String[] args) { - System.out.println("숫자 야구 게임을 시작합니다."); - boolean newGame; - do { - newGame = new GameController().playGame(); - } while (newGame); - } -} - -class GameController { - public boolean playGame() { - List comList = RandomNumberGenerator.generateRandomNumberList(); - while (true) { - System.out.print("숫자를 입력해주세요: "); - int ball = 0; - int strike = 0; - int num = InputHandler.getInputNumber(); - checkValid(num); - List myList = NumberUtil.getInts(num); - for (int i = 0; i < 3; i++) { - if (!Objects.equals(myList.get(i), comList.get(i))) { - boolean contains = comList.contains(myList.get(i)); - if (contains) { - ball++; - } - } - if (Objects.equals(myList.get(i), comList.get(i))) { - strike++; - } - } - GameOutput.printResult(ball, strike); - if (strike == 3) { - System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); - return InputHandler.askForNewGame(); - } - } - } - - private static void checkValid(int num) { - if (num < 100 || num > 999) { - throw new IllegalArgumentException("입력 범위를 초과했습니다."); - } - } -} - -class RandomNumberGenerator { - public static List generateRandomNumberList() { - Random random = new Random(); - List comList = new ArrayList<>(); - while (comList.size() < 3) { - int randomNumber = random.nextInt(9) + 1; - if (!comList.contains(randomNumber)) { - comList.add(randomNumber); - } - } - return comList; - } -} - -class InputHandler { - public static int getInputNumber() { - Scanner scanner = new Scanner(System.in); - int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환 - return number; - } - - public static boolean askForNewGame() { - System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); - int value = getInputNumber(); - if (value == 1) { - return true; - } else if (value == 2) { - return false; - } else { - throw new IllegalArgumentException("올바른 입력이 아닙니다."); - } - } -} - -class GameOutput { - public static void printResult(int ball, int strike) { - if (ball == 0 && strike == 0) { - System.out.println("낫싱"); - } else { - if (ball != 0) { - System.out.print(ball + "볼 "); - } - if (strike != 0) { - System.out.print(strike + "스트라이크"); - } - System.out.println(); - } - } -} - -class NumberUtil { - public static List getInts(int num) { - List myList = new ArrayList<>(); - myList.add(num / 100); - myList.add((num / 10) % 10); - myList.add(num % 10); - return myList; - } -} \ No newline at end of file +//package baseball; +// +//import java.util.ArrayList; +//import java.util.List; +//import java.util.Objects; +//import java.util.Random; +//import java.util.Scanner; +// +//public class baseball { +// public static void main(String[] args) { +// System.out.println("숫자 야구 게임을 시작합니다."); +// boolean newGame; +// do { +// newGame = new GameController().playGame(); +// } while (newGame); +// } +//} +// +//class GameController { +// public boolean playGame() { +// List comList = RandomNumberGenerator.generateRandomNumberList(); +// while (true) { +// System.out.print("숫자를 입력해주세요: "); +// int ball = 0; +// int strike = 0; +// int num = InputHandler.getInputNumber(); +// checkValid(num); +// List myList = NumberUtil.getInts(num); +// for (int i = 0; i < 3; i++) { +// if (!Objects.equals(myList.get(i), comList.get(i))) { +// boolean contains = comList.contains(myList.get(i)); +// if (contains) { +// ball++; +// } +// } +// if (Objects.equals(myList.get(i), comList.get(i))) { +// strike++; +// } +// } +// GameOutput.printResult(ball, strike); +// if (strike == 3) { +// System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); +// return InputHandler.askForNewGame(); +// } +// } +// } +// +// private static void checkValid(int num) { +// if (num < 100 || num > 999) { +// throw new IllegalArgumentException("입력 범위를 초과했습니다."); +// } +// } +//} +// +//class RandomNumberGenerator { +// public static List generateRandomNumberList() { +// Random random = new Random(); +// List comList = new ArrayList<>(); +// while (comList.size() < 3) { +// int randomNumber = random.nextInt(9) + 1; +// if (!comList.contains(randomNumber)) { +// comList.add(randomNumber); +// } +// } +// return comList; +// } +//} +// +//class InputHandler { +// public static int getInputNumber() { +// Scanner scanner = new Scanner(System.in); +// int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환 +// return number; +// } +// +// public static boolean askForNewGame() { +// System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); +// int value = getInputNumber(); +// if (value == 1) { +// return true; +// } else if (value == 2) { +// return false; +// } else { +// throw new IllegalArgumentException("올바른 입력이 아닙니다."); +// } +// } +//} +// +//class GameOutput { +// public static void printResult(int ball, int strike) { +// if (ball == 0 && strike == 0) { +// System.out.println("낫싱"); +// } else { +// if (ball != 0) { +// System.out.print(ball + "볼 "); +// } +// if (strike != 0) { +// System.out.print(strike + "스트라이크"); +// } +// System.out.println(); +// } +// } +//} +// +//class NumberUtil { +// public static List getInts(int num) { +// List myList = new ArrayList<>(); +// myList.add(num / 100); +// myList.add((num / 10) % 10); +// myList.add(num % 10); +// return myList; +// } +//} \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..15b3125c --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,113 @@ +//package baseball; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Random; +import java.util.Scanner; + +public class Application { + public static void main(String[] args) { + System.out.println("숫자 야구 게임을 시작합니다."); + boolean newGame; + do { + newGame = new GameController().playGame(); + } while (newGame); + } +} + +class GameController { + public boolean playGame() { + List comList = RandomNumberGenerator.generateRandomNumberList(); + while (true) { + System.out.print("숫자를 입력해주세요: "); + int ball = 0; + int strike = 0; + int num = InputHandler.getInputNumber(); + checkValid(num); + List myList = NumberUtil.getInts(num); + for (int i = 0; i < 3; i++) { + if (!Objects.equals(myList.get(i), comList.get(i))) { + boolean contains = comList.contains(myList.get(i)); + if (contains) { + ball++; + } + } + if (Objects.equals(myList.get(i), comList.get(i))) { + strike++; + } + } + GameOutput.printResult(ball, strike); + if (strike == 3) { + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return InputHandler.askForNewGame(); + } + } + } + + private static void checkValid(int num) { + if (num < 100 || num > 999) { + throw new IllegalArgumentException("입력 범위를 초과했습니다."); + } + } +} + +class RandomNumberGenerator { + public static List generateRandomNumberList() { + Random random = new Random(); + List comList = new ArrayList<>(); + while (comList.size() < 3) { + int randomNumber = random.nextInt(9) + 1; + if (!comList.contains(randomNumber)) { + comList.add(randomNumber); + } + } + return comList; + } +} + +class InputHandler { + public static int getInputNumber() { + Scanner scanner = new Scanner(System.in); + int number = Integer.parseInt(scanner.nextLine()); // 사용자 입력을 받아 정수로 변환 + return number; + } + + public static boolean askForNewGame() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int value = getInputNumber(); + if (value == 1) { + return true; + } else if (value == 2) { + return false; + } else { + throw new IllegalArgumentException("올바른 입력이 아닙니다."); + } + } +} + +class GameOutput { + public static void printResult(int ball, int strike) { + if (ball == 0 && strike == 0) { + System.out.println("낫싱"); + } else { + if (ball != 0) { + System.out.print(ball + "볼 "); + } + if (strike != 0) { + System.out.print(strike + "스트라이크"); + } + System.out.println(); + } + } +} + +class NumberUtil { + public static List getInts(int num) { + List myList = new ArrayList<>(); + myList.add(num / 100); + myList.add((num / 10) % 10); + myList.add(num % 10); + return myList; + } +} \ No newline at end of file diff --git a/src/test/baseballGameTest.java b/src/test/java/ApplicationTest.java similarity index 93% rename from src/test/baseballGameTest.java rename to src/test/java/ApplicationTest.java index 9337df1f..c755c8f6 100644 --- a/src/test/baseballGameTest.java +++ b/src/test/java/ApplicationTest.java @@ -2,7 +2,8 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.ByteArrayInputStream; -class baseballGameTest { +class ApplicationTest { + @Test void testPlayGameWithInvalidInput() { ByteArrayInputStream in = new ByteArrayInputStream("9999\n".getBytes()); @@ -11,4 +12,6 @@ void testPlayGameWithInvalidInput() { GameController gameController = new GameController(); assertThrows(IllegalArgumentException.class, gameController::playGame); } -} + + +} \ No newline at end of file