3.0.3 • Published 4 years ago

simple-chess-engine v3.0.3

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

Simple-Chess-Engine

simple-chess-engine is a multiplayer chess engine which keeps track of the board and produces valid possbile moves.

Features

  1. Accepts algebraic notation
  2. Lists possible valid moves
  3. Check detection
  4. Checkmate detection
  5. Stalemate detection
  6. Supports special moves (Castling)
  7. Supports undo
  8. 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...

Donate Donate (Supports GPay, Paytm, PhonePe, BHIM UPI, Credit Cards, Debit Cards, Netbanking...)

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago