This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.js
69 lines (62 loc) · 1.46 KB
/
cell.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
function Cell(i, j) {
this.i = i;
this.j = j
// walls array order: top, right, bottom, left
this.walls = [true, true, true, true];
this.visited = false;
this.highlight = function() {
var x = this.i * w;
var y = this.j * w;
noStroke();
fill(255, 0, 0, 100);
rect(x, y, w, w);
}
this.checkNeighbors = function() {
var neighbors = [];
var top = grid[index(i, j - 1)];
var right = grid[index(i + 1, j)];
var bottom = grid[index(i, j + 1)];
var left = grid[index(i - 1, j)];
if (top && !top.visited) {
neighbors.push(top);
}
if (right && !right.visited) {
neighbors.push(right);
}
if (bottom && !bottom.visited) {
neighbors.push(bottom);
}
if (left && !left.visited) {
neighbors.push(left);
}
if (neighbors.length > 0) {
var r = floor(random(0, neighbors.length));
return neighbors[r];
} else {
return undefined;
}
}
this.show = function() {
var x = this.i * w;
var y = this.j * w;
stroke(255);
if (this.walls[0]) {
line(x, y, x + w, y); // top wall
}
if (this.walls[1]) {
line(x + w, y, x + w, y + w); // right wall
}
if (this.walls[2]) {
line(x + w, y + w, x, y + w); // bottom wall
}
if (this.walls[3]) {
line(x, y + w, x, y); // left wall
}
if (this.visited) {
// color visited cell
noStroke();
fill(05, 150, 0, 100);
rect(x, y, w, w);
}
}
}