-
Notifications
You must be signed in to change notification settings - Fork 0
숫자 야구 게임 다시 구현 #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
base: main
Are you sure you want to change the base?
Changes from all commits
accfdb1
2a35348
6b22f1a
5766a55
a26e471
44d00e9
335fb93
39b0766
9badefb
af028d7
5f0eeca
f745b51
dd69532
e2b1f48
6e8e5da
390776e
e1aff36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ### 기능 목록 정리 | ||
| - [x] 컴퓨터의 숫자를 랜덤으로 가져온다. | ||
| - [x] 사용자의 숫자를 입력한다. | ||
| - [x] 입력이 3자리가 아니면 예외가 발생한다. | ||
| - [x] 입력이 숫자 형식이 아니면 예외가 발생한다. | ||
| - [x] 빈 값이 입력되면 예외가 발생한다. | ||
| - [x] 컴퓨터의 숫자와 사용자의 숫자를 비교한다. | ||
| - [x] 결과를 출력한다. | ||
| - [x] 같은 수가 같은 자리에 있으면 스트라이크, 다른 자리에 있으면 볼, 같은 수가 전혀 없으면 낫싱 | ||
| - [x] 3스트라이크가 나왔을 시, 성공 문구를 출력한다. | ||
| - [x] 재시작 여부를 입력한다. | ||
| - [x] 1또는 2가 입력되지 않으면 예외가 발생한다. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| package baseball; | ||
|
|
||
| import baseball.controller.Game; | ||
| import baseball.controller.BaseballGame; | ||
| import baseball.domain.RandomNumberGenerator; | ||
| import baseball.service.BaseballService; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| Game game = new Game(); | ||
| game.play(); | ||
| } | ||
| public static void main(String[] args) { | ||
| BaseballGame baseballGame = new BaseballGame(new RandomNumberGenerator(), | ||
| new BaseballService()); | ||
| baseballGame.start(); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package baseball.controller; | ||
|
|
||
| import baseball.domain.CompareResult; | ||
| import baseball.domain.Computer; | ||
| import baseball.domain.Player; | ||
| import baseball.domain.RandomNumberGenerator; | ||
| import baseball.domain.Retry; | ||
| import baseball.service.BaseballService; | ||
| import baseball.view.InputView; | ||
| import baseball.view.OutputView; | ||
| import java.util.Arrays; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class BaseballGame { | ||
|
|
||
| private static final String ALL_STRIKE = "3스트라이크"; | ||
|
|
||
| private final RandomNumberGenerator randomNumberGenerator; | ||
| private final BaseballService baseballService; | ||
|
|
||
| public BaseballGame(RandomNumberGenerator randomNumberGenerator, | ||
| BaseballService baseballService) { | ||
| this.randomNumberGenerator = randomNumberGenerator; | ||
| this.baseballService = baseballService; | ||
| } | ||
|
|
||
| public void start() { | ||
| OutputView.printStart(); | ||
| playGame(); | ||
| } | ||
|
|
||
| private void playGame() { | ||
| while (true) { | ||
| Computer computerNumber = generateComputerNumber(); | ||
| calculateNumber(computerNumber); | ||
| Retry retry = selectRetryCommand(); | ||
| if (isEnd(retry)) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void calculateNumber(Computer computerNumber) { | ||
| while (true) { | ||
| Player playerNumber = generatePlayerNumber(); | ||
| CompareResult compareResult = baseballService.compare(computerNumber, playerNumber); | ||
| OutputView.printResult(compareResult); | ||
| if (isResultAllStrike(compareResult)) { | ||
| OutputView.endMessage(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean isResultAllStrike(CompareResult compareResult) { | ||
| return compareResult.toString().equals(ALL_STRIKE); | ||
| } | ||
|
|
||
| private boolean isEnd(Retry retry) { | ||
| return retry == Retry.STOP; | ||
| } | ||
|
|
||
| private Computer generateComputerNumber() { | ||
| return randomNumberGenerator.generate(); | ||
| } | ||
|
|
||
| private Player generatePlayerNumber() { | ||
| OutputView.printMessage(); | ||
| String playerNumber = InputView.readPlayerNumber(); | ||
| return new Player(Arrays.stream(playerNumber.split("")) | ||
| .map(Integer::parseInt) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| private Retry selectRetryCommand() { | ||
| OutputView.restartMessage(); | ||
| String retryCommand = InputView.readRestartCommand(); | ||
| return Arrays.stream(Retry.values()) | ||
| .filter(retry -> retry.getCommand() == Integer.parseInt(retryCommand)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException("1 또는 2를 입력해야 합니다.")); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package baseball.domain; | ||
|
|
||
| public class CompareResult { | ||
|
|
||
| private static final String NOTHING_TEXT = "낫싱"; | ||
| private static final String BALL_TEXT = "볼"; | ||
| private static final String STRIKE_TEXT = "스트라이크"; | ||
|
|
||
| private final CountOfBalls count; | ||
|
|
||
| public CompareResult(CountOfBalls count) { | ||
| this.count = count; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| int ballCount = count.getBallCount(); | ||
| int strikeCount = count.getStrikeCount(); | ||
| ballCount -= strikeCount; | ||
|
|
||
| if (count.isNothing()) { | ||
| return NOTHING_TEXT; | ||
| } | ||
| if (count.isStrikeCountZero()) { | ||
| return String.format("%d%s", ballCount, BALL_TEXT); | ||
| } | ||
| if (count.isBallCountZero()) { | ||
| return String.format("%d%s", strikeCount, STRIKE_TEXT); | ||
| } | ||
| if (count.isThreeStrike()) { | ||
| return String.format("%d%s", strikeCount, STRIKE_TEXT); | ||
| } | ||
| return String.format("%d%s %d%s", ballCount, BALL_TEXT, strikeCount, STRIKE_TEXT); | ||
| } | ||
|
|
||
| public CountOfBalls getCountOfBalls() { | ||
| return count; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package baseball.domain; | ||
|
|
||
| public class CountOfBalls { | ||
|
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. Strike 카운트랑 Ball 카운트를 담당해주는 클래스라면 클래스 명을 CountOfBalls 말고 Strike까지 같이 담아주는 클래스명으로 지어보는 것이 어떨까요?
Owner
Author
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. 제가 생각한 건 Strike와 ball의 카운트를 세는 Balls 였습니다. |
||
|
|
||
| private int strikeCount; | ||
| private int ballCount; | ||
|
|
||
| public int getStrikeCount() { | ||
| return strikeCount; | ||
| } | ||
|
|
||
| public int getBallCount() { | ||
| return ballCount; | ||
| } | ||
|
|
||
| public void increaseBallCount() { | ||
| ballCount++; | ||
| } | ||
|
|
||
| public void increaseStrikeCount() { | ||
| strikeCount++; | ||
| } | ||
|
|
||
| public boolean isStrikeCountZero() { | ||
| return strikeCount == 0; | ||
| } | ||
|
|
||
| public boolean isBallCountZero() { | ||
| return ballCount == 0; | ||
| } | ||
|
|
||
| public boolean isThreeStrike() { | ||
| return strikeCount == 3; | ||
| } | ||
|
|
||
| public boolean isNothing() { | ||
| return isBallCountZero() && isStrikeCountZero(); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package baseball.domain; | ||
|
|
||
| public interface NumberGenerator { | ||
|
|
||
| Computer generate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package baseball.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Player { | ||
|
|
||
| private final List<Integer> playerNumber; | ||
|
|
||
| public Player(List<Integer> playerNumber) { | ||
| validatePlayerNumber(playerNumber); | ||
| this.playerNumber = playerNumber; | ||
| } | ||
|
|
||
| private void validatePlayerNumber(List<Integer> playerNumber) { | ||
| validateSize(playerNumber); | ||
| } | ||
|
|
||
| private void validateSize(List<Integer> playerNumber) { | ||
| if(playerNumber.size() != 3){ | ||
| throw new IllegalArgumentException("3자리를 입력 해야 합니다."); | ||
| } | ||
| } | ||
|
Comment on lines
+14
to
+22
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. validatePlayerNumber 메서드 내부에 직접 로직을 작성하시지않고 validateSize로 메서드 추출한 이유가 있을까요?
Owner
Author
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. 예외가 2개 이상 작성하려다가 지우는걸 깜빡한거 같아요 ! 감사합니다 |
||
|
|
||
| public int getPlayerNumberByIndex(int index) { | ||
| return playerNumber.get(index); | ||
| } | ||
| } | ||
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.
Computer 도메인이 있는데 RandomNumberGenerator는 computer 작업이니 computerService로 두는 것에 대해서는 어떻게 생각하세요?
실제 컴퓨터가 제공하는 기능으로 보고 구분하는것은 어떤가 싶어서 코멘트 남깁니다!
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.
그 생각은 못했는데, Computer에 작업이니 Computer가 담당하게 하는게 나을거 같네요 감사합니다 😁