-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathRowLinePositions.java
More file actions
45 lines (38 loc) · 1.25 KB
/
RowLinePositions.java
File metadata and controls
45 lines (38 loc) · 1.25 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
package ladder.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
public class RowLinePositions {
private List<Boolean> positionList = new ArrayList<>();
private int rowLineCount;
private Random random = new Random();
public RowLinePositions(int rowLineCount) {
this.rowLineCount = rowLineCount;
initializePositionList();
}
private void initializePositionList() {
positionList.add(random.nextBoolean());
IntStream.range(1, rowLineCount)
.forEach(index -> addRandomBoolean(index));
addTrueIfAllFalse();
}
private void addRandomBoolean(int index) {
if (positionList.get(index - 1)) {
positionList.add(false);
return;
}
positionList.add(random.nextBoolean());
}
private void addTrueIfAllFalse() {
boolean isAllFalse = positionList.stream()
.allMatch(e -> e.equals(Boolean.FALSE));
if (isAllFalse) {
positionList.set(random.nextInt(rowLineCount), Boolean.TRUE);
}
}
public boolean isPosition(int index) {
return positionList.get(index);
}
}