Install chess.js via npm
masterInstall the most recent version of chess.js from the NPM registry using the following command:
npm install chess.jsrepository·master·Indexed 26 days ago
https://github.com/jhlywa/chess.jsA TypeScript library for chess move generation, validation, execution, and game state detection (check, checkmate, stalemate). Version 1.4.0 provides functionality for managing game state via the Chess class, including FEN and PGN loading, legal move generation, and board state manipulation. It handles the logic of the game but does not include an AI engine.
Install the most recent version of chess.js from the NPM registry using the following command:
npm install chess.jsYou can use the Chess class to manage game state, generate valid moves, and detect game over conditions. The following example demonstrates how to instantiate a new game, loop through valid moves randomly until the game ends, and output the final game in PGN format.
import { Chess } from 'chess.js'
const chess = new Chess()
while (!chess.isGameOver()) {
const moves = chess.moves()
const move = moves[Math.floor(Math.random() * moves.length)]
chess.move(move)
}
console.log(chess.pgn())Use the move() method to execute a move. The method accepts several formats:
'e4').{ from, to, promotion }.null (to represent a null move).You can pass an optional { strict: boolean } configuration object. If strict is true, the move must be valid according to chess rules.
board() method returns a 2D array representing the 8x8 chessboard. Each element is either null (empty square) or an object containing the piece's square, type, and color.nagToGlyph function converts a Numeric Annotation Glyph (NAG) into its corresponding chess symbol string.Create a new instance of the Chess class to manage a chess game. You can optionally provide an initial FEN (Forsyth-Edwards Notation) string.
Use the skipValidation option if you want to bypass FEN validation during initialization.
The Chess class provides several methods to query the current state of the game:
inCheck(): Returns true if the current player's king is in check.isCheckmate(): Returns true if the game is over via checkmate.isDraw(): Returns true if the game is a draw.isGameOver(): Returns true if the game has ended.isStalemate(): Returns true if the game is a stalemate.isThreefoldRepetition(): Returns true if the game is a draw by threefold repetition.isInsufficientMaterial(): Returns true if there is insufficient material to force a mate.The moves() method returns an array of legal moves. You can filter moves by square, piece, or request verbose move objects.
Options:
square: Filter moves starting from a specific Square.piece: Filter moves for a specific PieceSymbol.verbose: If true, returns an array of Move objects instead of SAN strings.Use the loadPgn(pgn, options) method to load a game from a Portable Game Notation string.
Options:
strict: If true, enforces strict PGN compliance.newlineChar: Specifies the character used for newlines in the PGN.undo() method reverts the last move made in the game. It returns the Move object that was undone, or null if there were no moves to undo.Use the load(fen, options) method to set the game state to a specific FEN string.
Options:
skipValidation: If true, bypasses FEN validation.preserveHeaders: If true, preserves PGN headers (if applicable).