Position
Abstract base class representing a chess position. All chess variants extend this class.Properties
The current board state containing piece positions
Captured pieces available for dropping (used in Crazyhouse variant)
The side to move (
'white' or 'black')Castling rights and rook positions for both sides
En passant target square, if available
Remaining checks for both sides (used in 3check variant)
Number of halfmoves since last capture or pawn move (for 50-move rule)
Current move number (increments after black’s move)
The variant rules being used (
'chess', 'atomic', 'crazyhouse', etc.)Methods
reset()
Resets the position to the starting position for the variant.clone()
Creates a deep copy of the position.A new position instance with identical state
ctx()
Computes the current position context including checks, pins, and variant-specific state.Position context containing:
king: The current player’s king squareblockers: Pieces pinned to the kingcheckers: Pieces giving checkvariantEnd: Whether the variant ending condition is metmustCapture: Whether a capture is mandatory (Antichess)
dests(square, ctx?)
Computes legal destination squares for a piece.The square containing the piece to move
Optional pre-computed context (performance optimization)
Set of legal destination squares
dropDests(ctx?)
Computes legal squares for dropping pieces (Crazyhouse variant).Optional pre-computed context
Set of legal drop squares (empty for standard chess)
allDests(ctx?)
Computes legal moves for all pieces of the current player.Optional pre-computed context
Map from piece squares to their legal destination squares
isLegal(move, ctx?)
Checks if a move is legal in the current position.The move to validate (either
{ from, to, promotion? } or { role, to } for drops)Optional pre-computed context
true if the move is legal, false otherwiseplay(move)
Plays a move, updating the position. Does not validate legality.The move to play
isCheck()
Checks if the current player is in check.true if the current player’s king is under attackisCheckmate(ctx?)
Checks if the current player is checkmated.Optional pre-computed context
true if the position is checkmateisStalemate(ctx?)
Checks if the current player is stalemated.Optional pre-computed context
true if the position is stalemateisEnd(ctx?)
Checks if the game has ended (checkmate, stalemate, insufficient material, or variant ending).Optional pre-computed context
true if the game has endedisVariantEnd()
Checks if the variant-specific ending condition is met.true if variant ending condition is met (always false for standard chess)outcome(ctx?)
Determines the game outcome.Optional pre-computed context
Game outcome:
{ winner: 'white' | 'black' | undefined } or undefined if game continuesvariantOutcome(ctx?)
Determines variant-specific outcome.Optional pre-computed context
Variant outcome (always
undefined for standard chess)hasInsufficientMaterial(color)
Checks if a side has insufficient mating material.The color to check (
'white' or 'black')true if the side cannot possibly checkmateisInsufficientMaterial()
Checks if both sides have insufficient mating material.true if neither side can possibly checkmatehasDests(ctx?)
Checks if the current player has any legal moves.Optional pre-computed context
true if at least one legal move existstoSetup()
Converts the position to a Setup object.Setup object containing all position data
kingAttackers(square, attacker, occupied)
Finds all pieces of a color attacking a square.The square being attacked
The attacking color
The set of occupied squares
Set of squares containing pieces attacking the target square
Chess
Concrete implementation of standard chess rules.Static Methods
Chess.default()
Creates a new position with the standard starting position.A new chess position in the starting state
Chess.fromSetup(setup)
Creates a chess position from a Setup object.The position setup to validate and load
A Result containing either the valid Chess position or a PositionError
Instance Methods
clone()
Creates a deep copy of the chess position.A new Chess instance with identical state
Castles
Manages castling rights and paths for both sides.Properties
Set of squares containing rooks that can castle
Rook positions for each side and color:
{ white: { a: 0, h: 7 }, black: { a: 56, h: 63 } }Squares that must be empty for castling
Static Methods
Castles.default()
Creates castling state for standard chess starting position.Castles instance with all castling rights available
Castles.empty()
Creates castling state with no castling rights.Castles instance with no castling available
Castles.fromSetup(setup)
Creates castling state from a Setup object.The setup containing board and castling rights
Castles instance computed from the setup
Instance Methods
clone()
Creates a copy of the castling state.A new Castles instance with identical state
discardRook(square)
Removes castling rights for a rook.The square of the rook that moved or was captured
discardColor(color)
Removes all castling rights for a color (called when king moves).The color losing castling rights
Context
Position analysis context computed byctx().
Properties
The current player’s king square (undefined if no king)
Pieces pinned to the king (cannot move without exposing check)
Pieces giving check to the king
Whether the variant ending condition is met
Whether a capture is mandatory (Antichess only)
IllegalSetup
Enumeration of position validation errors.Values
'ERR_EMPTY' - Board has no pieces'ERR_OPPOSITE_CHECK' - The side not to move is in check'ERR_PAWNS_ON_BACKRANK' - Pawns on the first or eighth rank'ERR_KINGS' - Wrong number of kings'ERR_VARIANT' - Variant-specific validation errorPositionError
Error class for invalid positions.Constructor
The specific validation error
Utility Functions
pseudoDests(pos, square, ctx)
Computes pseudo-legal moves (ignoring checks and pins).The position to analyze
The square containing the piece
The position context
Set of pseudo-legal destination squares
equalsIgnoreMoves(left, right)
Compares two positions ignoring move counters.First position
Second position
true if positions are identical except for halfmoves/fullmovescastlingSide(pos, move)
Determines which side a move castles to.The position
The move to check
'a' for queenside, 'h' for kingside, or undefined if not castlingnormalizeMove(pos, move)
Normalizes a castling move to use the rook square as destination.The position
The move to normalize
The normalized move (unchanged if not castling)
isStandardMaterial(pos)
Checks if a position has standard material (no promoted pieces beyond the starting material).The chess position to check
true if the material is standardisImpossibleCheck(pos)
Checks if the current check is impossible (indicates invalid position).The position to validate
true if the check is impossible (e.g., triple check, multiple non-aligned sliding checkers)