feat(js): m01 util and m02 model with tests, fixtures, purity audit
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
5be3cebe71
commit
22c221a042
@@ -0,0 +1,39 @@
|
||||
import { todo } from './todo.js'
|
||||
|
||||
/** @typedef {{x: number, y: number}} Point */
|
||||
/** @typedef {'Up'|'Down'|'Left'|'Right'} Direction */
|
||||
/** @typedef {'Playing'|'Paused'|'GameOver'|'Won'} Status */
|
||||
/** @typedef {{width: number, height: number, snake: Point[], dir: Direction, nextDir: Direction,
|
||||
* apple: Point|null, seed: number, score: number, status: Status, frame: number}} GameState */
|
||||
|
||||
/** A grid cell `{x, y}`. */
|
||||
export const point = (x, y) => todo('m02')
|
||||
|
||||
/** Structural equality: same x and same y. */
|
||||
export const pointEquals = (a, b) => todo('m02')
|
||||
|
||||
/** The unit step for a direction: Up {0,-1}, Down {0,1}, Left {-1,0}, Right {1,0} (y grows downward). */
|
||||
export const delta = (dir) => todo('m02')
|
||||
|
||||
/** The direction pointing the other way. */
|
||||
export const opposite = (dir) => todo('m02')
|
||||
|
||||
/**
|
||||
* Everything that can happen to the game, as data:
|
||||
* `Tick`, `Turn(dir)`, `TogglePause`, `Restart`, `Frame`, each `{ tag, ... }`.
|
||||
* `Turn` is a function because it carries a direction; the rest are constants
|
||||
* (the getters below only exist so the stub compiles; replace them with plain values).
|
||||
*/
|
||||
export const Action = {
|
||||
get Tick() { return todo('m02') },
|
||||
Turn: (dir) => todo('m02'),
|
||||
get TogglePause() { return todo('m02') },
|
||||
get Restart() { return todo('m02') },
|
||||
get Frame() { return todo('m02') },
|
||||
}
|
||||
|
||||
/**
|
||||
* A fresh game: 20x20, snake [(10,10),(9,10),(8,10)] heading Right (dir and nextDir),
|
||||
* apple null, the given seed, score 0, status 'Playing', frame 0.
|
||||
*/
|
||||
export const initialState = (seed) => todo('m02')
|
||||
@@ -0,0 +1,4 @@
|
||||
// Placeholder body for unimplemented stubs: replace each `todo('mNN')` call with real code.
|
||||
export const todo = (module) => {
|
||||
throw new Error(`TODO: implement in ${module}`)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { todo } from './todo.js'
|
||||
|
||||
/** `range(n)` is `[0, 1, ..., n-1]`; `range(0)` is `[]`. */
|
||||
export const range = (n) => todo('m01')
|
||||
|
||||
/** `xs` without its last element; an empty list stays empty. Never mutates `xs`. */
|
||||
export const dropLast = (xs) => todo('m01')
|
||||
|
||||
/** True if some element `x` of `xs` satisfies `eq(x, p)`. `eq` lets callers compare structurally (e.g. `pointEquals`). */
|
||||
export const contains = (xs, p, eq) => todo('m01')
|
||||
|
||||
/** `pipe(x, f, g)` is `g(f(x))`: thread a value through functions left to right. */
|
||||
export const pipe = (x, ...fns) => todo('m01')
|
||||
|
||||
/** `compose(f, g)(x)` is `f(g(x))`: functions applied right to left. */
|
||||
export const compose = (...fns) => todo('m01')
|
||||
|
||||
/** Freeze `obj` and everything reachable from it, then return it (same object). */
|
||||
export const deepFreeze = (obj) => todo('m01')
|
||||
Reference in New Issue
Block a user