-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.js
75 lines (65 loc) · 1.74 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
70
71
72
73
74
75
class Cell {
constructor(i, j) {
this.i = i;
this.j = j;
this.visited = false;
this.walls = [true, true, true, true]; //top, right, bottom, left
}
index(i, j) {
if (i < 0 || j < 0 || i > rows - 1 || j > cols - 1) {
return undefined;
}
return grid[i][j];
}
checkNeighbours() {
const neigh = [];
const top = this.index(this.i - 1, this.j);
const right = this.index(this.i, this.j + 1);
const bottom = this.index(this.i + 1, this.j);
const left = this.index(this.i, this.j - 1);
if (top && !top.visited) {
neigh.push(top);
}
if (right && !right.visited) {
neigh.push(right);
}
if (bottom && !bottom.visited) {
neigh.push(bottom);
}
if (left && !left.visited) {
neigh.push(left);
}
if (neigh.length > 0) {
const r = floor(random(neigh.length));
return neigh[r];
} else {
return undefined;
}
}
show(c, strk) {
push();
noFill();
stroke(c);
strokeWeight(strk);
const y = this.i * w;
const x = this.j * w;
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, y + w, x + w, y + w);
}
if (this.walls[3]) {
line(x, y, x, y + w);
}
if (this.visited) {
fill(0);
noStroke();
rect(x, y, w, w);
}
pop();
}
}