// m13 life rules: the next generation is a function of the whole old board, and nothing else. import { LW, LH } from './model.js' import { intBelow } from '../random.js' /** @import { Point, LifeState, LifeAction } from '../types' */ // Every cell of the board, row-major (y, then x). Filtering this list keeps any result in canonical order for free. const board = Array.from({ length: LW * LH }, (_, i) => ({ x: i % LW, y: Math.floor(i / LW) })) /** Is `p` on the board? */ const onBoard = (p) => p.x >= 0 && p.x < LW && p.y >= 0 && p.y < LH // A cell's position in row-major order: the order the canonical list is sorted by. const index = (p) => p.y * LW + p.x const offsets = [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]] /** The up to eight neighbours of `p` that are on the board (the edges are dead: nothing wraps around). */ const neighbours = (p) => offsets.map(([dx, dy]) => ({ x: p.x + dx, y: p.y + dy })).filter(onBoard) /** * One generation, B3/S23: a live cell with 2 or 3 live neighbours stays alive, a dead cell with exactly 3 is born, * every other cell is dead afterwards. Every cell is judged on the *old* list only. A Set of the old cells' indices * makes "is this neighbour alive?" one lookup, so a generation is 1200 x 8 lookups however many cells live. * @type {(cells: Point[]) => Point[]} */ export const lifeStep = (cells) => { const alive = new Set(cells.map(index)) const isAlive = (p) => alive.has(index(p)) const count = (p) => neighbours(p).filter(isAlive).length return board.filter((p) => { const n = count(p) return n === 3 || (n === 2 && isAlive(p)) }) } /** Add a dead cell or remove a live one, keeping the list sorted row-major without duplicates. Off the board: unchanged. */ const toggle = (cells, p) => !onBoard(p) ? cells : cells.some((c) => index(c) === index(p)) ? cells.filter((c) => index(c) !== index(p)) : [...cells.filter((c) => index(c) < index(p)), { x: p.x, y: p.y }, ...cells.filter((c) => index(c) > index(p))] /** * A random board from `seed`: one `intBelow(4)` per cell in row-major order, the cell alive when the draw is 0 * (about one cell in four). Returns `[cells, nextSeed]`. * @type {(seed: number) => [Point[], number]} */ const randomCells = (seed) => { const [draws, next] = board.reduce( ([ds, s], _) => { const [d, s2] = intBelow(4)(s) return [[...ds, d], s2] }, /** @type {[number[], number]} */ ([[], seed])) return [board.filter((_, i) => draws[i] === 0), next] } const nextGeneration = (state) => ({ ...state, cells: lifeStep(state.cells), generation: state.generation + 1 }) /** @type {(state: LifeState, action: LifeAction) => LifeState} */ export const lifeUpdate = (state, action) => { switch (action.tag) { case 'Frame': return state.running ? nextGeneration(state) : state case 'Step': return state.running ? state : nextGeneration(state) case 'TogglePause': return { ...state, running: !state.running } // Clicking edits the world whether it runs or not: the click is one action between two generations. case 'Toggle': return { ...state, cells: toggle(state.cells, action.cell) } case 'Clear': return { ...state, cells: [], generation: 0 } case 'Randomize': { const [cells, seed] = randomCells(state.seed) return { ...state, cells, seed, generation: 0 } } default: return state } }