A chess engine written in modern javascript with a minimal codebase.
npm i bchess
<script src="https://cdn.jsdelivr.net/npm/bchess@1/dist/bchess.min.js"></script>
Simply include the library to start using it:
const { Chess, Action, Piece } = require("bchess");
const chess = new Chess({color: "white"});
You can also initialise a new game using FEN notation:
const chess = new Chess({fen:"kb5Q/p7/Pp6/1P6/4p3/4R3/4P1p1/6K1"});
Carry out the specified move
chess.move({from:"c2",to:"c4"});
A third option is available to specify the piece used for promotion (if available):
chess.move({from:"c2",to:"c4",promote:"Q"});
One or more Action flags.
The following is a list of possible flags:
- MOVE
- PLAYER_CAPTURE
- OPPONENT_CAPTURE
- CASTLE_KING
- CASTLE_QUEEN
- EN_PASSANT
- PROMOTE
- INVALID_ACTION
Set a piece down anywhere on the board:
chess.set({square: "h4", piece: new Piece("Q","white")});
Return an array of available moves a piece can make
chess.available({square:"e2"})
Get the current game history
chess.history
Returns an ascii string representing the current board
chess.ascii()
Example output:
+--------------------------+
8 | r n b q k b n r |
7 | p p p p p . p p |
6 | . . . . . P . . |
5 | . . . . . . . . |
4 | . . . . . . . . |
3 | . . . . . N . B |
2 | P P P P P P . P |
1 | R N B Q . R K . |
+--------------------------+
a b c d e f g h
The perspective of the board returned depends on the Chess myColor
property.
Property | Description | Values | Default |
---|---|---|---|
border | Toggle border display | true / false | true |
file | Toggle file display | true / false | true |
rank | Toggle rank display | true / false | true |
unicode | Display unicode characters for pieces instead of FEN notation | true / false | false |
Get the current game score
chess.score()
Fetch the information at a current square
chess.get({square:"g8"})
Returns the current game state in FEN notation.
chess.fen()
Example output:
rnbqkbnr/ppppp1pp/5P2/1Q6/8/5N1B/PPPPPP1P/RNBQ1RK1
Return the total move count (completed turns)
chess.moves
Undo the most recent move
chess.undo();
Whose turn is it?
chess.turn()
Returns pieces in check (and pieces checking them)
chess.check()
Returns checkmate status for both sides
chess.checkmate()
Returns stalemate status for both sides
chess.stalemate()
Return the SAN notation of a move
chess.moveToSAN({from:"f2",to:"f4"})