Thai checkers game logic implemented in TypeScript.
src/core/: source modules for board state, pieces, legal moves, game flow, and search/exploration.src/app/: interactive REPL application and demo scenarios.tests/core/: Jest test suites for game behavior and move selection.tests/app/: Jest tests for the REPL parser and rendering.tests/html/: jsdom workflow tests for the browser client.tests/e2e/: Playwright Chromium workflow and local static test server.dist/: generated build output from TypeScript. It is ignored by git.
- Node.js
- npm
npm install
npx playwright install chromiumnpm run build
npm test
npm run repl # interactive REPL with normal starting position
npm run demo # alias for npm run repl
npm run demo:branching-capture
npm run demo:loop-capture
npm run demo:loop-branching-capture1
npm run demo:loop-branching-capture2
npm run test:dom
npm run test:e2e
npm run test:allnpm run build compiles TypeScript into dist/. npm test runs all Jest suites through ts-jest in ESM mode. npm run lint runs ESLint over the source and test files.
Demo scripts:
npm run repl(ornpm run demo) starts the interactive REPL with a standard board.npm run demo:branching-captureloads a position where a piece has multiple capture paths to the same final square.npm run demo:loop-captureloads a position where a dame captures in a loop and lands back on its starting square.npm run demo:loop-branching-capture1loads a position where a dame has two mirror-image loop paths with the same from/to and captured pieces.npm run demo:loop-branching-capture2loads the same ring with an extra central piece, creating additional branches.
Inside the REPL:
<number>— apply the numbered legal move<from> <to>— apply a move by coordinates (e.g.d5 d1). If several moves share the same from/to and capture the same pieces, the first one is applied automatically; otherwise the REPL asks you to pick.trace <number>ortrace <from> <to>— show the full intermediate path of a moveundo/u— take back the last moveredo/r— re-apply a move undone withundonew/reset— start a fresh gamemoves/m— re-print the board and move menuhelp/h/?— show command helpquit/q— exit
demo1, demo2, demo31, and demo32 are hidden REPL commands that reload the branching-capture, loop-capture, and two loop-branching-capture demo positions.
npm run test:dom runs only the jsdom browser-client workflows. npm run test:e2e builds the project and runs the real Chromium workflow. npm run test:all runs both Jest and Playwright coverage.
Import from the package entry point after building:
import { Board, Game, Position, PieceColor, PieceType, CaptureTrace } from 'th-checkers';
const game = new Game(Board.setup());Game.getMoves() returns readonly Move[] without executing anything:
interface Move {
from: Position; // starting square
to: Position; // final landing square
captured: Position[]; // flat list of captured pieces (backward compatible)
trace?: CaptureTrace; // full capture path, present only for capture moves
}For non-capture moves, trace is undefined and captured is empty. For capture moves, trace provides the complete path:
const moves = game.getMoves();
const capture = moves.find(m => m.trace);
// CaptureTrace properties
capture.trace.sequence; // [captured₁, landing₁, captured₂, landing₂, …, finalLanding]
capture.trace.captured; // [captured₁, captured₂, …] — just the taken pieces
capture.trace.path(from); // [from, landing₁, landing₂, …, finalLanding] — travel path
capture.trace.length; // number of captures (sequence.length / 2)
capture.trace.finalLanding; // same as move.to
capture.trace.toString(); // "×C6 →D5 ×E4 →F3 ×G2 →H1"The trace is computed during move generation — no move needs to be executed to inspect it. Move.captured remains unchanged as the flat list for backward compatibility.
Thai checkers uses 8 pieces per side, so a valid board has at most 16 pieces. Board.encode() intentionally fits valid boards into 64 bits: 32 bits for occupied playable squares, 16 bits for piece color, and 16 bits for piece type. The color/type bits are stored in occupied-square scan order, not by absolute board square. Board.fromPieces() and Board.decode() reject boards with more than 16 occupied squares.
Board.getPieces(color) returns Map<number, PieceInfo>, keyed by Position.hash() rather than Position object identity. Use Position.fromIndex(index) when iterating if you need coordinates. Board.fromPieces() accepts either stable numeric keys or Position keys and rejects duplicate logical squares.