-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathLadder.java
56 lines (41 loc) · 1.52 KB
/
Ladder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Ladder {
private static final int MINIMUM_WIDTH = 2;
private static final int MINIMUM_HEIGHT = 1;
private final List<Line> lines;
public Ladder(int width, int height) {
validateSize(width, height);
this.lines = createLines(width, height);
}
public List<Line> getLines() {
return lines;
}
public int getEndPoint(int startPoint) {
int point = startPoint;
for (Line line : lines) {
point = line.getNextPoint(point);
}
return point;
}
private List<Line> createLines(int width, int height) {
int linksSize = width - 1;
DetachedRandomLinksGenerator linksGenerator = new DetachedRandomLinksGenerator(linksSize);
List<Line> lines = new ArrayList<>();
for (int i = 0; i < height; i++) {
Line line = new Line(linksGenerator);
lines.add(line);
}
return Collections.unmodifiableList(lines);
}
private void validateSize(int width, int height) {
if (width < MINIMUM_WIDTH) {
throw new IllegalArgumentException("사다리의 너비는 " + MINIMUM_WIDTH + "보다 짧을 수 없습니다. 전달된 값: " + width);
}
if (height < MINIMUM_HEIGHT) {
throw new IllegalArgumentException("사다리의 높이는 " + MINIMUM_HEIGHT + "보다 짧을 수 없습니다. 전달된 값: " + height);
}
}
}