-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathLine.java
More file actions
78 lines (66 loc) · 2.26 KB
/
Copy pathLine.java
File metadata and controls
78 lines (66 loc) · 2.26 KB
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
72
73
74
75
76
77
78
package domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Line {
private final List<Boolean> points;
public Line(List<Boolean> points) {
this.points = points;
}
public static Line generateFirst(LadderWidth width, BooleanGenerator generator) {
List<Boolean> points = new ArrayList<>();
boolean previous = false;
for (int i = 0; i < width.getIntervalCount(); i++) {
previous = addPoint(points, previous, false, generator);
}
return new Line(points);
}
public static Line generateNext(LadderWidth width, BooleanGenerator generator, Line previousLine) {
List<Boolean> points = new ArrayList<>();
boolean previous = false;
for (int i = 0; i < width.getIntervalCount(); i++) {
boolean above = previousLine.isConnectedAt(i);
previous = addPoint(points, previous, above, generator);
}
return new Line(points);
}
private static boolean addPoint(List<Boolean> points, boolean previous, boolean above, BooleanGenerator gen) {
boolean nextPoint = determineNext(previous, above, gen);
points.add(nextPoint);
return nextPoint;
}
private static boolean determineNext(boolean previous, boolean above, BooleanGenerator gen) {
if (previous || above) {
return false;
}
return gen.generate();
}
public boolean isConnectedAt(int index) {
return points.get(index);
}
public Position move(Position position) {
int currentIndex = position.getValue();
if (canMoveLeft(currentIndex)) {
return position.moveLeft();
}
if (canMoveRight(currentIndex)) {
return position.moveRight();
}
return position;
}
private boolean canMoveLeft(int currentIndex) {
if (currentIndex <= 0) {
return false;
}
return points.get(currentIndex - 1);
}
private boolean canMoveRight(int currentIndex) {
if (currentIndex >= points.size()) {
return false;
}
return points.get(currentIndex);
}
public List<Boolean> getPoints() {
return Collections.unmodifiableList(points);
}
}