diff options
-rw-r--r-- | src/world/cell.js | 41 | ||||
-rw-r--r-- | src/world/cell.test.js | 56 | ||||
-rw-r--r-- | src/world/proposal.js | 8 |
3 files changed, 105 insertions, 0 deletions
diff --git a/src/world/cell.js b/src/world/cell.js new file mode 100644 index 0000000..5783d72 --- /dev/null +++ b/src/world/cell.js @@ -0,0 +1,41 @@ +'use strict'; + +export function cells_update(cells, update_rules) { + return cells + .map((row, y) => row.map((cell, x) => [x, y, cell.type])) + .flat() + .reduce((acc, [x, y, type]) => [...acc, update_rules[type](cells, x, y)], []) + .filter(x => x !== undefined) +} + + +export function cells_valid(cells, proposal) { + if (!proposal.world_updates) { return true; } + return proposal.world_updates.reduce( + (acc, update) => { + const valid = + (update.x >= 0 && update.x < cells[0].length) && + (update.y >= 0 && update.y < cells.length) && + (cells[update.y][update.x].type == update.from) + return valid && acc; + }, + true + ); +} + + +export function cells_apply(cells, proposals) { + return proposals.reduce( + (acc, prop) => { + const change = (prop.world_updates || []).reduce( + (acc_, update) => { + acc_[update.y][update.x].type = update.to; + return acc_ + }, + [...acc] + ); + return change; + }, + [...cells] + ); +} diff --git a/src/world/cell.test.js b/src/world/cell.test.js new file mode 100644 index 0000000..a3749e5 --- /dev/null +++ b/src/world/cell.test.js @@ -0,0 +1,56 @@ +'use strict'; + +import { cells_update, cells_valid, cells_apply } from './cell.js'; + + +test("growth update rule", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + const update_rules = { + plant: () => {}, + empty: (cells, x, y) => { + if (cells[y][x+1].type === 'plant') { + return { world_updates: [{ x, y, from: 'empty', to: 'plant' }] }; + } + }, + }; + + expect(cells_update(cells, update_rules)).toEqual([ + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + cells[0][1] = { type: 'plant' }; + + expect(cells_update(cells, update_rules)).toEqual([ + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + cells[0][0] = { type: 'plant' }; + + expect(cells_update(cells, update_rules)).toEqual([]); +}); + + +test("growth update rule applied", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + expect(cells_apply(cells, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([ + [ { type: 'empty' }, { type: 'plant' }, { type: 'plant' } ], + ]); + + expect(cells_apply(cells, [ + { world_updates: [{ x: 2, y: 0, from: 'plant', to: 'empty' } ]}, + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' } ]}, + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' } ]}, + ])).toEqual([ + [ { type: 'plant' }, { type: 'plant' }, { type: 'empty' } ], + ]); +}); + + +test("check proposals agains cells for validity", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + expect(cells_valid(cells, { world_updates: [{ x: -1, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 0, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'blah' }] })).toBe(true); + expect(cells_valid(cells, { world_updates: [{ x: 2, y: 0, from: 'empty', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 2, y: 1, from: 'empty', to: 'blah' }] })).toBe(false); +}); diff --git a/src/world/proposal.js b/src/world/proposal.js index d1f2eb0..775466c 100644 --- a/src/world/proposal.js +++ b/src/world/proposal.js @@ -10,6 +10,14 @@ import { pairs, deepEqual } from '../util.js'; * } */ + +/* cell structure: + * { + * type: string + * flags: object + * } + */ + /* action structure: * { * name: string, |