Makes every game's rules a pure, deterministic TypeScript engine behind one game contract: the same
seed and moves give the same states on iOS, Android, Jest and Node, so bots, solvers, saves, undo,
replays and the daily level can all trust it. The skill ships the kit (src/core/game/: the contract
types, the difficulty scale and the test helpers), a complete template game (the public-domain puzzle
Lights Out) and a checker that proves the kit, the seeded RNG against its golden values and each
game's rules, by running them.
Rules that must hold
- The engine functions are pure and live in
src/games/<game-id>/rules/, one per file:create.ts,list-moves.ts,apply-move.ts,outcome.ts, importing onlysrc/coreand the game's ownrules/,levels/andgeom/. Why: bots, solvers, goldens and save replays call them headless thousands of times; one React Native, store or app import breaks all of that. - Rules follow the determinism policy: only
+ - * /,%, bitwise operators andMath.sqrt/imul/floor/round/abs/min/max/PI; no**, no otherMath.*, noMath.random,Date,performance.now,Intl,toLocale*orlocaleCompare(tests: no clock, no randomness either). Why: Hermes uses each platform's libm and locale data while Jest uses V8's, so replays and daily levels would diverge between an iPhone, an Android phone and CI (seeded-determinism-and-golden-vectors owns the policy). - All randomness comes from
src/core/rng/sfc32.ts, threaded as state. A game that draws aftercreate()keepsrng: RngStatein its state and every draw advances it; never re-seed from the move count. Why: same seed, same level, same daily and same replay on every device, and a continue or an undo cannot re-roll. sfc32.tsand its golden values never change:seedRng(1)draws1828152527, 3394835397, 2967886022, 2251045104, 4148684523andhashSeed('tasks:2026-09-26')is2847463752. Why: they are the compatibility contract of every generated level, daily and saved run; a different generator is a new module under a new name, and a question for the developer.- Copy the kit verbatim from
templates/src/core/game/and never rename a member (create,listMoves,applyMove,outcome,panMode,selectRegions,intentToMove,hud,undo,hints,continueRun;identityis{ id, nameId }). Why: the session host, the bots, the solver, the canvas skills and the checker all call these exact names and shapes. - The input is declared as data next to its intent function:
panMode('none'taps only,'swipe','drag','aim') andselectRegionsare literals in the module, andintentToMove(insrc/games/<game-id>/canvas/) returns only moveslistMoveslists and only for the intents that pan mode sends. Why: the screen enables exactly that gesture; a game expecting swipes on a'none'surface is unplayable, and legality decided anywhere butlistMoveslets bots and players disagree. - Difficulty is one scale, whole numbers 0..100 (
difficulty.ts):createclamps to it, tuning rows come fromrowFor, levels and the daily use 0..99, and 100 (ENDLESS_DIFFICULTY) is the endless run of a game with an endless mode, which is never won. Why: the curve, the tuning, the daily and the saved runs must mean the same numbers, and there is no mode argument to tell an endless run apart. - A selection is UI state, never a move. Tap-then-tap games list their selecting regions in
selectRegions([]for most);intentToMovereturnsnullfor a tap there and readsintent.selectedon the next tap. Why: aselectmove would be seen by bots, the solver, par, undo and the counters. - State, moves and events are JSON-safe: plain objects, arrays, finite numbers, strings, booleans,
null; noMap,Set,Date, classes,undefinedor typed arrays. Why: runs are saved as JSON after every move and replayed; anything JSON drops corrupts a save. applyMovenever changes its input and throwsRangeErrorfor an illegal move. Why: the undo history keeps old states by reference, and a loud failure beats a silently corrupted save.- **
listMovesis empty exactly whenoutcomeis notplaying, in a fixed order; each way of losing has its own<game-id>.lose.<reason>key in every catalog of the app; win scores are whole numbers= 0.** Why: bots, solvers and the host's end-of-run logic rely on it, and every visible sentence is a translated message.
- Events are past tense, kebab-case, and carry every id and from/to value the animation and the counters need. Why: the surface only receives the final state; the events alone explain how it got there.
- Every rules folder has example tests, fast-check properties (determinism, invariants, end state,
JSON round trip) and one pinned
GOLDEN_*value, at 95/95/95/90 coverage. Why: the tests are the specification, and properties alone let real bugs survive. - A saved run parses back to exactly itself.
parseState/parseMoverebuild a fresh typed value with explicit checks (neverjson as State) and returnnullotherwise;stateVersionandmigrateStatehandle older shapes. Why: a damaged or old save must drop only the run, not crash the app later. - The testing spec plays fair: the bot picks only offered moves and returns its own seeded stream,
and the
start,middle,win,loseexamples have exactly the outcome their names promise. Why: balance runs, hints, screenshots and E2E setup are built on them. - Once the canvas exists, the game is one typed
GameModuleinsrc/games/<game-id>/<game-id>-game.tswhoseidentity.idis the folder name, with a test that runs the contract with its input. Why: the host binds games only through this value, and the id names the folder, the catalog namespace and every saved run.
Workflow
- Classify and plan. Read references/engine-contract.md. Classify
the game (turn-based, simulate-then-replay, real-time) and write down in plain words: moves, win, each
way of losing, score, undo, hints, continue, an endless mode (difficulty 100), the input (
panMode, tap-then-tapselectRegions). If the design does not say, ask the developer (defaults: unlimited undo, or none when the game draws during play; no hints; no continue; no endless mode; taps only). - Install the kit once per app. Pin fast-check exactly in the root
devDependencies(the kit's, the RNG's and the rules' tests import it; without ittscfails with TS2307 on every copied test):npm install -D -E fast-check@4.10.2(verified on 2026-10-03; re-verify:npm view fast-check version). Runnode ${CLAUDE_SKILL_DIR}/scripts/check-rules-engine.mjs .; for eachkit-file-missingcopy the file fromtemplates/src/core/tosrc/core/verbatim (with its test).src/core/rng/sfc32.tsis canonical: if seeded-determinism-and-golden-vectors already put it there, keep that copy. Akit-member-missingline names an older copy: replace it and fix whattscreports. Read references/determinism-and-rng.md before anything random. - Copy the rules templates.
templates/src/games/__GAME_ID__/tosrc/games/<game-id>/, renaming files and replacing__GAME_ID__(kebab case),__GAME_PASCAL__and__GAME_CONST__(UPPER_SNAKE). The template is a complete small game (Lights Out with a move budget); keep its structure and replace its logic test by test. Every balance number lives in<game-id>-tuning.ts. - Add the catalog keys the templates name to every catalog of the app (
src/i18n/catalogs/<locale>.json, i18n-strings-and-catalogs): assets/template-catalog-keys.json holds each key with an English and an RTL text (.name,.hud.moves,.continue.more-moves,.lose.out-of-moves,.stats.*). A renamed or new key (a lose reason, a hud line, a counter) goes into every catalog too, and translations go through the app's review queue. - Write the rules test-first, in this order, following references/writing-rules.md
and references/testing-rules.md: types, tuning and
create(with its pinned golden),outcomeandlistMoves,applyMove, the rules members, persistence, counters, then the bot and example states. For each behaviour: one failing test, read the failure (an assertion, not an import error), minimum code, green. Imitateexamples/pig/for a game that draws during play, has an endless run or more than one way of losing. - Assemble the engine (
rules/<game-id>-engine.ts:<CONST>_ENGINEand<CONST>_RULES) and keep its contract test (<game-id>-engine.test.ts; addendlessfor a game with an endless mode). - Assemble the module once the canvas exists. When canvas-gestures-and-hit-testing has written
src/games/<game-id>/canvas/intent-to-move.ts, write<game-id>-game.tsand<game-id>-game.test.tsfromexamples/lights-out-module/, withpanModeandselectRegionsfrom step 1 and the intents the surface really sends. Until then the checker prints one SKIP for the module. - Format, lint and type-check:
npx eslint --fix src/games/<game-id> src/core/game,npx prettier --write src/games/<game-id>(import order depends on the game id),npm run typecheck. - Run the tests with coverage:
npx jest src/games/<game-id> --ci --selectProjects unit --coverage --collectCoverageFrom='src/games/<game-id>/rules/**/*.ts' --coverageThreshold='{}'(paths first;--coverageThreshold='{}'because a subset run cannot meet the global numbers) and read the table: rules stay at 95/95/95/90. When a module is finished, run Stryker on it (break threshold 75 %). - Run the check
node ${CLAUDE_SKILL_DIR}/scripts/check-rules-engine.mjs . --game <game-id>; fix everyFAILline (file, rule, fix) and rerun untilRESULT: PASS. Anengine-contractline names the seed and difficulty: add that case as an example test first, then fix the engine. Rules, options and troubleshooting: references/checker.md.
Definition of done
- The root
package.jsonpinsfast-checkexactly indevDependencies(nokit-test-dependency). -
src/core/game/holds the kit (ten contract files,difficulty.test.ts, the five testing helpers with their tests) andsrc/core/rng/sfc32.tswith its golden test, all passing. -
src/games/<game-id>/rules/has the types, tuning,create,list-moves,apply-move,outcome, engine, persistence and stats files, each with tests;testing/<game-id>-testing.tshas a bot and the four example states. - Every rules test file follows the loop: examples, fast-check properties, one
GOLDEN_*value; the rules folder is at 95/95/95/90. - Every catalog key the rules and the module name (hud, stats, continue, each
.lose.<reason>,.name) is in every catalog of the app. - Once the canvas exists,
<game-id>-game.tssetspanModeandselectRegions, and its test runsengineContractProblemswith the input (andendlesswhen the game has an endless mode) and returns[]. -
npm run typecheck,npx eslint --max-warnings 0andnpx prettier --checkpass forsrc/core/gameandsrc/games/<game-id>; no__GAME_placeholder is left. -
node ${CLAUDE_SKILL_DIR}/scripts/check-rules-engine.mjs . --game <game-id>printsRESULT: PASS
A passing run may print SKIP src/games/<game-id>/canvas/intent-to-move.ts [module-missing] not yet created (canvas-gestures-and-hit-testing creates it)
before the canvas surface exists, and SKIP src/i18n/catalogs [catalog-key-missing] not yet created (i18n-strings-and-catalogs creates it)
before the app has catalogs. An app with no game prints NOT APPLICABLE: the app has no game (...).
Anti-patterns
Math.random()"just for a quick shuffle", or a seed from the clock. Replays and daily levels break silently; draw fromstate.rng(ordrawOne/shufflefromsrc/core/rng/seeded-draws.ts).- Re-seeding inside
applyMovefrom the move count to avoid storing the stream. Two runs that reach the same position by different paths then draw different futures, and a continue re-rolls. - Mutating the state in place for speed (
state.cells[i] = 1). The undo stack and the saved snapshot share references; build new arrays withmapand spreads. - Returning the old state for an illegal move. The bug hides until a save is corrupted; throw.
- Legality in the gesture code, the screen or the store. Bots and solvers would disagree with
players; only
listMovesdecides, andintentToMoveasks it. - Choosing the pan mode in the screen. It belongs to the module next to
intentToMove, where the contract test proves the two agree. - A
selectmove for tap-then-tap. It pollutes bots, par, undo and the counters; list the region inselectRegionsand readintent.selected. - A
modeargument, or a tuning table indexed 0..3. Difficulty is one 0..100 scale; pick rows withrowForand mark the endless run with difficulty 100. - Casting
json as StateinparseState. A damaged or old save then crashes later; rebuild the typed value with explicit checks and returnnullotherwise. - Sentences in rules (
reasonKey: 'Out of moves!'). Return<game-id>.lose.<reason>keys; the app's i18n layer formats them in the user's language, left to right or right to left. - Editing a golden to make a test pass. A changed golden means shipped levels changed: change it
only on purpose, with a
Gate-Change:trailer, and never the RNG goldens. Math.atan2for a direction or**for a square. Use dot products, committed direction tables andx * x.
Files in this skill
| File | What it is | Read/run when |
|---|---|---|
| references/engine-contract.md | The contract member by member, where code lives, difficulty and endless, input and selection, rules, persistence, stats, testing, the module, contract tests, game classes, multi-package | Workflow step 1 |
| references/determinism-and-rng.md | The determinism policy applied to rules and why, the RNG API and goldens, randomness in create and during play, sims, particles, bots, the daily level |
Step 2, and before anything random |
| references/writing-rules.md | Types, tuning and create, outcome and listMoves, applyMove, rules members, persistence, counters, bot, engine and module assembly, size limits | Steps 3, 5, 6 and 7 |
| references/testing-rules.md | The loop, names, examples, the property checklist, pinned goldens, both contract tests, gates, commands, traps | Steps 5 and 9 |
| references/checker.md | The checker's options, config keys, every rule, expected SKIP and NOT APPLICABLE lines, multi-package, troubleshooting | Step 10, when a line is unclear |
templates/src/core/game/ |
The kit: ten contract files (copy verbatim) and difficulty.test.ts. This skill owns them; their canonical copy is synced into level-generation-and-solvers, game-session-host and game-balance-and-bots, so a change here reaches all four |
Step 2 |
templates/src/core/game/testing/ |
play-choices, play-bot, json-shape, engine-contract, contract-intents, each with its test |
Step 2 |
templates/src/core/rng/ |
sfc32.ts and its golden test (synced; seeded-determinism-and-golden-vectors owns them) |
Step 2 |
templates/src/games/__GAME_ID__/rules/ |
The template game (Lights Out): types, tuning, create, toggle-cells, list-moves, apply-move, outcome, engine, persistence, stats, and their tests | Step 3 |
templates/src/games/__GAME_ID__/testing/ |
Greedy bot and example states, with a test | Step 3 |
examples/lights-out-module/ |
The module assembly with its contract test and the shape of the canvas intent function (canvas-gestures-and-hit-testing owns that file) | Step 7 |
examples/pig/ |
Pig, a dice game that draws during play: the stream in the state, two ways of losing, an endless row, a continue, persistence of the stream, the hold-at-20 policy in rules/ (level-generation-and-solvers' witness uses the same file) re-exported as the bot, tests that read every knob from TUNING, and catalog-keys.json |
Step 5, for games that draw during play |
assets/template-catalog-keys.json |
Every catalog key the templates name, with an English and an RTL (Arabic) text | Step 4 |
assets/template-placeholders.json |
The values the self-test and the template verification fill in (lights-out) |
When adding a placeholder |
assets/shared.json |
Declares the synced files (check-lib, app-modules, the canonical RNG, and this skill's own kit, template rules and Pig hold policy, whose canonical copies other games skills sync) | When adding a shared file |
scripts/check-rules-engine.mjs |
Checker: kit files and members, the fast-check pin, RNG goldens (run for real), purity, determinism, JSON state, events, throws, reason and catalog keys, tests, engine and module assembly, a seeded run with random play and the bot, endless never won | Steps 2 and 10, and at the end |
scripts/lib/ |
game-layout.mjs (folders and options), kit-checks.mjs, rules-checks.mjs, engine-run.mjs, ts-scan.mjs; app-modules.mjs is synced (do not edit here) |
When changing the checker |
scripts/selftest.mjs |
Proves the checker: the templates and the module example must pass, the Pig example must pass, every planted bug must fail under its rule | After changing the checker, a template or an example |
scripts/check-lib.mjs |
Shared script library, synced (do not edit here) | Never by hand |
tests/bases/app/ |
The minimal app (package.json, tsconfig, catalogs) the fixtures lay the templates on | When the fixture base changes |
tests/fixtures/ |
check-rules-engine/: good/, one bad-* per rule, pass-* (before the canvas, before the catalogs, no game, kit only, the Pig example, installed skills, a custom games folder) and error-* cases |
When adding a rule to the checker |
Related skills
seeded-determinism-and-golden-vectors- the determinism policy,sfc32, seeds, salts and golden vectors.level-generation-and-solvers- generators, solvers, par, packs, stars and the daily level built oncreate.game-session-host- hosting the module: the game screen, undo, hints, continue, saves and results.canvas-gestures-and-hit-testing-intentToMove, the intents and the selection in the screen.skia-canvas-rendering- the surface that draws the state and plays the events.realtime-game-loop- real-time sims and simulate-then-replay physics.game-balance-and-bots- bot policies, simulations and the tuning file.new-game-scaffold- declaring the game and its catalog keys before the rules.unit-and-component-tests- Jest projects, fast-check, coverage and mutation thresholds.tdd-workflow- the red-green loop and evidence for every change.