-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathLadderValidator.java
More file actions
32 lines (25 loc) · 1.01 KB
/
LadderValidator.java
File metadata and controls
32 lines (25 loc) · 1.01 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
package model;
import java.util.*;
import java.util.stream.*;
public class LadderValidator {
private static final Random RANDOM = new Random();
public static void validate(List<Line> lines, int width) {
IntStream.rangeClosed(0, width)
.forEach(i -> validateColumn(lines, i, width));
}
private static void validateColumn(List<Line> lines, int col, int width) {
boolean emptyColumn = lines.stream().noneMatch(line -> hasBridgeAt(line, col, width));
if (emptyColumn) {
connect(lines.get(RANDOM.nextInt(lines.size())), col, width);
}
}
private static boolean hasBridgeAt(Line line, int col, int width) {
if (col == 0) return line.hasBridgeAt(col);
if (col == width) return line.hasBridgeAt(col - 1);
return line.hasBridgeAt(col - 1) || line.hasBridgeAt(col);
}
private static void connect(Line line, int col, int width) {
if (col == width) line.setBridgeAt(col - 1);
else line.setBridgeAt(col);
}
}