Documentation

Everything you need to integrate Moddable Chess into your project or extend it with new variants. Three ways to integrate: embed via iframe, mount directly via JavaScript, or query from an AI assistant.

Quick Start

The engine uses native ES modules. Import what you need:

<script type="module">
import MCE from './js/chess-engine.js';
import './js/chess-moves.js';
import './js/chess-play.js';
import './js/chess-variants.js';
import './js/board-renderer.js';

const game = MCE.createGame('standard');
MCE.renderBoard(document.getElementById('board'), game, {
  size: 480,
  onSquareClick: (sq) => { /* handle click */ }
});
</script>

Or use the game controller for a complete interactive experience (AI, undo, animation):

<script type="module">
import MCE from './js/chess-engine.js';
import './js/chess-moves.js';
import './js/chess-play.js';
import './js/chess-variants.js';
import './js/board-renderer.js';
import './js/chess-ai.js';
import { createGameController } from './js/game-controller-core.js';

const game = MCE.createGame('atomic');
const ctrl = createGameController(
  document.getElementById('board'), game,
  { players: { w: 'human', b: 'ai' }, aiDepth: 4 }
);
</script>

Architecture

The engine is split into focused ESM modules. Each extends the MCE namespace via side-effect imports:

FileExportsPurpose
chess-engine.jsMCE (default), createGame, loadFEN, toFEN, positionKey, registerVariant, registerPiece, effects APICore: board state, FEN, coordinates, variant/piece registry, effects system, board mutations
chess-moves.jspseudoLegalMoves, legalMoves, inCheckMove generation for all piece types including custom pieces
chess-play.jsmakeMove, unmakeMove, getStatusMove execution, undo, draw detection (threefold, insufficient material, 50-move)
chess-variants.jsvariantLegalMoves, getVariantStatus70 variant plugins with rule overrides
chess-units.jsregisterUnit, buildUnitHandler, getUnitUnit template system for declarative piece definitions
chess-ai.jsaiPickMove, AI_DIFFICULTIES, loadOpeningBookAlpha-beta search, difficulty presets, opening book support
board-renderer.jsrenderBoard, setTheme, setPieceStyle, captureBurstSVG board rendering, themes, animations
game-controller-core.jscreateGameControllerFull interaction loop: human/AI players, undo, move animation, callbacks (demo)
game-controller.js(play page orchestrator)Imports all modules + variants, handles embed postMessage API
replay.jscreateReplayStep through completed games move-by-move
ai-worker.js(Web Worker entry)Offloads AI search to a background thread
mcp/tools.js8 MCP tool handlersAI-callable chess analysis and puzzle generation
mcp/server.js(MCP server entry)Stdio MCP server for Claude Code / Claude Desktop

Embedding

Embed a playable chess board in any page with a single iframe:

<iframe
  src="https://chess.moddable.games/play/?embed=1&variant=atomic&boardonly=1"
  width="480" height="480" frameborder="0"
></iframe>

See API: Embedding for all parameters (themes, player names, accent colours, game modes).

MCP Server

The engine is available as an MCP server for Claude Code, Claude Desktop, or any MCP-compatible AI client. Eight tools expose game creation, move generation, position analysis, board rendering, and puzzle generation.

# Add to Claude Code
claude mcp add --transport stdio moddable-chess node mcp/server.js
ToolDescription
chess_list_variantsAll 70 variants with board sizes, win conditions, and descriptions
chess_get_legal_movesLegal moves for a position (FEN or variant start)
chess_analyze_positionAI evaluation with best move, score, and material balance
chess_validate_moveCheck if a move is legal and get the resulting position
chess_make_movesPlay a sequence of moves and return the final position
chess_get_opening_bookOpening book moves for a variant (where available)
chess_generate_puzzleGenerate tactical puzzles from random positions
chess_render_svgRender any position as a self-contained SVG image (no DOM required)

See API: MCP Server for full parameter documentation and examples.

Board Representation

The board is a flat array of size rows × cols. Index 0 is the top-left square (a8 in standard chess). Pieces are single characters — uppercase for White, lowercase for Black:

CharPieceVariants
P/pPawnAll
N/nKnightAll
B/bBishopAll
R/rRookAll
Q/qQueenAll
K/kKingAll
A/aArchbishop (B+N)Capablanca, Grand
C/cChancellor (R+N)Capablanca, Grand
S/sSage (1-step any direction)Courier
M/mMaharaja (Q+N compound)Maharaja, Amazon Chess

Variable Board Sizes

The engine supports arbitrary rectangular boards. Each game object has rows and cols properties. All coordinate functions accept an optional game context:

MCE.sq(row, col, game)  // → index
MCE.rc(index, game)    // → [row, col]
MCE.onBoard(row, col, game) // → boolean

Supported Variants (70)

KeyBoardWin Condition
standard8×8Checkmate
chess9608×8Checkmate
noCastling8×8Checkmate
torpedo8×8Checkmate
chigorin8×8Checkmate
almostChess8×8Checkmate
amazonChess8×8Checkmate
upsideDown8×8Checkmate
kingOfTheHill8×8Checkmate or king to centre
threeCheck8×8Checkmate or 3 checks
singleCheck8×81 check
fiveCheck8×8Checkmate or 5 checks
atomic8×8Explode opponent's king
rifle8×8Checkmate
extinction8×8Eliminate a piece type
antichess8×8Lose all pieces (stalemate = win)
giveaway8×8Lose all pieces (stalemate = loss)
suicideChess8×8Lose all pieces (stalemate = draw)
stalemateWins8×8Checkmate or stalemate opponent
codrus8×8Lose your king
racingKings8×8King to rank 8
marseillais8×8Checkmate (2 moves/turn)
progressive8×8Checkmate (escalating moves)
monsterChess8×8Checkmate (White: 2 moves)
duckChess8×8Capture king
fogOfWar8×8Capture king
horde8×8Checkmate or eliminate horde
maharaja8×8Checkmate
knightmate8×8Checkmate knight (royal)
makpong8×8Checkmate (king can't flee)
endgameChess8×8Checkmate
peasantsRevolt8×8Checkmate
pawnsOnly8×8Checkmate
breakthrough7×7Reach far rank
losAlamos6×6Checkmate
minichess5×5Checkmate
capablanca10×8Checkmate
grand10×10Checkmate
courier12×8Checkmate
diceChess8×8Checkmate (die constrains piece type)
gridChess8×8Checkmate (must cross grid lines)
checklessChess8×8Checkmate (no check unless mate)
noRetreat8×8Checkmate (no backward moves)
weakChess8×8Checkmate (weakest piece must move)
patrolChess8×8Checkmate (capture only when defended)
madrasiChess8×8Checkmate (same-type pieces paralyse)
omnicide8×8Lose all pieces (no forced captures)
darkChess8×8Capture king (total fog)
berserkChess8×8Checkmate (check grants bonus move)
benedictChess8×8Convert opponent's king
andernachChess8×8Checkmate (captures flip colour)
halfChess4×8Checkmate
dianaChess6×6Checkmate
pettyChess5×6Checkmate
berolinaChess8×8Checkmate (pawns move diagonally)
cylinderChess8×8Checkmate (files wrap a↔h)
toroidalChess8×8Checkmate (board wraps both axes)
leganChess8×8Checkmate (Berolina pawns, swapped royals)
hoppelPoppel8×8Checkmate (knights/bishops swap captures)
makruk8×8Checkmate (Thai chess)
ordaChess8×8Checkmate (asymmetric Mongol army)
einsteinChess8×8Checkmate (moves demote, captures promote)
displacementChess8×8Checkmate (swap with adjacent friendlies)
shatar8×8Bare king (Mongolian chess)
crazyhouse8×8Checkmate (drop captured pieces)
recruitmentChess8×8Checkmate (captured pieces join your army)
teleportChess8×8Checkmate (teleport to empty squares)
poisonChess8×8Checkmate (capture squares become toxic)
medusaChess8×8Checkmate (pieces petrify on sight)
immunizationChess8×8Checkmate (pieces gain capture immunity)

Sister Projects

Moddable Chess handles rectangular boards only. For other geometries:

Next Steps