simple-chess-engine
v3.1.0
Published
A Multiplayer chess engine
Downloads
5
Readme
Simple-Chess-Engine
simple-chess-engine
is a multiplayer chess engine which keeps track of the board and produces valid possbile moves.
Features
- Accepts algebraic notation
- Lists possible valid moves
- Check detection
- Checkmate detection
- Stalemate detection
- Supports special moves (Castling)
- Supports undo
- Load custom board new
API
- Create Game
- Get Game Status
- Get Possible Moves
- Move Piece
- Get Complete Board
- Undo
- Load Custom Board
- Reset Game
- Debug Board
Usage
const SimpleChessEngine = require('simple-chess-engine');
const game = new SimpleChessEngine();
/**
* Returns the current status of the game
*
* @returns {object}
{
"nextPlayer": "white",
"isCheck": false,
"isCheckmate": false,
"isStalemate": false
}
*/
game.getStatus();
/**
* Returns all the valid possible moves of a piece at the given position
* @param {string} position
*
* @returns {array}
["A3", "C3"]
*/
game.getPossibleMoves('B1');
/**
* Used to move a piece from source position to destination position
* @param {string} source
* @param {string} destination
*
* @returns {object}
{
"success": true | false,
"status" : {
"nextPlayer" : 'white' | 'black',
"isCheck" : true | false,
"isCheckmate": true | false,
"isStalemate": true | false
}
}
*
* NOTE :: In order to get the updated board, use the getBoard() method.
*/
game.makeMove('B1', 'C3');
/**
* Used to undo any previosly made move
*
* @returns {object}
{
"nextPlayer" : 'black' | 'white',
"isCheck" : true | false,
"isCheckmate": true | false,
"isStalemate": true | false
}
*
* NOTE :: In order to get the updated board, use the getBoard() method.
*/
game.undo();
/**
* Used to return the complete board
*
* @param {object} opts
* Supported opts:
* format: 'JSON' =>(Currently only JSON format is supported, any requested formats might be considered)
*
* @returns {object}
*
{
"A8": {
"color": "black",
javascriptiece": "rook"
},
"B8": {
"color": "black",
"piece": "bishop"
},
...
}
*/
game.getBoard();
let content = JSON.stringify({
A1: { color: "white", piece: "king" },
C1: { color: "black", piece: "king" },
A2: { color: "white", piece: "rook" }
});
let opts = { firstPlayer: 'white' };
/**
* Used to load custom board
* @param {string} content => Stringified JSON Object
* @param {object} opts
*
* Supported opts:
* firstPlayer = 'white' | 'black'
*
* @returns {object}
* Success Case
{
"success": "true",
"status": {
"nextPlayer": "white",
"isCheck": false,
"isCheckmate": false,
"isStalemate": false
}
}
* Failure Case
{
"success": "false",
"reasons": [...]
}
*/
game.loadBoard(content, opts);
let opts = { custom: true };
/**
* Used to reset the board
* @param {object} opts
*
* Supported opts:
* custom = true | false (resetGame with custom=true will reload the custom board which was previousy loaded (if any))
*
* NOTE :: resetGame with custom=false will remove custom board from memory
* NOTE :: In order to get the updated board, use the getBoard() method.
*/
game.resetGame();
/**
* Logs the complete chess board to the console
*/
game.displayBoard();
If you find this useful, you can support me by donating here...
(Supports GPay, Paytm, PhonePe, BHIM UPI, Credit Cards, Debit Cards, Netbanking...)