UNPKG

@chess-labs/core

Version:

A lightweight, framework-agnostic chess engine written entirely in TypeScript

93 lines (68 loc) โ€ข 2.42 kB
# Chess Core A minimal, standalone chess logic implementation in TypeScript. ## Features - ๐ŸŽฏ **Type-safe piece and board representations** - ๐Ÿ”ง **Pure logic with no UI or DOM dependencies** - โ™Ÿ๏ธ **Complete legal move calculation for all pieces** - ๐Ÿฐ **Full special rules support** (castling, en passant, promotion) - โœ… **Check, checkmate, and stalemate detection** - ๐Ÿ“ **FEN notation support** (import/export game states) - ๐Ÿ”„ **Easily portable** into any frontend or backend project - ๐Ÿงช **Unit test friendly architecture** with comprehensive test coverage ## Installation Once published to npm: ```bash npm install @chess-labs/core ``` ## Usage Example ```typescript import { initGameState, getLegalMoves, movePiece, gameStateToFen, fenToGameState } from '@chess-labs/core'; // Initialize a new game const gameState = initGameState(); // Get legal moves for a piece const moves = getLegalMoves({ row: 6, col: 4 }, gameState); // White pawn at e2 // Make a move const newGameState = movePiece( { row: 6, col: 4 }, // from e2 { row: 4, col: 4 }, // to e4 gameState ); // Convert to FEN notation if (newGameState) { const fen = gameStateToFen(newGameState); console.log(fen); // "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1" // Load from FEN const loadedGame = fenToGameState(fen); } ``` ## Architecture ``` src/ โ”œโ”€โ”€ types.ts # Core types (Piece, Board, Position) โ”œโ”€โ”€ board.ts # Board initialization & utilities โ”œโ”€โ”€ board.spec.ts # Board related tests โ”œโ”€โ”€ game.ts # Game state, turn tracking, move execution โ”œโ”€โ”€ game.spec.ts # Game logic tests โ”œโ”€โ”€ helper.ts # Utility functions โ”œโ”€โ”€ helper.spec.ts # Utility tests โ””โ”€โ”€ moves/ # Per-piece movement logic ``` ## Roadmap - โœ… Basic movement rules - โœ… Special rules (castling, en passant, promotion) - โœ… Check & checkmate detection - โœ… FEN support (NEW!) - PGN support - AI opponent integration examples ## Testing ```bash npm run test ``` Unit tests are written with Vitest, covering all movement and rule logic. ## Goals - Write maintainable, testable TypeScript logic - Decouple core engine from UI for maximum reusability - Build as an open-source learning and portfolio project ## Contributing Pull requests, suggestions, and feedback are welcome! Please refer to the issues tab to get started.