A modern, object-oriented chess engine built with TypeScript
npm install ts-chess-engineEngine class maintains the board state and game state
ChessEngine/
āāā src/
ā āāā index.ts # Index file exporting needed classes
ā āāā engine.ts # Main engine class (game logic coordinator)
ā āāā models/
ā ā āāā board.ts # Board representation
ā ā āāā coordinate.ts # Coordinate class
ā ā āāā gameState.ts # Gamestate enum
ā ā āāā move.ts # Move class
ā ā āāā pieceColor.ts # Piececolor enum
ā ā āāā pieceName.ts # Piecename enum
ā ā āāā specialMove.ts # Specialmove enum
ā āāā pieces/
ā ā āāā piece.ts # Piece interface
ā ā āāā pawn.ts # Pawn implementation
ā ā āāā rook.ts # Rook implementation
ā ā āāā knight.ts # Knight implementation
ā ā āāā bishop.ts # Bishop implementation
ā ā āāā queen.ts # Queen implementation
ā ā āāā king.ts # King implementation
ā āāā util/
ā āāā defaultPiecesSetup.ts # The standard chess pieces setup
āāā README.md # This file
`
š Getting Started
$3
#### NPM Package
`bash
npm install ts-chess-engine
`
#### From Source
`bash
Clone the repository
git clone https://github.com/Sandervg03/ChessEngine.git
cd ChessEngine
npm install
npm run build
`
$3
Initializing your board and engine
`typescript
import { ChessEngine, Board, Pawn, PieceColor } from "ts-chess-engine";
// Create a board with pieces. When you don't declare your pieces yourself,
// the board will have the standard chess setup listed in src/util/defaultPiecesSetup.ts.
const board = new Board([
new Pawn(PieceColor.white, new Coordinate(1, 1))
/ your pieces /
]);
// Create the engine
const engine = new ChessEngine(board);
`
Previewing all possible moves from a piece
`typescript
engine.previewMoves(new Coordinate(1, 1))
`
Making a move
`typescript
const piece = engine.board.getPieceAt(new Coordinate(1, 1));
const from = new Coordinate(1, 1);
const to = new Coordinate(1, 3);
const success = engine.move(new Move(piece, from, to));
`
Pawn promotion
`typescript
import { SpecialMove } from "ts-chess-engine"
if (to.y === 8 / or 1 for black /) {
const success = engine.move(new Move(piece, from, to), SpecialMove.PromoteQueen)
}
`
Keeping track of the game
`typescript
const gamestate: GameState = engine.gameState
if (gamestate === GameState.whiteWin) {
alert("White has won the game!")
}
``