-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeRunner.java
More file actions
108 lines (100 loc) · 3.86 KB
/
Copy pathMazeRunner.java
File metadata and controls
108 lines (100 loc) · 3.86 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
//Dewei Yu
//V00897211
public class MazeRunner {
Maze mazeToSolve;
A5Stack<MazeLocation> path;
FilePrinter fileWriter;
public MazeRunner(Maze aMaze) {
mazeToSolve = aMaze;
path = new A5Stack<MazeLocation>();
fileWriter = new FilePrinter();
}
/*
* Purpose: Determines whether there is a path from start to finish in this maze
* Parameters: MazeLocation start - starting coordinates of this maze
* MazeLocation finish - finish coordinates of this maze
* Returns: true if there is a path from start to finish
*/
public boolean solve(MazeLocation start, MazeLocation finish) {
fileWriter.println("Searching maze from start: " + start + " to finish: " + finish);
path.push(start);
return findPath(start, finish);
}
/*
* Purpose: Recursively determines if there is a path from cur to finish
* Parameters: MazeLocation cur - current cordinates in this maze
* MazeLocation finish - goal coordinates of this maze
* Returns: true if there is a path from cur to finish
*
* NOTE: This method updates the Maze's mazeData array when locations
* are visited to an 'o', and further updates locations to an 'x'
* if it is determined they lead to dead ends. If the finish
* location is found, the Stack named path should contain all of
* loations visited from the start location to the finish.
*/
private boolean findPath(MazeLocation cur, MazeLocation finish) {
int row = cur.getRow();
int col = cur.getCol();
mazeToSolve.setChar(row, col, 'o');
fileWriter.println("\n" + mazeToSolve.toString());
boolean ret;
if (cur.equals(finish)) {
ret = true;
} else {
ret = false;
int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};
for (int i = 0; i < 4; i++) {
int nextRow = cur.getRow() + dr[i];
int nextCol = cur.getCol() + dc[i];
if (checkRange(nextRow, nextCol) && mazeToSolve.getChar(nextRow, nextCol) == ' ') {
MazeLocation nextLoc = new MazeLocation(nextRow, nextCol);
if (!ret) {
this.path.push(nextLoc);
ret = findPath(nextLoc, finish) || ret;
}
if (!ret) {
this.path.pop();
}
}
}
}
if (!ret) {
mazeToSolve.setChar(row, col, 'x');
}
return ret; // so it compiles
}
boolean checkRange(int row, int col) {
return 0 <= row && row < this.mazeToSolve.getRows()
&& 0 <= col && col < this.mazeToSolve.getCols();
}
/*
* Purpose: Creates a string of maze locations found in the stack
* Parameters: None
* Returns: A String representation of maze locations
*/
public String getPathToSolution() {
String details = "";
while (!path.isEmpty()) {
details = path.pop() + "\n" + details;
}
return details;
}
/*
* Purpose: Print the results of the maze run. Outputs the locations
* visited on the path from start to finish if the maze is
* solvable, or that no path was found if it is not.
* Parameters: boolean - whether or not the maze was solved
* Returns void - nothing
*/
public void printResults(boolean solved) {
if (solved) {
fileWriter.println("\n*** Maze Solved ***");
fileWriter.println(getPathToSolution());
} else {
fileWriter.println("\n--- No path to solution found ---");
}
fileWriter.close();
}
}