diff --git a/README.md b/README.md index 8d7e8aee..e279a8ac 100644 --- a/README.md +++ b/README.md @@ -1 +1,18 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse + +## 구현할 기능 목록 + +### utils +- 숫자 야구 기능 (1~9 수로 이뤄진 3자리 숫자 야구 생성) + +### Service +- 게임 결과 판단 +- 게임 승리 여부 판단 + +### Controller +- 게임 결과 출력 + +### IO(Input and Output) +- 숫자 입력 : 정상적인 숫자 검증 (숫자 이외에는 다 예외처리 ) + - 숫자가 아닐경우 예외 발생 +- 게임 시작 / 종료/ 결과 메세지 출력 \ 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..bbb7fddd --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,7 @@ +import Controller.GameController; +public class Application { + public static void main(String[] args) { + GameController gameController = new GameController(); + gameController.run(); + } +} diff --git a/src/main/java/Controller/GameController.java b/src/main/java/Controller/GameController.java new file mode 100644 index 00000000..66bd1878 --- /dev/null +++ b/src/main/java/Controller/GameController.java @@ -0,0 +1,44 @@ +package Controller; + +import IO.*; +import utils.*; + +public class GameController { + public void run(){ + while(true){ + firstGameStart(); + if (!wantGame()){ + break; + } + } + } + + private void firstGameStart(){ + // random 숫자 생성 + int[] gameNumber = makeRandomNumber.getRandomNumbers(); + int[] userNumber; + while(true){ + try { + userNumber = Input.inputGameNumber(); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + return; + } + + int strikeCount = resultCalculation.countStrikes(gameNumber, userNumber); + int ballCount = resultCalculation.countBall(gameNumber, userNumber); + Output.printGameResult(strikeCount, ballCount); + + if (strikeCount == 3) { + System.out.println("정답 입니다! 게임 종료"); + break; + } + } + } + + private boolean wantGame() { + int want = Input.inputStartGame(); + + return want == 1; + } +} diff --git a/src/main/java/IO/Input.java b/src/main/java/IO/Input.java new file mode 100644 index 00000000..70d2da3a --- /dev/null +++ b/src/main/java/IO/Input.java @@ -0,0 +1,41 @@ +package IO; + +import java.util.*; + +public class Input { + // 유저에게서 정보를 입력 받는 역할 + private static Scanner sc = new Scanner(System.in); + + public static int inputStartGame() { + int input; + while(true){ + System.out.println("게임 시작은 1, 게임 종료는 2: "); + input = Integer.parseInt(sc.nextLine()); + if (input != 1 && input != 2){ + // 유효한 값 이외에 다른 게 들어오면 + throw new IllegalArgumentException("잘못된 입력 방식입니다."); + } + return input; + } + + } + + public static int[] inputGameNumber() { + System.out.print("숫자를 입력해 주세요 : "); + String input = sc.nextLine(); + + if (!input.matches("[0-9]{3}")) { + // 1-9 숫자 3개가 들어오지 않았다면 -> IllegalArgumentException 발생 + throw new IllegalArgumentException("잘못된 입력 방식입니다."); + } + int[] numbers = new int[3]; + for (int i = 0; i < 3; i++) { + char digitChar = input.charAt(i); + int digit = Character.getNumericValue(digitChar); + numbers[i] = digit; + } + + return numbers; + } + +} diff --git a/src/main/java/IO/Output.java b/src/main/java/IO/Output.java new file mode 100644 index 00000000..fae98807 --- /dev/null +++ b/src/main/java/IO/Output.java @@ -0,0 +1,20 @@ +package IO; + +public class Output { + public static void printGameResult(int strike, int ball) { + if (strike == 0 && ball == 0) { + System.out.println("낫싱"); + } else { + if (strike > 0) { + System.out.print(strike + "스트라이크"); + if (ball > 0) { + System.out.print(" "); + } + } + if (ball > 0) { + System.out.print(ball + "볼"); + } + System.out.println(); + } + } +} diff --git a/src/main/java/utils/makeRandomNumber.java b/src/main/java/utils/makeRandomNumber.java new file mode 100644 index 00000000..2b97f93c --- /dev/null +++ b/src/main/java/utils/makeRandomNumber.java @@ -0,0 +1,24 @@ +package utils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + +public class makeRandomNumber { + public static int[] getRandomNumbers() { + List numberList = new ArrayList<>(); + for (int i = 1; i <= 9; i++) { + numberList.add(i); + } + + Collections.shuffle(numberList); + + int[] result = new int[3]; + for (int i = 0; i < 3; i++) { + result[i] = numberList.get(i); + } + + return result; + } +} diff --git a/src/main/java/utils/resultCalculation.java b/src/main/java/utils/resultCalculation.java new file mode 100644 index 00000000..a1979c66 --- /dev/null +++ b/src/main/java/utils/resultCalculation.java @@ -0,0 +1,28 @@ +package utils; + +public class resultCalculation { + public static int countStrikes(int[] gameNumber, int[] userNumber) { + int strikeCount = 0; + for (int i = 0; i < 3; i++) { + if (gameNumber[i] == userNumber[i]) { + strikeCount++; + } + } + + return strikeCount; + } + + public static int countBall(int[] gameNumber, int[] userNumber) { + int ballCount = 0; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (gameNumber[i] == userNumber[j] && i != j) { + ballCount++; + } + } + } + + return ballCount; + } + +} \ No newline at end of file diff --git a/src/test/java/IO/InputTest.java b/src/test/java/IO/InputTest.java new file mode 100644 index 00000000..f28aa8d9 --- /dev/null +++ b/src/test/java/IO/InputTest.java @@ -0,0 +1,46 @@ +package IO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.*; + +class InputTest { + private InputStream systemIn; + + @BeforeEach + void setUp() { + systemIn = System.in; + } + + @AfterEach + void tearDown() { + System.setIn(systemIn); + } + + @Test + void inputStartGame() { + String input = "2\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + int choice = Input.inputStartGame(); + + assertEquals(2, choice); + } + + @Test + void inputGameNumber() { + String input = "456\n"; + InputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + int[] guess = Input.inputGameNumber(); + + assertArrayEquals(new int[]{4, 5, 6}, guess); + } +} \ No newline at end of file diff --git a/src/test/java/IO/OutputTest.java b/src/test/java/IO/OutputTest.java new file mode 100644 index 00000000..85a094ef --- /dev/null +++ b/src/test/java/IO/OutputTest.java @@ -0,0 +1,38 @@ +package IO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.*; + +class OutputTest { + private InputStream systemIn; + + @BeforeEach + void setUp() { + systemIn = System.in; + } + + @AfterEach + void tearDown() { + System.setIn(systemIn); + } + + @Test + @DisplayName("낫싱") + void printGameResult() { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + System.setOut(new PrintStream(output)); + + Output.printGameResult(0, 0); + + assertEquals("낫싱\n", output.toString()); + } + +} \ No newline at end of file diff --git a/src/test/java/utils/makeRandomNumberTest.java b/src/test/java/utils/makeRandomNumberTest.java new file mode 100644 index 00000000..5453a9c6 --- /dev/null +++ b/src/test/java/utils/makeRandomNumberTest.java @@ -0,0 +1,43 @@ +package utils; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.*; + +class makeRandomNumberTest { + private InputStream systemIn; + + @BeforeEach + void setUp() { + systemIn = System.in; + } + + @AfterEach + void tearDown() { + System.setIn(systemIn); + } + + @Test + @DisplayName(" 랜덤 숫자 생성 ") + void getRandomNumbers() { + int[] numbers = makeRandomNumber.getRandomNumbers(); + assertNotNull(numbers); + assertEquals(3, numbers.length); + for (int number : numbers) { + assertTrue(number >= 1 && number <= 9); + } + + for (int i = 0; i < numbers.length - 1; i++) { + for (int j = i + 1; j < numbers.length; j++) { + assertNotEquals(numbers[i], numbers[j]); + } + } + + } + +} \ No newline at end of file diff --git a/src/test/java/utils/resultCalculationTest.java b/src/test/java/utils/resultCalculationTest.java new file mode 100644 index 00000000..fff2f0d9 --- /dev/null +++ b/src/test/java/utils/resultCalculationTest.java @@ -0,0 +1,43 @@ +package utils; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.*; + +class resultCalculationTest { + private InputStream systemIn; + + @BeforeEach + void setUp() { + systemIn = System.in; + } + + @AfterEach + void tearDown() { + System.setIn(systemIn); + } + + @Test + @DisplayName("스트라이크가 1개 이상") + void countStrikes() { + int[] game = {1, 2, 3}; + int[] user = {1, 5, 3}; + int result = resultCalculation.countStrikes(game, user); + assertEquals(2, result); + } + + + @Test + @DisplayName("볼이 2개 이상") + void countBall() { + int[] game = {1, 2, 3}; + int[] user = {3, 5, 1}; + int result = resultCalculation.countBall(game, user); + assertEquals(2, result); + } +} \ No newline at end of file