@hiron-tez/chess
v2.1.0
Published
A chess engine. This is a fully functional game core that implements all the functionality and logic and provides an easy way to interact with the pieces.
Downloads
22
Maintainers
Readme
Chess engine
This is a fully functional game core that implements all the functionality and logic and provides an easy way to interact with the pieces.
Install
npm
npm i @hiron-tez/chess
pnpm
pnpm add @hiron-tez/chess
yarn
yarn add @hiron-tez/chess
bun
bun add @hiron-tez/chess
Usage
Basic
import { Board } from "@hiron-tez/chess";
const board = new Board();
await board.move("a2", "a4");
await board.autoMove();
await board.undo();
Advanced
import { CustomBoard, King, Pawn } from "@hiron-tez/chess";
const board = new CustomBoard([
new King("B2", Color.White),
new King("B8", Color.Black),
new Pawn("A2", Color.White),
new Pawn("B4", Color.Black),
]);
const pawnDoubleMove = await board.move("A2", "A4");
// { success: true, ...}
const enPassant = await board.move("B4", "A3");
// { success: true, ...}
const blackPawn = board.getPieceAt("A3");
// [instance of Piece]
const whitePawn = board.getPieceAt("A4");
// undefined
const checkColor = board.checkColor;
// Color.White
const checkmateColor = board.checkmateColor;
// null
const whiteKingPossibleMoves = board.getPossibleMoves("B2");
// [array of Position instances]
const isBlackPawnAtA3 = blackPawn?.isAt("A3");
// true
Documentation
Notation string
String that represents a position on a chess board. Allowed characters: a-h, A-H, 1-8. Format: "[File][Rank]"
- type:
string
- example:
"A1"
- type:
PointT
An object with x and y coordinates. Allowed values: 0-7
type:
object
example:
{ x: 0, y: 0 }
PositionInputT
PointT, Position or notation string
- type:
PointT
|Position
|string
- type:
Type
Enum that represents the type of a chess piece.
- members:
King
Queen
Rook
Bishop
Knight
Pawn
- members:
Color
Enum that represents the color of a chess piece.
- members:
White
Black
- members:
Position
A class with coordinates and useful methods and properties to work with positions
- params:
position
PositionInputT
- properties:
x
number - coordinatey
number - coordinatenotation
string | undefined - a string representation of the positionisValid
boolean - indicates whether the position is valid
- methods:
distanceTo
- get distance to a specified position- params:
position
PositionInputT
- returns number
- example:
position.distanceTo("A1");
- params:
- params:
Piece
A class representing a chess piece.
- properties:
type
Type (readonly) - the type of the piececolor
Color (readonly) - the color of the pieceoppositeColor
Color (readonly) - the color of the opposite teamposition
Position - the position of the pieceisMoved
boolean - whether the piece was already moved or not yetid
string - a unique identifier
- methods:
isAt
- check if the piece is at specified position- params:
position
PositionInputT
- returns boolean
- params:
- properties:
King, Queen, Rook, Bishop, Knight, Pawn (extend Piece)
Classes that represent chess pieces with their unique behaviors
- parameters:
position
PositionInputT - the position of the piececolor
Color - the color of the piece
- parameters:
CustomBoard
Chess game controller
- parameters:
pieces
Array<King | Queen | Rook | Bishop | Knight | Pawn> - the set of piecesoptions.colorToMove
Color (optional) - which team should move firstoptions.getPromotionVariant
EventHandler["GetPromotionVariant"] (optional)options.onBoardChange
EventHandler["BoardChange"] (optional)options.onCheck
EventHandler["Check"] (optional)options.onCheckmate
EventHandler["Checkmate"] (optional)options.onDraw
EventHandler["Draw"] (optional)options.onCheckResolve
EventHandler["CheckResolve"] (optional)options.onCheckmateResolve
EventHandler["CheckmateResolve"] (optional) (triggers only on undo)options.onDrawResolve
EventHandler["DrawResolve"] (optional) (triggers only on undo)options.onMove
EventHandler["Move"] (optional)options.onCapture
EventHandler["Capture"] (optional)options.onCastling
EventHandler["Castling"] (optional)options.onPromotion
EventHandler["Promotion"] (optional)
- properties:
status
Status - status of the gamecheckColor
Color | null - color of the team in checkcheckmateColor
Color | null - color of the team in checkmateisDraw
boolean - did game ended with a drawwinnerColor
Color | null - color of the winner teamcolorToMove
Color - color of the team that makes the next movepieces
Array<Piece> - the current set of piecescapturedPieces
Array<Piece> - the set of captured pieceshistory
Array<MoveT> - the list of moves
- methods:
move
- move a piece from and to a specified position- params:
startPosition
PositionInputendPosition
PositionInput
- returns Promise<MoveReturnT> - move result with details
- params:
undo
- undo the latest move- returns UndoReturnT
on
- set event handler. Overrides previous handler for the same event- params:
event
EventeventHandler
EventHandler
- params:
getPieceAt
- get a piece at the specified position- params:
position
PositionInputT
- returns Piece | undefined
- params:
getPiecesByColor
- get all the pieces of a specified team- params:
color
Color
- returns Array<Piece>
- params:
getCapturedPiecesByColor
- get all the captured pieces of a specified team- params:
color
Color
- returns Array<Piece>
- params:
getPossibleMoves
- get all possible moves of a specified piece- params:
position
PositionInput
- returns Array<Position> - positions for valid moves
- params:
evaluate
- evaluate current positions from the perspective of the current team to move- params:
depth
number (optional) (default: 2) - the depth of the forecast
- returns number - number in range from -Infinity (defeat) to Infinity (win)
- params:
autoMove
- move automatically- params:
depth
number (optional) (default: 2) - the depth of the forecast
- returns Promise<MoveReturnT> - move result with details
- params:
- parameters:
Board (extends CustomBoard)
Chess game controller with the prepared set of pieces
- parameters:
options
- same as in CustomBoard
- parameters:
Status
Enum that represents the current status of the game
- members:
Active
Check
Checkmate
Draw
- members:
Event
Enum that represents the type of an event
- members:
BoardChange
Check
Checkmate
Draw
CheckResolve
CheckmateResolve
DrawResolve
Move
Capture
Castling
Promotion
- members:
EventHandlerT
Event handlers types
- type:
object
GetPromotionVariant
- get the promotion variant for a pawn- parameters:
position
Position - the position of the pawn
- returns Type | Promise<Type>
- parameters:
BoardChange
- board change event handler- parameters:
pieces
Array<Piece> - the current piece set
- parameters:
Check
- check event handler- parameters:
color
Color - the color of the team that's in check
- parameters:
Checkmate
- checkmate event handler- parameters:
color
Color - the color of the team that's in checkmate
- parameters:
Draw
- draw event handlerCheckResolve
- check resolve event handlerCheckmateResolve
- checkmate resolve event handler. (Triggers only on undo after a checkmate)DrawResolve
- draw resolve event handler. (Triggers only on undo after a draw)Move
- piece movement event handler- parameters:
startPosition
Position - piece start positionendPosition
Position - piece end position
- parameters:
Capture
- piece capture event handler- parameters:
capturedPosition
Position - captured piece position
- parameters:
Castling
- castling event handler- parameters:
kingStartPosition
Position - king start positionkingEndPosition
Position - king end positionrookStartPosition
Position - rook start positionrookEndPosition
Position - rook end position
- parameters:
Promotion
- pawn promotion event handler- parameters:
position
Position - position of the pawn
- parameters:
- type:
MoveType
Enum that represents a type of a move
- members
Move
Capture
Castling
Promotion
- members
MoveT
Details of a move
- type:
object
type
MoveType - type of the movestartPosition
Position - initial position of the pieceendPosition
Position - final position of the piececapturedPosition
Position | undefined (exists iftype
isCapture
) - position of a captured piece. Differs fromendPosition
only on en-passant.castlingRookStartPosition
Position | undefined (exists iftype
isCastling
) - initial position of the castling rookcastlingRookEndPosition
Position | undefined (exists iftype
isCastling
) - final position of the castling rooknewPieceType
Type | undefined (exists iftype
isPromotion
) - the new piece type of the promoted pawnpieceId
string - id of the moved piece. Id of a promoted pawn changesisPieceFirstMove
boolean - indicates whether the piece moved for the first time
- type:
MoveReturnT
A wrapper around the
MoveT
that helps to handle successful and unsuccessful movessuccess
boolean - whether the move succeeded- ...
MoveT
- properties fromMoveT
(exist only ifsuccess
is true) reason
string - the reason for the failure (exists only ifsuccess
is false)
UndoReturnT
An object that helps to handle successful and unsuccessful undo events
success
boolean - whether the undo was successfulreason
string - the reason for the failure (exists only ifsuccess
is false)