or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

board-representation.mdfen-support.mdgame-analysis.mdgame-management.mdindex.mdmove-operations.mdpgn-support.md
tile.json

game-analysis.mddocs/

Game Analysis

Position analysis including check/mate detection, draw conditions, attack analysis, and castling rights management. Provides comprehensive tools for analyzing chess positions and determining game outcomes.

Capabilities

Check Detection

Determines if the current side to move is in check.

/**
 * Check if the current side to move is in check
 * @returns True if the current player's king is being attacked
 */
isCheck(): boolean;

/**
 * Alias for isCheck()
 * @returns True if the current player's king is being attacked
 */
inCheck(): boolean;

Usage Examples:

import { Chess } from "chess.js";

const chess = new Chess();

console.log(chess.isCheck()); // false - starting position

// Create check position
chess.load('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3');
console.log(chess.isCheck()); // true - white is in check

// Both methods work the same
console.log(chess.inCheck()); // true

Checkmate Detection

Determines if the current side to move is checkmated.

/**
 * Check if the current side to move is checkmated
 * @returns True if the current player is in check and has no legal moves
 */
isCheckmate(): boolean;

Usage Examples:

const chess = new Chess();

// Famous checkmate position - Scholar's Mate
chess.move('e4');
chess.move('e5');
chess.move('Bc4');
chess.move('Nc6');
chess.move('Qh5');
chess.move('Nf6');
chess.move('Qxf7#');

console.log(chess.isCheckmate()); // true
console.log(chess.isGameOver()); // true

Stalemate Detection

Determines if the current side to move is stalemated.

/**
 * Check if the current side to move is stalemated
 * @returns True if the current player is not in check but has no legal moves
 */
isStalemate(): boolean;

Usage Examples:

const chess = new Chess();

// Create stalemate position
chess.load('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78');
console.log(chess.isStalemate()); // true
console.log(chess.isGameOver()); // true
console.log(chess.isCheck()); // false - not in check, but no moves

Draw Conditions

Various methods to detect draw conditions.

/**
 * Check if the game is drawn by any condition
 * @returns True if game is drawn by 50-move rule, stalemate, insufficient material, or repetition
 */
isDraw(): boolean;

/**
 * Check if the game is drawn by the 50-move rule
 * @returns True if 50 moves have been made without a pawn move or capture
 */
isDrawByFiftyMoves(): boolean;

/**
 * Check if the game is drawn due to insufficient material
 * @returns True if neither side has enough material to deliver checkmate
 */
isInsufficientMaterial(): boolean;

/**
 * Check if the current position has occurred three or more times
 * @returns True if position has been repeated 3+ times
 */
isThreefoldRepetition(): boolean;

Usage Examples:

const chess = new Chess();

// Test 50-move rule
chess.load('4k3/4P3/4K3/8/8/8/8/8 b - - 100 78'); // 50 moves without pawn/capture
console.log(chess.isDrawByFiftyMoves()); // true

// Insufficient material - K vs K
chess.load('4k3/8/8/8/8/8/8/4K3 w - - 0 1');
console.log(chess.isInsufficientMaterial()); // true

// K+B vs K
chess.load('4k3/8/8/8/8/8/8/3BK3 w - - 0 1');
console.log(chess.isInsufficientMaterial()); // true

// K+N vs K  
chess.load('4k3/8/8/8/8/8/8/3NK3 w - - 0 1');
console.log(chess.isInsufficientMaterial()); // true

// Threefold repetition
const initialPosition = chess.fen();
chess.move('Nf3');
chess.move('Nf6');
chess.move('Ng1');
chess.move('Ng8'); // Back to initial position
console.log(chess.isThreefoldRepetition()); // false - only 2 times

chess.move('Nf3');
chess.move('Nf6');
chess.move('Ng1');
chess.move('Ng8'); // Third time in same position
console.log(chess.isThreefoldRepetition()); // true

Game Over Detection

Determines if the game has ended by any condition.

/**
 * Check if the game has ended
 * @returns True if game is over by checkmate, stalemate, or draw
 */
isGameOver(): boolean;

Usage Examples:

const chess = new Chess();

console.log(chess.isGameOver()); // false - game in progress

// Checkmate ends game
chess.load('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3');
chess.move('g3'); // Allows mate
chess.move('Qh3#');
console.log(chess.isGameOver()); // true

// Stalemate ends game
chess.load('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78');
console.log(chess.isGameOver()); // true

// Draw ends game
chess.load('4k3/8/8/8/8/8/8/4K3 w - - 0 1');
console.log(chess.isGameOver()); // true - insufficient material

Attack Analysis

Methods to analyze which squares are being attacked.

/**
 * Check if a square is being attacked by the specified color
 * @param square - Square to check
 * @param attackedBy - Color of attacking pieces
 * @returns True if square is under attack by the specified color
 */
isAttacked(square: Square, attackedBy: Color): boolean;

/**
 * Get all squares that are attacking the specified square
 * @param square - Square to analyze
 * @param attackedBy - Optional color filter (defaults to current player)
 * @returns Array of squares containing pieces that can attack the target square
 */
attackers(square: Square, attackedBy?: Color): Square[];

Usage Examples:

const chess = new Chess();

// Check if square is attacked
console.log(chess.isAttacked('f3', 'w')); // true - white pieces can attack f3
console.log(chess.isAttacked('f6', 'b')); // true - black pieces can attack f6

// Get attacking squares
const whitePawns = chess.attackers('f3');
console.log(whitePawns); // ['e2', 'g2'] - pawns that can capture on f3

// After a move
chess.move('e4');
const attackingE5 = chess.attackers('e5');
console.log(attackingE5); // Pieces that can capture on e5

// Check king safety
const whiteKingSquare = 'e1';
const blackAttackers = chess.attackers(whiteKingSquare, 'b');
console.log(blackAttackers.length === 0); // True if king is safe

Castling Rights

Methods to manage and query castling rights.

/**
 * Get castling rights for the specified color
 * @param color - Color to check castling rights for
 * @returns Object indicating kingside and queenside castling availability
 */
getCastlingRights(color: Color): CastlingRights;

/**
 * Set castling rights for the specified color
 * @param color - Color to modify castling rights for
 * @param rights - Castling rights to set
 * @returns True if rights were successfully updated
 */
setCastlingRights(color: Color, rights: Partial<CastlingRights>): boolean;

interface CastlingRights {
  /** Kingside castling available */
  k: boolean;
  /** Queenside castling available */  
  q: boolean;
}

Usage Examples:

import { Chess, KING, QUEEN } from "chess.js";

const chess = new Chess();

// Check initial castling rights
const whiteRights = chess.getCastlingRights('w');
console.log(whiteRights); // { k: true, q: true }

const blackRights = chess.getCastlingRights('b');
console.log(blackRights); // { k: true, q: true }

// Modify castling rights
const success = chess.setCastlingRights('w', { [KING]: false });
console.log(success); // true if successful

// Check updated rights
const updatedRights = chess.getCastlingRights('w');
console.log(updatedRights); // { k: false, q: true }

// Remove all castling rights
chess.setCastlingRights('b', { [KING]: false, [QUEEN]: false });

// Rights are automatically updated when pieces move
chess.move('e4');
chess.move('e5');
chess.move('Ke2'); // King moves, loses castling rights
const afterKingMove = chess.getCastlingRights('w');
console.log(afterKingMove); // { k: false, q: false }

Performance Testing

Method for testing move generation performance.

/**
 * Performance test for move generation (perft)
 * @param depth - Search depth
 * @returns Number of leaf nodes at the specified depth
 */
perft(depth: number): number;

Usage Examples:

const chess = new Chess();

// Performance test from starting position
console.log(chess.perft(1)); // 20 - 20 possible first moves
console.log(chess.perft(2)); // 400 - 400 possible positions after 2 plies
console.log(chess.perft(3)); // 8902 - more complex analysis

// Use for debugging move generation
const testPosition = 'r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1';
chess.load(testPosition);
console.log(chess.perft(2)); // Known result for this position

Types

type Color = 'w' | 'b';
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 CastlingRights {
  k: boolean;  // Kingside
  q: boolean;  // Queenside
}

// Constants for castling rights
const KING = 'k';
const QUEEN = 'q';