-
Notifications
You must be signed in to change notification settings - Fork 1
/
cell.js
75 lines (71 loc) · 2.28 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
function Cell(x,y) {
this.x = x;
this.y = y;
this.shown = false;
this.mine = false;
this.flag = false;
this.neighborCount = 0;
this.countNeighbor = function() {
for( let xoff = -1; xoff <= 1; xoff++ ) {
for( let yoff = -1; yoff <= 1; yoff++ ) {
const x = this.x-xoff;
const y = this.y-yoff;
if( outOfRange(x, cols) || outOfRange(y, rows) ) {
continue;
}
if( board[x][y].mine ) {
this.neighborCount++;
}
}
}
}
this.setShown = function() {
if( this.shown ) {
return;
}
this.shown = true;
this.flag = false;
if( this.neighborCount === 0 ) {
this.floodFill();
}
}
this.floodFill = function() {
for( let xoff = -1; xoff <= 1; xoff++ ) {
for( let yoff = -1; yoff <= 1; yoff++ ) {
const x = this.x-xoff;
const y = this.y-yoff;
if( outOfRange(x, cols) || outOfRange(y, rows) ) {
continue;
}
board[x][y].setShown();
}
}
}
this.show = function() {
stroke(0);
if( !this.shown ) {
fill(255);
rect(this.x*tileSize, this.y*tileSize, tileSize, tileSize);
if( this.flag ) {
fill(255,69,0);
offset = 6;
rect(this.x*tileSize+offset, this.y*tileSize+offset, tileSize-offset*2, tileSize-offset*2);
}
} else {
fill(200);
rect(this.x*tileSize, this.y*tileSize, tileSize, tileSize);
if( this.flag ) {
fill(255,69,0);
offset = 6;
rect(this.x*tileSize+offset, this.y*tileSize+offset, tileSize-offset*2, tileSize-offset*2);
}
if( this.mine ) {
fill(255);
ellipse(this.x*tileSize+tileSize/2, this.y*tileSize+tileSize/2, tileSize/2, tileSize/2);
} else if( this.neighborCount > 0 ) {
fill(0);
text(this.neighborCount, this.x*tileSize+tileSize/2, this.y*tileSize+tileSize/2+6);
}
}
}
}