-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLine.java
More file actions
60 lines (50 loc) · 1.79 KB
/
Line.java
File metadata and controls
60 lines (50 loc) · 1.79 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
50
51
52
53
54
55
56
57
58
59
60
package nextstep.ladder.domain.line;
import nextstep.ladder.domain.participant.Participant;
import nextstep.ladder.domain.participant.Participants;
public class Line {
private final int rowLineNumber;
private RowLinePositions rowLinePosition;
public Line(String[] participants) {
int participantNumber = participants.length;
rowLineNumber = participantNumber - 1;
rowLinePosition = RowLinePositions.create(rowLineNumber);
}
public Line(int rowLineNumber) {
this.rowLineNumber = rowLineNumber;
rowLinePosition = RowLinePositions.create(rowLineNumber);
}
public Line(int rowLineNumber, RowLinePositions rowLinePositions) {
this.rowLineNumber = rowLineNumber;
this.rowLinePosition = rowLinePositions;
}
public boolean isTruePosition(int index) {
return rowLinePosition.isTrue(index);
}
public void movableParticipants(Participants participants) {
participants.getParticipantList()
.stream()
.forEachOrdered(participant -> movableParticipant(participant));
}
public void movableParticipant(Participant participant) {
if (movableLeft(participant)) {
return;
}
movableRight(participant);
}
private boolean movableLeft(Participant participant) {
int index = participant.getCurrentIndex();
if (index != 0 && isTruePosition(index -1)) {
participant.moveLeft();
return true;
}
return false;
}
private boolean movableRight(Participant participant) {
int index = participant.getCurrentIndex();
if (index != rowLineNumber && isTruePosition(index)) {
participant.moveRight();
return true;
}
return false;
}
}