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.

Still developing our pieces chesster.js (and this documentation site) is currently under construction. Please note that there may be incomplete features and breaking changes while improvements are being made.

Installation

Install the chesster.js package using your preferred package manager.

Using npm

npm install chesster.js

Using yarn

yarn add chesster.js

Quick 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.

Screenshot of chesster.js demo website.
Play arrow_outward

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 soon

Random 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();
}
Under Construction This section is still being developed.

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

NameTypeDescription
fenstring?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

NameTypeDescription
gameStringRecursivePartial<GameState>?Optional GameState object

Example

// Coming soon

Basic Usage Guide

Learn the fundamentals of working with chesster.js for common chess application scenarios.

Under Construction This section is currently being developed.