forked from next-step/java-ladder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.java
56 lines (42 loc) · 1.14 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 nextstep.ladder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Line {
private final List<Boolean> points;
Line(int playerCount, Random random) {
this.points = new ArrayList<>();
for (int i = 0; i < playerCount - 1; i++) {
if (i == 0) {
points.add(random.nextBoolean());
continue;
}
if (points.get(i - 1) == true) {
points.add(false);
continue;
}
points.add(random.nextBoolean());
}
}
Line(List<Boolean> points) {
this.points = points;
}
public List<Boolean> getPoints() {
return points;
}
public int calculateNextPosition(int cursor) {
if (cursor == 0 && points.get(cursor)) {
return ++cursor;
}
if (cursor == points.size() && points.get(cursor - 1)) {
return --cursor;
}
if (points.get(cursor - 1)) {
return --cursor;
}
if (points.get(cursor)) {
return ++cursor;
}
return cursor;
}
}