npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

black-horse

v0.6.3

Published

Chess engine built in Javascript using the alpha-beta algorithm

Downloads

50

Readme

Black Horse

https://www.npmjs.com/package/black-horse

Javascript Chess Engine

Installation

npm install black-horse --save

Usage

Just import the library and all of Black Horse namespace will be available to you:

const BlackHorseChessEngine = require('black-horse');

Public API

All the methods use the coordinates system based on the following board:

/*
         8    r  n  b  q  k  b  n  r
         7    p  p  p  p  p  p  p  p
         6
         5
         4
         3
         2    P  P  P  P  P  P  P  P
         1    R  N  B  Q  K  B  N  R

              1  2  3  4  5  6  7  8
*/

initBoard()

Set the board to the default opening board.

BlackHorseChessEngine.initBoard();

printBoard()

Prints de current board to the console

BlackHorseChessEngine.printBoard();
/*
         8   r  n  b  q  k  b  n  r 
         7   p  p  p  p  p  p  p  p 
         6   .  .  .  .  .  .  .  . 
         5   .  .  .  .  .  .  .  . 
         4   .  .  .  .  .  .  .  . 
         3   .  .  .  .  .  .  .  . 
         2   P  P  P  P  P  P  P  P 
         1   R  N  B  Q  K  B  N  R 
 
             a  b  c  d  e  f  g  h 
*/

parseFEN(fenString)

Used to set the board to different state, using the FEN notation

BlackHorseChessEngine.parseFEN("2r3k1/1q1nbppp/r3p3/3pP3/p1pP4/P1Q2N2/1PRN1PPP/2R4K b - - 0 0");

getBoard()

Return the board object with all the current status, including the current FEN string

console.log(BlackHorseChessEngine.getBoard());
/*
{
      board:
       [ [ 10, 8, 9, 11, 12, 9, 8, 10 ],
         [ 7, 7, 7, 7, 7, 7, 7, 7 ],
         [ 0, 0, 0, 0, 0, 0, 0, 0 ],
         [ 0, 0, 0, 0, 0, 0, 0, 0 ],
         [ 0, 0, 0, 0, 0, 0, 0, 0 ],
         [ 0, 0, 0, 0, 0, 0, 0, 0 ],
         [ 1, 1, 1, 1, 1, 1, 1, 1 ],
         [ 4, 2, 3, 5, 6, 3, 2, 4 ] ],
      castle:
       [ { kingSide: true, queenSide: true },
         { kingSide: true, queenSide: true } ],
      pieceMaterial: [ 54100, 54100 ],
      pawnMaterial: [ 800, 800 ],
      capturedPieces: [ [], [] ],
      kingPosition: [ [ 1, 5 ], [ 8, 5 ] ],
      piecesCounter: [ null, 8, 2, 2, 2, 1, 1, 8, 2, 2, 2, 1, 1 ],
      hash: 726761150,
      color: 0,
      enPassant: false,
      fiftyMoveCounter: 0,
      fullmoveCounter: 0,
      pawnList: [ { list: [Object] }, { list: [Object] } ],
      pieceList: [ { list: [Object] }, { list: [Object] } ],
      fenString: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0'
}
*/

getPieceValidMoves(row, column)

getPieceValidMoves(notation)

Returns an Array of all the valid moves for the piece in the position indicated by the coordinates or by the Standart Algebraic Notation

console.log(BlackHorseChessEngine.getPieceValidMoves(2, 2)); 
console.log(BlackHorseChessEngine.getPieceValidMoves('b2')); 
/*
[ 
  { 
    algebraicNotation: 'b3',
    piece: 1,
    pieceDest: 0,
    color: 0,
    rowOrig: 2,
    columnOrig: 2,
    rowDest: 3,
    columnDest: 2,
    flag: null,
    promotedPiece: null,
    hash: 2127761049,
    score: -10 
  },
  { 
    algebraicNotation: 'b4',
    piece: 1,
    pieceDest: 0,
    color: 0,
    rowOrig: 2,
    columnOrig: 2,
    rowDest: 4,
    columnDest: 2,
    flag: null,
    promotedPiece: null,
    hash: 2127761049,
    score: -10 
  } 
]
*/

isCheckMate(color)

Checks if for the given color, is it in check mate or not

BlackHorseChessEngine.parseFEN("2Q4k/1R6/8/8/8/8/8/7K w - - 0 2");
//0 - white, 1 - black
BlackHorseChessEngine.isCheckMate(0); //false
BlackHorseChessEngine.isCheckMate(1); //true

searchNextMove(options)

options

{
    minDepth: 4, // min search depth - default 4
    maxSearchTime: 3000 //max search time spended in seconds - default 3000
}

Search the next move for the current color

BlackHorseChessEngine.initBoard();
const searchOptions = {
  minDepth:6, 
  maxSearchTime: 1000 
} 
const nextMove = BlackHorseChessEngine.searchNextMove(searchOptions);
/*
{
    "algebraicNotation": "d4",
    "columnDest": 4,
    "columnOrig": 4,
    "piece": 1,
    "pieceDest": 0,
    "promotedPiece": null,
    "rowDest": 4,
    "rowOrig": 2,
    "score": 10,
    "color": 0
}
*/

move(moveObject)

Search the next move for the current color. Can accept an object with the coordinates and move flag or can accept a string with the Standart Algebraic Notation

console.log(BlackHorseChessEngine.move({
                                   "columnDest": 4,
                                   "columnOrig": 4,
                                   "rowDest": 4,
                                   "rowOrig": 2,
                               }));
console.log(BlackHorseChessEngine.move('d4');
/*
{
  algebraicNotation: 'd4',
  board: { 'board object' },
  isCheckMate: false
}
*/

configs

Object containing all the configurations used in the chess engine.

Pieces codes

  • 0 - empty
  • 1 - White Pawn
  • 2 - White Knight
  • 3 - White Bishop
  • 4 - White Rook
  • 5 - White Queen
  • 6 - White King
  • 7 - Black Pawn
  • 8 - Black Knight
  • 9 - Black Bishop
  • 10 - Black Rook
  • 11 - Black Queen
  • 12 - Black King

Color codes

  • 0 - White
  • 1 - Black

Node.js version compatibility

12.0.0 or higher