1
1
package ladder .domain ;
2
2
3
3
import java .util .Objects ;
4
+ import java .util .stream .IntStream ;
4
5
5
6
public class Ladder {
6
7
@@ -19,7 +20,7 @@ public Participants getParticipants() {
19
20
}
20
21
21
22
public int getHeight () {
22
- return this .lines .size ();
23
+ return this .lines .height ();
23
24
}
24
25
25
26
public Lines getLines () {
@@ -31,6 +32,53 @@ public Line getLine(final int index) {
31
32
.get (index );
32
33
}
33
34
35
+ public int getLastVerticalLineIndex (final Participant participant ) {
36
+ return this .getLastVerticalLineIndex (this .participants .indexOf (participant ));
37
+ }
38
+
39
+ private int getLastVerticalLineIndex (final int firstVerticalLineIndex ) {
40
+ return IntStream .range (0 , this .lines .height ())
41
+ .reduce (firstVerticalLineIndex , (curVerticalLineIndex , curLineHeight ) -> {
42
+ if (canMoveLeft (curVerticalLineIndex , curLineHeight )) {
43
+ return curVerticalLineIndex - 1 ;
44
+ }
45
+
46
+ if (canMoveRight (curVerticalLineIndex , curLineHeight )) {
47
+ return curVerticalLineIndex + 1 ;
48
+ }
49
+
50
+ return curVerticalLineIndex ;
51
+ });
52
+ }
53
+
54
+ private boolean canMoveLeft (final int verticalLineIndex , final int curLineHeight ) {
55
+ if (isFirstVerticalLineIndex (verticalLineIndex )) {
56
+ return false ;
57
+ }
58
+
59
+ return this .lines .getLine (curLineHeight )
60
+ .isConnected (verticalLineIndex - 1 );
61
+ }
62
+
63
+ private boolean canMoveRight (final int verticalLineIndex , final int curLineHeight ) {
64
+ if (isLastVerticalLineIndex (verticalLineIndex )) {
65
+ return false ;
66
+ }
67
+
68
+ return this .lines .getLine (curLineHeight )
69
+ .isConnected (verticalLineIndex );
70
+ }
71
+
72
+
73
+ private boolean isFirstVerticalLineIndex (final int verticalLineIndex ) {
74
+ return verticalLineIndex == 0 ;
75
+ }
76
+
77
+
78
+ private boolean isLastVerticalLineIndex (final int verticalLineIndex ) {
79
+ return verticalLineIndex == this .lines .width ();
80
+ }
81
+
34
82
private void validateOrThrow (final Participants participants , final Lines lines ) {
35
83
if (Objects .isNull (participants ) || Objects .isNull (lines )) {
36
84
throw new IllegalArgumentException ("참가자나 사다리 라인은 null이 될 수 없습니다." );
0 commit comments