summaryrefslogtreecommitdiff
path: root/src/simulation/lattice_rules.js
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-09 16:46:33 -0600
committersanine <sanine.not@pm.me>2023-11-09 16:46:33 -0600
commit55b714abf83e01aa0ff513ad6ba4978f4b4da6cd (patch)
tree71518c2a6546681724f095424b755c2ab1ec8ecd /src/simulation/lattice_rules.js
parent819d8a51c5ba8b1bec06163dba9c3e0212f1120a (diff)
add basic game of life lattice rules
Diffstat (limited to 'src/simulation/lattice_rules.js')
-rw-r--r--src/simulation/lattice_rules.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/simulation/lattice_rules.js b/src/simulation/lattice_rules.js
new file mode 100644
index 0000000..0f46d0d
--- /dev/null
+++ b/src/simulation/lattice_rules.js
@@ -0,0 +1,53 @@
+import { pairs } from '../util.js';
+
+function mod(k, n) {
+ return ((k % n) + n) % n;
+}
+
+function pos_wrap(lattice, x, y) {
+ const height = lattice.length;
+ const width = lattice[0].length;
+ return [mod(x, width), mod(y, height)];
+}
+
+
+function neighbors(lattice, x, y) {
+ const offsets = [-1, 0, 1];
+ const positions = pairs(offsets, offsets)
+ .filter(([dx, dy]) => dx !== 0 || dy !== 0)
+ .map(([dx, dy]) => pos_wrap(lattice, x+dx, y+dy));
+ const neighbors = positions
+ .map(([x, y]) => [x, y, lattice[y][x]]);
+ return neighbors;
+}
+
+
+export const lattice_rules = {
+
+ empty: (lattice, x, y) => {
+ const num_active_neighbors = neighbors(lattice, x, y)
+ .map(([x, y, cell]) => cell.type)
+ .filter(type => type === 'mutable' || type === 'active')
+ .length;
+ if (num_active_neighbors === 3) {
+ return { world_updates: [{ x, y, from: 'empty', to: 'active' }] };
+ }
+ },
+
+ active: (lattice, x, y) => {
+ const num_active_neighbors = neighbors(lattice, x, y)
+ .map(([x, y, cell]) => cell.type)
+ .filter(type => type === 'mutable' || type === 'active')
+ .length;
+ if (num_active_neighbors < 2) {
+ return { world_updates: [{ x, y, from: 'active', to: 'empty' }] };
+ } else if (num_active_neighbors > 3) {
+ return { world_updates: [{ x, y, from: 'active', to: 'empty' }] };
+ }
+ },
+
+ mutable: () => {},
+ immutable: () => {},
+ flag: () => {},
+
+};