-
Notifications
You must be signed in to change notification settings - Fork 5
이동민 PR#2 코드리뷰 요청 #3
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
Open
pkalsh
wants to merge
42
commits into
biforest:pkalsh
Choose a base branch
from
pkalsh:pkalsh
base: pkalsh
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
a5b8549
docs(guide): 구현 기능 목록 작성
pkalsh 1649c59
feat: 게임 전체 골격 설계
pkalsh 34cbe18
feat: 프린터 구현
pkalsh 85863a6
refactor: 프린터 리팩토링
pkalsh d5f1041
feat: 입력기 구현
pkalsh f5b9d91
feat: 사용자 입력 숫자 검증 메소드 구현
pkalsh 32974a8
feat: 올바르지 않은 입력에 대한 경고 메시지 구현
pkalsh e35e434
refactor: try-catch 문 추출
pkalsh d5eeeaf
test: 입력 숫자 검증 테스트 케이스
pkalsh 02bd97d
feat: 컴퓨터의 세자리 난수 생성 메서드 구현
pkalsh fef00e1
refactor: 난수 생성 메서드 static 메서드화
pkalsh 7612ab7
test: 난수 생성기 테스트 작성
pkalsh 2b11336
feat: 결과 클래스 구현
pkalsh 6f9cf8f
feat: 사용자 숫자와 난수를 비교하는 메서드 구현
pkalsh 58835ef
fix: digits 접근 로직 변경
pkalsh d252b82
test: 비교하여 결과 설정하는 모든 테스트 케이스 구현
pkalsh 8ed095f
fix: 참조형 매개변수의 널값 검사 코드 추가
pkalsh 52121be
fix: 게임내 숫자 검증 메소드 변경
pkalsh 6dde9e5
fix: Scanner에 대한 예외 처리 추가
pkalsh c0f94c9
fix: root 클래스에 NullPointerException 에러 처리
pkalsh 098f663
fix: 게임 시작/종료 조건 변경
pkalsh 40d4fb7
fix: 출력 양식에 맞춰 출력문 수정
pkalsh 0ca42fc
fix: 입력 오류시 한번 더 입력을 받아야 정상적으로 작동하는 버그 수정
pkalsh f657de3
fix: 사용자 입력 받는 도중 비정상 입력 발생시 무한 루프를 도는 버그 수정
pkalsh eac391e
Update src/main/java/baseball/GameManager.java
pkalsh 38297df
style: 중괄호+공백삽입 컨벤션 적용
pkalsh 02816f9
style: [제어문 키워드와 여는 소괄호 사이에 공백 삽입] 컨벤션 적용
pkalsh e3a49c3
style: 명시적 변수 초기화 불필요
pkalsh 2437266
style: 상수의 위치 변경
pkalsh f47285c
refactor: 게임 내 숫자를 생성하고 입력받는 메소드에서 GameNumber 객체를 생성하는 방법 변경
pkalsh d1a6dd7
refactor: InputReceiver가 게임을 다시 시작하는가를 판단하는 기능을 분리
pkalsh 6f894de
fix: 게임 결과가 초기화되지 않아 게임을 재시작해도 승리하는 버그 수정
pkalsh cc08041
refactor: 로직을 드러내는 메소드명이 아닌 서비스 친화적인 메소드명으로 변경
pkalsh 98e2b64
refactor: NPE throw 코드 모두 삭제
pkalsh 35fa32e
refactor: 시드 번호와 입력 번호 비교 로직 책임 재분배 및 쪼개기
pkalsh 9aabea9
refactor: InputReceiver의 receiveUserNumber 메소드 반환 타입 변경
pkalsh 1652c2b
refactor: 상수를 private static final로 선언
pkalsh 46e34c8
style: 필드와 메소드, 메소드와 메소드 사이에 빈 라인 추가
pkalsh 0037ff6
style: 필드와 메소드 사이에 빈 라인 추가
pkalsh 6f3ca99
style: 할당 연산자 앞뒤에는 공백 삽입
pkalsh 58578cf
style: 스트림 사용시 가독성을 위해 개행
pkalsh 41f6bc6
refactor: for문의 반복 변수에는 임시 변수명 사용 가능
pkalsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.generator.RandomNumberGenerator; | ||
| import baseball.receiver.ConsoleInputReceiver; | ||
| import baseball.receiver.InputReceiver; | ||
| import baseball.number.GameNumber; | ||
| import baseball.printer.ConsolePrinter; | ||
| import baseball.printer.Printer; | ||
| import baseball.result.Result; | ||
|
|
||
| import java.util.InputMismatchException; | ||
| import java.util.Objects; | ||
| import java.util.Scanner; | ||
|
|
||
| public class GameManager { | ||
| private static final int START_GAME = 1; | ||
|
|
||
| private final Printer printer; | ||
| private final InputReceiver receiver; | ||
| private GameNumber seedNumber; | ||
| private GameNumber userNumber; | ||
|
|
||
| public GameManager(Scanner scanner) { | ||
| Objects.requireNonNull(scanner); | ||
| printer = new ConsolePrinter(); | ||
| receiver = new ConsoleInputReceiver(scanner); | ||
| } | ||
|
|
||
| public void run() { | ||
| printer.greet(); | ||
| printer.askStartGame(); | ||
|
|
||
| boolean continueGame = isContinueGame(); | ||
|
|
||
| while (continueGame) { | ||
| Result result = new Result(); | ||
| seedNumber = RandomNumberGenerator.generate(); | ||
| startGame(result); | ||
| printer.noticeWin(); | ||
| printer.askStartGame(); | ||
| continueGame = isContinueGame(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isContinueGame() { | ||
| int select = receiver.receiveContinueGameSelect(); | ||
| try { | ||
| throwIfInputIsNotOneOrTwo(select); | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| return isContinueGame(); | ||
| } | ||
| return (select == START_GAME); | ||
| } | ||
|
|
||
| private void throwIfInputIsNotOneOrTwo(int select) { | ||
| if (select != 1 && select != 2) { | ||
| throw new IllegalArgumentException("올바르지 않은 입력입니다.\n시작은 1, 종료는 2 입니다."); | ||
| } | ||
| } | ||
|
|
||
| private void startGame(Result result) { | ||
| while (!result.isWin()) { | ||
| boolean isValidInput = receiveUserInput(); | ||
|
|
||
| if (!isValidInput) { | ||
| continue; | ||
| } | ||
| result.calculateResult(seedNumber, userNumber); | ||
| printer.printResult(result); | ||
| } | ||
| } | ||
|
|
||
| private boolean receiveUserInput() { | ||
| try { | ||
| printer.requestUserNumber(); | ||
| String userInput = receiver.receiveUserNumber(); | ||
| userNumber = GameNumber.from(userInput); | ||
| } catch (IllegalArgumentException | InputMismatchException e) { | ||
| receiver.dealWithExceptionalInput(); | ||
| printer.warnInvalidInput(); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/baseball/generator/RandomNumberGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package baseball.generator; | ||
|
|
||
| import baseball.number.GameNumber; | ||
| import utils.RandomUtils; | ||
|
|
||
| public class RandomNumberGenerator { | ||
|
|
||
| public static GameNumber generate() { | ||
| int newSeed = 0; | ||
|
|
||
| for (int exponent = 0; exponent < 3; exponent++) { | ||
| int randomDigit = RandomUtils.nextInt(1, 9); | ||
| newSeed += (Math.pow(10.0, exponent) * randomDigit); | ||
| } | ||
|
|
||
| if (!GameNumber.validateNumber(newSeed)){ | ||
| return generate(); | ||
| } | ||
|
|
||
| return GameNumber.from(String.valueOf(newSeed)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package baseball.number; | ||
|
|
||
| public class GameNumber { | ||
| private String value; | ||
|
|
||
| private GameNumber(String number) { | ||
| value = number; | ||
| } | ||
|
|
||
| public static GameNumber from(String number) { | ||
| if (!validateNumber(Integer.parseInt(number))) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| return new GameNumber(number); | ||
| } | ||
|
|
||
| public static boolean validateNumber(int number) { | ||
pkalsh marked this conversation as resolved.
Show resolved
Hide resolved
Member
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. 중복 또는 범위를 체크하는 과정이 생각보다 복잡한것 같아요. |
||
| if (number <= 122 || number >=988) { | ||
| return false; | ||
| } | ||
|
|
||
| boolean[] digits = new boolean[10]; | ||
| String strNumber = Integer.toString(number); | ||
|
|
||
| for (char digit : strNumber.toCharArray()) { | ||
|
|
||
| int integerDigit = Integer.parseInt(String.valueOf(digit)); | ||
| if (integerDigit == 0) { | ||
| return false; | ||
| } | ||
| if (!digits[integerDigit]) { | ||
| digits[integerDigit] = true; | ||
| continue; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package baseball.printer; | ||
|
|
||
| import baseball.result.Result; | ||
|
|
||
| public class ConsolePrinter implements Printer { | ||
|
|
||
| @Override | ||
| public void greet() { | ||
| System.out.println("숫자 야구 게임입니다.\n" + | ||
| "게임을 시작하시겠습니까?"); | ||
| } | ||
|
|
||
| @Override | ||
| public void askStartGame() { | ||
| System.out.println("시작하려면 1, 종료하려면 2를 입력하세요."); | ||
| } | ||
|
|
||
| @Override | ||
| public void requestUserNumber() { | ||
| System.out.print("숫자를 입렧해주세요 : "); | ||
| } | ||
|
|
||
| @Override | ||
| public void warnInvalidInput() { | ||
| System.out.println("올바르지 않은 입력입니다.\n" + | ||
| "서로 다른 숫자 세 자리를 입력해주세요."); | ||
| } | ||
|
|
||
| @Override | ||
| public void printResult(Result result) { | ||
| if (result.isNothing()) { | ||
| System.out.println("낫싱"); | ||
| return ; | ||
| } | ||
|
|
||
| if (result.getBallCount() != 0) { | ||
| System.out.print(result.getBallCount() + "볼 "); | ||
| } | ||
|
|
||
| if (result.getStrikeCount() != 0) { | ||
| System.out.print(result.getStrikeCount() + "스트라이크"); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| @Override | ||
| public void noticeWin() { | ||
| System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package baseball.printer; | ||
|
|
||
| import baseball.result.Result; | ||
|
|
||
| public interface Printer { | ||
| void greet(); | ||
|
|
||
| void askStartGame(); | ||
|
|
||
| void requestUserNumber(); | ||
|
|
||
| void warnInvalidInput(); | ||
|
|
||
| void printResult(Result result); | ||
|
|
||
| void noticeWin(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package baseball.receiver; | ||
|
|
||
| import baseball.number.GameNumber; | ||
|
|
||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class ConsoleInputReceiver implements InputReceiver { | ||
|
|
||
| private final Scanner scanner; | ||
|
|
||
| public ConsoleInputReceiver(Scanner scanner) { | ||
| this.scanner = scanner; | ||
| } | ||
|
|
||
| @Override | ||
| public int receiveContinueGameSelect() { | ||
| int select; | ||
| try { | ||
| select = scanner.nextInt(); | ||
| } catch (InputMismatchException e) { | ||
| cleanScannerBuffer(); | ||
| System.out.println("잘못된 입력입니다. 숫자만 입력해주세요."); | ||
| return receiveContinueGameSelect(); | ||
| } | ||
|
|
||
| return select; | ||
| } | ||
|
|
||
| @Override | ||
| public String receiveUserNumber() throws IllegalArgumentException, InputMismatchException { | ||
| int number = scanner.nextInt(); | ||
| if (!GameNumber.validateNumber(number)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| return Integer.toString(number); | ||
| } | ||
|
|
||
| @Override | ||
| public void dealWithExceptionalInput() { | ||
| cleanScannerBuffer(); | ||
| } | ||
|
|
||
| private void cleanScannerBuffer() { | ||
| if (scanner.hasNextLine()) { | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package baseball.receiver; | ||
|
|
||
| public interface InputReceiver { | ||
|
|
||
| int receiveContinueGameSelect(); | ||
|
|
||
| String receiveUserNumber(); | ||
|
|
||
| void dealWithExceptionalInput(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.