Skip to content

SreekarMouli98/simple-chess-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Usage

Create Game

const SimpleChessEngine = require('simple-chess-engine');
const game = new SimpleChessEngine();

Get Game Status

/**
 * Returns the current status of the game
 * 
 * @returns {object}
 {
     "nextPlayer": "white",
     "isCheck": false,
     "isCheckmate": false,
     "isStalemate": false
 }
 */
game.getStatus();

Get Possible Moves

/**
 * Returns all the valid possible moves of a piece at the given position
 * @param {string} position
 * 
 * @returns {array}
 ["A3", "C3"]
 */
game.getPossibleMoves('B1');

Move Piece

/**
 * 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');

Undo

/**
 * 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();

Get Complete Board

/**
 * 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();

Load Custom Board

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);

Reset Game

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();

Debug Board

/**
 * 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...)