/
game-of-life.js
188 lines (174 loc) · 3.7 KB
/
game-of-life.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React from 'react';
import { Grid } from 'react-sim';
import Model from './framed-model';
const stale = `0000010000000000010000000000
0000000100010010000000000000
1110001000000000000000000001
1010000000000000000000000000
0000000000100000000010110000
0100000101010100000000000001
0000000000010100000001000001
0000000011100100000000100001
0000010000000000000000010000
0000000000010100000000000100`
.split('\n')
.map(d => d.split('').map(Number));
// helpers
function countNeighbors(x, y, grid) {
const height = grid.length;
if (!height) {
return 0;
}
const width = grid[0].length;
let n = 0;
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
const x1 = x + xOffset;
const y1 = y + yOffset;
if (
x1 < width &&
x1 > 0 &&
y1 < height &&
y1 > 0 &&
(x1 !== x || y1 !== y)
) {
n += grid[y1][x1];
}
}
}
return n;
}
export function updateGameOfLifeGrid({ data, complete }) {
let changes = 0;
const updatedGrid = data.map((row, y) =>
row.map((cell, x) => {
const neighbors = countNeighbors(x, y, data);
if (cell && (neighbors < 2 || neighbors > 3)) {
// living cell has too few or too many neighbors, and dies.
changes++;
return 0;
}
if (!cell && neighbors === 3) {
// dead cell has the right amount of neighbors, and lives!
changes++;
return 1;
}
// no change
return cell;
})
);
if (changes === 0) {
complete();
}
return updatedGrid;
}
export function updateGridNoComplete({ data }) {
return data.map((row, y) =>
row.map((cell, x) => {
const neighbors = countNeighbors(x, y, data);
if (cell && (neighbors < 2 || neighbors > 3)) {
// living cell has too few or too many neighbors, and dies.
return 0;
}
if (!cell && neighbors === 3) {
// dead cell has the right amount of neighbors, and lives!
return 1;
}
// no change
return cell;
})
);
}
export function initGrid({ height, width, density }, random = Math.random) {
return Array(height)
.fill(0)
.map(row =>
Array(width)
.fill(0)
.map(() => Number(random() < density))
);
}
const GameOfLife = props => (
<Model
auto={false}
controls={{
param: 'density',
maxValue: 1,
step: 0.01,
resetOnChange: true,
label: 'Grid density',
}}
showTimeSlider={false}
updateData={updateGameOfLifeGrid}
delay={100}
initData={initGrid}
initialParams={{
height: 28,
width: 28,
density: 0.15,
}}
{...props}
>
<Grid />
</Model>
);
export const Step2 = () => (
<Model
initData={initGrid}
initialParams={{
height: 10,
width: 28,
density: 0.15,
}}
>
<Grid />
</Model>
);
export const Step3 = () => (
<Model
initData={() => stale}
updateData={updateGridNoComplete}
initialParams={{
height: 10,
width: 28,
density: 0.15,
}}
>
<Grid />
</Model>
);
export const Step4 = () => (
<Model
initData={() => stale}
updateData={updateGameOfLifeGrid}
initialParams={{
height: 10,
width: 28,
density: 0.15,
}}
>
<Grid />
</Model>
);
export const Step5 = () => (
<Model
controls={{
param: 'density',
resetOnChange: true,
maxValue: 1,
step: 0.01,
label: 'Grid density',
}}
showTimeSlider={false}
initData={initGrid}
updateData={updateGameOfLifeGrid}
initialParams={{
height: 10,
width: 28,
density: 0.15,
}}
>
<Grid />
</Model>
);
export default GameOfLife;