This is a fully functional game core that implements all the functionality and logic and provides an easy way to interact with the pieces.
npm i @hiron-tez/chess
pnpm add @hiron-tez/chess
yarn add @hiron-tez/chess
bun add @hiron-tez/chess
import { Board } from "@hiron-tez/chess";
const board = new Board();
await board.move("a2", "a4");
await board.autoMove();
await board.undo();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 MoveT]
const isBlackPawnAtA3 = blackPawn?.isAt("A3");
// true-
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:
KingQueenRookBishopKnightPawn
- members:
-
Color
Enum that represents the color of a chess piece.
- members:
WhiteBlack
- members:
-
Position
A class with coordinates and useful methods and properties to work with positions
- params:
positionPositionInputT
- properties:
xnumber - coordinateynumber - coordinatenotationstring | undefined - a string representation of the positionisValidboolean - indicates whether the position is valid
- methods:
distanceTo- get distance to a specified position- params:
positionPositionInputT
- returns number
- example:
position.distanceTo("A1");
- params:
- params:
-
Piece
A class representing a chess piece.
- properties:
typeType (readonly) - the type of the piececolorColor (readonly) - the color of the pieceoppositeColorColor (readonly) - the color of the opposite teampositionPosition - the position of the pieceisMovedboolean - whether the piece was already moved or not yetidstring - a unique identifier
- methods:
isAt- check if the piece is at specified position- params:
positionPositionInputT
- returns boolean
- params:
- properties:
-
King, Queen, Rook, Bishop, Knight, Pawn (extend Piece)
Classes that represent chess pieces with their unique behaviors
- parameters:
positionPositionInputT - the position of the piececolorColor - the color of the piece
- parameters:
-
CustomBoard
Chess game controller
- parameters:
piecesArray<King | Queen | Rook | Bishop | Knight | Pawn> - the set of piecesoptions.colorToMoveColor (optional) - which team should move firstoptions.getPromotionVariantEventHandler["GetPromotionVariant"] (optional)options.onBoardChangeEventHandler["BoardChange"] (optional)options.onCheckEventHandler["Check"] (optional)options.onCheckmateEventHandler["Checkmate"] (optional)options.onDrawEventHandler["Draw"] (optional)options.onCheckResolveEventHandler["CheckResolve"] (optional)options.onCheckmateResolveEventHandler["CheckmateResolve"] (optional) (triggers only on undo)options.onDrawResolveEventHandler["DrawResolve"] (optional) (triggers only on undo)options.onMoveEventHandler["Move"] (optional)options.onCaptureEventHandler["Capture"] (optional)options.onCastlingEventHandler["Castling"] (optional)options.onPromotionEventHandler["Promotion"] (optional)
- properties:
statusStatus - status of the gamecheckColorColor | null - color of the team in checkcheckmateColorColor | null - color of the team in checkmateisDrawboolean - did game ended with a drawwinnerColorColor | null - color of the winner teamcolorToMoveColor - color of the team that makes the next movepiecesArray<Piece> - the current set of piecescapturedPiecesArray<Piece> - the set of captured pieceshistoryArray<MoveT> - the list of moves
- methods:
move- move a piece from and to a specified position- params:
startPositionPositionInputendPositionPositionInput
- 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:
eventEventeventHandlerEventHandler
- params:
getPieceAt- get a piece at the specified position- params:
positionPositionInputT
- returns Piece | undefined
- params:
getPiecesByColor- get all the pieces of a specified team- params:
colorColor
- returns Array<Piece>
- params:
getCapturedPiecesByColor- get all the captured pieces of a specified team- params:
colorColor
- returns Array<Piece>
- params:
getPossibleMoves- get all possible moves of a specified piece- params:
positionPositionInput
- returns Array<MoveT> - list of valid moves
- params:
evaluate- evaluate current positions from the perspective of the current team to move- params:
depthnumber (optional) (default: 2) - the depth of the forecast
- returns number - number in range from -Infinity (defeat) to Infinity (win)
- params:
autoMove- move automatically- params:
depthnumber (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:
ActiveCheckCheckmateDraw
- members:
-
Event
Enum that represents the type of an event
- members:
BoardChangeCheckCheckmateDrawCheckResolveCheckmateResolveDrawResolveMoveCaptureCastlingPromotion
- members:
-
EventHandlerT
Event handlers types
- type:
object GetPromotionVariant- get the promotion variant for a pawn- parameters:
positionPosition - the position of the pawn
- returns Type | Promise<Type>
- parameters:
BoardChange- board change event handler- parameters:
piecesArray<Piece> - the current piece set
- parameters:
Check- check event handler- parameters:
colorColor - the color of the team that's in check
- parameters:
Checkmate- checkmate event handler- parameters:
colorColor - 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:
startPositionPosition - piece start positionendPositionPosition - piece end position
- parameters:
Capture- piece capture event handler- parameters:
capturedPositionPosition - captured piece position
- parameters:
Castling- castling event handler- parameters:
kingStartPositionPosition - king start positionkingEndPositionPosition - king end positionrookStartPositionPosition - rook start positionrookEndPositionPosition - rook end position
- parameters:
Promotion- pawn promotion event handler- parameters:
positionPosition - position of the pawn
- parameters:
- type:
-
MoveType
Enum that represents a type of a move
- members
MoveCaptureCastlingPromotion
- members
-
MoveT
Details of a move
- type:
object typeMoveType - type of the movestartPositionPosition - initial position of the pieceendPositionPosition - final position of the piececapturedPositionPosition | undefined (exists iftypeisCapture) - position of a captured piece. Differs fromendPositiononly on en-passant.castlingRookStartPositionPosition | undefined (exists iftypeisCastling) - initial position of the castling rookcastlingRookEndPositionPosition | undefined (exists iftypeisCastling) - final position of the castling rooknewPieceTypeType | undefined (exists iftypeisPromotion) - the new piece type of the promoted pawnpieceIdstring - id of the moved piece. Id of a promoted pawn changesisPieceFirstMoveboolean - indicates whether the piece moved for the first time
- type:
-
MoveReturnT
A wrapper around the
MoveTthat helps to handle successful and unsuccessful movessuccessboolean - whether the move succeeded- ...
MoveT- properties fromMoveT(exist only ifsuccessis true) reasonstring - the reason for the failure (exists only ifsuccessis false)
-
UndoReturnT
An object that helps to handle successful and unsuccessful undo events
successboolean - whether the undo was successfulreasonstring - the reason for the failure (exists only ifsuccessis false)