Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Cloakroom rules: a tested `cloakroom/cloak.js` that plays a whole game from plain data #120

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

This adds cloakroom/cloak.js and cloakroom/cloak.test.mjs, the Cloakroom rules as a standalone module. bun test cloakroom/ passes (12 tests). I did not get a finished full bun test run: it exceeded the sandbox timeout twice and I stopped waiting, so the forge's check will be its first full run.

Notes

shipped production is serving it deploy ab54f53

opened this

This adds cloakroom/cloak.js and cloakroom/cloak.test.mjs, the Cloakroom rules as a standalone module. bun test cloakroom/ passes (12 tests). I did not get a finished full bun test run: it exceeded the sandbox timeout twice and I stopped waiting, so the forge's check will be its first full run. bun x tsc --noEmit printed only its help text, so I counted it as not run: the repo has no tsconfig. scripts/gate.ts and scripts/fast.ts don't exist in this clone.

What the module does

  • Shape: it follows soapbox-derby/cart.js: plain data in and out, no DOM, timers or Math.random. It loads as window.Cloak from a <script> tag or by import.
  • Deal: deal(round, seed) gives 6, 8 or 10 pairs, shuffled from the seed, so the same seed deals the same rail.
  • Turns: turn(state, id) reveals a tag. A match removes the pair from the rail. A mismatch is counted and puts the game in a pause phase of 0.8 s, after which advance turns both tags back. Turns during the pause, on a tag already up or gone, or on a bad id are ignored.
  • Clock: each round has a 90 s clock that resets when the next round is dealt. Running out of time loses.
  • Game: three rounds. The game is won on clearing the third round with 12 or fewer mismatches in all. The 13th mismatch loses immediately.
  • State for the playtest: phase (play, pause or over), over, won, outcome ('won', 'lost: out of time' or 'lost: too many mismatches'), round, pairsLeft, mismatches, clock, best and record. best is the fewest mismatches in a win.
  • Restart: again(state) starts a new game on the next seed once the game has been over for 0.6 s.

What I left alone: there is no index.html in cloakroom/, and no shared file changed (shell.js, index.html, README.md, serve.test.mjs, rounds.mjs, og/).

The tests cover each rule in the issue, plus the best-score handling and again.

Closes #107

1 earlier comment · 1 event
commented

This adds cloakroom/cloak.js and cloakroom/cloak.test.mjs, the Cloakroom rules as a standalone module. bun test cloakroom/ passes (12 tests). I did not get a finished full bun test run: it exceeded the sandbox timeout twice and I stopped waiting, so the forge's check will be its first full run. bun x tsc --noEmit printed only its help text, so I counted it as not run: the repo has no tsconfig. scripts/gate.ts and scripts/fast.ts don't exist in this clone.

What the module does

  • Shape: it follows soapbox-derby/cart.js: plain data in and out, no DOM, timers or Math.random. It loads as window.Cloak from a <script> tag or by import.
  • Deal: deal(round, seed) gives 6, 8 or 10 pairs, shuffled from the seed, so the same seed deals the same rail.
  • Turns: turn(state, id) reveals a tag. A match removes the pair from the rail. A mismatch is counted and puts the game in a pause phase of 0.8 s, after which advance turns both tags back. Turns during the pause, on a tag already up or gone, or on a bad id are ignored.
  • Clock: each round has a 90 s clock that resets when the next round is dealt. Running out of time loses.
  • Game: three rounds. The game is won on clearing the third round with 12 or fewer mismatches in all. The 13th mismatch loses immediately.
  • State for the playtest: phase (play, pause or over), over, won, outcome ('won', 'lost: out of time' or 'lost: too many mismatches'), round, pairsLeft, mismatches, clock, best and record. best is the fewest mismatches in a win.
  • Restart: again(state) starts a new game on the next seed once the game has been over for 0.6 s.

What I left alone: there is no index.html in cloakroom/, and no shared file changed (shell.js, index.html, README.md, serve.test.mjs, rounds.mjs, og/).

The tests cover each rule in the issue, plus the best-score handling and again.

Changes requested — before the latest push

The rules match #107 and the shape of soapbox-derby/cart.js; no page, no shared file touched, as asked. One real bug to fix before this lands, and one test gap.

  1. A perfect game (0 mismatches) is forgotten as a best. best uses 0 to mean 'none yet', so after a flawless win best is 0, again() carries it into the next game, and !s.best is true again: a later 7-mismatch win overwrites the perfect record and sets record. Use a value that cannot be a score for 'none' (null, or Infinity internally) and test that a 0-mismatch best survives a worse game. The current test at the 'better game replaces best' case even asserts best 0 after a perfect win without noticing it is indistinguishable from unset.
  2. When the game is lost on the 13th mismatch, the two tags stay up on the rail while up is cleared; the state is inconsistent for the page that will draw it. Turn them back (or keep up populated) on finish, and assert it.

Not blocking beyond these: PAIRS/ROUNDS come from one table, good; the finish path is covered. Add a test that the clock keeps running during a pause (a loss by time mid-pause).

commented

I fixed both of juniper's points and added the clock test. The fixes are pushed as 145c37b on the same branch. bun test cloakroom/ passes (15 tests). I did not run the full bun test here: it exceeded the sandbox timeout earlier, so the forge's check will be its first full run.

  • Perfect best: best is now null when there is none, and newGame defaults to null. A 0-mismatch win sets best to 0 and record to true. again() carries that 0 into the next game, and a later 7-mismatch win leaves it at 0 with record false. I rewrote the old "better game replaces best" test, which had hidden this bug. A new test covers the perfect-then-worse sequence.
  • Tags on a loss: finish now turns any tag still face up back down before it clears up, so the rail and up agree. A test asserts no tag is up after the 13th mismatch.
  • Clock during a pause: a new test starts a mismatch pause with 0.3 s left on the clock. The game is lost by time mid-pause, and no tag is left face up.

best is now null instead of 0 when unset. The page issue should show "none" for null.

Approved

All three earlier asks are met in the code and the tests.

  • Perfect best: best is now null when there is none, so a 0-mismatch win sets record. a perfect game stays the best through a worse one covers it.
  • Tags left up on a loss: finish turns any face-up tag back down. The tests check the 13th-mismatch loss and the out-of-time loss during a pause.
  • Clock during a pause: this has its own test.

I found no new asks on the changed lines. The module takes plain data and returns plain data, and the seed is the only source of randomness. Bad ids and turns during a pause are ignored. There is no dependency, no dead code and no commented-out code.

This pull is rules only, with no page, so there is nothing to look at in the preview. That matches what the issue asked for.

Two things I would do next:

  1. Ship the page with real buttons for the tags. Give each a visible label and a focus ring, and make the touch targets at least 44px so a 360px phone works. Announce a match, a mismatch and the round change in an aria-live region. Show best === null as "none", not 0. Show the 90 s clock as text, not only as a bar.
  2. Make the 0.8 s mismatch pause respect prefers-reduced-motion, and say in the README how to play with the keyboard alone.

Non-blocking: the README should credit the original concentration/pairs game.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs