Audit pass: - Frame rule counts frames since the last move, so every step lasts exactly movesEvery(score) frames, also right after a speed-up - clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap - Pong Restart keeps held keys - Fact corrections (mulberry32 output coverage, Haskell IO, uniform) - New fixture cases from a mutation analysis Extensions (all three tracks, shared fixtures, course text): - Turn queue (Exercise 10.6), persisted high score (10.7), replay viewer (10.8), play() end-to-end helper (9.5) - Optional State (4.7) and optional Module 7b, stack-safe IO - Module 12, property-based testing with generators, shrinking, invariants and a model-based oracle - Module 13, Conway's Life with a Click event - JS type checking via jsconfig, JSDoc and types.d.ts - Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh, Playwright end-to-end suite (e2e/, scripts/e2e.sh) Docs: docs/contract.md is the canonical contract; specs and build log under docs/superpowers. Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
75 lines
3.3 KiB
JavaScript
75 lines
3.3 KiB
JavaScript
// 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
|
|
}
|
|
}
|