-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameOfLife.js
61 lines (60 loc) · 1.55 KB
/
gameOfLife.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
function update() {
cellsCopy = [];
for (var x = 0; x < cellsW; x++) {
cellsCopy[x] = [];
for (var y = 0; y < cellsH; y++) {
var count = checkNeighbors(x, y);
if (cells[x][y]) {
if (count < 2) {
// Any live cell with fewer than two live neighbours dies, (underpopulation).
cellsCopy[x][y] = false;
} else if (count === 2 || count === 3) {
// Any live cell with two or three live neighbours lives on to the next generation.
cellsCopy[x][y] = true;
} else if (count > 3) {
// Any live cell with more than three live neighbours dies (overpopulation).
cellsCopy[x][y] = false;
}
} else {
if (count === 3) {
// Any dead cell with exactly three live neighbours becomes a live cell (reproduction).
cellsCopy[x][y] = true;
}
}
}
}
// Copy new array
for (var x = 0; x < cellsW; x++) {
for (var y = 0; y < cellsH; y++) {
cells[x][y] = cellsCopy[x][y];
}
}
}
function checkNeighbors(x, y) {
var count = 0;
if (x != 0 && cells[x-1][y]) {
count++;
}
if (x != 0 && y < cellsH-1 && cells[x-1][y+1]) {
count++;
}
if (x != 0 && y != 0 && cells[x-1][y-1]) {
count++;
}
if (x < cellsW-1 && y != 0 && cells[x+1][y-1]) {
count++;
}
if (y != 0 && cells[x][y-1]) {
count++;
}
if (x < cellsW-1 && cells[x+1][y]) {
count++;
}
if (y < cellsH-1 && cells[x][y+1]) {
count++;
}
if (x < cellsW-1 && y < cellsH-1 && cells[x+1][y+1]) {
count++;
}
return count;
}