-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.js
35 lines (31 loc) · 774 Bytes
/
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
class Cell {
constructor(x, y, alive) {
this.x = x;
this.y = y;
this.alive = alive;
this.neighbours = [];
}
draw() {
stroke(0);
fill(this.alive ? 255 : 40);
rect(this.x * cellW, this.y * cellH, cellW, cellH);
}
update() {
let liveNeighbours = 0;
let livesOn = false;
this.neighbours.forEach(neighbour => {
liveNeighbours += neighbour.alive;
});
if (this.alive) {
if (liveNeighbours < 2) livesOn = false;
else if (liveNeighbours < 4) livesOn = true;
else if (liveNeighbours > 3) livesOn = false;
} else {
if (liveNeighbours >= 3) livesOn = true;
}
bufferGrid[this.x][this.y] = livesOn;
}
setNeighbours(neighbours) {
this.neighbours = neighbours;
}
}