chesster.js docs
Introduction
chesster.js is a dependency-free TypeScript chess library focused on performance and accuracy. It provides a complete implementation of chess rules, move validation and game state management.
Installation
Install the chesster.js package using your preferred package manager.
Using npm
npm install chesster.jsUsing yarn
yarn add chesster.jsQuick Start
Below is a simple example that demonstrates how to create a new chess game, make moves, undo moves, and query the game state. The library handles all move validation, game rules, and state management automatically.
import { Chesster } from "chesster.js";
// Create a new game
const game = new Chesster();
// Get all legal moves
const moves = game.moves();
// Make a move
game.move(moves[0]);
// Undo a move
game.undo();
// Check if game is over
if (game.isGameOver()) {
console.log("gg");
}Demo
Check out chesster.js in action with this interactive demo site. Play against an AI opponent or play locally on the same device. The source code for the demo is available on GitHub.

Examples
Complete examples to help you get started building chess applications with chesster.js.
Simple Game Loop
A basic game loop that plays through a predefined sequence of moves.
// coming soonRandom Moves
Simulate a game where the players make random legal moves until the game ends.
for (let i = 0; i < NUM_GAMES; i++) {
function getRandomMove(game) {
const moves = game.moves();
return moves[Math.floor(Math.random() * moves.length)];
}
function playRandomGame() {
const game = new Chesster();
let moveCount = 0;
while (!game.isGameOver()) {
const move = getRandomMove(game);
game.move(move);
moveCount++;
}
console.log(`game ended after ${moveCount} moves`);
if (game.white.isCheckmated || game.black.isCheckmated) {
const winner = game.turn === WHITE ? "black" : "white";
console.log(`checkmate: ${winner} wins!`);
} else if (game.isStalemate) {
console.log("draw: stalemate");
} else if (game.isDraw) {
console.log("draw");
}
return game;
}
// Play a random game
playRandomGame();
}Chesster
The Chesster class is the main entry point for managing chess games. It handles move validation, game state, and provides methods for querying the current position.
FEN String Constructor
new Chesster(fen?: string)Creates a new chess game instance. Optionally accepts a FEN string to initialize a specific board position.
Parameters
| Name | Type | Description |
|---|---|---|
fen | string? | Optional FEN string |
Example
// Create a new game with starting position
const game = new Chess();
// Create from custom FEN (also starting position)
const customGame = new Chess('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');Game State Constructor
new Chesster(gameState?: RecursivePartial<GameState>)Creates a new chess game instance. Optionally accepts a partial game state to initialize a specific game position.
Parameters
| Name | Type | Description |
|---|---|---|
gameString | RecursivePartial<GameState>? | Optional GameState object |
Example
// Coming soonBasic Usage Guide
Learn the fundamentals of working with chesster.js for common chess application scenarios.