-
Notifications
You must be signed in to change notification settings - Fork 0
/
cells.js
69 lines (69 loc) · 2.02 KB
/
cells.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
class Cell {
constructor(i, j) {
this.i = i;
this.j = j;
this.walls = [true, true, true, true];
this.visited = false;
this.traversed = false;
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.highlight = function () {
var x = this.i * w;
var y = this.j * w;
noStroke();
fill(100, 128, 255, 200);
rect(x, y, w, w);
};
this.show = function (r=100, g=0, b=128, a=100) {
var x = this.i * w;
var y = this.j * w;
stroke(255);
if (this.walls[0]) {
line(x, y, x + w, y);
}
if (this.walls[1]) {
line(x + w, y, x + w, y + w);
}
if (this.walls[2]) {
line(x + w, y + w, x, y + w);
}
if (this.walls[3]) {
line(x, y + w, x, y);
}
if(!this.visited){
noStroke();
fill(0, 0, 0, 0);
rect(x, y, w, w);
}
if (this.visited) {
noStroke();
fill(r, g, b, a);
rect(x, y, w, w);
}
};
}
}