-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.java
486 lines (365 loc) · 15.1 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* class Maze
*
* Data structure and logic for Maze
* COSC 102, Colgate University
*
* You are supposed to modify various parts of this file.
* The main program should be launched by running MazePlay.main()
*/
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
/**
* Data structure for the Maze program.
* <p>
* The class <code>Maze</code> serves both as the data structure for a maze to be played and the place where the logic for the maze solver resides.
* You are supposed to modify various parts of this file.
* <p>
* The two constructors for the Maze structure populate data that you must define so that the various get methods for the structure of the maze work properly.
* One constructor that reads the maze structure from a file is already implemented.
* You will have to implement the constructor that generates a random maze of a given number of rows and columns.
* <p>
* The <code>solve()</code> method needs to be implemented, and will contain the logic for the maze solver.
* It should run a depth-first search from the start of the maze (cell [0,0]).
* As the solver is running, it should update the display with its location using the <code>MazePlay.setState()</code> method.
* The MazePlay GUI will reflect the solver state whenever this method is called by coloring parts of the maze in the GUI.
*
* @author Sara Sirota, Yuxin David Huang '16, Colgate University
*/
public class Maze {
/** Name of the program. */
private final String prog = "MazePlay";
/** Title of the maze. */
private String title;
/** Number of rows. */
private int rows;
/** Number of columns. */
private int cols;
/** The representation of the maze as an int array. */
private int[][] maze;
/**
* Creates a randomly generated maze of a given size.
*
* @param rows the maze height (vertical dimension, number of rows)
* @param cols the maze width (horizontal dimension, number of columns)
*/
public Maze(int rows, int cols) {
title = String.format("rand(%dx%d)", rows, cols);
this.rows = rows;
this.cols = cols;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
maze[r][c] = 0;
}
}
DisjointSet ds = new DisjointSet(rows * cols);
Random rd = new Random();
while (ds.count() != 1) {
int row1 = rd.nextInt(rows);
int col1 = rd.nextInt(cols);
int row2, col2; // adjacent cell to the random cell picked
if (row1 == 0 && col1 == 0) {
// top left
// pick a random number between 0 and 1.
// if 0, then pick the cell to the right;
// if 1, then pick the cell to the bottom.
if (rd.nextInt(2) == 0) {
row2 = row1;
col2 = col1 + 1;
} else {
row2 = row1 + 1;
col2 = col1;
}
} else if (row1 == 0 && col1 == cols) {
// top right
// same way of picking as top left
if (rd.nextInt(2) == 0) {
row2 = row1;
col2 = col1 - 1;
} else {
row2 = row1 + 1;
col2 = col1;
}
} else if (row1 = rows && col1 == 0) {
// bottom left
if (rd.nextInt(2) == 0) {
row2 = row1;
col2 = col1 + 1;
} else {
row2 = row1 - 1;
col2 = col1;
}
} else if (row1 = rows && col1 == cols) {
// bottom right
if (rd.nextInt(2) == 0) {
row2 = row1;
col2 = col1 - 1;
} else {
row2 = row1 - 1;
col2 = col1;
}
} else if (row1 == 0) {
// top border but not corner
// 3 choices:
// if 0, then choose left
// if 1, then choose bottom
// if 2, then choose right
int rand = rd.nextInt(3);
if (rand == 0) {
row2 = row1;
col2 = col1 - 1;
} else if (rand == 1) {
row2 = row1 + 1;
col2 = col1;
} else {
row2 = row1;
col2 = col1 + 1;
}
} else if (row1 == rows) {
// bottom border but not corner
} else if (col1 == 0) {
// left border but not corner
} else if (col1 == cols) {
// right border but not corner
} else {
// inside
}
// finished picking a random wall
// breaking wall:
if (row2 > row1) {
if (getBot(row1, col1) == true) {
setBot(row1, col1, false);
ds.union(row1 * cols + col1, row2 * cols + col2);
}
} else if (row2 < row1) {
// fill
} else if (col2 < col1) {
// fill
} else if (col2 > col1) {
// fill
}
} // end of while
} // end of Maze(int, int)
/**
* Creates a maze object from a file.
*
* @param filename The file containing the maze to load.
* @throws IOException if an input/output error occurs while trying to read the given input file
*/
public Maze(String filename) throws IOException {
Scanner scan = null;
try {
scan = new Scanner(new FileReader(filename));
} catch (IOException e) {
throw new IOException(prog + ": error opening filename " + filename);
}
if (!scan.hasNextLine())
throw new IOException(prog + ": file " + filename + " is empty");
String[] line = scan.nextLine().split("\\s+");
if (line.length < 3 || !(line[0].equals("maze")))
throw new IOException(prog + ": " + filename + " is not a maze");
int w = 0, h = 0;
try {
w = Integer.parseInt(line[1]);
h = Integer.parseInt(line[2]);
if (w < 1 || h < 1)
throw new NumberFormatException();
} catch (NumberFormatException e) {
throw new IOException(prog + ": " + filename + " contains a maze with illegal dimension(s)");
}
this.rows = h;
this.cols = w;
for (int i = 0; i < w * h; i++) {
int col = i%w, row = i/w;
if (!scan.hasNextLine())
throw new IOException(prog + ": " + filename + " missing cell descriptions starting at [" + row + "," + col + "]");
String cell[] = scan.nextLine().split("\\s+");
if (cell.length < 2)
throw new IOException(prog + ": " + filename + " contains bad description for cell[" + row + "," + col + "]");
boolean r, b;
try {
r = (Integer.parseInt(cell[0]) != 0) ? true : false;
b = (Integer.parseInt(cell[1]) != 0) ? true : false;
} catch (NumberFormatException e) {
throw new IOException(prog + ": " + filename + " contains bad description for cell[" + row + "," + col + "]");
}
setRight(row, col, r);
setBot(row, col, b);
} // end of for
title = filename;
} // end of Maze(String)
/**
* Returns the width of the maze.
*
* @return width (horizontal dimension, or number of columns) of the maze, provided on construction
*/
public int width() {
return cols;
} // end of width()
/**
* Returns the height of the maze.
*
* @return height (vertical dimension, or number of rows) of the maze, provided on construction
*/
public int height() {
return rows;
} // end of height()
/**
* Gets the title of the maze, used in the window's title bar.
*
* @return the maze title
*/
public String title() {
return title;
} // end of title();
/**
* Indicates whether or not a cell's right wall is present in the maze.
*
* @param row row number (vertical coordinate) of the cell whose properties are being examined
* @param col column number (horizontal coordinate) of the cell whose properties are being examined
* @return <code>true</code> if the right wall of the cell is present in the maze,
* false otherwise
* @throws IndexOutOfBoundsException if <code>col < 0</code>, <code>row < 0</code>,
* <code>col > = width</code>, or <code>row >= height</code>
*/
public boolean getRight(int row, int col) throws IndexOutOfBoundsException {
return maze[row][col] == 2 || maze[row][col] == 3;
} // end of getRight(int, int)
/**
* Indicates whether or not a cell's bottom wall is present in the maze.
*
* @param row row number (vertical coordinate) of the cell whose properties are being examined
* @param col column number (horizontal coordinate) of the cell whose properties are being examined
* @return <code>true</code> if the bottom wall of the cell is present in the maze,
* false otherwise
* @throws IndexOutOfBoundsException if <code>col < 0</code>, <code>row < 0</code>,
* <code>col > = width</code>, or <code>row >= height</code>
*/
public boolean getBot(int row, int col) throws IndexOutOfBoundsException {
return maze[row][col] == 1 || maze[row][col] == 3;
} // end of getBot(int, int)
/**
* Sets whether or not the right wall of a maze cell exists.
*
* @param row row number (vertical coordinate) of the cell whose properties are being changed
* @param col column number (horizontal coordinate) of the cell whose properties are being changed
* @param b <code>true</code> if the right wall should exist, <code>false</code> if it should not exist
* @throws IndexOutOfBoundsException if <code>col < 0</code>, <code>row < 0</code>,
* <code>col > = width</code>, or <code>row >= height</code>
*/
public void setRight(int row, int col, boolean b) throws IndexOutOfBoundsException {
if (b) {
if (maze[row][col] == 0 || maze[row][col] == 1) {
maze[row][col] += 2;
}
} else {
if (maze[row][col] == 2 || maze[row][col] == 3) {
maze[row][col] -= 2;
}
}
} // end of setRight(int, int, boolean)
/**
* Sets whether or not the bottom wall of a maze cell exists.
*
* @param row row number (vertical coordinate) of the cell whose properties are being changed
* @param col column number (horizontal coordinate) of the cell whose properties are being changed
* @param b <code>true</code> if the bottom wall should exist, <code>false</code> if it should not exist
* @throws IndexOutOfBoundsException if <code>col < 0</code>, <code>row < 0</code>,
* <code>col > = width</code>, or <code>row >= height</code>
*/
public void setBot(int row, int col, boolean b) throws IndexOutOfBoundsException {
if (b) {
if (maze[row][col] == 0 || maze[row][col] == 2) {
maze[row][col]++;
}
} else {
if (maze[row][col] == 1 || maze[row][col] == 3) {
maze[row][col]--;
}
}
} // end of setBot(int, int, boolean)
/**
* Runs a depth-first search to explore the maze, starting at the top-left cell (row 0, column 0).
*
* <p>
* The parameter <code>player</code> given to this method gives access to the <code>MazePlay</code> object that represents the GUI for the maze.
* You should use the <code>setState()</code> function of <code>MazePlay</code> to keep track of where your solver is going during its exploration.
* This method is also hooked into the thread-control mechanism that keeps the solver running.
*
*
* @param player A reference to the MazePlay object that represents the GUI displaying the maze
* @throws ThreadDeath if the player window gets closed during execution of the search
*/
public void solve(MazePlay player) throws ThreadDeath {
int currRow = 0;
int currCol = 0;
player.setState(currRow, currCol, MazePlay.F);
while (!(currRow == rows - 1 && currCol == cols - 1)) {
if (getBot(currRow, currCol) == true) {
}
}
} // end of solve(MazePlay)
private void recursiveSolveHelper(int row, int col, MazePlay player) {
player.setState(row, col, MazePlay.F);
if (row == rows - 1 && col == cols - 1) {
return;
} else {
if (row == 0 && col == 0) {
// top left
if (getRight(row, col)) {
recursiveSolveHelper(row, col + 1, player)
}
} else if (row == 0 && col == cols) {
// top right
} else if (row = rows && col == 0) {
// bottom left
} else if (row = rows && col == cols) {
// bottom right
} else if (row == 0) {
// top border but not corner
} else if (row == rows) {
// bottom border but not corner
} else if (col == 0) {
// left border but not corner
} else if (col == cols) {
// right border but not corner
} else {
// inside
}
}
}
/**
* Saves a maze object to a file.
* <p>
* <b>This method has already been implemented.</b>
*
*
* @param filename The file in which to store the maze. If the file exists, it will be overwritten.
* @throws IOException if an error occurs while trying to write the maze to file
*/
public void save(String filename) throws IOException {
PrintWriter pw = new PrintWriter(filename);
int w = width();
int h = height();
pw.printf("maze %d %d%n", w, h);
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
try {
int r = getRight(row, col) ? 1 : 0;
int b = getBot(row, col) ? 1 : 0;
pw.printf("%d %d%n", r, b);
} catch (IndexOutOfBoundsException e) {
pw.println();
String err = String.format("%s: error while writing file at cell [%d,%d]", prog, row, col);
pw.println(err);
pw.close();
throw new IOException(err);
}
}
}
pw.close();
title += String.format(":%s", filename);
} // end of save(String)
} // end of Maze