-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.java
424 lines (355 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import java.util.Random;
import java.awt.Color;
/*************************************************************************
*
*************************************************************************/
import java.util.Observer;
import java.util.Observable;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Maze implements Observer {
public enum MazeType {
SINGLE_GAP, POPEN_SOLVABLE, BLANK
}
/** Updates the drawing of the maze. */
public void update(Observable o, Object arg) {
MazeExplorer me = (MazeExplorer) o;
StdDraw.clear();
draw();
for (int i = 0; i < N * N; i += 1) {
if (me.marked[i]) {
drawEdges(i, me);
}
}
for (int i = 0; i < N * N; i += 1) {
if (me.marked[i]) {
draw(i, me);
}
}
StdDraw.show(DRAW_DELAY_MS);
}
/** Returns neighbor vertices of vertex v. */
public Iterable<Integer> adj(int v) {
int x = toX(v);
int y = toY(v);
TreeSet<Integer> neighbors = new TreeSet<Integer>();
if (!wallExists(x, y, "North")) {
neighbors.add(xyTo1D(x, y + 1));
}
if (!wallExists(x, y, "East")) {
neighbors.add(xyTo1D(x + 1, y));
}
if (!wallExists(x, y, "South")) {
neighbors.add(xyTo1D(x, y - 1));
}
if (!wallExists(x, y, "West")) {
neighbors.add(xyTo1D(x - 1, y));
}
return neighbors;
}
/** Returns x coordinate for given vertex.
* For example if N = 10, and V = 12, returns 2. */
public int toX(int v) {
return v % N + 1;
}
/** Returns y coordinate for given vertex.
* For example if N = 10, and V = 12, returns 1. */
public int toY(int v) {
return v / N + 1;
}
/** Returns one dimensional coordinate for vertex in position x, y. */
public int xyTo1D(int x, int y) {
return (y - 1) * N + (x - 1);
}
// returns true if wall exists
private boolean wallExists(int x, int y, String s) {
int tx = targetX(x, s);
int ty = targetY(y, s);
boolean ooBounds = (tx == 0 || ty == 0 || tx == N+1 || ty == N+1);
if (ooBounds)
return true;
if (s.equals("North"))
return north[x][y];
if (s.equals("East"))
return east[x][y];
if (s.equals("South"))
return south[x][y];
if (s.equals("West"))
return west[x][y];
return true;
}
/** Returns number of spaces in the maze. */
public int V() {
return N * N;
}
/** Returns size of the maze. */
public int N() {
return N;
}
/** Creates a Maze from the given config file. */
public Maze(String configFilename) {
In in = new In(configFilename);
int rseed = 0;
N = 10;
DRAW_DELAY_MS = 50;
MazeType mt = MazeType.SINGLE_GAP;
double pOpen = 0.48;
String configPatternString = "(\\w+)\\s*=\\s*([a-zA-Z0-9_.]+)";
Pattern configPattern = Pattern.compile(configPatternString);
while (!in.isEmpty()) {
String thisLine = in.readLine();
if (thisLine.length() == 0 || thisLine.charAt(0) == '%') {
continue;
}
Matcher m = configPattern.matcher(thisLine);
if (m.find()) {
String variable = m.group(1);
String value = m.group(2);
switch(variable) {
case "N": N = Integer.parseInt(value); break;
case "rseed": rseed = Integer.parseInt(value); break;
case "pOpen": pOpen = Double.parseDouble(value); break;
case "DRAW_DELAY_MS": DRAW_DELAY_MS = Integer.parseInt(value); break;
case "MazeType":
if (value.equals("SINGLE_GAP")) {
mt = MazeType.SINGLE_GAP;
}
if (value.equals("POPEN_SOLVABLE")) {
mt = MazeType.POPEN_SOLVABLE;
}
if (value.equals("BLANK")) {
mt = MazeType.BLANK;
}
break;
default:
break;
}
}
}
init(rseed, pOpen, mt);
}
/** Creates a Maze with given N, random seed, parameter p (not used by
* all maze types), and a maze type. */
public Maze(int N, int rseed, double pOpen, MazeType mt) {
this.N = N;
init(rseed, pOpen, mt);
}
/** Initializes maze based on parameters set up by constructors. */
private void init(int rseed, double p, MazeType mt) {
StdDraw.setXscale(0, N+2);
StdDraw.setYscale(0, N+2);
rgen = new Random(rseed);
if (mt == MazeType.SINGLE_GAP) {
generateSingleGapMaze();
}
if (mt == MazeType.POPEN_SOLVABLE) {
generatePopenSolvableMaze(p);
}
if (mt == MazeType.BLANK) {
generateBlankMaze();
}
}
private void generateBlankMaze() {
/** Create arrays of all false. */
north = new boolean[N+2][N+2];
east = new boolean[N+2][N+2];
south = new boolean[N+2][N+2];
west = new boolean[N+2][N+2];
}
// generate the maze
private void generateSingleGapMaze(int x, int y, boolean[][] marked) {
marked[x][y] = true;
// while there is an unmarked neighbor
while (!marked[x][y+1] || !marked[x+1][y] || !marked[x][y-1] || !marked[x-1][y]) {
// pick random neighbor (could use Knuth's trick instead)
while (true) {
double r = rgen.nextDouble();
if (r < 0.25 && !marked[x][y+1]) {
north[x][y] = south[x][y+1] = false;
generateSingleGapMaze(x, y + 1, marked);
break;
}
else if (r >= 0.25 && r < 0.50 && !marked[x+1][y]) {
east[x][y] = west[x+1][y] = false;
generateSingleGapMaze(x+1, y, marked);
break;
}
else if (r >= 0.5 && r < 0.75 && !marked[x][y-1]) {
south[x][y] = north[x][y-1] = false;
generateSingleGapMaze(x, y-1, marked);
break;
}
else if (r >= 0.75 && r < 1.00 && !marked[x-1][y]) {
west[x][y] = east[x-1][y] = false;
generateSingleGapMaze(x-1, y, marked);
break;
}
}
}
}
// generate the maze starting from lower left
private void generateSingleGapMaze() {
boolean[][] marked = new boolean[N + 2][N + 2];
for (int x = 0; x < N+2; x++) marked[x][0] = marked[x][N+1] = true;
for (int y = 0; y < N+2; y++) marked[0][y] = marked[N+1][y] = true;
// initialize all walls as present
north = new boolean[N+2][N+2];
east = new boolean[N+2][N+2];
south = new boolean[N+2][N+2];
west = new boolean[N+2][N+2];
for (int x = 0; x < N+2; x++)
for (int y = 0; y < N+2; y++)
north[x][y] = east[x][y] = south[x][y] = west[x][y] = true;
generateSingleGapMaze(1, 1, marked);
}
private void generatePopenSolvableMaze(double pOpen) {
// initialize all walls as present
north = new boolean[N+2][N+2];
east = new boolean[N+2][N+2];
south = new boolean[N+2][N+2];
west = new boolean[N+2][N+2];
for (int x = 0; x < N+2; x++)
for (int y = 0; y < N+2; y++)
north[x][y] = east[x][y] = south[x][y] = west[x][y] = true;
for (int x = 1; x < N + 1; x += 1) {
for (int y = 1; y < N + 1; y += 1) {
double r = rgen.nextDouble();
if (r < pOpen) {
if (inBounds(x, y+1)) {
north[x][y] = south[x][y+1] = false;
}
}
r = rgen.nextDouble();
if (r < pOpen) {
if (inBounds(x+1, y)) {
east[x][y] = west[x+1][y] = false;
}
}
r = rgen.nextDouble();
if (r < pOpen) {
if (inBounds(x, y-1)) {
south[x][y] = north[x][y-1] = false;
}
}
r = rgen.nextDouble();
if (r < pOpen) {
if (inBounds(x-1, y)) {
west[x][y] = east[x-1][y] = false;
}
}
}
}
}
/** Returns true if (x, y) is inside the bounds of the maze. */
private boolean inBounds(int x, int y) {
return (!(x == 0 || x == N + 1 || y == 0 || y == N + 1));
}
// Returns x coordinate of target given source location
// and direction
private int targetX(int x, String s) {
if (s.equals("West"))
return x - 1;
if (s.equals("East"))
return x + 1;
return x;
}
// Returns y coordinate of target given source location
// and direction
private int targetY(int y, String s) {
if (s.equals("South"))
return y - 1;
if (s.equals("North"))
return y + 1;
return y;
}
/** Draws a filled circle of desired color c in cell i. */
private void draw(int i, Color c) {
StdDraw.setPenColor(c);
int x = toX(i);
int y = toY(i);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
}
/** Draws a filled circle of desired color c in cell i. */
private void draw(int x, int y, Color c) {
StdDraw.setPenColor(c);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
}
/* Draws the state of cell i, including any back edges. */
private void draw(int i, MazeExplorer me) {
int x = toX(i);
int y = toY(i);
if (me.marked[i]) {
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
}
if (me.distTo[i] < Integer.MAX_VALUE) {
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(x + 0.5, y + 0.5, Integer.toString(me.distTo[i]));
}
}
private void drawEdges(int i, MazeExplorer me) {
int x = toX(i);
int y = toY(i);
if (me.edgeTo[i] < Integer.MAX_VALUE) {
StdDraw.setPenColor(StdDraw.MAGENTA);
int fromX = toX(me.edgeTo[i]);
int fromY = toY(me.edgeTo[i]);
StdDraw.line(fromX + 0.5, fromY + 0.5, x + 0.5, y + 0.5);
}
}
// draw the maze
private void draw() {
StdDraw.setPenColor(StdDraw.BLACK);
for (int x = 1; x <= N; x++) {
for (int y = 1; y <= N; y++) {
if (south[x][y]) StdDraw.line(x, y, x + 1, y);
if (north[x][y]) StdDraw.line(x, y + 1, x + 1, y + 1);
if (west[x][y]) StdDraw.line(x, y, x, y + 1);
if (east[x][y]) StdDraw.line(x + 1, y, x + 1, y + 1);
}
}
}
/* Draws the maze with all spots numbered by 1D index. */
private void drawDotsByIndex() {
for (int i = 0; i < V(); i += 1) {
int x = toX(i);
int y = toY(i);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(x + 0.5, y + 0.5, Integer.toString(i));
}
StdDraw.show();
}
/* Draws the maze with all spots numbered by x, y coordinates. */
private void drawDotsByXY() {
for (int i = 0; i < V(); i += 1) {
int x = toX(i);
int y = toY(i);
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(x + 0.5, y + 0.5, Integer.toString(x) + ", " + Integer.toString(y));
}
StdDraw.show();
}
// a test client
/* public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int rseed = Integer.parseInt(args[1]);
Maze maze = new Maze(N, rseed, 0.48, MazeType.POPEN_SOLVABLE);
StdDraw.show(0);
maze.draw();
// MazeExplorer mdfp = new MazeAStarPath(maze, 4, 4, N, N);
MazeExplorer mdfp = new MazeCycles(maze);
mdfp.solve();
}*/
private int N; // dimension of maze
private boolean[][] north; // is there a wall to north of cell i, j
private boolean[][] east;
private boolean[][] south;
private boolean[][] west;
private static Random rgen;
public static int DRAW_DELAY_MS = 50;
}