-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
90 lines (81 loc) · 1.76 KB
/
sketch.js
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
const w = 50;
let rows;
let cols;
let grid;
let visited = 1;
let maxVisited;
let current;
let stack = [];
let listVisited = [];
function setup() {
createCanvas(800, 600);
rows = floor(height / w);
cols = floor(width / w);
maxVisited = rows * cols;
grid = new Grid(rows, cols);
grid.generate();
current = grid.getCell(0, 0);
stack.push(current);
current.visited = true;
listVisited.push(current);
frameRate(60);
}
function draw() {
background(57, 91, 80);
let next = current.getNeighbor(grid);
if (next) {
breakBarrier(current, next);
current = next;
stack.push(current);
current.visited = true;
listVisited.push(current);
visited++;
} else {
next = stack.pop();
current = next;
}
// colour the visited cells
for (let cell of listVisited) {
cell.fillSpecial(color(197, 209, 235));
}
// colour the current cell
current.fillSpecial(color(255, 127, 80));
grid.show();
// end when maze is generated and current returns to (0, 0)
if (visited == maxVisited) {
current.fillSpecial(color(255, 127, 80));
grid.show();
if (current == grid.getCell(0, 0)) {
noLoop();
console.log("FINSHED!!");
fill(255, 0, 0);
noStroke();
textSize(12);
textStyle(BOLD);
textAlign(CENTER, CENTER);
text("START", w / 2, w / 2);
text("FINISH", width - w / 2, height - w / 2);
}
}
}
function breakBarrier(c1, c2) {
xDiff = c1.x - c2.x;
yDiff = c1.y - c2.y;
if (yDiff == 1) {
// c1 below c2
c1.walls.top = false;
c2.walls.bottom = false;
} else if (yDiff == -1) {
// c2 below c1
c2.walls.top = false;
c1.walls.bottom = false;
} else if (xDiff == 1) {
//c1 to the right of c2
c1.walls.left = false;
c2.walls.right = false;
} else if (xDiff == -1) {
//c2 to the right of c1
c2.walls.left = false;
c1.walls.right = false;
}
}