Move generation, validation, and execution with support for standard algebraic notation, object notation, and advanced move analysis. Provides comprehensive move handling for all chess rules including castling, en passant, and promotion.
Generates all legal moves from the current position with various filtering options.
/**
* Generate legal moves from current position
* @param options - Move generation options
* @returns Array of move strings (SAN) or Move objects if verbose
*/
moves(): string[];
moves(options: { square: Square }): string[];
moves(options: { piece: PieceSymbol }): string[];
moves(options: { square: Square; piece: PieceSymbol }): string[];
moves(options: { verbose: true }): Move[];
moves(options: { verbose: false }): string[];
moves(options: MovesOptions): string[] | Move[];
interface MovesOptions {
/** Return detailed Move objects instead of SAN strings */
verbose?: boolean;
/** Generate moves only for pieces on this square */
square?: Square;
/** Generate moves only for this piece type */
piece?: PieceSymbol;
}Usage Examples:
import { Chess } from "chess.js";
const chess = new Chess();
// Get all legal moves as SAN strings
const moves = chess.moves();
console.log(moves); // ['a3', 'a4', 'b3', 'b4', 'c3', 'c4', ...]
// Get moves for specific square
const e2Moves = chess.moves({ square: 'e2' });
console.log(e2Moves); // ['e3', 'e4']
// Get moves for specific piece type
const knightMoves = chess.moves({ piece: 'n' });
console.log(knightMoves); // ['Na3', 'Nc3', 'Nf3', 'Nh3']
// Get verbose move objects
const verboseMoves = chess.moves({ verbose: true });
console.log(verboseMoves[0]);
// {
// color: 'w', from: 'a2', to: 'a3', piece: 'p',
// san: 'a3', lan: 'a2a3', before: '...', after: '...'
// }Executes a move on the board, supporting multiple input formats.
/**
* Make a move on the board
* @param move - Move in SAN string, object notation, or null move
* @param options - Move execution options
* @returns Move object with detailed information
* @throws Error if move is illegal
*/
move(move: string, options?: MoveOptions): Move;
move(move: MoveInput, options?: MoveOptions): Move;
move(move: null, options?: MoveOptions): Move; // Null move
interface MoveInput {
from: string;
to: string;
promotion?: string;
}
interface MoveOptions {
/** Use strict SAN parsing (reject non-standard notation) */
strict?: boolean;
}Usage Examples:
const chess = new Chess();
// Standard Algebraic Notation (SAN)
const move1 = chess.move('e4');
console.log(move1.san); // 'e4'
// Object notation
const move2 = chess.move({ from: 'e7', to: 'e5' });
// Promotion
const promotionMove = chess.move({ from: 'a7', to: 'a8', promotion: 'q' });
// Long algebraic notation (permissive parser)
chess.move('g1f3'); // Allowed by default
chess.move('Ng1-f3'); // Also allowed
// Strict parsing
try {
chess.move('g1f3', { strict: true }); // Throws error
} catch (error) {
console.error('Non-standard notation rejected');
}
// Null move (pass turn)
chess.move(null); // or chess.move('--')Reverts the last move made.
/**
* Undo the last move
* @returns Move object that was undone, or null if no moves to undo
*/
undo(): Move | null;Usage Examples:
const chess = new Chess();
chess.move('e4');
chess.move('e5');
console.log(chess.moves().length); // Legal moves from current position
const lastMove = chess.undo();
console.log(lastMove?.san); // 'e5'
chess.undo(); // Undo white's e4
const noMove = chess.undo(); // null - no more moves to undoReturns the complete move history of the game.
/**
* Get the move history of the game
* @param options - History options
* @returns Array of move strings or Move objects
*/
history(): string[];
history(options: { verbose: true }): Move[];
history(options: { verbose: false }): string[];
history(options: HistoryOptions): string[] | Move[];
interface HistoryOptions {
/** Return detailed Move objects instead of SAN strings */
verbose?: boolean;
}Usage Examples:
const chess = new Chess();
chess.move('e4');
chess.move('e5');
chess.move('Nf3');
// Get SAN history
const history = chess.history();
console.log(history); // ['e4', 'e5', 'Nf3']
// Get detailed history
const verboseHistory = chess.history({ verbose: true });
console.log(verboseHistory[0]);
// {
// color: 'w', from: 'e2', to: 'e4', piece: 'p',
// san: 'e4', lan: 'e2e4', before: '...', after: '...'
// }The Move class represents a chess move with comprehensive information and analysis methods.
class Move {
/** Color of the moving piece */
color: Color;
/** Source square */
from: Square;
/** Destination square */
to: Square;
/** Type of piece that moved */
piece: PieceSymbol;
/** Type of captured piece (if any) */
captured?: PieceSymbol;
/** Promotion piece type (if any) */
promotion?: PieceSymbol;
/** Move in Standard Algebraic Notation */
san: string;
/** Move in Long Algebraic Notation */
lan: string;
/** FEN string before the move */
before: string;
/** FEN string after the move */
after: string;
/**
* @deprecated Move flags as string - use descriptor methods instead
*/
flags: string;
}class Move {
/** Check if this move captures a piece (excluding en passant) */
isCapture(): boolean;
/** Check if this move is an en passant capture */
isEnPassant(): boolean;
/** Check if this move is a pawn promotion */
isPromotion(): boolean;
/** Check if this move is a two-square pawn move */
isBigPawn(): boolean;
/** Check if this move is kingside castling */
isKingsideCastle(): boolean;
/** Check if this move is queenside castling */
isQueensideCastle(): boolean;
}Usage Examples:
const chess = new Chess();
// Regular move
const move1 = chess.move('e4');
console.log(move1.isCapture()); // false
console.log(move1.isBigPawn()); // true (two-square pawn move)
chess.move('d5');
const capture = chess.move('exd5');
console.log(capture.isCapture()); // true
console.log(capture.captured); // 'p'
// Castling
chess.load('r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1');
const castle = chess.move('O-O');
console.log(castle.isKingsideCastle()); // true
// Promotion
chess.load('8/P7/8/8/8/8/8/8 w - - 0 1');
const promotion = chess.move('a8=Q');
console.log(promotion.isPromotion()); // true
console.log(promotion.promotion); // 'q'type Color = 'w' | 'b';
type PieceSymbol = 'p' | 'n' | 'b' | 'r' | 'q' | 'k';
type Square = 'a8' | 'b8' | 'c8' | 'd8' | 'e8' | 'f8' | 'g8' | 'h8' |
'a7' | 'b7' | 'c7' | 'd7' | 'e7' | 'f7' | 'g7' | 'h7' |
'a6' | 'b6' | 'c6' | 'd6' | 'e6' | 'f6' | 'g6' | 'h6' |
'a5' | 'b5' | 'c5' | 'd5' | 'e5' | 'f5' | 'g5' | 'h5' |
'a4' | 'b4' | 'c4' | 'd4' | 'e4' | 'f4' | 'g4' | 'h4' |
'a3' | 'b3' | 'c3' | 'd3' | 'e3' | 'f3' | 'g3' | 'h3' |
'a2' | 'b2' | 'c2' | 'd2' | 'e2' | 'f2' | 'g2' | 'h2' |
'a1' | 'b1' | 'c1' | 'd1' | 'e1' | 'f1' | 'g1' | 'h1';
interface MovesOptions {
verbose?: boolean;
square?: Square;
piece?: PieceSymbol;
}
interface MoveInput {
from: string;
to: string;
promotion?: string;
}
interface MoveOptions {
strict?: boolean;
}
interface HistoryOptions {
verbose?: boolean;
}