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

Migrated from github.com/amirmushichge/pixel-plays

Village Draughts rules: a tested `village-draughts/board.js` with forced captures, multi-jumps, crowning and a seeded computer opponent, from plain data, in a two-file pull with a test file that runs in under a second #289

Merged opened this

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

Merged

Village Draughts now has its rules in village-draughts/board.js and a test file, village-draughts/board.test.mjs. bun test village-draughts/board.test.mjs passes 11 tests in about 55 ms, well under the one-second limit. This is a two-file pull with no page and no catalogue entry.

Notes

shipped production is serving it deploy ab54f53

opened this

Village Draughts now has its rules in village-draughts/board.js and a test file, village-draughts/board.test.mjs. bun test village-draughts/board.test.mjs passes 11 tests in about 55 ms, well under the one-second limit. This is a two-file pull with no page and no catalogue entry.

I didn't run scripts/gate.ts because the repository doesn't have one. bun x tsc --noEmit printed only the compiler's usage text (no tsconfig in the repo), so there was no type check. I also didn't run the full suite; the forge does that on push.

What it does

  • Opening: start(seed) sets out 8x8 with twelve men a side, on dark squares only. The player moves first, up the board.
  • Legal moves: moves(state) lists every legal move, as {from, path, captures}.

- Men step diagonally forward; kings go any direction. - A capture is forced, so when one exists only captures are listed. - A jump must continue while it can, so only the full path is listed. - A man that reaches the far row is crowned and its move ends there, even if a king could have jumped on.

  • Playing a move: move(state, from, path) plays a listed move and returns {ok, state, why}. A refused move returns the same state with a reason:

- illegal - must-capture - no-piece - bad-input - not-your-turn - game-over

  • Opponent: reply(state) always takes a capture and prefers the one that takes the most pieces. With no capture it crowns a man if a step can, and otherwise takes any step. Ties are broken by a seeded generator (mulberry32) whose state is kept in the game state, so the same seed and inputs give the same game. There are no timers and no Math.random.
  • End of game: the side to move loses if it has no pieces or no legal move. The game then ends with a win or loss line that reports the pieces taken, for example "You lose: you have no move left. Pieces taken: you 0, them 1".

Choices to check

  • Captured pieces leave the board at once, so a king can't jump the same piece twice.
  • I added fromBoard(rows, turn, seed), which sets a game out from plain rows. The tests use it to build their positions.

What the tests cover: the opening, steps, jumps, forced capture, a continued multi-jump, crowning, king moves, refused and out-of-turn moves, the opponent's capture preference, same seed giving the same reply, a whole game repeating exactly, and both end lines. The first six assert only the player's side.

To try it: bun test village-draughts/board.test.mjs.

Closes #276

4 earlier comments · 3 events
commented

Village Draughts now has its rules in village-draughts/board.js and a test file, village-draughts/board.test.mjs. bun test village-draughts/board.test.mjs passes 11 tests in about 55 ms, well under the one-second limit. This is a two-file pull with no page and no catalogue entry.

I didn't run scripts/gate.ts because the repository doesn't have one. bun x tsc --noEmit printed only the compiler's usage text (no tsconfig in the repo), so there was no type check. I also didn't run the full suite; the forge does that on push.

What it does

  • Opening: start(seed) sets out 8x8 with twelve men a side, on dark squares only. The player moves first, up the board.
  • Legal moves: moves(state) lists every legal move, as {from, path, captures}.

- Men step diagonally forward; kings go any direction. - A capture is forced, so when one exists only captures are listed. - A jump must continue while it can, so only the full path is listed. - A man that reaches the far row is crowned and its move ends there, even if a king could have jumped on.

  • Playing a move: move(state, from, path) plays a listed move and returns {ok, state, why}. A refused move returns the same state with a reason:

- illegal - must-capture - no-piece - bad-input - not-your-turn - game-over

  • Opponent: reply(state) always takes a capture and prefers the one that takes the most pieces. With no capture it crowns a man if a step can, and otherwise takes any step. Ties are broken by a seeded generator (mulberry32) whose state is kept in the game state, so the same seed and inputs give the same game. There are no timers and no Math.random.
  • End of game: the side to move loses if it has no pieces or no legal move. The game then ends with a win or loss line that reports the pieces taken, for example "You lose: you have no move left. Pieces taken: you 0, them 1".

Choices to check

  • Captured pieces leave the board at once, so a king can't jump the same piece twice.
  • I added fromBoard(rows, turn, seed), which sets a game out from plain rows. The tests use it to build their positions.

What the tests cover: the opening, steps, jumps, forced capture, a continued multi-jump, crowning, king moves, refused and out-of-turn moves, the opponent's capture preference, same seed giving the same reply, a whole game repeating exactly, and both end lines. The first six assert only the player's side.

To try it: bun test village-draughts/board.test.mjs.

Changes requested — before the latest push

The rules read correctly and I found no security problem: there is no I/O, no eval, and no Math.random. One thing blocks this, and there is a second smaller point.

1. Fix first: the opponent's stated rule is only partly tested. The Tests item in your list says every added behaviour needs a test that would fail without it. choose() in board.js has three tiers: longest capture, then crown a man if a step can, then any step by seed. Only the first tier and the seed-tie behaviour are tested. Nothing would fail if the crowning preference were deleted. Add a test where the opponent has a free crowning step and a non-crowning step. Seed it so the unfiltered pool would pick the wrong one for at least some seeds, and assert it crowns every time (loop over several seeds). Opponent king moves, both stepping and jumping backwards, are also untested. All the king tests use the player's side, and as you noted the first six tests assert only the player's side. Add one opponent king test, ideally a king multi-jump that changes direction partway.

2. fromBoard(rows, turn, seed) is exported but does not validate its input. It is a public boundary. Rows that are not 8 strings of 8 allowed characters, or a turn other than player/opponent, make moves() throw on g[mr][mc] or index SIDES with undefined. Validate at the boundary and return a clear refusal, as move() does for bad-input, and test it. Alternatively keep it test-only and drop it from the exported API, but then say so.

Minor: the header comment says The page (not yet written) draws what this decides. That will go stale the moment the page lands, so cut it.

Everything else I checked holds: forced capture, multi-jump continuation, crowning ending the move, same-state refusals, seed determinism, both end lines, and the measured time stated in the pull.

commented

The pull now covers both of sentinel's points and the minor one. bun test village-draughts/board.test.mjs passes 14 tests (245 assertions) in about 60 ms, under the one-second limit. bun x tsc --noEmit printed only the compiler's usage text, so it checked nothing, and there is no scripts/gate.ts to run. The first two pushes of this round had a failing test, which the third push fixed. The branch head, bcc499a, is green.

Changes

  • Crowning tier: a new test gives the opponent two crowning steps and two non-crowning steps. It replies under 30 different seeds and asserts a king appears on row 7 every time. Deleting the crowning preference would make it fail for some seeds.
  • Opponent kings: a new test checks a king's four steps and a multi-jump that changes direction: [1,2] to [3,4] to [1,6], down and then back up. It also checks that a man cannot jump backwards.
  • fromBoard validation: it is still exported and now validates its input. It returns {ok, state, why}, with why of bad-input for:

- rows that are not 8 strings of 8 characters from .poPO; - a piece on a light square; - a turn other than player or opponent.

The turn defaults to player. A test covers each kind of bad input.

  • Stale comment: I cut the "page (not yet written)" line from the header comment.
  • Test layouts: two existing test layouts had pieces on light squares. The new validation exposed this, so I moved those pieces to dark squares. The rules themselves are unchanged.

I didn't run the full suite; the forge's check does that on push.

commented

I made no change for this failure, because the log you sent shows no failing test.

  • What the log shows: it ends mid-run, after ferris-wheel (about the sixth playtest group alphabetically), and every line before the cut is (pass). No assertion error appears in it. That fits the deploy hitting its "test took longer than 600s" or CPU limit while the whole suite was running.
  • Why my pull isn't the cause: it adds only village-draughts/board.js and village-draughts/board.test.mjs. It adds no page or catalogue entry, so the other games' playtests never run it.
  • Local result: bun test village-draughts/board.test.mjs passes all 14 tests in about 60 ms. The tests load no page, renderer or server.

The issue says not to cut the rules or tests to get under that limit, so I haven't. If the deploy fails again with an actual assertion error, send that part of the log and I'll fix it.

Changes requested — before the latest push

The rules read correctly and the tests are thorough. One change is needed, in the fromBoard boundary check.

Fix: fromBoard accepts inherited property names as a turn. The check is !SIDES[turn || PLAYER] || typeof turn === "object". SIDES is a plain object, so SIDES["constructor"], SIDES["toString"] and SIDES["__proto__"] are all truthy. A call like fromBoard(rows, "constructor", 1) passes validation and then runs moves() with a turn that has no dir or other. The result is garbage state or a throw, which is the failure the previous round asked you to close. I could not run this: the clone I was given does not contain the pull's files. This comes from reading board.js alone.

Replace the check with an exact comparison: const side = turn === undefined ? PLAYER : turn; if (side !== PLAYER && side !== OPPONENT) .... Then add "constructor", "toString" and "__proto__" to the bad-turn list in the fromBoard refuses bad rows and bad turns test, so the test fails without the fix.

Everything else holds. The pull states the measured time. There is no page and no catalogue entry, which is what the issue asked for. Draughts is a traditional game, so no creator credit is due. I have no other changes to request.

commented

This pull adds the Village Draughts rules as plain data with no DOM, in village-draughts/board.js, and their tests in village-draughts/board.test.mjs. There is no page and no catalogue entry yet.

Rules

  • Opening: start(seed) sets out an 8x8 board with twelve men a side, the player to move first.
  • Legal moves: moves(state) lists every legal move as {from, path, captures}. Men step diagonally forward and kings in any direction. Captures are forced, and a jump must continue while it can, so only the full path is listed. A man reaching the far row is crowned and its move ends there. Captured pieces leave the board at once.
  • Player's move: move(state, from, path) plays a listed move. Anything else comes back with the same state and a reason: illegal, must-capture, no-piece, bad-input, not-your-turn or game-over.
  • Opponent: reply(state) follows a stated rule:

- take the capture that takes the most pieces; - otherwise, crown a man if a step can; - otherwise, take any legal step.

Ties are broken by a seeded generator (mulberry32) whose state is kept in the game state. There are no timers and no Math.random.

  • End of game: a side with no pieces or no legal move loses. The game ends with a line reporting the pieces taken, such as You win: the opponent has no move left. Pieces taken: you 1, them 0.
  • Custom positions: fromBoard(rows, turn, seed) sets a game out from plain rows, for puzzles and tests. It returns {ok, state, why} and refuses with bad-input unless:

- the rows are 8 strings of 8 characters from .poPO; - pieces stand only on dark squares; - the turn is exactly player or opponent, defaulting to player when left out.

Names inherited from plain objects, such as constructor, toString and __proto__, are refused. Without that fix, the new test fails.

Tests: the 14 tests cover:

  • the opening position;
  • steps and jumps;
  • forced capture;
  • a multi-jump that must continue;
  • crowning that ends the move;
  • king moves for both sides, including an opponent king's multi-jump that changes direction;
  • refused and out-of-turn moves;
  • the opponent's capture preference;
  • its crowning preference, checked across 30 seeds;
  • the same seed giving the same reply, and a whole game repeating exactly;
  • both end lines;
  • fromBoard refusing bad input.

bun test village-draughts/board.test.mjs passes all 14 tests in about 60 ms on its own. The test calls the rules module directly and loads no page, renderer or server. bun x tsc --noEmit printed only its usage text and checked nothing. The clone has no scripts/gate.ts, so that wasn't run.

An earlier preview deploy log stopped mid-run in other games' playtests, after ferris-wheel, with every line before the cut passing and no assertion error. That looks like the suite's time or CPU limit rather than a failure in this pull, which touches only these two files.

To try it: bun test village-draughts/board.test.mjs.

🤖 Generated with Claude Code

Approved

Both earlier asks are met. (1) The opponent's crowning tier now has a test across 30 seeds, and opponent kings are tested for steps, a direction-changing multi-jump, and a man refusing to jump backwards. (2) fromBoard now validates rows and turn and returns bad-input, with a test per kind of bad input, including inherited names like 'constructor'. The stale header comment is gone. I read the diff only: the clone does not contain the pull's files, so I did not run the tests myself. The pull states 14 tests in about 60 ms, and there is no page here, so no preview to exercise. Next I would do two things. 1) Land the playable page and catalogue entry, with a keyboard-operable board (buttons per square, not click-only) and a text status line from line. 2) Credit draughts/checkers as the traditional game in the README. Non-blocking: describe spells out 'you'/'them' counts, so keep that wording when the page shows it.

commented

Left #276 open: 1 unticked item remain (moves(state) lists every legal move for the side to move: diagonal steps forward for men and in any direction for kings, and jumps over an opposing piece to an empty square. When a capture is available only captures are legal, and a multi-jump must continue while it can. move(state, from, path) plays a listed move and refuses anything else, or a move made out of turn. A man reaching the far row is crowned and its move ends there.).

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs