-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.java
executable file
·401 lines (352 loc) · 13 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
/*
* 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.
*/
public class Maze
{
private int rows, cols;
private int maze[][][];
/**
* Returns the width of the maze.
* <p>
* <b>This method has already been implemented.</b>
*
* @return width (horizontal dimension, or number of columns) of the maze, provided on construction
*/
public int width()
{ return cols; }
/**
* Returns the height of the maze.
* <p>
* <b>This method has already been implemented.</b>
*
* @return height (vertical dimension, or number of rows) of the maze, provided on construction
*/
public int height()
{ return rows; }
/**
* Gets the title of the maze, used in the window's title bar.
* <p>
* <b>This method has already been implemented.</b>
*
* @return the maze title
*/
public String title()
{ return title; }
/**
* Indicates whether or not a cell's right wall is present in the maze
* <p>
* <b>You must implement this method.</b>
*
*
* @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
{
int[] walls = new int[2];
try
{
walls = maze[row][col];
}
catch (IndexOutOfBoundsException e)
{
System.err.print("Error: row or column was out of bounds.");
return false;
}
if (walls[0] == 1)
return true;
return false;
}
/**
* Indicates whether or not a cell's bottom wall is present in the maze
* <p>
* <b>You must implement this method.</b>
*
*
* @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
{
int[] walls = new int[2];
try
{
walls = maze[row][col];
}
catch (IndexOutOfBoundsException e)
{
System.err.print("Error: row or column was out of bounds.");
return false;
}
if (walls[1] == 1)
return true;
return false;
}
/**
* Sets whether or not the right wall of a maze cell exists.
* <p>
* <b>You must implement this method.</b>
*
*
* @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
{
int[] walls = new int[2];
try
{
walls = maze[row][col];
}
catch (IndexOutOfBoundsException e)
{
System.err.print("Error: row or column was out of bounds.");
}
if (b)
walls[0] = 1;
else
walls[0] = 0;
}
/**
* Sets whether or not the bottom wall of a maze cell exists.
* <p>
* <b>You must implement this method.</b>
*
*
* @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
{
int[] walls = new int[2];
try
{
walls = maze[row][col];
}
catch (IndexOutOfBoundsException e)
{
System.err.print("Error: row or column was out of bounds.");
}
if (b)
walls[0] = 1;
else
walls[0] = 0;
}
/**
* Runs a depth-first search to explore the maze, starting at the top-left cell (row 0, column 0).
* <p>
* <b>You must implement this method.</b>
* <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
{
}
/**
* Creates a randomly generated maze of a given size.
* <p>
* <b>You must implement this method.</b>
*
*
* @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;
int[][][] maze = new int[rows][cols][3];
for (int r = 0; r < rows; r++)
{
for(int c = 0; c < cols; c++)
{
maze[r][c][0] = 1;
maze[r][c][1] = 1;
if (r == rows - 1 && c == cols - 1)
{
maze[r][c][0] = 0;
}
}
}
//This is where we need to write the code that generates the random maze via walls
//It would working by getRight & getBot then based on those results, it would setRight/setBot
DisjointSets d = new DisjointSets(rows * cols +1); //specific row * col refers to its singleton
d.union(0,1);
Random random = new Random();
int[] walls = new int[2];
while (d.count() > 1) //when d.count = 1, maze is connected
{
int randomd = random.nextInt(d.size());
int row = (randomd/cols)-1;
if (randomd != 0 && randomd % cols == 0) //the right column
{
int col = cols-1;
walls = maze[row][col];
wallBreaker(walls, d, true, false, randomd, cols, row, col);
}
else if ( randomd > (d.size() - cols))//the bottom row
{
int col = randomd%cols;
walls = maze[row][col];
wallBreaker(walls, d, false, true,randomd, cols, row, col);
}
else //any cells besides the right columns and bottom row
{
int col = randomd%cols;
walls = maze[row][col];
wallBreaker(walls, d, true, true,randomd, cols, row, col);
}
}
}
public void wallBreaker(int[] walls, DisjointSets d, boolean breakbottom, boolean breakright, int rand, int cols, int row, int col)
{
if (breakbottom && ! breakright && d.find(rand) != d.find(rand + cols)) //right column
{
setBot(row, col, false);
d.union(rand, rand + cols);
}
else if (breakright && walls[0] == 1 && d.find(rand) != d.find(rand + 1))//bottom row
{
setRight(row, col, false);
d.union(rand, rand + 1);
if (breakbottom && d.find(rand + 1) != d.find(rand + cols))//everything else
{
setBot(row, col, false);
d.union(rand, rand + cols);
}
}
}
/**
* Creates a maze object from a file.
* <p>
* <b>This method has already been implemented.</b>
*
*
* @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);
}
title = filename;
}
/**
* 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);
}
private final String prog = "MazePlay";
private String title;
}