Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[충남대 BE_송민주] 미션 제출합니다 #156

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# java-baseball-precourse
# java-baseball-precourse

## 구현할 기능 목록

### utils
- 숫자 야구 기능 (1~9 수로 이뤄진 3자리 숫자 야구 생성)

### Service
- 게임 결과 판단
- 게임 승리 여부 판단

### Controller
- 게임 결과 출력

### IO(Input and Output)
- 숫자 입력 : 정상적인 숫자 검증 (숫자 이외에는 다 예외처리 )
- 숫자가 아닐경우 예외 발생
- 게임 시작 / 종료/ 결과 메세지 출력
7 changes: 7 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller.GameController;
public class Application {
public static void main(String[] args) {
GameController gameController = new GameController();
gameController.run();
}
}
44 changes: 44 additions & 0 deletions src/main/java/Controller/GameController.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions src/main/java/IO/Input.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
20 changes: 20 additions & 0 deletions src/main/java/IO/Output.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/utils/makeRandomNumber.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/utils/resultCalculation.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
46 changes: 46 additions & 0 deletions src/test/java/IO/InputTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
38 changes: 38 additions & 0 deletions src/test/java/IO/OutputTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
43 changes: 43 additions & 0 deletions src/test/java/utils/makeRandomNumberTest.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}

}

}
43 changes: 43 additions & 0 deletions src/test/java/utils/resultCalculationTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}