node-chess
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

Node-chess

A fully isomorphic and extensible chess engine with position analysis, board analysis, and computer opposition.

NPM version Travis build status

What does it do? How is it extensible?

Node-chess allows you extend the board and add your own rules. The analysis engine and computer player will automatically factor these changes/additions into their calculations and adjust accordingly.

With node-chess you can:

  • define your own pieces and modify existing pieces
    • (re-)define their notation, movement, value, and capture logic
  • add, change, and remove rules such as win and loss conditions
  • extend the existing engine to improve the calculations for your own variants

Installation

Add it as a dependency to your project

npm install node-chess

In the browser

<script src="node-chess.js">
    var game = chess.classic.engine();
</script> 

The engine comes with a 'classic' implementation of Chess.

var chess = require("node-chess");
var game = chess.classic.engine();

Using the board

// Move the E2 pawn to E4
game.movePiece( { from: { file: 5, rank: 2 } , to: { file: 5, rank: 4 } });
 
// Move the B8 knight to C6 
game.movePiece( { from: { file: 2, rank: 8 }, to: { file: 3, rank: 6 } });
 
// Try and make an invalid move
// No piece is on B8
game.movePiece({ from: { file: 2, rank: 8 }, to: { file: 3, rank: 6 } }); === null; // true
 
// Promotion
// To a queen (by default)
game.movePiece({ from: { file: 1, rank: 7 }, to: { file: 1, rank: 8 } });
 
// To a rook
game.movePiece({ from: { file: 1, rank: 7 }, to: { file: 1, rank: 8 } }, "r");
 
// Print the available moves of the C6 knight to the console
console.log(classicEngine.getMoves({ file: 3, rank: 6 }));

Defining your own pieces

The 'super knight' moves 3 squares laterally before moving 1 square on the opposite axis!

var Direction = require("./engine/direction");
var customEngine = new chess.Engine();
 
var upLeft = makeMove(-1, 3);
var upRight = makeMove(1, 3);
 
var downLeft = makeMove(-1, -3);
var downRight = makeMove(1, -3);
 
var leftUp = makeMove(-3, 1);
var leftDown = makeMove(-3, -1);
 
var rightUp = makeMove(3, 1);
var rightDown = makeMove(3, -1);
 
function makeMove(file, rank) {
    return {
        canCapture: true,
        canMove: true,
        transforms: { file: file, rank: rank, canJump: true }
    }
}
 
var superKnight = {
    name: "SuperKnight",
    movement: [upLeft, upRight, downLeft, downRight, leftUp, leftDown, rightUp, rightDown],
    canQueen: false,
    canSpawn: true,
    value: 3.5,
    notation: "s"
}
 
customEngine.pieces.push(superKnight);

Analytics

Build Status

Package Sidebar

Install

npm i node-chess

Weekly Downloads

2

Version

1.3.1

License

MIT

Last publish

Collaborators

  • seikho