-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
64 lines (57 loc) · 1.12 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
let cols, rows;
let w = 20;
let grid = [];
let current;
let stack = [];
function setup() {
createCanvas(600, 600);
cols = floor(width/w);
rows = floor(height/w);
for(let j=0;j<rows;j++) {
for(let i=0;i<cols;i++) {
var cell = new Cell(i,j);
grid.push(cell);
}
}
current = grid[0];
}
function draw() {
background(51);
for(let i=0;i<grid.length;i++) {
grid[i].show();
}
current.visited = true;
current.highlight();
let next = current.checkNeighbors();
if(next) {
next.visited = true;
stack.push(current);
removeWalls(current,next);
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) {
let 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;
}
let 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;
}
}