-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cell.js
68 lines (55 loc) · 1.34 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
function Cell(r, c) {
this.c = c;
this.r = r;
this.walls = [true, true, true, true];
this.visited = false;
this.neighbors = [];
this.cost = 0;
this.show = function (s, c) {
// debugger;
let w = size;
let x = this.c * w;
let y = this.r * w;
stroke(c);
// strokeCap(PROJECT);
strokeWeight(s);
// top
if (this.walls[0])
line(x, y, x + w, y);
// right
if (this.walls[1])
line(x + w, y, x + w, y + w);
// bottom
if (this.walls[2])
line(x, y + w, x + w, y + w);
// left
if (this.walls[3])
line(x, y, x, y + w);
}
this.checkNeighbors = function () {
let neighbors = [];
let top = grid[getIndex(this.r - 1, this.c)];
let right = grid[getIndex(this.r, this.c + 1)];
let bottom = grid[getIndex(this.r + 1, this.c)];
let left = grid[getIndex(this.r, this.c - 1)];
(top && !top.visited) && neighbors.push(top);
(right && !right.visited) && neighbors.push(right);
(bottom && !bottom.visited) && neighbors.push(bottom);
(left && !left.visited) && neighbors.push(left);
let rIndex = floor(random() * neighbors.length);
n = neighbors[rIndex];
if (n) {
let alreadyNeigbor = false;
for (const neighbor of this.neighbors) {
if (n == neighbor) {
alreadyNeigbor = true;
}
}
if (!alreadyNeigbor) {
this.neighbors.push(n);
n.neighbors.push(this);
}
}
return n;
}
}