-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.java
386 lines (347 loc) · 12.7 KB
/
Maze.java
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import java.io.*;
import java.util.*;
/**
* Maze.java
* Solution to the Assignment 3: Mazes (Part 1).
* CS 201: Data Structures - Winter 2019
*
* @author Eric Alexander
* @author Sneha Narayan (modified Jan 31, 2019)
* @author Charlie Bushman (modified Feb 03, 2019)
*/
public class Maze {
private ArrayList<ArrayList<MazeSquare>> rowList;
private int w, h;
private int startRow, startCol, endRow, endCol;
// MazeSquare is implemented as an inner class
// to simplify the file structure a little bit.
private class MazeSquare {
private int r, c;
private boolean top, bottom, left, right,
start, end;
private boolean visited;
private MazeSquare(int r, int c,
boolean top, boolean bottom, boolean left, boolean right,
boolean start, boolean end) {
this.r = r;
this.c = c;
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
this.start = start;
this.end = end;
this.visited = false;
}
boolean hasTopWall() {
return top;
}
boolean hasBottomWall() {
return bottom;
}
boolean hasLeftWall() {
return left;
}
boolean hasRightWall() {
return right;
}
boolean isStart() {
return start;
}
boolean isEnd() {
return end;
}
int getRow() {
return r;
}
int getCol() {
return c;
}
boolean isVisited() {
return visited;
}
void visit() {
visited = true;
}
/**
* Returns the string representation of the MazeSquare object
*
* @return is the string representation
*/
public String toString() {
return "(" + Integer.toString(r) + " " + Integer.toString(c) + ")";
}
}
/**
* Construct a new Maze
*/
public Maze() {
rowList = new ArrayList<ArrayList<MazeSquare>>();
}
/**
* Load Maze in from given file
*
* @param fileName the name of the file containing the Maze structure
*/
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]);
// Second line of file is "startCol startRow"
lineParams = scanner.nextLine().split(" ");
startCol = Integer.parseInt(lineParams[0]);
startRow = Integer.parseInt(lineParams[1]);
// Third line of file is "endCol endRow"
lineParams = scanner.nextLine().split(" ");
endCol = Integer.parseInt(lineParams[0]);
endRow = Integer.parseInt(lineParams[1]);
// Read the rest of the lines (L or | or _ or -)
String line;
int rowNum = 0;
boolean top, bottom, left, right;
boolean start, end;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
rowList.add(new ArrayList<MazeSquare>());
// Loop through each cell, creating MazeSquares
for (int i = 0; i < line.length(); i++) {
// For top, check row above, if there is one
if (rowNum > 0) {
top = rowList.get(rowNum-1).get(i).hasBottomWall();
} else {
top = true;
}
// For right, check cell to the right, if there is one
if (i < line.length() - 1 ) {
char nextCell = line.charAt(i+1);
if (nextCell == 'L' || nextCell == '|') {
right = true;
} else {
right = false;
}
} else {
right = true;
}
// For left and bottom, switch on the current character
switch (line.charAt(i)) {
case 'L':
left = true;
bottom = true;
break;
case '_':
left = false;
bottom = true;
break;
case '|':
left = true;
bottom = false;
break;
case '-':
left = false;
bottom = false;
break;
default:
left = false;
bottom = false;
}
// Check to see if this is the start or end spot
start = startCol == i && startRow == rowNum;
end = endCol == i && endRow == rowNum;
// Add a new MazeSquare
rowList.get(rowNum).add(new MazeSquare(rowNum, i, top, bottom, left, right, start, end));
}
rowNum++;
}
}
/**
* Print the Maze to the Console
*/
public void print() {
LLStack<MazeSquare> soln = getSolution();
ArrayList<MazeSquare> currRow;
MazeSquare currSquare;
// Print each row of text based on top and left
for (int r = 0; r < rowList.size(); r++) {
currRow = rowList.get(r);
// First line of text: top wall
for (int c = 0; c < currRow.size(); c++) {
System.out.print("+");
if (currRow.get(c).hasTopWall()) {
System.out.print("-----");
} else {
System.out.print(" ");
}
}
System.out.println("+");
// Second line of text: left wall then space
for (int c = 0; c < currRow.size(); c++) {
if (currRow.get(c).hasLeftWall()) {
System.out.print("|");
} else {
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println("|");
// Third line of text: left wall, then space, then start/end/sol, then space
for (int c = 0; c < currRow.size(); c++) {
currSquare = currRow.get(c);
if (currSquare.hasLeftWall()) {
System.out.print("|");
} else {
System.out.print(" ");
}
System.out.print(" ");
// YOU WILL ADD CODE HERE
// If currSquare is part of the solution, mark it with *
if (currSquare.isStart() && currSquare.isEnd()) {
System.out.print("SE ");
} else if (currSquare.isStart() && !currSquare.isEnd()) {
System.out.print("S ");
} else if (!currSquare.isStart() && currSquare.isEnd()) {
System.out.print("E ");
} else {
if(soln.contains(currSquare)) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
}
System.out.println("|");
// Fourth line of text: same as second
for (int c = 0; c < currRow.size(); c++) {
if (currRow.get(c).hasLeftWall()) {
System.out.print("|");
} else {
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println("|");
}
// Print last row of text as straight wall
for (int c = 0; c < rowList.get(0).size(); c++) {
System.out.print("+-----");
}
System.out.println("+");
}
/**
* Determines what the next square over is in the input direction from the
* input square.
*
* @param s is the square whose neighbor will be returned
* @param direction is the direction that the neighbor should be from s
* @return is the neighbor to s in direction direction
*/
public MazeSquare getNeighbor(MazeSquare s, String direction) {
switch(direction) {
case "right":
if(!s.hasRightWall()) {
return rowList.get(s.getRow()).get(s.getCol() + 1);
}
break;
case "left":
if(!s.hasLeftWall()) {
return rowList.get(s.getRow()).get(s.getCol() - 1);
}
break;
case "top":
if(!s.hasTopWall()) {
return rowList.get(s.getRow() - 1).get(s.getCol());
}
break;
case "bottom":
if(!s.hasBottomWall()) {
return rowList.get(s.getRow() + 1).get(s.getCol());
}
break;
}
return null; // There is a wall in the way
}
/**
* 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() {
LLStack<MazeSquare> soln = new LLStack<MazeSquare>();
soln.push(rowList.get(startRow).get(startCol));
rowList.get(startRow).get(startCol).isVisited();
MazeSquare top = null;
while(!soln.isEmpty()) {
top = soln.peek();
if(top.isEnd()) {
return soln; // We're finished!
} else if((getNeighbor(top, "right") != null
&& !getNeighbor(top, "right").isVisited())) {
soln.push(getNeighbor(top, "right")); // Has a neighbor
getNeighbor(top, "right").visit();
} else if((getNeighbor(top, "left") != null
&& !getNeighbor(top, "left").isVisited())) {
soln.push(getNeighbor(top, "left"));
getNeighbor(top, "left").visit();
} else if((getNeighbor(top, "top") != null
&& !getNeighbor(top, "top").isVisited())) {
soln.push(getNeighbor(top, "top"));
getNeighbor(top, "top").visit();
} else if((getNeighbor(top, "bottom") != null
&& !getNeighbor(top, "bottom").isVisited())) {
soln.push(getNeighbor(top, "bottom"));
getNeighbor(top, "bottom").visit();
} else {
soln.pop(); // No unvisited neighbors
}
}
System.out.println("THIS MAZE IS BOGUS");
return new LLStack<MazeSquare>(); // Maze is unsolvable
}
// 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();
boolean solvable = false;
if(!maze.getSolution().isEmpty()) {
solvable = true;
}
// Writes maze to file
BufferedWriter bw = null;
try {
//Specify the file name and path here
File file = new File("moreMazeResults.txt");
FileWriter fw = new FileWriter(file, true);
bw = new BufferedWriter(fw);
bw.append(maze.w + " " + maze.h + " " + String.valueOf(solvable)
+ "\n");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try{
if(bw!=null) { bw.close(); }
} catch(Exception ex) {
System.out.println("Error in closing the BufferedWriter "+ex);
}
}
}
}