Skip to content

Commit 1b6c617

Browse files
author
Kun Cai
committed
adding some refacoring stuffs.
1 parent 9c688bc commit 1b6c617

20 files changed

+386
-147
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
2) assume robot can only move through spaces " " within the maze
99
3) the explorer should search for a path from the starting position "S" to the "F" position until it finds one or until it exhausts all possibilities.
10-
In addition, it should mark the path it finds (if any) in the maze.
10+
In addition, it should mark the path it finds (if any) in the maze.
11+
4) assume the maze map is sealed completed in a way that no empty spaces are allowed in each edge of the maze map.
12+
5) assume the explorer will randomly select an open route to proceed if there were more than one adjacent empty spaces to current square/cell.

maze.iml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
88
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
99
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
10+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
1011
<excludeFolder url="file://$MODULE_DIR$/target" />
1112
</content>
1213
<orderEntry type="inheritedJdk" />
1314
<orderEntry type="sourceFolder" forTests="false" />
14-
<orderEntry type="library" name="Maven: junit:junit:4.6" level="project" />
15+
<orderEntry type="library" name="Maven: junit:junit:4.9" level="project" />
16+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
17+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-all:1.3" level="project" />
1518
</component>
1619
</module>
1720

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
<dependency>
1313
<groupId>junit</groupId>
1414
<artifactId>junit</artifactId>
15-
<version>4.6</version>
15+
<version>4.9</version>
1616
</dependency>
17+
18+
<dependency>
19+
<groupId>org.hamcrest</groupId>
20+
<artifactId>hamcrest-all</artifactId>
21+
<version>1.3</version>
22+
</dependency>
23+
1724
</dependencies>
1825
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package uk.gov.dwp.maze;
2+
3+
/**
4+
* Created by User on 28/10/2014.
5+
*/
6+
public class AppMain {
7+
8+
public static void main(String[] args) {
9+
MazeBuilder builder = new FileMazeBuilder("src/main/resources/maze3.txt");
10+
Maze maze = builder.build();
11+
System.out.println(maze);
12+
13+
Explorer e = new Explorer(maze);
14+
e.exploreMaze();
15+
}
16+
}

src/main/java/uk/gov/dwp/maze/Direction.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/uk/gov/dwp/maze/Explorer.java

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,66 @@ public class Explorer {
66

77
private boolean backward;
88
private boolean done;
9-
private int[] movement;
109
private Maze maze;
1110

12-
public Explorer() {
11+
public Explorer(Maze maze) {
12+
if (maze == null)
13+
throw new IllegalArgumentException("Null maze in explorer.");
1314

15+
this.maze = maze;
1416
}
1517

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())) {
1921
System.out.println(maze);
2022
}
2123
}
2224

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+
*/
2369
private boolean move(final int row, final int col){
2470
//System.out.println(maze);
2571
Square currentSquare = maze.getSquare(row, col);
@@ -33,18 +79,19 @@ private boolean move(final int row, final int col){
3379
done = true;
3480
return true;
3581
} 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.
3783
maze.markExplored(row, col, true);
3884
Square nextSquare = turn(row, col);
3985

4086
if (nextSquare != null) {
4187
if (move(nextSquare.getRow(), nextSquare.getColumn())) {
4288
System.out.println(maze);
89+
printExplorersPath();
4390
return !done;
4491
}
4592
} else {
4693
// get previous explored square
47-
Square previousExplored = maze.getPreviousExplored();
94+
Square previousExplored = getPreviousExplored();
4895
this.backward = true;
4996
if (move(previousExplored.getRow(), previousExplored.getColumn())) {
5097
System.out.println(maze);
@@ -56,46 +103,47 @@ private boolean move(final int row, final int col){
56103
}
57104

58105
/**
106+
* Randomly turn left/right, may also choose forward/backward
59107
*
60-
* @param row
61-
* @param col
108+
* @param row row ordinal
109+
* @param col column ordinal
62110
* @return null if there are not open square in the next move.
63111
*/
64112
private Square turn(final int row, final int col){
65113
Set<Square> openSquares = getOpenAdjacentSquares(row, col);
66-
List<Square> list = new ArrayList<Square>();
67-
list.addAll(openSquares);
68-
69114
// if there are more than one open squares in front randomly pick one up.
70-
int size = list.size();
115+
int size = openSquares.size();
71116
if (size > 0) {
72117
Random generator = new Random();
73118
int index = generator.nextInt(size);
74-
return list.get(index);
119+
return openSquares.toArray(new Square[size])[index];
75120
}
76121
return null;
77122
}
78123

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+
*/
79133
private Set<Square> getOpenAdjacentSquares(final int currentRow, final int currentCol) {
80134
Set<Square> openSquares = new HashSet<Square>();
81135

82136
for (int i = 0; i <= 3; i ++) {
83137
if (i == 0 && maze.getSquare(currentRow, currentCol - 1).isOpen() &&
84138
!maze.isExplored(currentRow, currentCol - 1)) {
85139
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() &&
89141
!maze.isExplored(currentRow - 1, currentCol)) {
90142
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() &&
94144
!maze.isExplored(currentRow + 1, currentCol)) {
95145
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() &&
99147
!maze.isExplored(currentRow, currentCol + 1)) {
100148
openSquares.add(maze.getSquare(currentRow, currentCol + 1));
101149
}

src/main/java/uk/gov/dwp/maze/ExplorerState.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package uk.gov.dwp.maze;
2+
3+
import java.io.File;
4+
5+
/**
6+
* Created by kcai on 28/10/2014.
7+
*/
8+
public class FileMazeBuilder implements MazeBuilder {
9+
10+
private File file;
11+
12+
public FileMazeBuilder(String filePath) {
13+
try {
14+
this.file = new File(filePath);
15+
} catch (NullPointerException npe) {
16+
throw new IllegalArgumentException("File path cannot be null.");
17+
}
18+
}
19+
20+
@Override
21+
public Maze build() {
22+
try {
23+
Square[][] map = MazeFactory.buildMazeMap(file);
24+
return new Maze(map);
25+
} catch (IllegalArgumentException ia) {
26+
return null;
27+
}
28+
}
29+
}

src/main/java/uk/gov/dwp/maze/GenericExplorer.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)