UNPKG

@chess-labs/core

Version:

A lightweight, framework-agnostic chess engine written entirely in TypeScript

331 lines (318 loc) 11.4 kB
/** * Enumeration defining chess piece types */ declare enum PieceType { PAWN = "pawn", ROOK = "rook", KNIGHT = "knight", BISHOP = "bishop", QUEEN = "queen", KING = "king" } /** * Enumeration defining chess piece colors */ declare enum Color { WHITE = "white", BLACK = "black" } /** * Interface representing a position on the chess board * col: 0-7 (a-h) * row: 0-7 (1-8) */ interface Position { col: number; row: number; } /** * Interface representing a chess piece */ interface Piece { type: PieceType; color: Color; hasMoved?: boolean; } /** * Type representing a chess board * Implemented as a 2D array (8x8) * null means an empty square */ type Board = Array<Array<Piece | null>>; /** * Type for special chess moves */ type SpecialMove = 'castling' | 'en-passant' | 'promotion' | 'two-square-advance'; /** * Type representing a possible move */ interface Move { from: Position; to: Position; capture?: boolean; special?: SpecialMove; capturedPiecePosition?: Position; } /** * Interface representing the state of a chess game */ interface GameState { board: Board; currentTurn: Color; moveHistory: Array<{ from: Position; to: Position; piece: Piece; captured?: Piece; special?: SpecialMove; promotedTo?: PieceType; }>; isCheck: boolean; isCheckmate: boolean; isStalemate: boolean; castlingRights?: { whiteKingside: boolean; whiteQueenside: boolean; blackKingside: boolean; blackQueenside: boolean; }; enPassantTarget?: string; halfmoveClock?: number; fullmoveNumber?: number; } /** * Initialize an 8x8 chess board for a standard chess game. */ declare const initBoard: () => Board; /** * Check if the position is within the chess board. */ declare const isValidPosition: (position: Position) => boolean; /** * Return the piece at the specified position. * Returns null if the position is invalid or there is no piece. */ declare const getPieceAt: (position: Position, board: Board) => Piece | null; /** * Place a piece at the specified position on the board. * Returns true if successful, false if the position is invalid. */ declare const placePiece: (board: Board, position: Position, piece: Piece) => boolean; /** * Remove a piece from the specified position on the board. * Returns the removed piece if successful, null if the position is invalid or empty. */ declare const removePiece: (board: Board, position: Position) => Piece | null; /** * Move a piece from one position to another. * Returns the captured piece (if any) or true if the move was successful, * false if the source position is invalid or empty. */ declare const movePiece$1: (board: Board, from: Position, to: Position) => Piece | boolean; /** * Clear a position on the board by setting it to null. * Returns true if successful, false if the position is invalid. */ declare const clearPosition: (board: Board, position: Position) => boolean; /** * Clear the entire chess board by setting all positions to null. * @param board - The chess board to clear * @returns The cleared board */ declare const clearBoard: (board: Board) => Board; /** * Check if all positions between two positions (from, to) are empty. * Works only on straight paths (horizontal, vertical, diagonal). */ declare const isPathClear: (from: Position, to: Position, board: Board) => boolean; /** * Convert algebraic notation (e.g., "e4") to coordinates */ declare const algebraicToPosition: (algebraic: string) => Position; /** * Clone a chess board deeply to avoid reference issues. * This creates a completely new copy of the board with all pieces. */ declare const cloneBoard: (board: Board) => Board; /** * Convert coordinates to algebraic notation */ declare const positionToAlgebraic: (position: Position) => string; /** * Moves a piece on the chess board following chess rules. * * @param from - Starting position of the piece * @param to - Target position for the piece * @param gameState - Current game state * @param promotionType - Optional piece type to promote a pawn to (defaults to Queen) * @param skipStatusUpdate - Internal flag to prevent recursion in validation checks * @returns A new game state with the updated board or null if the move is invalid */ declare const movePiece: (from: Position, to: Position, gameState: GameState, promotionType?: PieceType, skipStatusUpdate?: boolean) => GameState | null; /** * Checks if a move is valid according to chess rules * * @param from - Starting position * @param to - Target position * @param gameState - Current game state * @returns True if the move is valid, false otherwise */ declare const isValidMove: (from: Position, to: Position, gameState: GameState) => boolean; /** * Initialize a new game state with the standard chess setup * @returns Initial game state with pieces in starting positions */ declare const initGameState: () => GameState; /** * Change the current turn from one player to another * @param gameState - Current game state * @returns New game state with updated current player */ declare const switchTurn: (gameState: GameState) => GameState; /** * Add a move to the game history * @param gameState - Current game state * @param from - Starting position of the move * @param to - Ending position of the move * @param piece - The piece that was moved * @param captured - Optional captured piece * @param special - Optional special move type * @returns New game state with updated move history */ declare const addMoveToHistory: (gameState: GameState, from: Position, to: Position, piece: Piece, captured?: Piece, special?: SpecialMove) => GameState; /** * Get the current player's color * @param gameState - Current game state * @returns The color of the current player */ declare const getCurrentPlayer: (gameState: GameState) => Color; /** * Get the move history * @param gameState - Current game state * @returns Array of move history entries */ declare const getMoveHistory: (gameState: GameState) => Array<{ from: Position; to: Position; piece: Piece; captured?: Piece; special?: SpecialMove; }>; /** * Check if a specific player is in check * @param gameState - Current game state * @param color - Color of the player to check * @returns True if the player is in check, false otherwise */ declare const isPlayerInCheck: (gameState: GameState, color: Color) => boolean; /** * Check if a player is in checkmate * @param gameState - Current game state * @param color - Color of the player to check * @returns True if the player is in checkmate, false otherwise */ declare const isCheckmate: (gameState: GameState, color: Color) => boolean; /** * Check if a player is in stalemate * @param gameState - Current game state * @param color - Color of the player to check * @returns True if the player is in stalemate, false otherwise */ declare const isStalemate: (gameState: GameState, color: Color) => boolean; /** * Update game state for check, checkmate and stalemate * @param gameState - Current game state * @returns Updated game state with check, checkmate and stalemate flags */ declare const updateGameStatus: (gameState: GameState) => GameState; /** * Get all possible moves for a pawn at the given position */ declare const getPawnMoves: (position: Position, gameState: GameState) => Move[]; /** * Get all possible moves for a rook at the given position */ declare const getRookMoves: (position: Position, gameState: GameState) => Move[]; /** * Calculates all possible moves for a knight. * Knight moves in an L-shape and can jump over other pieces. */ declare const getKnightMoves: (position: Position) => Position[]; /** * Calculates all possible moves for a bishop. * Bishops move diagonally and are blocked by other pieces. */ declare const getBishopMoves: (from: Position, gameState: GameState) => Move[]; /** * Get all possible moves for a queen at the given position. * The queen combines the movement of the rook (horizontal/vertical) * and the bishop (diagonal). */ declare const getQueenMoves: (position: Position, gameState: GameState) => Move[]; /** * Returns all possible moves for a king at the given position. */ declare const getKingMoves: (position: Position, gameState: GameState) => Move[]; /** * Returns all legal moves for the piece at the given position. * This function acts as a dispatcher that routes to piece-specific movement logic. * * @param position The position of the piece * @param gameState The current game state * @returns An array of legal moves for the piece */ declare const getLegalMoves: (position: Position, gameState: GameState) => Move[]; declare const arePositionsEqual: (a: Position, b: Position) => boolean; /** * Converts a chess piece to its FEN character representation * @param piece - The chess piece to convert * @returns Single character representing the piece in FEN notation */ declare const pieceToFenChar: (piece: Piece) => string; /** * Converts a FEN character to a chess piece * @param fenChar - Single character from FEN notation * @returns Chess piece object or null if invalid */ declare const fenCharToPiece: (fenChar: string) => Piece | null; /** * Converts a chess board to FEN board representation (piece placement only) * @param board - 8x8 chess board * @returns FEN string representing piece positions */ declare const boardToFenPieces: (board: Board) => string; /** * Converts FEN piece placement to a chess board * @param fenPieces - FEN string representing piece positions * @returns 8x8 chess board */ declare const fenPiecesToBoard: (fenPieces: string) => Board; /** * Determines castling rights from current game state * @param gameState - Current game state * @returns Castling rights string for FEN */ declare const getCastlingRights: (gameState: GameState) => string; /** * Gets en passant target square from game state * @param gameState - Current game state * @returns En passant target square or '-' if none */ declare const getEnPassantTarget: (gameState: GameState) => string; /** * Converts a complete game state to FEN notation * @param gameState - Current game state * @returns Complete FEN string */ declare const gameStateToFen: (gameState: GameState) => string; /** * Converts a FEN string to a game state * @param fen - FEN notation string * @returns Game state object */ declare const fenToGameState: (fen: string) => GameState; /** * Standard starting position FEN */ declare const STARTING_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; export { type Board, Color, type GameState, type Move, type Piece, PieceType, type Position, STARTING_FEN, type SpecialMove, addMoveToHistory, algebraicToPosition, arePositionsEqual, boardToFenPieces, clearBoard, clearPosition, cloneBoard, fenCharToPiece, fenPiecesToBoard, fenToGameState, gameStateToFen, getBishopMoves, getCastlingRights, getCurrentPlayer, getEnPassantTarget, getKingMoves, getKnightMoves, getLegalMoves, getMoveHistory, getPawnMoves, getPieceAt, getQueenMoves, getRookMoves, initBoard, initGameState, isCheckmate, isPathClear, isPlayerInCheck, isStalemate, isValidMove, isValidPosition, movePiece, movePiece$1 as movePieceOnBoard, pieceToFenChar, placePiece, positionToAlgebraic, removePiece, switchTurn, updateGameStatus };