-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.java
More file actions
executable file
·145 lines (123 loc) · 3.87 KB
/
MazeSolver.java
File metadata and controls
executable file
·145 lines (123 loc) · 3.87 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* This class finds a path through a maze which contains no cycles.
*
* @author Rylie Nelson and Eric Preston
*/
public class MazeSolver {
/** Stores the representation of the maze in a two-dimensional array of Strings. */
private String[][] mazeArray;
/** The height of the maze. */
private int mazeHeight = 0;
/** The width of the maze. */
private int mazeWidth = 0;
/**
* The constructor for the maze.
* @param filename The name of the file that contains a representation of a maze.
*/
public MazeSolver(String filename) {
String mazeString = readFile(filename);
createArrayOfMaze(mazeString);
solveMaze(0, 1);
}
/**
* Reads a file and returns a single string representation of that file.
*
* @param filename The name of the file to be read.
* @return a string representation of the file.
*/
private String readFile(String filename) {
File file = new File(filename);
Scanner reader = null;
try {
reader = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
reader.useDelimiter("");
String theMaze = "";
mazeHeight++;
while (reader.hasNext()) {
String temp = reader.next();
if (temp.equals("\n")) {
mazeHeight++;
mazeWidth = 0;
} else if (temp.equals("X") || temp.equals(" ")) {
mazeWidth++;
}
theMaze += temp;
}
mazeWidth = mazeWidth / 2;
reader.close();
return theMaze;
}
/**
* First this method removes all newline characters, then it places the string representation of the maze
* into a two-dimensional array.
*
* @param mazeString The string to be processed.
*/
private void createArrayOfMaze(String mazeString) {
for (int i = 0; i < mazeString.length(); i++) { //removes all instances of newline characters, as they disrupt the process of the loop below
if (mazeString.charAt(i) == '\n') {
mazeString = mazeString.substring(0, i) + mazeString.substring(i + 1);
}
}
mazeArray = new String[mazeHeight][mazeWidth];
for (int height = 0; height < mazeHeight; height++) {
for (int width = 0; width < mazeWidth; width++) {
if (!mazeString.isEmpty()) {
mazeArray[height][width] = mazeString.substring(0, 2);
mazeString = mazeString.substring(2);
}
}
}
}
/**
* A recursive function that determines the path through the maze.
*
* @param row The row of the maze.
* @param column The column of the maze.
* @return True when the function reaches the end of the maze. False otherwise.
*/
private boolean solveMaze(int row, int column) {
mazeArray[row][column] = "+ ";
if (row == mazeHeight - 1 && column == mazeWidth - 2) { //base case, we have found the exit
return true;
}
boolean mazeFound = false;
if (row - 1 >= 0 && mazeArray[row - 1][column].equals(" ")) { //if above is valid to visit
mazeFound = solveMaze(row - 1, column);
}
if (column + 1 < mazeWidth && mazeArray[row][column + 1].equals(" ") && !mazeFound) { //if right is valid to visit
mazeFound = solveMaze(row, column + 1);
}
if (row + 1 < mazeHeight && mazeArray[row + 1][column].equals(" ") && !mazeFound) { //if below is valid to visit
mazeFound = solveMaze(row + 1, column);
}
if (column - 1 >= 0 && mazeArray[row][column - 1].equals(" ") && !mazeFound) { //if left is valid to visit
mazeFound = solveMaze(row, column - 1);
}
if (mazeFound) {
//mazeArray[row][column] = "+ ";
} else {
mazeArray[row][column] = " ";
}
return mazeFound;
}
/**
* Prints a graphical representation of the maze as it is being solved.
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int height = 0; height < mazeHeight; height++) {
for (int width = 0; width < mazeWidth; width++) {
sb.append(mazeArray[height][width]);
}
sb.append("\n");
}
return sb.toString();
}
}