-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.java
More file actions
351 lines (320 loc) · 12.1 KB
/
Maze.java
File metadata and controls
351 lines (320 loc) · 12.1 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Maze.java
* A class for loading and printing mazes from files.
*
* @author Alvin Bierley
* @author Chris Kitchen
*
* Received assistance from former Data Structures student Peter in the load & print functions
*
* September 23, 2018
*/
import java.io.*;
import java.util.*;
public class Maze
{
// this is the 2d array that stores the maze
private ArrayList<ArrayList<MazeSquare>> rowList;
// width and height of maze
private int w, h;
// x and y coordinates of the starting point (begins at zero)
private int sx, sy;
// x and y coordinates of the finishing point
private int fx, fy;
// LLStack of MazeSquare Objects
private LLStack<MazeSquare> mazeStack = new LLStack<MazeSquare>();
/**
* Constructor for the Maze class
*/
public Maze()
{
rowList = new ArrayList<ArrayList<MazeSquare>>();
}
/**
* Load in a Maze from a given file
*
* @param fileName the name of the file containing the maze
*/
public void load(String fileName)
{
// Create a scanner for the given file
Scanner scanner = null;
try
{
scanner = new Scanner(new File(fileName));
} catch (FileNotFoundException e)
{
System.err.println(e);
System.exit(1);
}
// First line of file is "w h"
String[] lineParams = scanner.nextLine().split(" ");
w = Integer.parseInt(lineParams[0]);
h = Integer.parseInt(lineParams[1]);
// Reads the coordinates for the start square
lineParams = scanner.nextLine().split(" ");
sx = Integer.parseInt(lineParams[0]);
sy = Integer.parseInt(lineParams[1]);
// Reads the coordinates for the finish square
lineParams = scanner.nextLine().split(" ");
fx = Integer.parseInt(lineParams[0]);
fy = Integer.parseInt(lineParams[1]);
// stores the line of symbols in the text file
String line;
int row = 0;
boolean top, bottom, left, right, start, finish;
// default values for bottom and left
bottom = false;
left = false;
// reads the text while there is another line to read and stores the info for MazeSquare
while (scanner.hasNextLine())
{
line = scanner.nextLine();
rowList.add(new ArrayList<MazeSquare>());
// iterates through each symbol in the line
// Peter explained how if and else statements could be implemented
for (int i=0; i<line.length(); i++)
{
if (row==0)
{
top = true;
}
else
{
top = rowList.get(row-1).get(i).hasBottomWall();
}
if (i<line.length()-1)
{
char nextSquare = line.charAt(i+1);
if (nextSquare == 'L' || nextSquare == '|')
{
right = true;
}
else
{
right = false;
}
}
else
{
right = true;
}
if (line.charAt(i) == 'L')
{
left = true;
bottom = true;
}
else if (line.charAt(i) == '|')
{
left = true;
bottom = false;
}
else if (line.charAt(i) == '_')
{
left = false;
bottom = true;
}
else if (line.charAt(i) == '-')
{
left = false;
bottom = false;
}
// determines the start and finish point based off coordinates
start = sx == i && sy == row;
finish = fx == i && fy == row;
rowList.get(row).add(new MazeSquare(row, i, top, bottom, left, right, start, finish));
}
row++;
}
}
/**
* Computes and returns a solution to this maze. If there are multiple
* solutions, only one is returned, and getSolution() makes no guarantees about
* which one. However, the returned solution will not include visits to dead
* ends or any backtracks, even if backtracking occurs during the solution
* process.
*
* @return a LLStack of MazeSquare objects containing the sequence of squares
* visited to go from the start square (bottom of the stack) to the
* finish square (top of the stack).
*/
public LLStack<MazeSquare> getSolution() {
MazeSquare start = rowList.get(sy).get(sx);
MazeSquare end = rowList.get(fy).get(fx);
MazeSquare firstVisited = rowList.get(sy).get(sx);
// pushes the start square onto the stack
mazeStack.push(firstVisited);
firstVisited.visit();
return solveTheMaze();
}
/**
* Finds the solution for the maze
* If no solution is available, "maze is unsolvable" is printed
* Returns a stack of MazeSquare objects containing the order of squares visited from the
* start square to the finish square
*/
public LLStack<MazeSquare> solveTheMaze() {
// Keeps on repeating until the maze is solved or until the stack is empty
while (!mazeStack.isEmpty() && (mazeStack.peek().getR() != fy || mazeStack.peek().getC() != fx)) {
// peeks at the top of the stack
MazeSquare peek = mazeStack.peek();
/*
if the current square does not have a bottom wall and hasn't yet been visited, the
MazeSquare below the current MazeSquare is pushed onto the stack and is marked as visited
*/
if (!peek.hasBottomWall() && !rowList.get(peek.getR() + 1).get(peek.getC()).isVisited()) {
mazeStack.push(rowList.get(peek.getR() + 1).get(peek.getC()));
rowList.get(peek.getR() + 1).get(peek.getC()).visit();
}
/*
if the current square does not have a right wall and hasn't yet been visited, the MazeSquare
to the right of the the current MazeSquare is pushed to the top of the stack and is marked
as visited
*/
else if (!peek.hasRightWall() && !rowList.get(peek.getR()).get(peek.getC() + 1).isVisited()) {
mazeStack.push(rowList.get(peek.getR()).get(peek.getC() + 1));
rowList.get(peek.getR()).get(peek.getC() + 1).visit();
}
/*
if the current square does not have a top wall and hasn't yet been visited, the MazeSquare
above the current MazeSquare is pushed to the top of the stack and is marked as visited
*/
else if (!peek.hasTopWall() && !rowList.get(peek.getR() - 1).get(peek.getC()).isVisited()) {
mazeStack.push(rowList.get(peek.getR() - 1).get(peek.getC()));
rowList.get(peek.getR() - 1).get(peek.getC()).visit();
}
/*
if the current square does not have a left wall and hasn't yet been visited, the MazeSquare
to the left of the current MazeSquare is pushed to the top of the stack and is marked as
visited
*/
else if (!peek.hasLeftWall() && !rowList.get(peek.getR()).get(peek.getC() - 1).isVisited()) {
mazeStack.push(rowList.get(peek.getR()).get(peek.getC() - 1));
rowList.get(peek.getR()).get(peek.getC() - 1).visit();
}
// if none of the above is valid, then the top of the stack is popped off
else {
mazeStack.pop();
}
}
// prints that the maze is unsolvable once the stack is empty
if (mazeStack.isEmpty()) {
System.out.println("Maze is unsolvable.");
}
return mazeStack;
}
/**
* Print the Maze to System.out
*/
public void print()
{
// solution stack for the given maze
LLStack <MazeSquare> solution = getSolution();
// Peter explained how to use ArrayList effectively
ArrayList<MazeSquare> currentRow;
MazeSquare currentSquare;
// iterates through the rows in the maze
for (int r=0; r<rowList.size(); r++)
{
currentRow = rowList.get(r);
// prints out the top line of the square
for (int c=0; c<currentRow.size(); c++)
{
System.out.print("+");
if (currentRow.get(c).hasTopWall())
{
System.out.print("-----");
}
else
{
System.out.print(" ");
}
}
System.out.println("+");
// prints out the second line of the square
for (int c=0; c<currentRow.size(); c++)
{
if (currentRow.get(c).hasLeftWall())
{
System.out.print("| ");
}
else
{
System.out.print(" ");
}
}
System.out.println("|");
// prints out the third line, adds 'S' or 'F' if necessary
for (int c=0;c<currentRow.size(); c++)
{
if(currentRow.get(c).hasLeftWall())
{
System.out.print("|");
}
else
{
System.out.print(" ");
}
if(currentRow.get(c).start() && currentRow.get(c).finish())
{
System.out.print(" SF ");
}
else if (currentRow.get(c).start() && !currentRow.get(c).finish())
{
System.out.print(" S ");
}
else if (!currentRow.get(c).start() && currentRow.get(c).finish())
{
System.out.print(" F ");
}
else if (solution.contains(currentRow.get(c))) {
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println("|");
// prints out the fourth line of the square
for (int c=0; c<currentRow.size(); c++)
{
if (currentRow.get(c).hasLeftWall())
{
System.out.print("| ");
}
else
{
System.out.print(" ");
}
}
System.out.println("|");
// prints out bottom wall of the maze
if (r == rowList.size()-1)
{
for (int c=0; c<currentRow.size(); c++)
{
System.out.print("+");
if (currentRow.get(c).hasBottomWall())
{
System.out.print("-----");
}
}
System.out.println("+");
}
}
}
// This main program acts as a simple unit test for the
// load-from-file and print-to-System.out Maze capabilities.
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: java Maze mazeFile");
System.exit(1);
}
Maze maze = new Maze();
maze.load(args[0]);
maze.print();
}
}