-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.js
90 lines (80 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
86
87
88
89
90
function Cell(i,j){
this.i = i;
this.j = j;
this.x = i * size;
this.y = j * size;
this.walls = [true,true,true,true];
this.visited = false;
this.draw = function(){
noFill();
stroke(255);
if (this.walls[0]){
line(this.x, this.y, this.x+size, this.y);
}
if (this.walls[1]){
line(this.x+size, this.y, this.x+size, this.y+size);
}
if (this.walls[2]){
line(this.x+size, this.y+size, this.x, this.y+size);
}
if (this.walls[3]){
line(this.x, this.y+size, this.x, this.y);
}
if (this.visited){
noStroke();
fill(255, 0, 255, 100);
rect(this.x,this.y,size,size);
} else {
noStroke();
fill(255,255,255, 100);
rect(this.x,this.y,size,size);
fill(0,0,200);
rect(this.x+(size / 2 - 1), this.y+(size / 2 - 1), size/4, size/4);
}
}
this.drawse = function(){
fill(200,0,200);
noStroke();
rect(this.x, this.y, size, size);
noFill();
stroke(255);
if (this.walls[0]){
line(this.x, this.y, this.x+size, this.y);
}
if (this.walls[1]){
line(this.x+size, this.y, this.x+size, this.y+size);
}
if (this.walls[2]){
line(this.x+size, this.y+size, this.x, this.y+size);
}
if (this.walls[3]){
line(this.x, this.y+size, this.x, this.y);
}
}
}
Cell.prototype.checkNeighbors = function(){
let neighbors = [];
let top = grid[index(this.i,this.j-1)];
if (top && !top.visited){
neighbors.push(top);
}
let bottom = grid[index(this.i,this.j+1)];
if (bottom && !bottom.visited){
neighbors.push(bottom);
}
let left = grid[index(this.i-1, this.j)];
if (left && !left.visited){
neighbors.push(left);
}
let right = grid[index(this.i+1, this.j)];
if (right && !right.visited){
neighbors.push(right);
}
//console.log(neighbors);
if (neighbors.length > 0){
let r = floor(random(0,neighbors.length));
return neighbors[r];
} else {
return undefined;
}
}