A fast & universal chesslibrary with Basic AI support. This is the core part of the @chesslib bundle.
I created this chesslibrary because the available chesslibraries don't allow implementation of realtime chess games that are not turn based. This limitation prevented me implementing my mobile game. So i came up with a solution. I also realized that there are only a few chess libraries available that support fast move generation.
It was also quite difficult to implement extra chesspieces for chess-variants that are quite new and more interesting.
This chesslibrary features :
This library is also used for:
npm install @chesslib/core
https://www.npmjs.com/package/@chesslib/core
Get all available moves
import { Chessboard, Positions, COLORS } from "@chesslib/core";
var board = Chessboard.fromPosition(Positions.default);
// Get all possible moves
var moves = board.getFENMoves();
if(board.tryMoveFEN(moves[0].from, moves[0].to))
{
console.log("Move was valid");
}
Get all available moves for BLACK
// Get all possible moves for BLACK
var moves = board.getFENMoves(COLORS.BLACK);
Get all available moves for A2 Pawn?
// Get all possible moves for BLACK
var moves = board.getFENMovesatFEN("a2");
Validate if move is possible?
// Get all possible moves for BLACK
if(board.isValidMoveFEN("a2", "a4"))
{
console.log("Move is valid");
}
Check if king was captured
// Get all possible moves
if(board.findPieces("K").length == 1)
{
console.log("GAME OVER!");
}
For more please see examples folder
The AI is a simple implementation of a basic board evaluation. The AI is trying every moves possible confirming the board value is better for a given move. The search depth can be configured, and due to it's high CPU intensivity it is recommended to use it with webworkers in browsers, and run it as a seperate process or thread in nodejs if required.
AI REPO:
https://github.com/azarus/chesslib-ai
The example use of the AI is available in the repo under examples/ folder.
addHistory(piece: Piece, data: any);
boardSize: {x: number, y: number}
charIndex(index: number): string;
clone(): Chessboard
clearSquare(square: Square);
createPiece(type: string, color: number | COLORS, square: Square);
doMove(move: MoveObject);
doMoveFEN(move: { from: string, to: string});
FENtoPosition(fen: string): {x: number, y: number};
FENToSquare(fen: string): SVGPathSegCurvetoQuadraticRel
findPieces(pieceType: string, color: number | COLORS): Piece[]
getBoard(): { BoardObject }
setBoard(board: { BoardObject });
savePositions(): any[]
loadPositions(positions: any[]);
import { Chessboard, Positions } from "@chesslib/core";
var board = Chessboard.fromPosition(Positions.default);
// Get all possible moves
var moves = board.getFENMoves();
if(board.tryMoveFEN(moves[0].from, moves[0].to))
{
console.log("Move was valid");
}
For more please see examples folder
Use the lib together with:
and
to create your game. Have happy coding :)
Apache 2.0