1.0.23 โข Published 6 months ago
@chess-labs/core v1.0.23
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:
npm install @chess-labs/coreUsage Example
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 logicRoadmap
- โ Basic movement rules
- โ Special rules (castling, en passant, promotion)
- โ Check & checkmate detection
- โ FEN support (NEW!)
- PGN support
- AI opponent integration examples
Testing
npm run testUnit 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.