-
Notifications
You must be signed in to change notification settings - Fork 7
[숫자 야구 게임] 오형석 과제 제출합니다. #4
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
base: main
Are you sure you want to change the base?
Changes from 16 commits
db044d1
fe9df38
2cfe679
39cb370
7565849
fda843a
55d87f5
c60d71a
e2a59d5
7813451
cbefa9c
2610b08
761a4a3
7ad0a30
1775f41
19c33c2
5d2e098
5020974
77d324b
ef70136
38e1fff
29a08d9
0b9de01
554f4dc
6374258
75726f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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에서 세개의 중복되지 않은 숫자 입력 받기 | ||
| - 올바른 입력인지 체크 | ||
| - 입력에 대한 결과 도출 | ||
| - 결과 출력 | ||
| - 게임 재시작 여부 받기 |
| 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; | ||
|
|
||
| int strike = 0; | ||
| int ball = 0; | ||
|
|
||
| ArrayList<Integer> randomNumbers = new ArrayList<>(); | ||
| ArrayList<Integer> inputNumbers = new ArrayList<>(); | ||
|
||
|
|
||
| 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; | ||
| } | ||
| } | ||
| } | ||
|
||
|
|
||
| private void restartOrExit() { | ||
| String input = Console.readLine(); | ||
| if (input.equals(Constant.RESTART.value)) { | ||
| start(); | ||
| } | ||
| } | ||
|
||
|
|
||
| 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를 입력하세요"); | ||
|
||
| 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 + "스트라이크"); | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음을 고려하여 리팩토링 해보세요.
|
||
| } | ||
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inputString을 메소드의 파라미터로 받는 것이 더 나아보입니다. 새로운 String을 검증할 때마다 객체를 생성해줘야하는 번거로움이 있네요. |
||
|
|
||
| public void handleException() { | ||
| lengthCheck(); | ||
| wrongInputCheck(); | ||
| } | ||
|
||
|
|
||
| private void lengthCheck() { | ||
| if (inputString.length() != BaseballGame.number) { | ||
| throw new IllegalArgumentException("정해진 개수의 숫자만 입력해주세요"); | ||
| } | ||
| } | ||
|
||
|
|
||
| private void wrongInputCheck() { | ||
| Set<Integer> set = new HashSet<>(); | ||
| String pattern = "^[" + BaseballGame.start + "-" + BaseballGame.end +"]*$"; | ||
|
||
| 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(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constant는 별도의 클래스로 분리시키는 것이 좋습니다.
There was a problem hiding this comment.
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;
}
하나의 클래스가 요렇게만 쓰여도 괜찮을까요?
There was a problem hiding this comment.
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로 선언해주는 것도 괜찮고요