diff options
author | sanine <sanine.not@pm.me> | 2023-11-11 15:04:36 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-11 15:04:36 -0600 |
commit | f7b0ed22b2fd0ddafc4a84dde1ad8e3208144844 (patch) | |
tree | e290cda13f58eb9a2e3cf2e5e7b05748a0c8e982 /src/simulation | |
parent | c2466a35fb1edce6088b72df17444631f7034e07 (diff) |
gol changes make sound
Diffstat (limited to 'src/simulation')
-rw-r--r-- | src/simulation/lattice_rules.js | 13 | ||||
-rw-r--r-- | src/simulation/lattice_rules.test.js | 108 |
2 files changed, 69 insertions, 52 deletions
diff --git a/src/simulation/lattice_rules.js b/src/simulation/lattice_rules.js index 0f46d0d..db027a3 100644 --- a/src/simulation/lattice_rules.js +++ b/src/simulation/lattice_rules.js @@ -30,7 +30,10 @@ export const lattice_rules = { .filter(type => type === 'mutable' || type === 'active') .length; if (num_active_neighbors === 3) { - return { world_updates: [{ x, y, from: 'empty', to: 'active' }] }; + return { world_updates: [{ + x, y, from: 'empty', to: 'active', + flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] }, + }]}; } }, @@ -39,10 +42,14 @@ export const lattice_rules = { .map(([x, y, cell]) => cell.type) .filter(type => type === 'mutable' || type === 'active') .length; + const die = { world_updates: [{ + x, y, from: 'active', to: 'empty', + flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] }, + }]}; if (num_active_neighbors < 2) { - return { world_updates: [{ x, y, from: 'active', to: 'empty' }] }; + return die; } else if (num_active_neighbors > 3) { - return { world_updates: [{ x, y, from: 'active', to: 'empty' }] }; + return die; } }, diff --git a/src/simulation/lattice_rules.test.js b/src/simulation/lattice_rules.test.js index e541add..4b78116 100644 --- a/src/simulation/lattice_rules.test.js +++ b/src/simulation/lattice_rules.test.js @@ -13,85 +13,95 @@ function apply(f, n, x0) { test("blinker", () => { const L = { type: 'active', flags: {} }; - const D = { type: 'empty', flags: {} }; + const _ = { type: 'empty', flags: {} }; + const l = { type: 'active', flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] } }; + const d = { type: 'empty', flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] } }; const lattice = [ - [ D, D, D, D, D ], - [ D, D, D, D, D ], - [ D, L, L, L, D ], - [ D, D, D, D, D ], - [ D, D, D, D, D ], + [ _, _, _, _, _ ], + [ _, _, _, _, _ ], + [ _, L, L, L, _ ], + [ _, _, _, _, _ ], + [ _, _, _, _, _ ], ]; const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] }; expect(world_update(world).lattice).toEqual([ - [ D, D, D, D, D ], - [ D, D, L, D, D ], - [ D, D, L, D, D ], - [ D, D, L, D, D ], - [ D, D, D, D, D ], + [ _, _, _, _, _ ], + [ _, _, l, _, _ ], + [ _, d, L, d, _ ], + [ _, _, l, _, _ ], + [ _, _, _, _, _ ], + ]); + expect(world_update(world_update(world)).lattice).toEqual([ + [ _, _, _, _, _ ], + [ _, _, d, _, _ ], + [ _, l, L, l, _ ], + [ _, _, d, _, _ ], + [ _, _, _, _, _ ], ]); - expect(world_update(world_update(world)).lattice).toEqual(lattice); }); test("glider", () => { const L = { type: 'active', flags: {} }; - const D = { type: 'empty', flags: {} }; + const _ = { type: 'empty', flags: {} }; + const l = { type: 'active', flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] } }; + const d = { type: 'empty', flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] } }; const lattice = [ - [ D, D, D, D, D, D ], - [ D, D, D, L, D, D ], - [ D, L, D, L, D, D ], - [ D, D, L, L, D, D ], - [ D, D, D, D, D, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, _, L, _, _ ], + [ _, L, _, L, _, _ ], + [ _, _, L, L, _, _ ], + [ _, _, _, _, _, _ ], + [ _, _, _, _, _, _ ], ]; const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] }; //expect(world_update(world).lattice).toEqual([ expect(apply(world_update, 1, world).lattice).toEqual([ - [ D, D, D, D, D, D ], - [ D, D, L, D, D, D ], - [ D, D, D, L, L, D ], - [ D, D, L, L, D, D ], - [ D, D, D, D, D, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, l, d, _, _ ], + [ _, d, _, L, l, _ ], + [ _, _, L, L, _, _ ], + [ _, _, _, _, _, _ ], + [ _, _, _, _, _, _ ], ]); expect(apply(world_update, 2, world).lattice).toEqual([ - [ D, D, D, D, D, D ], - [ D, D, D, L, D, D ], - [ D, D, D, D, L, D ], - [ D, D, L, L, L, D ], - [ D, D, D, D, D, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, d, l, _, _ ], + [ _, _, _, d, L, _ ], + [ _, _, L, L, l, _ ], + [ _, _, _, _, _, _ ], + [ _, _, _, _, _, _ ], ]); expect(apply(world_update, 3, world).lattice).toEqual([ - [ D, D, D, D, D, D ], - [ D, D, D, D, D, D ], - [ D, D, L, D, L, D ], - [ D, D, D, L, L, D ], - [ D, D, D, L, D, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, _, d, _, _ ], + [ _, _, l, _, L, _ ], + [ _, _, d, L, L, _ ], + [ _, _, _, l, _, _ ], + [ _, _, _, _, _, _ ], ]); expect(apply(world_update, 4, world).lattice).toEqual([ - [ D, D, D, D, D, D ], - [ D, D, D, D, D, D ], - [ D, D, D, D, L, D ], - [ D, D, L, D, L, D ], - [ D, D, D, L, L, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, _, _, _, _ ], + [ _, _, d, _, L, _ ], + [ _, _, l, d, L, _ ], + [ _, _, _, L, l, _ ], + [ _, _, _, _, _, _ ], ]); }); test("beehive", () => { const L = { type: 'active', flags: {} }; - const D = { type: 'empty', flags: {} }; + const _ = { type: 'empty', flags: {} }; const lattice = [ - [ D, D, D, D, D, D ], - [ D, D, L, L, D, D ], - [ D, L, D, D, L, D ], - [ D, D, L, L, D, D ], - [ D, D, D, D, D, D ], + [ _, _, _, _, _, _ ], + [ _, _, L, L, _, _ ], + [ _, L, _, _, L, _ ], + [ _, _, L, L, _, _ ], + [ _, _, _, _, _, _ ], ]; const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] }; |