Skip to content
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
wants to merge 8 commits into
base: mjin1220
Choose a base branch
from
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,57 @@ pobi honux crong jk
| |-----| |
|-----| |-----|
```

## 3단계 - 사다리(게임 실행)

### 기능 요구사항

- 사다리 실행 결과를 출력해야 한다.
- 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다.

### 프로그래밍 요구사항

- 자바 8의 스트림과 람다를 적용해 프로그래밍한다.
- 규칙 6: 모든 엔티티를 작게 유지한다.
- 규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다.

#### 실행 결과

- 위 요구사항에 따라 4명의 사람을 위한 5개 높이 사다리를 만들 경우, 프로그램을 실행한 결과는 다음과 같다.

```text
참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)
pobi,honux,crong,jk

실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)
꽝,5000,꽝,3000

최대 사다리 높이는 몇 개인가요?
5

사다리 결과

pobi honux crong jk
|-----| |-----|
| |-----| |
|-----| | |
| |-----| |
|-----| |-----|
꽝 5000 꽝 3000

결과를 보고 싶은 사람은?
pobi

실행 결과

결과를 보고 싶은 사람은?
all

실행 결과
pobi : 꽝
honux : 3000
crong : 꽝
jk : 5000
```

39 changes: 32 additions & 7 deletions src/main/java/ladder/LadderMain.java
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));
}
}
}
42 changes: 0 additions & 42 deletions src/main/java/ladder/domain/BridgeLine.java

This file was deleted.

64 changes: 0 additions & 64 deletions src/main/java/ladder/domain/BridgeLines.java

This file was deleted.

80 changes: 67 additions & 13 deletions src/main/java/ladder/domain/Ladder.java
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));
}
Comment on lines +35 to +37

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를 사용해야 합니다.

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에게 맡겨볼 수 있지 않을까요?


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이 될 수 없습니다.");
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/ladder/domain/LadderFactory.java
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

Choose a reason for hiding this comment

The 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);
}
}
4 changes: 1 addition & 3 deletions src/main/java/ladder/domain/LadderHeight.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ladder.domain;

import ladder.exception.InvalidLadderHeightException;

import java.util.Objects;

public class LadderHeight {
Expand All @@ -16,7 +14,7 @@ public LadderHeight(final int value) {

private void validateOrThrow(final int value) {
if (value <= 0) {
throw new InvalidLadderHeightException();
throw new IllegalArgumentException("유효하지 않은 사다리 높이입니다.");
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/ladder/domain/LadderResult.java
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);
}
}
Loading