-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.js
121 lines (93 loc) · 2.12 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// cell.js
function Cell(i , j){
this.x = i;
this.y = j;
this.walls = [true, true, true, true];
this.visited = false;
this.highlight = function(r,g,b){
var i = this.x * scl;
var j = this.y * scl;
fill(r,g,b);
rect(i,j,scl,scl);
}
this.removeWalls = function(next){
// console.log("removing walls");
if(this.x - next.x === 1){
this.walls[0] = false;
next.walls[2] = false;
// console.log(this.walls);
} else if(this.x - next.x === -1) {
this.walls[2] = false;
next.walls[0] = false;
}
if(this.y - next.y === -1)
{
this.walls[1] = false;
next.walls[3] = false;
} else if(this.y - next.y === 1) {
this.walls[3] = false;
next.walls[1] = false;
}
}
this.index = function(i,j){
if(i < 0 || i > (rows - 1) || j < 0 || j > (cols - 1) ) return -1;
else return i * cols + j;
}
this.checkNeighbours = function(boolean){
var neighbours = [];
var top = grid[this.index(i-1 , j)];
var right = grid[this.index(i, j+1)];
var bottom = grid[this.index(i+1, j)];
var left = grid[this.index(i, j-1)];
if(top && !(!top.visited ^ boolean)) neighbours.push(top);
if(right && !(!right.visited ^ boolean)) neighbours.push(right);
if(bottom && !(!bottom.visited ^ boolean)) neighbours.push(bottom);
if(left && !(!left.visited ^ boolean)) neighbours.push(left);
// console.log (neighbours.length);
if(neighbours.length > 0)
{
// if(neighbours.length > 1)
return neighbours[floor(random(0,neighbours.length))];
// else
// return neighbours[neighbours.length-1];
} else {
return undefined;
}
}
this.show = function(){
var i = this.x * scl;
var j = this.y * scl;
if(this.visited)
{
noStroke();
noFill();
rect(i,j,scl,scl);
stroke(255);
}
stroke(255);
// TOP
if (this.walls[0])
{
stroke(255);
line(i ,j ,i ,j+scl);
}
// RIGHT
if (this.walls[1])
{
stroke(255);
line(i ,j + scl ,i + scl ,j+scl);
}
// BOTTOM
if (this.walls[2])
{
stroke(255);
line(i+scl ,j + scl ,i+scl,j);
}
// LEFT
if (this.walls[3])
{
stroke(255);
line(i ,j ,i+scl,j);
}
};
}