Coder Social home page Coder Social logo

chess-rules's Introduction

NPM version Build Status Dependency Status Coverage Status

Chess Rules implementation as a standalone module.

Install

As a Node module

$ npm install chess-rules

This will install rules module in the node_modules folder.

Using Bower

$ bower install chess-rules
bower chess-rules#*             cached git://github.com/ChessCorp/chess-rules.git#0.8.0
bower chess-rules#*           validate 0.8.0 against git://github.com/ChessCorp/chess-rules.git#*
bower chess-rules#*                new version for git://github.com/ChessCorp/chess-rules.git#*
bower chess-rules#*            resolve git://github.com/ChessCorp/chess-rules.git#*
bower chess-rules#*           download https://github.com/ChessCorp/chess-rules/archive/0.10.1.tar.gz
bower chess-rules#*            extract archive.tar.gz
bower chess-rules#*           resolved git://github.com/ChessCorp/chess-rules.git#0.10.1
bower chess-rules#~0.10.1      install chess-rules#0.10.1

chess-rules#0.10.1 bower_components/chess-rules

Usage

Import the main rules object

> var chessRules = require('chess-rules');

> chessRules
{ getInitialPosition: [Function: getInitialPosition],
  positionToString: [Function: positionToString],
  getAvailableMoves: [Function: getAvailableMoves],
  applyMove: [Function: applyMove],
  computeDiffs: [Function: computeDiffs],
  applyDiffs: [Function: applyDiffs],
  pgnToMove: [Function: pgnToMove],
  moveToPgn: [Function: moveToPgn],
  getGameStatus: [Function: getGameStatus] 
  }

Instantiate an initial position model

var position = chessRules.getInitialPosition();

The position model used by the internal engine has the following structure:

{ turn: 'W',
  castlingFlags: { W: { K: true, Q: true }, B: { K: true, Q: true } },
  lastPawnMoveColumn: null,
  check: false,
  board: 
   [ { type: 'R', side: 'W' },
     { type: 'N', side: 'W' },
     { type: 'B', side: 'W' },
     { type: 'Q', side: 'W' },
     { type: 'K', side: 'W' },
     { type: 'B', side: 'W' },
     { type: 'N', side: 'W' },
     { type: 'R', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     { type: 'P', side: 'W' },
     null,
     null,
     null,
     ...
   ]
}

The position can be logged more conveniently using the provided function positionToString:

> console.log(chessRules.positionToString(position))
WHITE KQkq
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  

An extra parameter can be provided to use extended charsets but may not be supported by all terminals:

> console.log(chessRules.positionToString(position, true))
WHITE KQkq
8         
7         
6                 
5                 
4                 
3                 
2         
1         
  a b c d e f g h 

Get available moves for a given position

Moves are expressed as a pair of coordinates on the board. The offset value is computed row by row starting at A1.

> var availableMoves = chessRules.getAvailableMoves(position);
undefined

> availableMoves
[ { src: 1, dst: 16 },
  { src: 1, dst: 18 },
  { src: 6, dst: 21 },
  { src: 6, dst: 23 },
  { src: 8, dst: 16 },
  { src: 8, dst: 24 },
  { src: 9, dst: 17 },
  { src: 9, dst: 25 },
  { src: 10, dst: 18 },
  { src: 10, dst: 26 },
  { src: 11, dst: 19 },
  { src: 11, dst: 27 },
  { src: 12, dst: 20 },
  { src: 12, dst: 28 },
  { src: 13, dst: 21 },
  { src: 13, dst: 29 },
  { src: 14, dst: 22 },
  { src: 14, dst: 30 },
  { src: 15, dst: 23 },
  { src: 15, dst: 31 } ]
> 

Converting a move from / to PGN movetext notation

PGN move text is described here https://en.wikipedia.org/wiki/Portable_Game_Notation

Converting move text to move vector:

> chessRules.pgnToMove(position, 'e4')
{ src: 12, dst: 28 }

> chessRules.pgnToMove(position, 'Pe2e4')
{ src: 12, dst: 28 }

> chessRules.pgnToMove(position, 'Nf3')
{ src: 6, dst: 21 }

Converting move vector to PGN move text:

> chessRules.moveToPgn(position, { src: 6, dst: 23 });
'Nh3'

Note: the position must always be provided because the way that the moves are shortened requires access to the current piece positions to make these transforms.

Update the position by playing moves

The applyMove will apply a move vector to create a new updated position structure. The original position object remains unchanged.

> var moveE4 = chessRules.pgnToMove(position,'e4');
undefined

> var updatedPosition = chessRules.applyMove(position, moveE4);
undefined

> console.log(chessRules.positionToString(position))
WHITE KQkq
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 
undefined

> console.log(chessRules.positionToString(updatedPosition))
BLACK KQkq
8 r n b q k b n r 
7 p p p p p p p p 
6 . . . . . . . . 
5 . . . . . . . . 
4 . . . . P . . . 
3 . . . . . . . . 
2 P P P P . P P P 
1 R N B Q K B N R 
  a b c d e f g h 
undefined
> 

Determine game status

A game status can be either:

  • OPEN - The game is still in progress
  • PAT - The game has ended as one of the players couldn't move anymore while not being checkmate
  • WHITEWON - The white player won
  • BLACKWON - The black player won
> chessRules.getGameStatus(position)
'OPEN'

Find pieces on the board

There is a findPiece function to locate a piece on the board. The search has to provide a piece type, and an optional side. If the side is not provided, the position's current player side is used.

> chessRules.findPiece(position,'K','W')
[ { offset: 4, x: 4, y: 0 } ]
> chessRules.findPiece(position,'K','B')
[ { offset: 60, x: 4, y: 7 } ]
> chessRules.findPiece(position,'K')
[ { offset: 4, x: 4, y: 0 } ]
> chessRules.findPiece(position,'B')
[ { offset: 2, x: 2, y: 0 }, { offset: 5, x: 5, y: 0 } ]

License

MIT © Yannick Kirschhoffer

chess-rules's People

Contributors

akshitkrnagpal avatar alcibiade avatar dohbedoh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.