@mady9613/chess-engine
v2.0.2
Published
Comprehensive chess engine for move validation and game state management.
Maintainers
Readme
♜ @mady9613/chess-engine
A complete chess engine with move validation, FEN support, and utilities
📦 npm • 🐙 GitHub • 📚 Docs • 🐛 Issues
---# @mady9613/chess-engine
Comprehensive chess engine for move validation and game state management.
- npm: https://www.npmjs.com/package/@mady9613/chess-engine
- Author: mady9613
Installation
npm install @mady9613/chess-engineQuick Start
import { ChessGame } from '@mady9613/chess-engine';
const game = new ChessGame();
const move = game.move('e2e4');
console.log(move.success); // true
console.log(move.san); // "e4"
console.log(game.getFEN()); // Current FENMain Class: ChessGame
Create game
const game = new ChessGame(); // start position
const gameFromFen = new ChessGame(fen); // from FENCommon methods
move(move, promotionPiece?)undo(),redo(),canUndo(),canRedo()reset()loadFEN(fen),getFEN()loadPGN(pgn),getPGN()getBoard(),getPieceAt(square),getTurn()getValidMoves(square),getAllValidMoves()isValidMove(move)isCheck(),isCheckmate(),isStalemate(),isDraw(),isGameOver()isThreefoldRepetition(),isFiftyMoveRule(),isInsufficientMaterial()getWinner(),getResult()getMoveHistory(),getMoves(),getLastMove(),getMoveCount()goToMove(index),goToStart(),goToEnd()toJSON(),toObject(),clone(),fromJSON(data)
Static utility
ChessGame.isValidFEN(fen)
Move Input Format
Supported move input styles:
- UCI-like string:
"e2e4","e7e8q" - Object format:
{ from: 'e2', to: 'e4' } - Object with promotion:
{ from: 'e7', to: 'e8', promotion: 'q' }
Promotion pieces: q, r, b, n
Move Result Shape
game.move(...) returns:
{
success: true,
san: 'e4',
piece: 'wp',
capture: false,
check: false,
checkmate: false,
stalemate: false,
promotion: null
}If invalid:
{ success: false, error: 'Illegal move' }Named Exports
The package also exposes lower-level helpers from src/index.js.
Validators
getValidMovesgetAllLegalMovesisMoveLegalisMoveLegalBasicisMoveLegalQuick
Piece move generators
getPawnMovesgetKnightMovesgetBishopMovesgetRookMovesgetQueenMovesgetKingMoves
Board / notation / FEN utilities
getInitialBoardPositiongetEmptyBoardcopyBoardalgebraicToCoordcoordToAlgebraicisValidCoordgetPieceAtisSquareAttackeddoesPieceAttackSquaregetSANgetDetailedSANparseSANboardToFENFENToBoardgetStartingFENisValidFEN
Core move helpers
executeMoveisCaptureMoveisPromotionMovegetMoveSAN
Example: Validate & Play Loop
import { ChessGame } from '@mady9613/chess-engine';
const game = new ChessGame();
const moves = ['e2e4', 'e7e5', 'g1f3', 'b8c6'];
for (const m of moves) {
const result = game.move(m);
if (!result.success) {
console.log('Invalid:', result.error);
break;
}
console.log(result.san, game.getFEN());
}License
MIT
