Skip to content

Commit 021c504

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent f5393b1 commit 021c504

File tree

11 files changed

+236
-30
lines changed

11 files changed

+236
-30
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111

1212
- 사다리의 높이와 넓이를 입력받아 생성한다.
1313
- 넓이와 높이는 2 이상 24 이하 여야한다.
14-
- 클래스는 3개 이상의 인스턴스 변수 가질 수 없다.
15-
- 모든 엔티티 작게 유지해야 한다.
14+
- 사다리의 시작 지점과 도착 지점을 출력한다.
1615

1716
## 구현 기능
1817

1918
### 사다리
2019

21-
- 여러 개의 라인을 가진다.
22-
- 전체 사다리 구조를 나타내며, 사다리가 완성될 때까지 여러 번 라인을 생성해 재시도한다.
20+
- 여러 개의 라인을 가지며 전체 사다리 구조를 나타낸다.
21+
- 라인을 반복 생성하며, 인접 라인 간 연결이 최소 한 번 이상 존재하는 유효한 사다리를 생성한다.
2322
- 완성된 사다리를 반환하며, 유효하지 않으면 예외를 발생시킨다.
23+
- 시작 인덱스를 받아 각 라인의 연결을 따라 최종 위치를 계산한다.
2424

2525
### 라인 생성기
2626

27-
- 포인트 생성기를 사용해 한 줄의 포인트들을 생성한다.
27+
- 포인트 간 연결 여부를 생성하고, 라인을 구성한다.
2828
- 인접 포인트 간 연결이 겹치지 않도록 제어한다.
2929

3030
### 라인
3131

3232
- 한 줄을 구성하는 포인트들의 리스트를 가진다.
33-
- 각 포인트의 오른쪽 연결 상태를 관리한다.
33+
- 각 포인트의 오른쪽 연결 상태를 관리하고, 이동 방향을 계산한다.
3434

3535
### 포인트 생성기
3636

@@ -45,16 +45,22 @@
4545

4646
```text
4747
사다리의 넓이는 몇 인가요?
48-
4
48+
5
4949
5050
사다리의 높이는 몇 인가요?
5151
5
52-
실행결과
5352
54-
|-----| |-----|
55-
|-----| |-----|
56-
| |-----| |
57-
| | | |
58-
| | |-----|
53+
실행결과
5954
55+
|-----| |-----| |
56+
| | | |-----|
57+
|-----| | | |
58+
| |-----| |-----|
59+
|-----| |-----| |
60+
61+
0 -> 1
62+
1 -> 3
63+
2 -> 2
64+
3 -> 0
65+
4 -> 4
6066
```

src/main/java/controller/LadderController.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import domain.Height;
44
import domain.Width;
55
import domain.dto.RequestLadder;
6+
import domain.dto.ResponseLadder;
7+
import domain.dto.ResponseLadderResult;
68
import domain.ladder.Ladder;
79
import domain.ladder.LadderFactory;
810
import strategy.LineGenerator;
@@ -23,7 +25,7 @@ public void play() {
2325
LadderFactory factory = new LadderFactory();
2426
Ladder ladder = factory.draw(width, height, lineGenerator);
2527

26-
drawLadder(ladder);
28+
drawLadder(ladder, width);
2729
}
2830

2931
private RequestLadder inputLadderSettings() {
@@ -37,8 +39,13 @@ private LineGenerator createLineGenerator() {
3739
return new RandomLineGenerator(pointGenerator);
3840
}
3941

40-
private void drawLadder(final Ladder ladder) {
42+
private void drawLadder(final Ladder ladder, final Width width) {
4143
OutputView.printLadderResultTitle();
42-
OutputView.drawLadder(ladder);
44+
45+
ResponseLadder responseLadder = ResponseLadder.from(ladder);
46+
OutputView.drawLadder(responseLadder);
47+
48+
ResponseLadderResult responseResult = ResponseLadderResult.of(ladder, width);
49+
OutputView.printLadderResult(responseResult);
4350
}
4451
}

src/main/java/domain/Direction.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package domain;
2+
3+
public enum Direction {
4+
LEFT(-1),
5+
DOWN(0),
6+
RIGHT(1)
7+
;
8+
9+
private final int offset;
10+
11+
Direction(final int offset) {
12+
this.offset = offset;
13+
}
14+
15+
public int moveFrom(final int index) {
16+
return index + offset;
17+
}
18+
}

src/main/java/domain/Line.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ public boolean hasConnectionAt(final int index) {
1818
return points.get(index).right();
1919
}
2020

21+
public Direction directionAt(final int index) {
22+
if (canMoveRight(index)) {
23+
return Direction.RIGHT;
24+
}
25+
if (canMoveLeft(index)) {
26+
return Direction.LEFT;
27+
}
28+
return Direction.DOWN;
29+
}
30+
31+
private boolean canMoveRight(final int index) {
32+
return index < points.size() - 1 && points.get(index).right();
33+
}
34+
35+
private boolean canMoveLeft(final int index) {
36+
return 0 < index && points.get(index - 1).right();
37+
}
38+
2139
public List<Point> getPoints() {
2240
return points;
2341
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package domain.dto;
2+
3+
import domain.ladder.Ladder;
4+
import java.util.List;
5+
6+
public record ResponseLadder(
7+
List<ResponseLine> lines
8+
) {
9+
10+
public static ResponseLadder from(final Ladder ladder) {
11+
List<ResponseLine> lines = ladder.getLines().stream()
12+
.map(ResponseLine::from)
13+
.toList();
14+
return new ResponseLadder(lines);
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package domain.dto;
2+
3+
import domain.Width;
4+
import domain.ladder.Ladder;
5+
import java.util.List;
6+
import java.util.stream.IntStream;
7+
8+
public record ResponseLadderResult(
9+
List<String> results
10+
) {
11+
12+
private static final String LADDER_RESULT_ARROW = " -> ";
13+
14+
public static ResponseLadderResult of(final Ladder ladder, final Width width) {
15+
List<String> result = IntStream.range(0, width.value())
16+
.mapToObj(start -> start + LADDER_RESULT_ARROW + ladder.move(start))
17+
.toList();
18+
return new ResponseLadderResult(result);
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package domain.dto;
2+
3+
import domain.Line;
4+
import domain.Point;
5+
import java.util.List;
6+
7+
public record ResponseLine(
8+
List<Boolean> connections
9+
) {
10+
11+
public static ResponseLine from(final Line line) {
12+
List<Boolean> connections = line.getPoints().stream()
13+
.limit(line.getPoints().size() - 1)
14+
.map(Point::right)
15+
.toList();
16+
return new ResponseLine(connections);
17+
}
18+
}

src/main/java/domain/ladder/Ladder.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package domain.ladder;
22

3+
import domain.Direction;
34
import domain.Line;
45
import domain.Width;
6+
import java.util.Collections;
57
import java.util.List;
68
import java.util.stream.IntStream;
79

@@ -10,7 +12,7 @@ public class Ladder {
1012
private final List<Line> lines;
1113

1214
private Ladder(final List<Line> lines) {
13-
this.lines = List.copyOf(lines);
15+
this.lines = lines;
1416
}
1517

1618
public static Ladder from(final List<Line> lines) {
@@ -27,7 +29,16 @@ private boolean isConnectedBetween(final int index) {
2729
.anyMatch(line -> line.hasConnectionAt(index));
2830
}
2931

32+
public int move(final int startIndex) {
33+
int position = startIndex;
34+
for (Line line : lines) {
35+
Direction direction = line.directionAt(position);
36+
position = direction.moveFrom(position);
37+
}
38+
return position;
39+
}
40+
3041
public List<Line> getLines() {
31-
return lines;
42+
return Collections.unmodifiableList(lines);
3243
}
3344
}

src/main/java/view/OutputView.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package view;
22

3-
import domain.Line;
4-
import domain.Point;
5-
import domain.ladder.Ladder;
6-
import java.util.List;
3+
import domain.dto.ResponseLadder;
4+
import domain.dto.ResponseLadderResult;
5+
import domain.dto.ResponseLine;
76

87
public final class OutputView {
98

109
private static final String LADDER_RESULT_TITLE = "실행결과";
10+
1111
private static final String BLANK = " ";
1212
private static final String VERTICAL = "|";
1313
private static final String HORIZONTAL = "-----";
@@ -21,29 +21,36 @@ public static void printLadderResultTitle() {
2121
System.out.println();
2222
}
2323

24-
public static void drawLadder(final Ladder ladder) {
25-
for (Line line : ladder.getLines()) {
24+
public static void drawLadder(final ResponseLadder ladder) {
25+
for (ResponseLine line : ladder.lines()) {
2626
drawLine(line);
2727
}
2828
}
2929

30-
private static void drawLine(final Line line) {
30+
private static void drawLine(final ResponseLine line) {
3131
StringBuilder builder = new StringBuilder();
3232
builder.append(BLANK);
3333

34-
List<Point> points = line.getPoints();
35-
for (int i = 0; i < points.size() - 1; i++) {
34+
for (boolean connected : line.connections()) {
3635
builder.append(VERTICAL);
37-
builder.append(drawHorizontalIfConnected(points.get(i)));
36+
builder.append(drawHorizontalIfConnected(connected));
3837
}
38+
3939
builder.append(VERTICAL);
4040
System.out.println(builder);
4141
}
4242

43-
private static String drawHorizontalIfConnected(final Point point) {
44-
if (point.right()) {
43+
private static String drawHorizontalIfConnected(final boolean connected) {
44+
if (connected) {
4545
return HORIZONTAL;
4646
}
4747
return BLANK;
4848
}
49+
50+
public static void printLadderResult(final ResponseLadderResult result) {
51+
System.out.println();
52+
for (String line : result.results()) {
53+
System.out.println(line);
54+
}
55+
}
4956
}

src/test/java/domain/LineTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ void shouldGeneratePoint_whenGivenWidth() {
2828
.containsExactly(true, false, true, false);
2929
}
3030

31+
@Test
32+
@DisplayName("특정 위치에 연결이 있는지 확인한다.")
33+
void shouldCheckCorrectConnection() {
34+
// given
35+
LineGenerator generator = new FixedLineGenerator(List.of(new boolean[]{true, false, true}));
36+
Line line = generator.generate(3);
37+
38+
// when & then
39+
assertThat(line.hasConnectionAt(0)).isTrue();
40+
assertThat(line.hasConnectionAt(1)).isFalse();
41+
assertThat(line.hasConnectionAt(2)).isTrue();
42+
}
43+
44+
@Test
45+
@DisplayName("특정 위치에서 이동 방향을 올바르게 반환한다.")
46+
void shouldReturnCorrectDirection() {
47+
// given
48+
LineGenerator generator = new FixedLineGenerator(List.of(new boolean[]{true, false, true, false}));
49+
Line line = generator.generate(4);
50+
51+
// when & then
52+
assertThat(line.directionAt(0)).isEqualTo(Direction.RIGHT);
53+
assertThat(line.directionAt(1)).isEqualTo(Direction.LEFT);
54+
assertThat(line.directionAt(2)).isEqualTo(Direction.RIGHT);
55+
assertThat(line.directionAt(3)).isEqualTo(Direction.LEFT);
56+
}
57+
3158
@Test
3259
@DisplayName("반환된 리스트는 수정 불가능하다.")
3360
void shouldReturnUnmodifiableList() {

0 commit comments

Comments
 (0)