-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathLine.java
More file actions
49 lines (37 loc) · 1.25 KB
/
Line.java
File metadata and controls
49 lines (37 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
46
47
48
49
package domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static domain.LadderStep.randomTrueOrFalse;
public class Line {
private final List<LadderStep> ladderSteps;
public Line(int width) {
this.ladderSteps = new ArrayList<>();
createLine(width);
}
public void createLine(int width) {
ladderSteps.add(LadderStep.FROM_BOOLEAN.apply(randomTrueOrFalse()));
for (int i = 1; i < width - 1; i++) {
ladderSteps.add(ladderSteps.get(i - 1).nextStep());
}
}
public void decideWhereToGo(Position position) {
int ladderOrder = position.getPosition();
if (canMoveRight(ladderOrder)) {
position.moveRight(ladderSteps.size());
return;
}
if (canMoveLeft(ladderOrder)) {
position.moveLeft();
}
}
private boolean canMoveRight(int ladderOrder) {
return ladderOrder < ladderSteps.size() && ladderSteps.get(ladderOrder).canMove();
}
private boolean canMoveLeft(int ladderOrder) {
return ladderOrder != 0 && ladderSteps.get(ladderOrder - 1).canMove();
}
public List<LadderStep> getLine() {
return Collections.unmodifiableList(ladderSteps);
}
}