-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMazeGenDF.java
More file actions
71 lines (63 loc) · 1.67 KB
/
MazeGenDF.java
File metadata and controls
71 lines (63 loc) · 1.67 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
package amazeing;
import java.awt.Point;
import java.util.Random;
import java.util.ArrayList;
/**
* Class representing a maze generator using the recursive "depth first" algorithm
* @author Nicholas
*
*/
public class MazeGenDF implements MazeGen {
private int width;
private int height;
private boolean[][] visited;
private boolean[][]open;
private Random r = new Random();
/**
* Generates maze based on a given Point
* @param p - point
*/
private void generateMaze(Point p) {
boolean deadEnd = false;
while(!deadEnd) {
ArrayList<Point> candidates = new ArrayList<Point>();
if(p.x > 0 && !visited[p.x-1][p.y])
candidates.add(new Point(p.x-1, p.y));
if(p.x < width-1 && !visited[p.x+1][p.y])
candidates.add(new Point(p.x+1, p.y));
if(p.y > 0 && !visited[p.x][p.y-1])
candidates.add(new Point(p.x, p.y-1));
if(p.y < height-1 && !visited[p.x][p.y+1])
candidates.add(new Point(p.x, p.y+1));
if(candidates.isEmpty()) {
deadEnd = true;
}
else {
int i = r.nextInt(candidates.size());
Point q = candidates.get(i);
visited[q.x][q.y] = true;
open[2*q.x+1][2*q.y+1] = true;
open[p.x+q.x+1][p.y+q.y+1] = true;
//points.push(q);
//points.push(q);
generateMaze(q);
}
}
}
@Override
/**
* Generates a maze based on a given width and height
*/
public boolean[][] generateMaze(int width, int height) {
this.width = width;
this.height = height;
visited = new boolean[width][height];
open = new boolean[2*width+1][2*height+1];
visited[0][0] = true;
open[1][1] = true;
generateMaze(new Point(0,0));
open[1][0] = true;
open[2 * width - 1][2 * height] = true;
return open;
}
}