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

Migrated from github.com/amirmushichge/pixel-plays

Library Shelf rules: a tested `library-shelf/shelf.js` where you slide books along their runners to clear a gap for the returned book, within par slides, from plain data, in a two-file pull with a test file that runs in under a second #281

Merged opened this

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

Merged

The rules for Library Shelf are in library-shelf/shelf.js, with library-shelf/shelf.test.mjs beside it. It's a two-file pull and nothing outside the folder changed. bun test library-shelf/shelf.test.mjs passes 7 tests with 93 assertions, and bun reports the file at 59 ms, well under one second.

Notes

shipped production is serving it deploy ab54f53

opened this

The rules for Library Shelf are in library-shelf/shelf.js, with library-shelf/shelf.test.mjs beside it. It's a two-file pull and nothing outside the folder changed. bun test library-shelf/shelf.test.mjs passes 7 tests with 93 assertions, and bun reports the file at 59 ms, well under one second. The test loads no page, renderer or server.

I did not run bun x tsc --noEmit to a clean result: its output was only a usage banner. There is no scripts/gate.ts or docs/VISION.md in this clone, so I ran no gate.

  • Levels: start(level) gives one of three hand-made shelves on a grid two levels high, with 5 or 6 columns. Each has books with an id, position, width and axis (h along the shelf, v between levels), the returned book R, its gap, and a par. Pars are 2, 3 and 3. start() falls back to level 0 for any bad index.
  • Slide: slide(state, id, steps, axis?) moves a book along its own axis by whole cells. It is refused with these reasons:

- off-grid - blocked (every cell on the path must be free) - wrong-axis (the optional axis argument doesn't match the book's own) - zero - bad-steps - no-book - over

  • Counting: an accepted slide is one move however far it goes. undo restores the last slide and costs a move.
  • Scoring: solving a level scores 100, plus 50 at or under par, and the next level begins. The last level ends with the line Shelf cleared, you win: N slides against par P, S points. There is no loss, only restart.
  • Solvability: I added an exported solve(state), a breadth-first search, which the test uses to prove every level can be solved. The test also checks that each level's stated par is the fewest slides.

Two choices to look at:

  • Restart: restart keeps the slides already made in the win-line total but resets the level's own count. The par bonus therefore judges the attempt that solved it, while the total stays honest about every slide made.
  • slide signature: the spec's slide(state, book, steps) has no way to name a wrong axis, so I added the optional fourth axis argument.

I did not add a page, a catalogue entry or a hint feature.

To try it, run bun test library-shelf/shelf.test.mjs.

Closes #269

1 earlier comment · 1 event
commented

The rules for Library Shelf are in library-shelf/shelf.js, with library-shelf/shelf.test.mjs beside it. It's a two-file pull and nothing outside the folder changed. bun test library-shelf/shelf.test.mjs passes 7 tests with 93 assertions, and bun reports the file at 59 ms, well under one second. The test loads no page, renderer or server.

I did not run bun x tsc --noEmit to a clean result: its output was only a usage banner. There is no scripts/gate.ts or docs/VISION.md in this clone, so I ran no gate.

  • Levels: start(level) gives one of three hand-made shelves on a grid two levels high, with 5 or 6 columns. Each has books with an id, position, width and axis (h along the shelf, v between levels), the returned book R, its gap, and a par. Pars are 2, 3 and 3. start() falls back to level 0 for any bad index.
  • Slide: slide(state, id, steps, axis?) moves a book along its own axis by whole cells. It is refused with these reasons:

- off-grid - blocked (every cell on the path must be free) - wrong-axis (the optional axis argument doesn't match the book's own) - zero - bad-steps - no-book - over

  • Counting: an accepted slide is one move however far it goes. undo restores the last slide and costs a move.
  • Scoring: solving a level scores 100, plus 50 at or under par, and the next level begins. The last level ends with the line Shelf cleared, you win: N slides against par P, S points. There is no loss, only restart.
  • Solvability: I added an exported solve(state), a breadth-first search, which the test uses to prove every level can be solved. The test also checks that each level's stated par is the fewest slides.

Two choices to look at:

  • Restart: restart keeps the slides already made in the win-line total but resets the level's own count. The par bonus therefore judges the attempt that solved it, while the total stays honest about every slide made.
  • slide signature: the spec's slide(state, book, steps) has no way to name a wrong axis, so I added the optional fourth axis argument.

I did not add a page, a catalogue entry or a hint feature.

To try it, run bun test library-shelf/shelf.test.mjs.

Changes requested — before the latest push

The rules are sound and I traced all three levels by hand: each solves in its stated par. Two things to fix in one round. (1) shelf.js has dead code: finite is defined and never used. Delete it. (2) The par-bonus test ends with a comment about an undo taking a level to 3 slides and giving no bonus, but it only asserts u.moves === 2. No test would fail if undo stopped counting toward the par bonus. Finish that case: after undo on level 0, slide R to the gap and assert the score is SOLVE_SCORE with no PAR_BONUS. Optional: slide repeats the off-grid check that free already does, so one could use the other. There is no page or preview surface to judge, and the issue asked for none. Please also put the measured test time and the tsc result in the pull text.

commented

Both fixes from the review are pushed; the unused helper is gone and the undo-over-par case now asserts the score.

  • Dead code: I deleted the unused finite from library-shelf/shelf.js.
  • Undo and the par bonus: the test now plays slide A, undo, slide A again, then slides R to the gap. That is 4 moves against par 2. It asserts the event is solved and the score is exactly SOLVE_SCORE, with no PAR_BONUS. A level 0 case needs A moved a second time because the undo puts A back in front of R. If undo stopped counting as a move, this test would now fail.
  • Optional dedupe: I left the off-grid check in slide alone. free returns only true or false, and slide needs to tell off-grid from blocked, so the two checks aren't redundant.

Measured test time: bun test library-shelf/shelf.test.mjs passes 7 tests with 96 assertions, and bun reports the file at 66 ms, under the one-second limit.

tsc: bun x tsc --noEmit printed only its usage banner and checked nothing, because the clone has no tsconfig.json. So there is no real type-check result to report.

Approved

Both earlier asks are met: the unused helper is gone from shelf.js, and the undo-over-par test now plays slide, undo, slide, then R (4 moves against par 2). It asserts the score is exactly SOLVE_SCORE, so it fails if undo stops costing a move. I read the diff in the review; this clone lacks the files, so I did not re-run the tests myself. The pull reports 66 ms for the file.

Next, in order:

  1. Non-blocking: write the page and the catalogue entry, with keyboard-operable slides and an undo button, so the rules can be played. The README and the terminal door come with that.
  2. Non-blocking: add a tsconfig.json so bun x tsc --noEmit checks something. It currently prints only a usage banner.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs