-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathLine.java
56 lines (44 loc) · 1.7 KB
/
Line.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 domain;
import utils.RandomUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
public class Line {
private final List<Boolean> points;
public Line(List<Boolean> points) {
this.points = List.copyOf(points);
}
public List<Boolean> getPoints() {
return List.copyOf(points);
}
public boolean hasBridgeAt(int index) {
return points.get(index);
}
public Line setBridgeAt(int index) {
List<Boolean> newPoints = new ArrayList<>(points);
newPoints.set(index, true);
return new Line(newPoints);
}
public static void applyBridges(List<Boolean> points, Set<Integer> reserved, Line prev, boolean isReserved) {
IntStream.range(0, points.size()).forEach(i -> {
boolean shouldAddBridge = (isReserved && reserved.contains(i)) || (!isReserved && RandomUtil.nextBoolean());
if (shouldAddBridge && isValidBridgePosition(points, prev, i)) {
points.set(i, true);
}
});
}
public static void ensureOneBridge(List<Boolean> points, Line prev) {
if (points.contains(true)) return;
IntStream.range(0, points.size())
.filter(i -> isValidBridgePosition(points, prev, i))
.findFirst()
.ifPresent(i -> points.set(i, true));
}
private static boolean isValidBridgePosition(List<Boolean> points, Line prev, int index) {
if (prev != null && prev.hasBridgeAt(index)) return false;
if (index > 0 && points.get(index - 1)) return false;
if (index < points.size() - 1 && points.get(index + 1)) return false;
return true;
}
}