-
Notifications
You must be signed in to change notification settings - Fork 743
/
Copy pathLine.java
47 lines (39 loc) · 1.34 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
package nextstep.ladder.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
public class Line {
private final List<Boolean> points;
public Line(int countOfPerson, Random random) {
this.points = new ArrayList<>();
this.points.add(random.nextBoolean());
IntStream.range(0, countOfPerson - 2)
.forEach(index -> initPoint(index, random.nextBoolean()));
validateLine(countOfPerson);
}
private void validateLine(int countOfPerson) {
IntStream.range(0, countOfPerson - 2)
.filter(index -> points.get(index) && points.get(index + 1))
.forEach(point -> {
throw new IllegalArgumentException("사다리 라인이 겹칩니다");
});
}
private void initPoint(int index, boolean nextBoolean) {
if (this.points.get(index) && nextBoolean || this.points.get(index) && !nextBoolean
|| !this.points.get(index) && !nextBoolean) {
this.points.add(false);
return;
}
if (!this.points.get(index) && nextBoolean) {
this.points.add(true);
}
}
public List<Boolean> getPoints() {
return this.points;
}
@Override
public String toString() {
return this.points.toString();
}
}