-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.js
85 lines (76 loc) · 2.24 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
76
77
78
79
80
81
82
83
84
85
function Cell(i, j) {
this.i = i;
this.j = j;
// Top, Right, Bottom, Left
this.walls = [true, true, true, true]; // which walls are active in cell
this.visited = false;
// Check the neighbors around the cell
// Choose a random neighbor that exists and
// Hasn't been visited, else return undefined
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)];
// Check if there's a neighbor in each direction
// Check if they've been visited before
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 the cell has neighbors, pick one
// at random and return it
if (neighbors.length > 0) {
var r = floor(random(0, neighbors.length));
return neighbors[r];
} else {
return undefined;
}
};
// Highlight the current cell
this.highlight = function () {
var x = this.i * w + halfWindow * 0.5 + 3; // cell corner x
var y = this.j * w + 3; // cell corner y
noStroke();
fill(0, 0, 255, 100); // cell highlight color: rgb(0,0,255,100)
rect(x, y, w, w);
};
// Prints borders around the cell
this.show = function () {
var x = this.i * w + 3 + halfWindow * 0.5; // cell corner x
var y = this.j * w + 3; // cell corner y
stroke(255, 204, 0); // Cell color: rgb(255,204,0)
//Prints a line for each wall
if (this.walls[0]) {
line(x, y, x + w, y); // Top
}
if (this.walls[1]) {
line(x + w, y, x + w, y + w); // Right
}
if (this.walls[2]) {
line(x + w, y + w, x, y + w); // Bottom
}
if (this.walls[3]) {
line(x, y + w, x, y); // Left
}
// If the cell has been visited, highlight it
if (this.visited) {
noStroke(); // set the cell to visited and make it purple
fill(98, 102, 101, 75); // cell color rgb(98, 102, 101, 75)
rect(x, y, w, w);
}
};
this.lineTrace = function (direction) {
if (direction == 0) {
}
};
}