-
Notifications
You must be signed in to change notification settings - Fork 740
3주차 - Step3 사다리(게임 실행) 리뷰 요청드립니다. #207
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
Changes from all commits
680d774
e361f56
daa0bf8
9a7128f
af9ca67
2ef101a
6047e63
f53137a
cc0def7
d459ab6
f4cec19
4b01e7c
3b0ebe1
2c6b1ac
ae7057d
8365d36
a0f0ec7
a06e462
2fece83
2176f90
e0e3115
cf6667f
0cafc8f
3babbd1
86a3441
7586d14
f345856
828993f
a3ebeed
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,19 +1,29 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.domain.Height; | ||
| import ladder.domain.Ladder; | ||
| import ladder.domain.Players; | ||
| import ladder.domain.*; | ||
| import ladder.view.InputView; | ||
| import ladder.view.OutputView; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Application { | ||
|
|
||
| public static void main(String[] args) { | ||
| Players players = Players.of(InputView.askPlayers()); | ||
| Prizes prizes = Prizes.from(InputView.askPrizes(), players.numberOfPlayers()); | ||
| Height height = Height.from(InputView.askHeight()); | ||
|
|
||
| Ladder ladder = Ladder.from(players, height); | ||
| OutputView.drawLadder(players, ladder, prizes); | ||
|
|
||
| GameResult gameResult = GameResult.of(players, ladder, prizes); | ||
| String wantedPlayer = InputView.askWhichResultWant(); | ||
|
|
||
| OutputView.printResult(players, ladder); | ||
| if ("all".equals(wantedPlayer)) { | ||
|
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. 매직 넘버란 코드 안에 작성된 구체적인 숫자 혹은 문자열을 말합니다. 그리고 매직 넘버의 단점은 아래와 같습니다.
그런 매직 넘버는 의미를 나타낼 수 있는 상수로 치환하여 코드의 가독성을 높여 보는 것은 어떨까요? |
||
| OutputView.printAllResult(gameResult); | ||
| return; | ||
| } | ||
| OutputView.printSingleResult(gameResult.findResult(wantedPlayer)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package ladder.domain; | ||
|
|
||
| public class Bar { | ||
|
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. "
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. 질문이 있습니다..!! 이 부분은 아직 제대로 이해가 안되서 바로 Bar 객체를 뜯어고치기보다 저의 얕은 시야로는 Bar에 방향을 표현하는 Direction을 가지게 하는 것이 Bar는 Point 2개를 사용하는 방법과는 조금 다르게 Bar들을 배열로 생각하고 Point 2개를 사용하면 사다리를 타서 플레이어의 위치를 변화시킨다는 핵심 로직이 Position travel(Position position) {
if (bars.get(position.getPosition()).isExist()) {
return position.moveToRight(); // 사용자와 같은 위치에 bar가 있으면 사용자 위치 1 증가
}
if (position.isMovableToLeft()
&& bars.get(position.getLeftPosition()).isExist()) {
return position.moveToLeft(); // 사용자의 왼쪽에 bar가 있으면 사용자 위치 1 감소
}
return position; // 위의 경우가 아니면 위치 유지(패스)
}당연히 문제가 있기 때문에 Bar 객체가 방향을 가지도록 조언을 주셨을 것 같은데요! (수업시간에 강사님께서 Point 2개로 구현하시는 것을 배우긴했지만.. 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. 질문에 대한 답변은 Slack을 통해 남겼어요. 👍 |
||
|
|
||
| private final boolean bar; | ||
|
|
||
| private Bar(boolean bar) { | ||
| this.bar = bar; | ||
| } | ||
|
|
||
| public static Bar from(boolean doesBarExist) { | ||
| return new Bar(doesBarExist); | ||
| } | ||
|
|
||
| public boolean isExist() { | ||
| return bar; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package ladder.domain; | ||
|
|
||
| @FunctionalInterface | ||
| public interface BarGenerator { | ||
|
|
||
| boolean generateBar(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package ladder.domain; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class GameResult { | ||
| private static final String ALERT_FOR_INVALID_PLAYER_NAME = "입력하신 플레이어는 존재하지 않습니다."; | ||
|
|
||
| private final Map<Player, Prize> resultMap; | ||
|
|
||
| private GameResult(Map<Player, Prize> resultMap) { | ||
| this.resultMap = new HashMap<>(resultMap); | ||
| } | ||
|
|
||
| public static GameResult of(Players players, Ladder ladder, Prizes prizes) { | ||
| return new GameResult(playGame(players, ladder, prizes)); | ||
| } | ||
|
|
||
| private static Map<Player, Prize> playGame(Players players, Ladder ladder, Prizes prizes) { | ||
| Map<Player, Prize> resultMap = new HashMap<>(); | ||
| int numberOfPlayers = players.numberOfPlayers(); | ||
|
|
||
| for (int i = 0; i < numberOfPlayers; i++) { | ||
| Player player = players.getPlayers().get(i); | ||
|
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.
|
||
| Position finalPosition = ladder.goThroughLinesFrom(Position.from(i)); | ||
| Prize prize = prizes.getPrizes().get(finalPosition.getPosition()); | ||
|
|
||
| resultMap.put(player, prize); | ||
| } | ||
| return resultMap; | ||
| } | ||
|
|
||
| public String findResult(String wantedPlayer) { | ||
| Player playerWanted = resultMap.keySet().stream() | ||
| .filter(player -> player.isSame(wantedPlayer)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException(ALERT_FOR_INVALID_PLAYER_NAME)); | ||
|
|
||
| return resultMap.get(playerWanted).getPrize(); | ||
| } | ||
|
|
||
| public Map<Player, Prize> getResultMap() { | ||
| return Collections.unmodifiableMap(resultMap); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,29 +4,40 @@ | |
| import java.util.List; | ||
|
|
||
| class LineMaker { | ||
| private static final int NUMBER_OF_FIRST_AND_LAST_BAR = 2; | ||
|
|
||
| private List<Boolean> randomBars = new ArrayList<>(); | ||
| private BarGeneratorImpl barGenerator = new BarGeneratorImpl(); | ||
| private List<Bar> randomBars; | ||
| private RandomBarGenerator barGenerator = new RandomBarGenerator(); | ||
|
|
||
| List<Boolean> generateBars(int numberOfPlayers) { | ||
| randomBars.add(barGenerator.generateBar()); | ||
| for (int i = 1; i < numberOfPlayers - 1; i++) { | ||
| boolean previousBarExist = randomBars.get(i - 1); | ||
| addNextBar(previousBarExist); | ||
| } | ||
| addBlankBar(); | ||
| List<Bar> generateBars(int numberOfPlayers) { | ||
| this.randomBars = new ArrayList<>(); | ||
| generateFirstBar(); | ||
| generateMiddleBars(numberOfPlayers); | ||
| generateLastBar(); | ||
| return randomBars; | ||
| } | ||
|
|
||
| private void addNextBar(boolean previousBarExist) { | ||
| if (previousBarExist) { | ||
| addBlankBar(); | ||
| private void generateFirstBar() { | ||
| randomBars.add(Bar.from(barGenerator.generateBar())); | ||
| } | ||
|
|
||
| private void generateMiddleBars(int numberOfPlayers) { | ||
| int spaceForMiddleBars = numberOfPlayers - NUMBER_OF_FIRST_AND_LAST_BAR; | ||
| for (int i = 0; i < spaceForMiddleBars; i++) { | ||
| Bar previousBar = randomBars.get(i); | ||
| addNextBar(previousBar); | ||
| } | ||
| } | ||
|
|
||
| private void addNextBar(Bar previousBar) { | ||
| if (previousBar.isExist()) { | ||
| generateLastBar(); | ||
| return; | ||
| } | ||
| randomBars.add(barGenerator.generateBar()); | ||
| randomBars.add(Bar.from(barGenerator.generateBar())); | ||
| } | ||
|
|
||
| private void addBlankBar() { | ||
| randomBars.add(false); | ||
| private void generateLastBar() { | ||
|
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. 메서드 이름은 메서드의 정확한 동작을 구체적으로 나타내는 것이 좋겠죠?
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. 마지막 Bar는 모두 비어있는 Bar라서 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. 네, 맞습니다. 👍 |
||
| randomBars.add(Bar.from(Boolean.FALSE)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package ladder.domain; | ||
|
|
||
| public class Position { | ||
|
|
||
| private static final int MOVE_ONE_STEP_TO_RIGHT = 1; | ||
| private static final int MOVE_ONE_STEP_TO_LEFT = -1; | ||
| static final String ALERT_UNMOVABLE_TO_LEFT = "더 이상 왼쪽으로 이동할 수 없습니다."; | ||
| private int position; | ||
|
|
||
| private Position(int currentPosition) { | ||
| this.position = currentPosition; | ||
| } | ||
|
|
||
| public static Position from(int currentPosition) { | ||
| return new Position(currentPosition); | ||
| } | ||
|
|
||
| public boolean isSameWith(int targetPosition) { | ||
| return position == targetPosition; | ||
| } | ||
|
|
||
| Position moveToRight() { | ||
| return from(this.position + MOVE_ONE_STEP_TO_RIGHT); | ||
| } | ||
|
|
||
| Position moveToLeft() { | ||
| validationMovableToLeft(); | ||
| return from(this.position + MOVE_ONE_STEP_TO_LEFT); | ||
| } | ||
|
|
||
| int getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| int getLeftPosition() { | ||
| validationMovableToLeft(); | ||
| return this.position + MOVE_ONE_STEP_TO_LEFT; | ||
| } | ||
|
|
||
| private void validationMovableToLeft() { | ||
| if (unmovableToLeft()) { | ||
| throw new IllegalArgumentException(ALERT_UNMOVABLE_TO_LEFT); | ||
| } | ||
| } | ||
|
|
||
| private boolean unmovableToLeft() { | ||
| return position <= 0; | ||
| } | ||
| } |
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.
Players와Prizes를 가진GameInfo클래스를 만들어 보면 어떨까요?