-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.java
More file actions
246 lines (201 loc) · 11.7 KB
/
Copy pathMazeSolver.java
File metadata and controls
246 lines (201 loc) · 11.7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// This assignment was pretty challenging for me. Nearly I studied everday to finish it and this is the best I can do.
// But I learned so many different methods and now I am better with debugging than I was before.
package javaAssignments.A1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class MazeSolver {
// Method to read the maze grid from a text file
public static char[][] readMazeGrid(String input) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(input));
String line;
int rows = 0;
// Find out the number of rows in the maze
while((line = reader.readLine()) != null) {
rows++;
}
reader.close();
reader = new BufferedReader(new FileReader(input));
char [][] mazeGrid = new char[rows][];
// Read the file again and fill the maze grid with characters
int row = 0;
while((line = reader.readLine()) != null) {
mazeGrid[row] = line.toCharArray(); // Change the string into a char array
row++;
}
reader.close();
return mazeGrid; // Return the maze grid
}
// Method to add walls around the maze to avoid boundary issues
public static char[][] addWalls(char [][] mazeGrid) {
Integer rowNum = mazeGrid.length; // Number of rows in the original maze
Integer colNum = mazeGrid[0].length; // Number of columns in the original maze
// Create a new maze grid with extra rows and columns for walls
char[][] newMazeGrid = new char[rowNum+2][colNum+2];
// Add walls to new maze grid (top, bottom, right and left side)
for (int i = 0; i < newMazeGrid.length; i++) {
int a = (newMazeGrid[0].length) - 1;
newMazeGrid[i][0] = '#'; // Left wall
newMazeGrid[i][a] = '#'; // Right wall
}
for (int j = 0; j < newMazeGrid[0].length; j++) {
int b = (newMazeGrid.length) - 1;
newMazeGrid[0][j] = '#'; // Top wall
newMazeGrid[b][j] = '#'; // Bottom wall
}
// Add the original maze inside the new grid, surrounded by walls
for (int i = 0; i < mazeGrid.length; i++) {
for (int j = 0; j < mazeGrid[0].length; j++) {
newMazeGrid[i+1][j+1] = mazeGrid[i][j] ;
}
}
// Return the new maze grid with walls
return newMazeGrid;
}
// Method to print the maze
public static void printMaze(char [][] mazeGrid){
for(int i = 0; i < mazeGrid.length; i++){ // Row part
for(int j = 0; j < mazeGrid[i].length; j++){ // Col part
System.out.print(mazeGrid[i][j] + " ");
}
// Move to the next line after printing a row
System.out.println();
}
}
// Maze solving method using a simple left-hand-rule kinda approach
public static char[][] mazeSolver(char [][] newMazeGrid, int startX, int startY) {
// Start point in the maze
Integer curr_X = startX + 1; // X: row number , adding one to match input to new wall maze
Integer curr_Y = startY + 1; // Y: col number , adding one to match input to new wall maze
int[][] visited = new int[newMazeGrid.length][newMazeGrid[0].length]; // Create an array for visited cells
int[][] visitedBack = new int[newMazeGrid.length][newMazeGrid[0].length]; // Array for visited backtrack cells
Integer direction = 1; // Start facing East; 0: South(g), 1: East(d), 2: North(k), 3: West(b)
Integer startPointX = startX + 1, startPointY = startY + 1; // Current position
visited[startPointX][startPointY] = 1; // Mark the start point as visited cell
// Main maze solving logic
while (true) {
boolean flag_moved = false; // Flag to check if the algorithm moved
// Move East if not wall and not visited
if ((newMazeGrid[curr_X][curr_Y + 1] != '#') && (visited[curr_X][curr_Y + 1] != 1)) {
curr_Y++;
visited[curr_X][curr_Y] = 1; // Mark current cell as visited
direction = 1; // Update direction to North to turn left
flag_moved = true;
// Keep moving straight unless its in exit or not wall ahead
while ((newMazeGrid[curr_X][curr_Y + 1] != '#') && (newMazeGrid[curr_X][curr_Y] != 'X')) {
curr_Y++;
visited[curr_X][curr_Y] = 1;
}
// Move North if not wall and not visited
} else if ((newMazeGrid[curr_X - 1][curr_Y] != '#') && (visited[curr_X - 1][curr_Y] != 1)) {
curr_X--;
visited[curr_X][curr_Y] = 1; // Mark current cell as visited
direction = 2; // Update direction to West to turn left
flag_moved = true;
// Keep moving straight unless its in exit or not wall ahead
while ((newMazeGrid[curr_X - 1][curr_Y] != '#') && (newMazeGrid[curr_X][curr_Y] != 'X')) {
curr_X--;
visited[curr_X][curr_Y] = 1;
}
// Move West if not wall and not visited
} else if ((newMazeGrid[curr_X][curr_Y - 1] != '#') && (visited[curr_X][curr_Y - 1] != 1)) {
curr_Y--;
visited[curr_X][curr_Y] = 1; // Mark current cell as visited
direction = 3; // Update direction to South to turn left
flag_moved = true;
// Keep moving straight unless its in exit or not wall ahead
while ((newMazeGrid[curr_X][curr_Y - 1] != '#') && (newMazeGrid[curr_X][curr_Y] != 'X')) {
curr_Y--;
visited[curr_X][curr_Y] = 1;
}
// Move South if not wall and not visited
} else if ((newMazeGrid[curr_X + 1][curr_Y] != '#') && (visited[curr_X + 1][curr_Y] != 1)) {
curr_X++;
visited[curr_X][curr_Y] = 1; // Mark current cell as visited
direction = 0; // Update direction to East to turn left
flag_moved = true;
// Keep moving straight unless its in exit or not wall ahead
while ((newMazeGrid[curr_X + 1][curr_Y] != '#') && (newMazeGrid[curr_X][curr_Y] != 'X')) {
curr_X++;
visited[curr_X][curr_Y] = 1;
}
}
// Backtracking
if (!flag_moved) { // Backtrack while not unvisited cell
visitedBack[curr_X][curr_Y] = 1; // Mark the start point as visited cell in Backtrack visited array
direction = (direction + 1) % 4; // Turn left after each loop
// Move forward if; direction is correct, no wall ahead, not revisited
if ((direction == 3) && (newMazeGrid[curr_X][curr_Y - 1] != '#') && (visitedBack[curr_X][curr_Y - 1] != 1)) {
curr_Y--;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
// Keep moving forward while; no wall ahead, visited and no way on the left
while ((newMazeGrid[curr_X][curr_Y - 1] != '#') && (visited[curr_X][curr_Y] == 1) &&
(newMazeGrid[curr_X + 1][curr_Y] != '.')) {
curr_Y--;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
}
// Move forward if; direction is correct, no wall ahead, not revisited
} else if ((direction == 0) && (newMazeGrid[curr_X + 1][curr_Y] != '#') && (visitedBack[curr_X + 1][curr_Y] != 1)) {
curr_X++;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
// Keep moving forward while; no wall ahead, visited and no way on the left
while ((newMazeGrid[curr_X + 1][curr_Y] != '#') && (visited[curr_X][curr_Y] == 1) &&
(newMazeGrid[curr_X][curr_Y + 1] != '.')) {
curr_X++;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
}
// Move forward if; direction is correct, no wall ahead, not revisited
} else if ((direction == 1) && (newMazeGrid[curr_X][curr_Y + 1] != '#') && (visitedBack[curr_X][curr_Y + 1] != 1)) {
curr_Y++;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
// Keep moving forward while; no wall ahead, visited and no way on the left
while ((newMazeGrid[curr_X][curr_Y + 1] != '#') && (visited[curr_X][curr_Y] == 1) &&
(newMazeGrid[curr_X - 1][curr_Y] != '.')) {
curr_Y++;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
}
// Move forward if; direction is correct, no wall ahead, not revisited
} else if ((direction == 2) && (newMazeGrid[curr_X - 1][curr_Y] != '#') && (visitedBack[curr_X - 1][curr_Y] != 1)) {
curr_X--;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
// Keep moving forward while; no wall ahead, visited and no way on the left
while ((newMazeGrid[curr_X - 1][curr_Y] != '#') && (visited[curr_X][curr_Y] == 1) &&
(newMazeGrid[curr_X][curr_Y - 1] != '.')) {
curr_X--;
visitedBack[curr_X][curr_Y] = 1; // Mark current cell as revisited
}
}
}
// Check if we've reached the exit ('X')
if (newMazeGrid[curr_X][curr_Y] == 'X') {
System.out.println("You found the exit! ");
System.out.println("Exit location: " + newMazeGrid[curr_X][curr_Y] + " x:" + curr_X + ",y:" + curr_Y);
break;
} else if (curr_X == startPointX && curr_Y == startPointY) {
System.out.println("There is no solution!");
break;
}
}
return newMazeGrid; // Returns maze grid with walls around it
}
public static void main(String[] args) throws IOException {
// Check correct input argument length
if (args.length != 3 ) {
System.out.println("Usage: java MazeSolver <x-coordinate> <y-coordinate> <maze-file>");
return;
}
try {
int startX = Integer.parseInt(args[0]); // Set 0. arg to X start location
int startY = Integer.parseInt(args[1]); // Set 1. arg to Y start location
String mazeFile = args[2]; // Set 2. arg to string mazeFile as file name
char[][] mazeGrid = MazeSolver.readMazeGrid(mazeFile); // Read the txt file
char[][] newMazeGrid = MazeSolver.addWalls(mazeGrid); // Add walls around it
mazeSolver(newMazeGrid, startX, startY); // Call maze solver function
} catch (NumberFormatException e) { // Check the first two arg to be integers
System.out.println("Error: Coordinates must be integers.");
} catch (FileNotFoundException e) { // Check if the file exist or not inside the directory
System.out.println("Error: Maze file not found.");
}
}
}