-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
80 lines (64 loc) · 1.27 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
var cols, rows;
var w = 20;
var grid = [];
var current;
var stack = [];
function setup() {
createCanvas(600, 600);
cols = floor(width / w);
rows = floor(height / w);
//frameRate(5);
for (var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var cell = new Cell(i, j);
grid.push(cell);
}
}
current = grid[0];
}
function draw() {
background(51);
for (var i = 0; i < grid.length; i++) {
grid[i].show();
}
// Depth-first search
current.visited = true;
current.highlight();
// STEP 1
var next = current.checkNeighbors();
if (next) {
next.visited = true;
// STEP 2
stack.push(current);
// STEP 3
removeWalls(current, next);
// STEP 4
current = next;
} else if (stack.length > 0) {
current = stack.pop();
}
}
function index(i, j) {
if (i < 0 || j < 0 || i > cols - 1 || j > rows - 1) {
return -1;
}
return i + j * cols;
}
function removeWalls(a, b) {
var x = a.i - b.i;
if (x === 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
var y = a.j - b.j;
if (y === 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}