Import the necessary modules and create a position from FEN notation:
import { Chess } from 'chessops/chess';import { parseFen } from 'chessops/fen';// Parse a FEN string to create a Setupconst setup = parseFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1').unwrap();// Create a Chess position from the setupconst pos = Chess.fromSetup(setup).unwrap();
The .unwrap() method extracts the value from a Result type. In production code, you should handle errors appropriately.
3
Generate and make moves
Use the position to generate legal moves and play them:
import { parseSquare } from 'chessops';// Get all legal movesconst moves = pos.allDests();console.log(`${moves.size} legal destinations available`);// Get legal moves from a specific square (e4)const e2 = parseSquare('e2')!;const movesFromE2 = pos.dests(e2);// Make a move (e2-e4)const e4 = parseSquare('e4')!;pos.play({ from: e2, to: e4 });
4
Check position status
Query the position to check game state:
// Check if position is checkmateif (pos.isCheckmate()) { console.log('Checkmate!');}// Check if position is in checkif (pos.isCheck()) { console.log('Check!');}// Get the outcome if game is overconst outcome = pos.outcome();if (outcome) { console.log('Game over. Winner:', outcome.winner || 'Draw');}
Here’s a complete example that demonstrates parsing FEN, making moves, and checking for checkmate:
import { Chess } from 'chessops/chess';import { parseFen, makeFen } from 'chessops/fen';import { parseSquare } from 'chessops';import { parseSan, makeSan } from 'chessops/san';// Parse Scholar's Mate position (4 moves before mate)const fenString = 'r1bqkbnr/ppp2Qpp/2np4/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 4';const setup = parseFen(fenString).unwrap();const pos = Chess.fromSetup(setup).unwrap();// Check position statusconsole.log('Is checkmate?', pos.isCheckmate()); // trueconsole.log('Is check?', pos.isCheck()); // trueconst outcome = pos.outcome();if (outcome) { console.log('Winner:', outcome.winner); // 'white'}// Working with the starting positionconst startSetup = parseFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1').unwrap();const startPos = Chess.fromSetup(startSetup).unwrap();// Parse and play moves in SAN notationconst move1 = parseSan(startPos, 'e4');if (move1) { const sanNotation = makeSan(startPos, move1); console.log('Playing:', sanNotation); // 'e4' startPos.play(move1);}// Get current FENconst currentFen = makeFen(startPos.toSetup());console.log('Current position:', currentFen);
Chessops uses bitboards internally for efficient position representation and move generation. The SquareSet class provides methods for working with sets of squares.
import { Crazyhouse, Atomic, Antichess, KingOfTheHill } from 'chessops/variant';import { parseFen } from 'chessops/fen';// Create a Crazyhouse positionconst setup = parseFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1').unwrap();const crazyhousePos = Crazyhouse.fromSetup(setup).unwrap();// Atomic chessconst atomicPos = Atomic.fromSetup(setup).unwrap();// Each variant has its own rules for move generation and game end conditions