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/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 new file mode 100644 index 00000000..ddfdfd26 --- /dev/null +++ b/src/main/baseball/baseball.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 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/java/ApplicationTest.java b/src/test/java/ApplicationTest.java new file mode 100644 index 00000000..c755c8f6 --- /dev/null +++ b/src/test/java/ApplicationTest.java @@ -0,0 +1,17 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.io.ByteArrayInputStream; + +class ApplicationTest { + + @Test + void testPlayGameWithInvalidInput() { + ByteArrayInputStream in = new ByteArrayInputStream("9999\n".getBytes()); + System.setIn(in); + + GameController gameController = new GameController(); + assertThrows(IllegalArgumentException.class, gameController::playGame); + } + + +} \ No newline at end of file