Skip to content

API reference

TypeDescription
PositionImmutable game state (pieces, side to move, castling, en passant, clocks, hash).
MoveA move with from, to, optional promotion, and flag.
SquareBranded number for a board square (0–63).
ColorColor.White or Color.Black.
PieceTypePieceType.Pawn, PieceType.Knight, PieceType.Bishop, PieceType.Rook, PieceType.Queen, PieceType.King.

Helpers: square, file, rank, fileOf, rankOf, squareFromFileRank, oppositeColor.

Concepts. A Position is the full board state (pieces, side to move, castling, en passant, etc.). A Move is an object with from and to squares (0–63), optional promotion, and a flag (normal, castling, en passant, etc.). You get moves from getLegalMoves / getPseudoLegalMoves or by parsing notation with fromSan / fromUci; you don’t construct them manually. FEN is position notation (one string = whole board). SAN and UCI are move notations (one string = one move); use toSan / fromSan or toUci / fromUci to convert between Move and strings.


FunctionDescription
fromFen(fen: string): PositionParse a FEN string into a position. Throws on invalid FEN.
toFen(pos: Position): stringSerialize a position to FEN.
STARTING_FENFEN of the standard starting position.
EMPTY_POSITIONPosition with no pieces, White to move.

Board helpers (for building or inspecting positions): occupied, colorBB, typeBB, pieceAt, kingSquare, setPiece, removePiece. squareToAlgebraic(sq) returns e.g. "e4".


FunctionDescription
getLegalMoves(pos: Position): Move[]All legal moves in the current position.
getPseudoLegalMoves(pos: Position): Move[]Pseudo-legal moves (may leave own king in check).

FunctionDescription
makeMove(pos: Position, move: Move): PositionReturns a new position after playing the move. Throws if no piece at source.

FunctionDescription
isCheck(pos: Position): booleanSide to move is in check.
isCheckmate(pos: Position): booleanSide to move is checkmated.
isStalemate(pos: Position): booleanSide to move is stalemated.
isInsufficientMaterial(pos: Position): booleanNeither side can mate.
isFiftyMoveRule(pos: Position): booleanFifty-move rule draw.
isSquareAttacked(pos, sq, byColor): booleanWhether sq is attacked by byColor.

Convert between Move objects and SAN (e.g. "e4", "Qxf7#") or UCI (e.g. "e2e4", "e7e8q").

SAN (Standard Algebraic Notation) is human-readable: piece letter (if not a pawn), optional disambiguation, optional x, destination, optional =Q or #/+. Examples: e4, Nf3, Qxf7#, O-O. SAN depends on the position (e.g. which knight moved), so toSan and fromSan both take the current position.

UCI is engine-style: four characters (from-square + to-square in lowercase), plus one letter for promotion (e.g. e7e8q). Examples: e2e4, g1f3. The string format does not depend on the position; toUci(move) only needs the move. fromUci(pos, uci) still needs the position to validate that the move is legal.

FunctionDescription
toSan(pos: Position, move: Move): stringStandard Algebraic Notation, e.g. "e4", "Nf3".
toUci(move: Move): stringUCI string, e.g. "e2e4", "e7e8q".
`fromUci(pos: Position, uci: string): Movenull`
`fromSan(pos: Position, san: string): Movenull`

FunctionDescription
perft(pos: Position, depth: number): numberPerft node count for the given depth.
divide(pos: Position, depth: number): DivideResult[]Perft “divide” (per-move counts).
computeHash(pos: Position): bigintZobrist hash for the position.