-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
55 lines (47 loc) · 1.17 KB
/
sketch.js
File metadata and controls
55 lines (47 loc) · 1.17 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
var rows, cols;
var w = 20;
var grid = [];
var current; //current cell
var stack = [];
function setup() {
createCanvas(400, 400);
cols = floor(width / w);
rows = floor(height / w);
for (var y = 0; y < rows; y++) {
for (var x = 0; x < cols; x++) {
var cell = new Cell(x, y);
grid.push(cell);
}
}
current = grid[0];
// frameRate(4);
}
function draw() {
background(51);
for (var i = 0; i < grid.length; i++) {
grid[i].show();
}
//Recursive backtracker - Step 1
current.visited = true;
current.highlight();
var next = current.checkNeighbors();
if (next) {
next.visited = true;
//Recursive backtracker - Step 2.1.2
stack.push(current);
//Recursive backtracker - Step 2.1.3
removeWalls(current, next);
current = next;
} else if (stack.length > 0) {
current = stack.pop();
}
}
//Formula to get the cell in the grid
function index(i, j) {
//Checking the edge cases (cells with 3 neighbors)
if (!(i < 0 || j < 0 || i > cols - 1 || j > rows - 1)) {
return i + j * cols;
} else {
return -1;
}
}