Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
db044d1
docs(README.md): list functions
kuk6933 Jun 28, 2023
fe9df38
fix(build.gradle): change java version
kuk6933 Jun 28, 2023
2cfe679
feat(BaseballGame.java): make BaseballGame class and number methods
kuk6933 Jun 28, 2023
39cb370
feat(ExceptionHandler): make ExceptionHandler and methods to verify i…
kuk6933 Jun 28, 2023
7565849
feat(BaseballGame): game result checking methods
kuk6933 Jun 28, 2023
fda843a
feat(Random): make random class for generate random numbers
kuk6933 Jun 28, 2023
55d87f5
feat(BaseballGame): print game result
kuk6933 Jun 28, 2023
c60d71a
feat(BaseballGame): take a input and derive game result
kuk6933 Jun 28, 2023
e2a59d5
feat(BaseballGame): make flow managing method and method that is take…
kuk6933 Jun 28, 2023
7813451
feat(Application): start number baseball game
kuk6933 Jun 28, 2023
cbefa9c
fix: keep coding convention
kuk6933 Jun 28, 2023
2610b08
fix(Constant): make constant for restart or exit
kuk6933 Jun 29, 2023
761a4a3
fix(Random): fix return type and change constant to static variable
kuk6933 Jun 29, 2023
7ad0a30
fix(Validation): edit class name, change throw condition and use regex
kuk6933 Jun 29, 2023
1775f41
fix(ExceptionHandler): change class name ExceptionHandler -> Validation
kuk6933 Jun 29, 2023
19c33c2
fix(BaseballGame): make static variables, change variable type, logic…
kuk6933 Jun 29, 2023
5d2e098
fix(Constant): edit wrong value
kuk6933 Jun 30, 2023
5020974
rafactor(ExceptionMessage): use enum
kuk6933 Jun 30, 2023
77d324b
refactor(GameMessage): make game message(print) using enum
kuk6933 Jun 30, 2023
ef70136
refactor(Rule): make another class to manage game rule variables
kuk6933 Jun 30, 2023
38e1fff
refactor(Validation): seperate methods
kuk6933 Jun 30, 2023
29a08d9
refactor(Random): BaseballGame.~~ -> Rule.~~
kuk6933 Jun 30, 2023
0b9de01
refactor(BaseballGame): seperate methods, use enum and make some chan…
kuk6933 Jun 30, 2023
554f4dc
refactor: delete space
kuk6933 Jun 30, 2023
6374258
refactor: make print using enum
kuk6933 Jun 30, 2023
75726f0
refactor: edit final variable naming
kuk6933 Jul 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 9 additions & 129 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,129 +1,9 @@
# 과제 - 숫자 야구 게임

## 🔍 진행 방식

- 과제는 **기능 요구 사항, 프로그래밍 요구 사항, 과제 진행 요구 사항** 세 가지로 구성되어 있다.
- 세 개의 요구 사항을 만족하기 위해 노력한다. 특히 기능을 구현하기 전에 기능 목록을 만들고, 기능 단위로 커밋 하는 방식으로 진행한다.
- 기능 요구 사항에 기재되지 않은 내용은 스스로 판단하여 구현한다.

---

## 📈 과제 진행 및 제출 방법

- 과제는 [java-baseball](https://github.com/LandvibeDev/java-baseball) 저장소를 Fork/Clone해 시작한다.
- **기능을 구현하기 전에 java-baseball-precourse/README.md 파일에 구현할 기능 목록을 정리**해 추가한다.
- **Git의 커밋 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위**로 추가한다.
- [AngularJS Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 참고해 commit log를 남긴다.
- 과제 진행 및 제출 방법은 [우아한코스 과제 제출 문서](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 를 참고한다.
- base repository를 `LandvibeDev/java-baseball`로 지정해서 PR 생성하면됨

<br>

### 테스트 실행 가이드

- 터미널에서 `java -version`을 실행하여 Java 버전이 14인지 확인한다. 또는 Eclipse 또는 IntelliJ IDEA와 같은 IDE에서 Java 14로 실행되는지 확인한다.
- 터미널에서 Mac 또는 Linux 사용자의 경우 `./gradlew clean test` 명령을 실행 하고,
Windows 사용자의 경우 `gradlew.bat clean test` 명령을 실행할 때 동작 하는지 만 확인(테스트는 실패).

---

## 🚀 기능 요구사항

기본적으로 1부터 9까지 서로 다른 수로 이루어진 3자리의 수를 맞추는 게임이다.

- 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 포볼 또는 낫싱이란 힌트를 얻고, 그 힌트를 이용해서 먼저 상대방(컴퓨터)의 수를 맞추면 승리한다.
- 예) 상대방(컴퓨터)의 수가 425일 때
- 123을 제시한 경우 : 1스트라이크
- 456을 제시한 경우 : 1볼 1스트라이크
- 789를 제시한 경우 : 낫싱
- 위 숫자 야구 게임에서 상대방의 역할을 컴퓨터가 한다. 컴퓨터는 1에서 9까지 서로 다른 임의의 수 3개를 선택한다. 게임 플레이어는 컴퓨터가 생각하고 있는 3개의 숫자를 입력하고, 컴퓨터는 입력한 숫자에 대한 결과를 출력한다.
- 이 같은 과정을 반복해 컴퓨터가 선택한 3개의 숫자를 모두 맞히면 게임이 종료된다.
- 게임을 종료한 후 게임을 다시 시작하거나 완전히 종료할 수 있다.
- 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.
- 아래의 프로그래밍 실행 결과 예시와 동일하게 입력과 출력이 이루어져야 한다.

<br>

## ✍🏻 입출력 요구사항

### ⌨️ 입력

- 3자리의 수
- 게임이 끝난 경우 재시작/종료를 구분하는 1과 2 중 하나의 수

### 🖥 출력

- 입력한 수에 대한 결과를 볼, 스트라이크 개수로 표시

```
1볼 1스트라이크
```

- 하나도 없는 경우

```
낫싱
```

- 3개의 숫자를 모두 맞힐 경우

```
3스트라이크
3개의 숫자를 모두 맞히셨습니다! 게임 종료
```

### 💻 실행 결과 예시

```
숫자를 입력해주세요 : 123
1볼 1스트라이크
숫자를 입력해주세요 : 145
1볼
숫자를 입력해주세요 : 671
2볼
숫자를 입력해주세요 : 216
1스트라이크
숫자를 입력해주세요 : 713
3스트라이크
3개의 숫자를 모두 맞히셨습니다! 게임 종료
게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.
1
숫자를 입력해주세요 : 123
1볼
```

<br>

---

## 🎱 프로그래밍 요구사항
-
- JDK 14 버전에서 실행 가능해야 한다.
- 프로그램을 실행하는 시작점은 `Application`의 `main()`이다.
- `build.gradle` 파일을 변경할 수 없고, 외부 라이브러리를 사용하지 않는다.
- [Java 코드 컨벤션](https://naver.github.io/hackday-conventions-java) 가이드를 준수하며 프로그래밍한다.
- 프로그램 종료 시 `System.exit()`를 호출하지 않는다.
- 프로그램 구현이 완료되면 `ApplicationTest`의 모든 테스트가 성공해야 한다.
- 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 이름을 수정하거나 이동하지 않는다.
- indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용한다.
- 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다.
- 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메소드)를 분리하면 된다.
- 3항 연산자를 쓰지 않는다.
- 함수(또는 메소드)가 한 가지 일만 하도록 최대한 작게 만들어라.
- JUnit 5와 AssertJ를 이용하여 본인이 정리한 기능 목록이 정상 동작함을 테스트 코드로 확인한다.
- 함수(또는 메서드)의 길이가 15라인을 넘어가지 않도록 구현한다.
- 함수(또는 메서드)가 한 가지 일만 잘 하도록 구현한다.
- else 예약어를 쓰지 않는다.
- 힌트: if 조건절에서 값을 return하는 방식으로 구현하면 else를 사용하지 않아도 된다.
- else를 쓰지 말라고 하니 switch/case로 구현하는 경우가 있는데 switch/case도 허용하지 않는다.
- Java Enum을 적용한다.
- 도메인 로직에 단위 테스트를 구현해야 한다. 단, UI(System.out, System.in, Scanner) 로직은 제외한다.
- 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 분리해 구현한다.

### 라이브러리 - Randoms, Console

- JDK에서 기본 제공하는 Random, Scanner API 대신 [`camp.nextstep.edu.missionutils`](https://github.com/woowacourse-projects/mission-utils)에서 제공하는 `Randoms`, `Console` API를 활용해 구현해야 한다.
- Random 값 추출은 `camp.nextstep.edu.missionutils.Randoms`의 `pickNumberInRange()`를 활용한다.
- 사용자가 입력하는 값은 `camp.nextstep.edu.missionutils.Console`의 `readLine()`을 활용한다.
- 프로그램 구현을 완료했을 때 `src/test/java` 디렉터리의 `ApplicationTest`에 있는 모든 테스트 케이스가 성공해야 한다. **테스트가 실패할 경우 0점 처리한다.**
#숫자야구

##기능
- 1~9에서 세개의 중복되지 않은 무작위 숫자 뽑기
- 사용자로부터 1~9에서 세개의 중복되지 않은 숫자 입력 받기
- 올바른 입력인지 체크
- 입력에 대한 결과 도출
- 결과 출력
- 게임 재시작 여부 받기
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(14)
languageVersion = JavaLanguageVersion.of(11)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public class Application {
public static void main(String[] args) {
//TODO: 숫자 야구 게임 구현
BaseballGame baseballGame = new BaseballGame();
baseballGame.start();
}
}
97 changes: 97 additions & 0 deletions src/main/java/baseball/BaseballGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package baseball;

import java.util.ArrayList;

import camp.nextstep.edu.missionutils.Console;

public class BaseballGame {
static int start = 1;
static int end = 9;
static int number = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant는 별도의 클래스로 분리시키는 것이 좋습니다.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public class Rule {
static int start = 1;
static int end = 9;
static int number = 3;
}

하나의 클래스가 요렇게만 쓰여도 괜찮을까요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spring의 Http Status Code 관련 클래스를 참고하는 것도 도움될 것 같네요 :) 변하지 않는 값이면 final로 선언해주는 것도 괜찮고요


int strike = 0;
int ball = 0;

ArrayList<Integer> randomNumbers = new ArrayList<>();
ArrayList<Integer> inputNumbers = new ArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

외부에서 해당 값들을 변경하지 못하도록 개선해봅시다.
초기화는 생성자에서 처리해주는 것이 나아보입니다.


public void start() {
getRandomNumbers();
playGame();
restartOrExit();
}

private void getRandomNumbers() {
Random random = new Random();
randomNumbers = random.makeRandomNumbers();
}

private void getInputNumbers() {
inputNumbers.clear();
System.out.println("숫자를 입력해주세요 :");
String inputString = Console.readLine();
Validation validation = new Validation(inputString);
validation.handleException();
for (int i = 0; i < number; i++) {
inputNumbers.add(inputString.charAt(i) - '0');
}
}

private void playGame() {
while (true) {
getInputNumbers();
checkStrikeAndBall();
getResult();
if (isSuccess()) {
break;
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

성공과 실패여부에 대한 처리로직 모두를 코드에 표현해주는 것이 나아보입니다.

if (!isSuccess()){
    continue;
}
break;


private void restartOrExit() {
String input = Console.readLine();
if (input.equals(Constant.RESTART.value)) {
start();
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input의 경우의 수를 나누어서 각 경우에 따른 로직이 메소드에 담기도록 리팩토링 해보세요.


private void checkStrikeAndBall() {
strike = 0;
ball = 0;
for (int i = 0; i < number; i++) {
if (randomNumbers.get(i) == inputNumbers.get(i)) {
strike++;
inputNumbers.set(i, -1);
}
for (int j = 0; j < number; j++) {
if (randomNumbers.get(i) == inputNumbers.get(j)) {
ball++;
}
}
}
}

private boolean isSuccess() {
if (strike == number) {
System.out.println(number + "개의 숫자를 모두 맞히셨습니다! 게임 종료");
System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

출력 자체를 메소드로 분리시켜보세요.

return true;
}
return false;
}

private void getResult() {
if (strike == 0 && ball == 0) {
System.out.println("낫싱");
}
if (ball > 0) {
System.out.print(ball + "볼 ");
if (strike == 0) {
System.out.println();
}
}
if (strike > 0) {
System.out.println(strike + "스트라이크");
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다음을 고려하여 리팩토링 해보세요.

  • 출력 기능을 다른 메소드로 분리시키기
  • early return등을 활용하여 불필요한 if문 참조 줄이기
  • if(strike ==0)이 필요하지 않도록 개선해보기

}
12 changes: 12 additions & 0 deletions src/main/java/baseball/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package baseball;

public enum Constant {
RESTART("1"),
EXIT("0");

public String value;

Constant(String value) {
this.value = value;
}
}
24 changes: 24 additions & 0 deletions src/main/java/baseball/Random.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package baseball;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import camp.nextstep.edu.missionutils.Randoms;

public class Random {

public ArrayList<Integer> makeRandomNumbers() {
Set<Integer> numberSet = new HashSet<>();
ArrayList<Integer> randomNumbers = new ArrayList<>();
while (numberSet.size() < BaseballGame.number) {
int randomNumber = Randoms.pickNumberInRange(BaseballGame.start, BaseballGame.end);
if (numberSet.contains(randomNumber)) {
continue;
}
randomNumbers.add(randomNumber);
numberSet.add(randomNumber);
}
return randomNumbers;
}
}
39 changes: 39 additions & 0 deletions src/main/java/baseball/Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package baseball;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class Validation {

String inputString;

Validation(String inputString) {
this.inputString = inputString;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputString을 메소드의 파라미터로 받는 것이 더 나아보입니다. 새로운 String을 검증할 때마다 객체를 생성해줘야하는 번거로움이 있네요.


public void handleException() {
lengthCheck();
wrongInputCheck();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleException()이라는 메소드명이 부자연스럽습니다. 입력에 대한 validation을 처리하는 메소드로 판단되므로 더 자연스러운 메소드명을 지어봅시다. 결국 Validation이라는 클래스를 부자연스럽게 설계해서 발생한 문제라고 판단됩니다.


private void lengthCheck() {
if (inputString.length() != BaseballGame.number) {
throw new IllegalArgumentException("정해진 개수의 숫자만 입력해주세요");
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception Message도 별도의 클래스에 분리할 수 있겠네요.


private void wrongInputCheck() {
Set<Integer> set = new HashSet<>();
String pattern = "^[" + BaseballGame.start + "-" + BaseballGame.end +"]*$";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런건 별도의 메소드로 분리해도 괜찮을 것 같습니다. 다만 정규표현식을 사용하는 것이 좋은 코드인지는 고민할 필요가 있어보이네요.

for (int i = 0; i < BaseballGame.number; i++) {
if (!Pattern.matches(pattern, inputString)) {
throw new IllegalArgumentException("정해진 범위 안의 숫자만 입력해주세요");
}
set.add(inputString.charAt(i) - '0');
}
if (set.size() != BaseballGame.number) {
throw new IllegalArgumentException();
}
}
}