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

Migrated from github.com/amirmushichge/pixel-plays

Shunting Yard rules: a tested `shunting-yard/yard.js` that sorts numbered wagons through sidings from plain data #171

Merged opened this

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

Merged

I added the Shunting Yard rules in shunting-yard/yard.js and tests in shunting-yard/yard.test.mjs. The new test file passes (12 tests). I didn't run bun scripts/gate.ts, and the repository has no scripts/fast.ts.

Notes

shipped production is serving it deploy ab54f53

opened this

I added the Shunting Yard rules in shunting-yard/yard.js and tests in shunting-yard/yard.test.mjs. The new test file passes (12 tests). I didn't run bun scripts/gate.ts, and the repository has no scripts/fast.ts. bun x tsc --noEmit printed only its usage text, so I can't say the type check is clean. Nothing outside the folder is touched.

Rules

  • Tracks: a yard has an arrival track, 2 sidings that each hold 3 wagons, and an empty departure track. Level 1 has 3 wagons and each level adds one, up to level 5.
  • start(seed, level): the wagons are shuffled from the seed, so the same seed and level always give the same yard. A shuffle is kept only if a solver finishes it within that level's target, so every yard can be solved. solve(state) is that solver and returns the shortest list of [from, to] moves.
  • move(state, from, to): it takes the end wagon of one track to the end of another. It refuses a move from an empty track, onto a full siding, or onto the departure track out of order. Each accepted move counts one, and a refused move returns the same state object with a why.
  • Levels and the end: solving a level starts the next one with one more wagon, and the move count carries over. Solving level 5 ends the game and reports levels cleared and moves used. It's a win if the total is at or under the sum of the level targets (5, 7, 9, 11 and 13 moves) and a loss if it's over.

Choices

  • Departure order: the departure track takes only the next wagon in sequence (1, then 2, and so on), since nothing else could ever finish.
  • Extra refusals: I added three refusals beyond the issue's list: leaving the departure track, moving back onto the arrival track, and moving after the game has ended.
  • Win line: the issue said "per level", but I judge the win on the total moves against the summed targets. If you want it per level, that's a small change.
  • Level seeds: each later level's shuffle comes from the seed plus the level number, so a whole run is reproducible from one seed.
  • Not done: no page and no catalogue entry, as the issue says.

To try it, run bun test shunting-yard.

Closes #162

Approved

Rules only, as issue #162 asks. It touches only shunting-yard/yard.js and its test, with no shell, town or rounds changes, and it follows the lock-keeper and allotment rules pattern. It has no I/O, network, storage or DOM, and the seed and level are clamped at the boundary, so I found nothing to flag on security. Each rule in the issue has a test: determinism, the solver per level, each refusal with the same state object returned, the move count, level advance, and the win and loss lines. The state is plain data and there is no dead code. The README and catalogue wiring are rightly left for the page pull.

The win line is judged on total moves against the summed targets, where the issue says per level. The pull discloses this and it is a sound reading, so I did not block on it. I could not run the tests here because this clone has the base commit, not the head, so I relied on the pull being green.

For the page pull, not this one: the rules never detect a stuck yard, for example when both sidings are full of blockers. The status stays playing with no legal winning move, so the page will need a restart or a stuck check.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs