-
Notifications
You must be signed in to change notification settings - Fork 709
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
[사다리타기] 3단계 - 사다리(게임 실행) #1605
Open
mjin1220
wants to merge
8
commits into
next-step:mjin1220
Choose a base branch
from
mjin1220:step3
base: mjin1220
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
[사다리타기] 3단계 - 사다리(게임 실행) #1605
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f738f30
feat: InputView는 원시값을 넘기는 책임만 가지도록 변경
mjin1220 0ae2c77
feat: 이펙티브 자바 72에 따라 표준 예외를 사용하도록 적용
mjin1220 237bd4e
refactor: 자바 패키지 이동
mjin1220 917f659
feat: 수직 라인에서 수평 라인으로 변경
mjin1220 563d83e
feat: Ladder 생성 유효성 검사 추가
mjin1220 b15eece
docs: 3단계 요구사항 업데이트
mjin1220 92fd74d
feat: Ladder 멤버변수 중 "LadderHeight" 제거
mjin1220 dd87d38
feat: LadderResult 구현
mjin1220 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 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 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 |
---|---|---|
@@ -1,17 +1,42 @@ | ||
package ladder; | ||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.LadderHeight; | ||
import ladder.domain.Participants; | ||
import ladder.strategy.BridgeLinesRandomStrategy; | ||
import ladder.domain.*; | ||
import ladder.view.InputView; | ||
import ladder.view.ResultView; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class LadderMain { | ||
|
||
private static final String LADDER_RESULT_CONFIRM_COMMAND_ALL = "all"; | ||
private static final String LADDER_RESULT_CONFIRM_COMMAND_BREAK = "-1"; | ||
|
||
public static void main(String[] args) { | ||
final Participants participants = InputView.inputParticipants(); | ||
final LadderHeight ladderHeight = InputView.inputLadderHeight(); | ||
ResultView.print(new Ladder(participants, ladderHeight, new BridgeLinesRandomStrategy())); | ||
final Participants participants = new Participants(InputView.inputParticipantNames() | ||
.stream() | ||
.map(Participant::new) | ||
.collect(Collectors.toList())); | ||
final LadderResults ladderResults = new LadderResults(InputView.inputLadderResults() | ||
.stream() | ||
.map(LadderResult::new) | ||
.collect(Collectors.toList())); | ||
final LadderHeight ladderHeight = new LadderHeight(InputView.inputLadderHeight()); | ||
final Ladder ladder = LadderFactory.create(participants, ladderHeight); | ||
ResultView.printLadder(ladder, ladderResults); | ||
|
||
while (true) { | ||
final String targetParticipantName = InputView.inputTargetLadderParticipantName(); | ||
if (LADDER_RESULT_CONFIRM_COMMAND_ALL.equalsIgnoreCase(targetParticipantName)) { | ||
ResultView.printAllLadderResults(ladder, ladderResults); | ||
continue; | ||
} | ||
|
||
if (LADDER_RESULT_CONFIRM_COMMAND_BREAK.equalsIgnoreCase(targetParticipantName)) { | ||
ResultView.printConfirmationEndMessage(); | ||
break; | ||
} | ||
|
||
ResultView.printLadderResult(ladder, ladderResults, new Participant(targetParticipantName)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,33 +1,87 @@ | ||
package ladder.domain; | ||
|
||
import ladder.strategy.BridgeLinesStrategy; | ||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
|
||
public class Ladder { | ||
|
||
private final Participants participants; | ||
private final LadderHeight height; | ||
private final BridgeLines bridgeLines; | ||
private final Lines lines; | ||
|
||
public Ladder(final Participants participants, final Lines lines) { | ||
validateOrThrow(participants, lines); | ||
|
||
public Ladder(final Participants participants, final LadderHeight height, final BridgeLinesStrategy strategy) { | ||
this.participants = participants; | ||
this.height = height; | ||
this.bridgeLines = strategy.create(participants.size() - 1, height); | ||
this.lines = lines; | ||
} | ||
|
||
public Participants getParticipants() { | ||
return new Participants(participants); | ||
} | ||
|
||
public LadderHeight getHeight() { | ||
return this.height; | ||
public int getHeight() { | ||
return this.lines.height(); | ||
} | ||
|
||
public Lines getLines() { | ||
return this.lines; | ||
} | ||
|
||
public Line getLine(final int index) { | ||
return this.lines.getLines() | ||
.get(index); | ||
} | ||
|
||
public int getLastVerticalLineIndex(final Participant participant) { | ||
return this.getLastVerticalLineIndex(this.participants.indexOf(participant)); | ||
} | ||
|
||
private int getLastVerticalLineIndex(final int firstVerticalLineIndex) { | ||
return IntStream.range(0, this.lines.height()) | ||
.reduce(firstVerticalLineIndex, (curVerticalLineIndex, curLineHeight) -> { | ||
if (canMoveLeft(curVerticalLineIndex, curLineHeight)) { | ||
return curVerticalLineIndex - 1; | ||
} | ||
|
||
if (canMoveRight(curVerticalLineIndex, curLineHeight)) { | ||
return curVerticalLineIndex + 1; | ||
} | ||
|
||
return curVerticalLineIndex; | ||
}); | ||
} | ||
|
||
public BridgeLines getBridgeLines() { | ||
return bridgeLines; | ||
private boolean canMoveLeft(final int verticalLineIndex, final int curLineHeight) { | ||
if (isFirstVerticalLineIndex(verticalLineIndex)) { | ||
return false; | ||
} | ||
|
||
return this.lines.getLine(curLineHeight) | ||
.isConnected(verticalLineIndex - 1); | ||
} | ||
|
||
private boolean canMoveRight(final int verticalLineIndex, final int curLineHeight) { | ||
if (isLastVerticalLineIndex(verticalLineIndex)) { | ||
return false; | ||
} | ||
|
||
return this.lines.getLine(curLineHeight) | ||
.isConnected(verticalLineIndex); | ||
} | ||
|
||
|
||
private boolean isFirstVerticalLineIndex(final int verticalLineIndex) { | ||
return verticalLineIndex == 0; | ||
} | ||
|
||
|
||
private boolean isLastVerticalLineIndex(final int verticalLineIndex) { | ||
return verticalLineIndex == this.lines.width(); | ||
} | ||
|
||
public BridgeLine getBridgeLine(final int index) { | ||
return this.bridgeLines.getBridgeLines() | ||
.get(index); | ||
private void validateOrThrow(final Participants participants, final Lines lines) { | ||
if (Objects.isNull(participants) || Objects.isNull(lines)) { | ||
throw new IllegalArgumentException("참가자나 사다리 라인은 null이 될 수 없습니다."); | ||
} | ||
} | ||
} |
This file contains 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 ladder.domain; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LadderFactory { | ||
|
||
private LadderFactory() { | ||
} | ||
Comment on lines
+8
to
+9
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. 👍 |
||
|
||
public static Ladder create(final Participants participants, final LadderHeight height) { | ||
Lines lines = new Lines(IntStream.range(0, height.getValue()) | ||
.mapToObj(index -> LineFactory.create(participants.size())) | ||
.collect(Collectors.toList())); | ||
return new Ladder(participants, lines); | ||
} | ||
} |
This file contains 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 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,37 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class LadderResult { | ||
|
||
private final String value; | ||
|
||
public LadderResult(final String value) { | ||
validateOrThrow(value); | ||
|
||
this.value = value; | ||
} | ||
|
||
private void validateOrThrow(final String value) { | ||
if (Objects.isNull(value) || value.isBlank()) { | ||
throw new IllegalArgumentException("사다리 결과는 빈 문자열일 수 없습니다."); | ||
} | ||
} | ||
|
||
public String getValue() { | ||
return this.value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
LadderResult that = (LadderResult) o; | ||
return Objects.equals(value, that.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} |
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.
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.
이 메서드는 view에서 호출 되고 있는데 view가 console이 아니라 web으로 변경된다면 호출 될 수 있을까요?
참여자의 결과를 찾는건 비즈니스 로직이라고 생각해요.
이 부분은 view에서 호출 하는 것이 아닌 controller에서 도메인 모델에게 시키고 결과를 view로 전달해야 하지 않을까요?
view에서는 getter만 사용해서 출력만 담당하는 것이 어떨까요?
일반적으로 getter는 필드 그대로를 꺼내는 것을 의미합니다.
과정중에 getter 사용을 하지 말라는 것은 비즈니스를 풀어내는 도메인 모델끼리 getter를 사용하지 말라는 것이고 view에서는 getter를 사용해야 합니다.
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.
ladder에 많은 비즈니스 로직이 있는 것 같아요. 어느 정도는 line에게 위임할 수 있지 않을까요?
Line#isConnected
를 제외하면 아무런 역할이 없는 것 같아서요.실제로 Line 필드에
List<Boolean> connections
가 있어서요. index 1은 해당 line을 통과하면 어떤 index가 되는 지를 line에게 맡겨볼 수 있지 않을까요?