feat(js): m04 random and spawn with fixtures and tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-28 23:42:36 +01:00
co-authored by Claude Sonnet 5.5
parent 2e39f23bb3
commit 08b462d5cc
11 changed files with 1332 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
import { todo } from './todo.js'
/**
* A `Random<A>` is a plain function from a seed to a pair: `seed => [value, nextSeed]`.
* Seeds and values are unsigned 32-bit integers. Nothing is hidden or mutated: the caller carries the seed.
*/
/**
* The generator (mulberry32). With `s = (seed + 0x6D2B79F5) mod 2^32`:
* t = imul(s ^ (s >>> 15), s | 1)
* t = t ^ (t + imul(t ^ (t >>> 7), t | 61))
* value = (t ^ (t >>> 14)) >>> 0
* Returns `[value, s]`: the new seed is `s`.
*/
export const nextUint32 = (seed) => todo('m04')
/** A Random that always gives `a` and leaves the seed untouched. */
export const pure = (a) => todo('m04')
/** Transform the value a Random produces; the seed threading is unchanged. */
export const map = (r, f) => todo('m04')
/** Run `r`, feed its value to `f` to pick the next Random, and run that with the seed `r` left behind. */
export const flatMap = (r, f) => todo('m04')
/** A Random giving a number in `[0, n)`: `nextUint32` value modulo `n`. */
export const intBelow = (n) => todo('m04')
/** Run a Random from a seed: `[value, nextSeed]`. */
export const run = (r, seed) => todo('m04')
+17
View File
@@ -0,0 +1,17 @@
import { todo } from './todo.js'
/** Every board cell not covered by the snake, row-major (y outer loop, x inner loop). The apple does not count as occupied. */
export const freeCells = (state) => todo('m04')
/**
* Put an apple on a random free cell, drawing from `state.seed` (use `intBelow` and store the new seed).
* Unchanged if there already is an apple or the status is not 'Playing'.
* If no cell is free the board is full: status becomes 'Won' (seed unchanged, no apple).
*/
export const spawnApple = (state) => todo('m04')
/** One game step: `advance`, then make sure there is an apple. */
export const tick = (state) => todo('m04')
/** A fresh game (`initialState`) with its first apple already placed. */
export const newGame = (seed) => todo('m04')