Board state access, piece manipulation, and visual representation methods. Provides comprehensive tools for accessing and modifying the chess board, including piece placement, removal, and various board visualizations.
Returns a 2D array representation of the current board position.
/**
* Get 2D array representation of the current board
* @returns 8x8 array where each cell contains piece info or null for empty squares
*/
board(): (PieceInfo | null)[][];
interface PieceInfo {
/** Square identifier */
square: Square;
/** Piece type */
type: PieceSymbol;
/** Piece color */
color: Color;
}Usage Examples:
import { Chess } from "chess.js";
const chess = new Chess();
const board = chess.board();
// Access specific squares
console.log(board[0][0]); // a8 - black rook
// { square: 'a8', type: 'r', color: 'b' }
console.log(board[4][4]); // e4 - empty square
// null
// Iterate through board
for (let rank = 0; rank < 8; rank++) {
for (let file = 0; file < 8; file++) {
const piece = board[rank][file];
if (piece) {
console.log(`${piece.color}${piece.type} on ${piece.square}`);
}
}
}Returns the piece on a specific square.
/**
* Get the piece on a specific square
* @param square - Square to check
* @returns Piece object or undefined if square is empty
*/
get(square: Square): Piece | undefined;
interface Piece {
/** Piece type */
type: PieceSymbol;
/** Piece color */
color: Color;
}Usage Examples:
const chess = new Chess();
const piece = chess.get('e1');
console.log(piece); // { type: 'k', color: 'w' } - white king
const empty = chess.get('e4');
console.log(empty); // undefined - empty square
// Check if square has specific piece
const whiteKing = chess.get('e1');
if (whiteKing?.type === 'k' && whiteKing?.color === 'w') {
console.log('White king found on e1');
}Places a piece on the specified square.
/**
* Place a piece on the specified square
* @param piece - Piece to place
* @param square - Target square
* @returns True if piece was successfully placed, false otherwise
*/
put(piece: Piece, square: Square): boolean;
interface Piece {
type: PieceSymbol;
color: Color;
}Usage Examples:
const chess = new Chess();
chess.clear(); // Start with empty board
// Place pieces
const success1 = chess.put({ type: 'k', color: 'w' }, 'e1');
console.log(success1); // true
const success2 = chess.put({ type: 'q', color: 'b' }, 'd8');
console.log(success2); // true
// Cannot place second king of same color
const failed = chess.put({ type: 'k', color: 'w' }, 'e8');
console.log(failed); // false
// Invalid piece type
const invalid = chess.put({ type: 'x' as any, color: 'w' }, 'a1');
console.log(invalid); // falseRemoves and returns the piece from the specified square.
/**
* Remove piece from the specified square
* @param square - Square to clear
* @returns Removed piece or undefined if square was empty
*/
remove(square: Square): Piece | undefined;Usage Examples:
const chess = new Chess();
// Remove a piece
const removed = chess.remove('e2');
console.log(removed); // { type: 'p', color: 'w' } - white pawn
// Try to remove from empty square
const empty = chess.remove('e2');
console.log(empty); // undefined
// Removing king updates castling rights
chess.remove('e1'); // Removes white king, disables white castlingReturns all squares containing the specified piece.
/**
* Find all squares containing the specified piece
* @param piece - Piece to search for
* @returns Array of squares containing the piece
*/
findPiece(piece: Piece): Square[];Usage Examples:
const chess = new Chess();
// Find all white pawns
const whitePawns = chess.findPiece({ type: 'p', color: 'w' });
console.log(whitePawns); // ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2']
// Find black king
const blackKing = chess.findPiece({ type: 'k', color: 'b' });
console.log(blackKing); // ['e8']
// Find non-existent piece
chess.remove('d1'); // Remove white queen
const whiteQueens = chess.findPiece({ type: 'q', color: 'w' });
console.log(whiteQueens); // [] - empty arrayReturns a string containing an ASCII diagram of the current position.
/**
* Get ASCII representation of the current board
* @returns Multi-line string showing the board position
*/
ascii(): string;Usage Examples:
const chess = new Chess();
console.log(chess.ascii());
/*
+------------------------+
8 | r n b q k b n r |
7 | p p p p p p p p |
6 | . . . . . . . . |
5 | . . . . . . . . |
4 | . . . . . . . . |
3 | . . . . . . . . |
2 | P P P P P P P P |
1 | R N B Q K B N R |
+------------------------+
a b c d e f g h
*/
chess.move('e4');
chess.move('e5');
console.log(chess.ascii());
/*
+------------------------+
8 | r n b q k b n r |
7 | p p p p . p p p |
6 | . . . . . . . . |
5 | . . . . p . . . |
4 | . . . . P . . . |
3 | . . . . . . . . |
2 | P P P P . P P P |
1 | R N B Q K B N R |
+------------------------+
a b c d e f g h
*/Returns the color of a chess square.
/**
* Get the color of a chess square
* @param square - Square to check
* @returns 'light', 'dark', or null if invalid square
*/
squareColor(square: Square): 'light' | 'dark' | null;Usage Examples:
const chess = new Chess();
console.log(chess.squareColor('a1')); // 'dark'
console.log(chess.squareColor('a2')); // 'light'
console.log(chess.squareColor('h1')); // 'light'
console.log(chess.squareColor('h8')); // 'dark'
// Invalid square
console.log(chess.squareColor('z9' as any)); // null
// Use for bishop placement validation
const bishop = chess.get('c1');
const bishopSquareColor = chess.squareColor('c1'); // 'light'
// This bishop can only move to light squaresReturns a unique hash for the current position (for repetition detection).
/**
* Get unique hash for current position
* @returns Hexadecimal string representing position hash
*/
hash(): string;Usage Examples:
const chess = new Chess();
const initialHash = chess.hash();
chess.move('e4');
const afterE4Hash = chess.hash();
console.log(initialHash !== afterE4Hash); // true - different positions
chess.undo();
const backToInitialHash = chess.hash();
console.log(initialHash === backToInitialHash); // true - same position
// Use for position tracking
const positionMap = new Map<string, number>();
positionMap.set(chess.hash(), 1);type Color = 'w' | 'b';
type PieceSymbol = 'p' | 'n' | 'b' | 'r' | 'q' | 'k';
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 Piece {
type: PieceSymbol;
color: Color;
}
interface PieceInfo {
square: Square;
type: PieceSymbol;
color: Color;
}