-
Notifications
You must be signed in to change notification settings - Fork 743
/
Copy pathLadderGenerator.java
71 lines (54 loc) · 2.74 KB
/
LadderGenerator.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ladder.domain;
import ladder.exception.IllegalLadderParameterException;
import java.util.HashSet;
import java.util.Set;
public class LadderGenerator {
private static final double DEFAULT_CHANCE = 0.5;
private final LadderGenerateStrategy ladderGenerateStrategy;
public LadderGenerator() {
this.ladderGenerateStrategy = () -> Math.random() < DEFAULT_CHANCE;
}
public LadderGenerator(LadderGenerateStrategy ladderGenerateStrategy) {
this.ladderGenerateStrategy = ladderGenerateStrategy;
}
public Ladder generate(int height, int width) {
checkHeightAndWidth(height, width);
VerticalLines verticalLines = VerticalLines.create(width);
HorizontalLines horizontalLines = createHorizontalLines(verticalLines, height);
return new Ladder(verticalLines, horizontalLines);
}
private HorizontalLines createHorizontalLines(VerticalLines verticalLines, int height) {
HashSet<HorizontalLine> horizontalLineHashSet = new HashSet<>();
for (int i = 0; i < height; i++) {
Set<HorizontalLine> sameHeightHorizontalLineSet = createSameHeightHorizontalLineSet(verticalLines, i);
horizontalLineHashSet.addAll(sameHeightHorizontalLineSet);
}
return new HorizontalLines(horizontalLineHashSet, height);
}
private Set<HorizontalLine> createSameHeightHorizontalLineSet(VerticalLines verticalLines, int height) {
HashSet<HorizontalLine> sameHeightHorizontalLineHashSet = new HashSet<>();
for (int i = 0; i < verticalLines.getMaxWidth() - 1; i++) {
VerticalLine nowVerticalLine = verticalLines.getVerticalLineByIndex(i);
VerticalLine nextVerticalLine = verticalLines.getVerticalLineByIndex(i + 1);
boolean notExistPreviousHorizontalLine = sameHeightHorizontalLineHashSet.stream()
.noneMatch(horizontalLine ->
horizontalLine.getRightVerticalLine() == nowVerticalLine
);
if (notExistPreviousHorizontalLine && ladderGenerateStrategy.canGenerate()) {
sameHeightHorizontalLineHashSet.add(
new HorizontalLine(
new AdjacentVerticalLines(nowVerticalLine, nextVerticalLine),
height
)
);
}
}
return sameHeightHorizontalLineHashSet;
}
private void checkHeightAndWidth(int height, int width) {
if (height < HorizontalLine.MINIMUM_HEIGHT
|| width < Ladder.MINIMUM_VERTICAL_LINES_QUANTITY) {
throw new IllegalLadderParameterException(String.format("입력된 높이 : %d, 입력된 너비 : %d", height, width));
}
}
}