-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.js
62 lines (60 loc) · 1.32 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
function Cell(i,j){
this.i = i;
this.j = j;
this.visited = false;
this.walls = [true, true, true, true];
this.checkNeighbors = function() {
let neighbors = [];
let top = grid[index(i, j - 1)];
let right = grid[index(i + 1, j)];
let bottom = grid[index(i, j + 1)];
let 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) {
let r = floor(random(0,neighbors.length));
return neighbors[r];
} else {
return undefined;
}
};
this.highlight = function() {
let x = this.i * w;
let y = this.j * w;
noStroke();
fill(0, 0, 255, 100);
rect(x, y, w, w);
}
this.show = function() {
let x = this.i * w;
let 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(255, 0, 255, 100);
rect(x, y, w, w);
}
};
}