-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze_Generator.pde
63 lines (63 loc) · 1.53 KB
/
Maze_Generator.pde
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
int l= 20;
Cell current;
Cell[][] grid;
ArrayList<Cell> stack= new ArrayList<Cell>();
boolean completed= false, imageSaved= false;
void setup() {
fullScreen(P2D);
//size(600, 600, P2D);
//strokeWeight(2);
grid= new Cell[width/l][height/l];
for (int x=0; x<width/l; x++) {
for (int y=0; y<height/l; y++) {
grid[x][y]= new Cell(x, y);
}
}
noCursor();
current= grid[0][0];
}
void removeWalls(Cell a, Cell b) {
PVector dir= new PVector(a.x-b.x, a.y-b.y);
if (dir.x==-1) {
a.walls[1]= false;
b.walls[3]= false;
} else if (dir.x==1) {
a.walls[3]= false;
b.walls[1]= false;
} else if (dir.y==-1) {
a.walls[2]= false;
b.walls[0]= false;
} else if (dir.y==1) {
a.walls[0]= false;
b.walls[2]= false;
}
}
void draw() {
background(0);
current.visited= true;
ArrayList<Cell> neighbours= current.neighbours();
if (neighbours.size()>0) {
Cell next= neighbours.get(int(random(neighbours.size())));
removeWalls(current, next);
stack.add(current);
current= next;
} else if (stack.size()>0) {
current= stack.get(stack.size()-1);
stack.remove(stack.size()-1);
} else {
completed= true;
}
for (int x=0; x<width/l; x++) {
for (int y=0; y<height/l; y++) {
Cell cell= grid[x][y];
if(cell.visited) cell.show();
}
}
//saveFrame("frames/frame-#######.tif");
if (completed && !imageSaved) {
save("image/Maze.jpg");
noLoop();
imageSaved= true;
print("image saved");
}
}