-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
85 lines (75 loc) · 1.82 KB
/
cell.js
File metadata and controls
85 lines (75 loc) · 1.82 KB
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, w) {
this.i = i;
this.j = j;
this.x = i * w;
this.y = j * w;
this.w = w;
this.neighborCount = 0;
this.bee = false;
this.revealed = false;
}
Cell.prototype.show = function() {
stroke(0);
noFill();
rect(this.x, this.y, this.w, this.w);
if (this.revealed) {
if (this.bee) {
fill(127);
ellipse(this.x + this.w * 0.5, this.y + this.w * 0.5, this.w * 0.5);
} else {
fill(200);
rect(this.x, this.y, this.w, this.w);
if (this.neighborCount > 0) {
textAlign(CENTER);
fill(0);
text(this.neighborCount, this.x + this.w * 0.5, this.y + this.w - 6);
}
}
}
}
Cell.prototype.countBees = function() {
if (this.bee) {
this.neighborCount = -1;
return;
}
var total = 0;
for (var xoff = -1; xoff <= 1; xoff++) {
var i = this.i + xoff;
if (i < 0 || i >= cols) continue;
for (var yoff = -1; yoff <= 1; yoff++) {
var j = this.j + yoff;
if (j < 0 || j >= rows) continue;
var neighbor = grid[i][j];
if (neighbor.bee) {
total++;
}
}
}
this.neighborCount = total;
}
Cell.prototype.contains = function(x, y) {
return (x > this.x && x < this.x + this.w && y > this.y && y < this.y + this.w);
}
Cell.prototype.reveal = function() {
this.revealed = true;
if (this.neighborCount == 0) {
// flood fill time
this.floodFill();
}
}
Cell.prototype.floodFill = function() {
for (var xoff = -1; xoff <= 1; xoff++) {
var i = this.i + xoff;
if (i < 0 || i >= cols) continue;
for (var yoff = -1; yoff <= 1; yoff++) {
var j = this.j + yoff;
if (j < 0 || j >= rows) continue;
var neighbor = grid[i][j];
// Note the neighbor.bee check was not required.
// See issue #184
if (!neighbor.revealed) {
neighbor.reveal();
}
}
}
}