@@ -6,20 +6,66 @@ public class Explorer {
6
6
7
7
private boolean backward ;
8
8
private boolean done ;
9
- private int [] movement ;
10
9
private Maze maze ;
11
10
12
- public Explorer () {
11
+ public Explorer (Maze maze ) {
12
+ if (maze == null )
13
+ throw new IllegalArgumentException ("Null maze in explorer." );
13
14
15
+ this .maze = maze ;
14
16
}
15
17
16
- public void exploreMaze (Maze m ) {
17
- this . maze = m ;
18
- if (move (3 , 3 )) {
18
+ public void exploreMaze () {
19
+ Square startSquare = maze . getStartSquare () ;
20
+ if (move (startSquare . getRow (), startSquare . getColumn () )) {
19
21
System .out .println (maze );
20
22
}
21
23
}
22
24
25
+ /**
26
+ * print the accumulated paths lead to the 'F' point.
27
+ */
28
+ public String printExplorersPath () {
29
+ StringBuilder result = new StringBuilder (maze .getWidth () * maze .getHeight ());
30
+
31
+ Square [][] map = maze .getSquare ();
32
+ for (int row = 0 ; row < maze .getHeight (); row ++) {
33
+ for (int col = 0 ; col < maze .getWidth (); col ++) {
34
+
35
+ if (map [row ][col ].isWalled ()) {
36
+ result .append ('X' );
37
+ } else if (map [row ][col ].isStart ()) {
38
+ result .append ('S' );
39
+ } else if (map [row ][col ].isExit ()) {
40
+ result .append ('F' );
41
+ } else if (maze .getPath ().isOnPath (maze .getSquare (row , col ))) {
42
+ result .append ('.' );
43
+ } else {
44
+ result .append (' ' );
45
+ }
46
+ }
47
+ result .append ('\n' );
48
+ }
49
+ System .out .println (result .toString ());
50
+ return result .toString ();
51
+ }
52
+
53
+ /**
54
+ * Get the previous explored square.
55
+ *
56
+ * @return {@link uk.gov.dwp.maze.Square} previously explored in current path.
57
+ */
58
+ public Square getPreviousExplored () {
59
+ return maze .getPath ().getPreviousExploredSquare ();
60
+ }
61
+
62
+ /**
63
+ *
64
+ *
65
+ * @param row
66
+ * @param col
67
+ * @return
68
+ */
23
69
private boolean move (final int row , final int col ){
24
70
//System.out.println(maze);
25
71
Square currentSquare = maze .getSquare (row , col );
@@ -33,18 +79,19 @@ private boolean move(final int row, final int col){
33
79
done = true ;
34
80
return true ;
35
81
} else {
36
- // can hit dead end if goes into this block - so we need to going backward until there is another open square .
82
+ // can hit dead end if goes into this block - so we need to going backward until it hits another open route .
37
83
maze .markExplored (row , col , true );
38
84
Square nextSquare = turn (row , col );
39
85
40
86
if (nextSquare != null ) {
41
87
if (move (nextSquare .getRow (), nextSquare .getColumn ())) {
42
88
System .out .println (maze );
89
+ printExplorersPath ();
43
90
return !done ;
44
91
}
45
92
} else {
46
93
// get previous explored square
47
- Square previousExplored = maze . getPreviousExplored ();
94
+ Square previousExplored = getPreviousExplored ();
48
95
this .backward = true ;
49
96
if (move (previousExplored .getRow (), previousExplored .getColumn ())) {
50
97
System .out .println (maze );
@@ -56,46 +103,47 @@ private boolean move(final int row, final int col){
56
103
}
57
104
58
105
/**
106
+ * Randomly turn left/right, may also choose forward/backward
59
107
*
60
- * @param row
61
- * @param col
108
+ * @param row row ordinal
109
+ * @param col column ordinal
62
110
* @return null if there are not open square in the next move.
63
111
*/
64
112
private Square turn (final int row , final int col ){
65
113
Set <Square > openSquares = getOpenAdjacentSquares (row , col );
66
- List <Square > list = new ArrayList <Square >();
67
- list .addAll (openSquares );
68
-
69
114
// if there are more than one open squares in front randomly pick one up.
70
- int size = list .size ();
115
+ int size = openSquares .size ();
71
116
if (size > 0 ) {
72
117
Random generator = new Random ();
73
118
int index = generator .nextInt (size );
74
- return list . get ( index ) ;
119
+ return openSquares . toArray ( new Square [ size ])[ index ] ;
75
120
}
76
121
return null ;
77
122
}
78
123
124
+ /**
125
+ * Finding the adjacent empty spaces to the current square.
126
+ *
127
+ * TODO simplify this method and get rid of if/else
128
+ *
129
+ * @param currentRow row ordinal
130
+ * @param currentCol column ordinal
131
+ * @return open Squares in a Set
132
+ */
79
133
private Set <Square > getOpenAdjacentSquares (final int currentRow , final int currentCol ) {
80
134
Set <Square > openSquares = new HashSet <Square >();
81
135
82
136
for (int i = 0 ; i <= 3 ; i ++) {
83
137
if (i == 0 && maze .getSquare (currentRow , currentCol - 1 ).isOpen () &&
84
138
!maze .isExplored (currentRow , currentCol - 1 )) {
85
139
openSquares .add (maze .getSquare (currentRow , currentCol - 1 ));
86
- }
87
-
88
- if (i == 1 && maze .getSquare (currentRow - 1 , currentCol ).isOpen () &&
140
+ } else if (i == 1 && maze .getSquare (currentRow - 1 , currentCol ).isOpen () &&
89
141
!maze .isExplored (currentRow - 1 , currentCol )) {
90
142
openSquares .add (maze .getSquare (currentRow - 1 , currentCol ));
91
- }
92
-
93
- if (i == 2 && maze .getSquare (currentRow + 1 , currentCol ).isOpen () &&
143
+ } else if (i == 2 && maze .getSquare (currentRow + 1 , currentCol ).isOpen () &&
94
144
!maze .isExplored (currentRow + 1 , currentCol )) {
95
145
openSquares .add (maze .getSquare (currentRow + 1 , currentCol ));
96
- }
97
-
98
- if (i == 3 && maze .getSquare (currentRow , currentCol + 1 ).isOpen () &&
146
+ } else if (i == 3 && maze .getSquare (currentRow , currentCol + 1 ).isOpen () &&
99
147
!maze .isExplored (currentRow , currentCol + 1 )) {
100
148
openSquares .add (maze .getSquare (currentRow , currentCol + 1 ));
101
149
}
0 commit comments