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-rulesbower chess-rules#* cached git://github.com/ChessCorp/chess-rules.git#0.8.0bower chess-rules#* validate 0.8.0 against git://github.com/ChessCorp/chess-rules.git#*bower chess-rules#* new version
Usage
Import the main rules object
> var chessRules = ; > 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;
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:
> consoleWHITE KQkq8 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:
> consoleWHITE KQkq8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 6 5 4 3 2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ a b c d e f g h
Import/export position in FEN Format
Positions can be transformed to FEN string, for details about this notation, see description of FEN format
This is an example on how to export a position to FEN string (position is the initial position):
> chessRules'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
This is an example on how to load a FEN string as a position object:
> position = chessRules> consoleBLACK 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;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 src: 12 dst: 28 > chessRules src: 12 dst: 28 > chessRules src: 6 dst: 21
Converting move vector to PGN move text:
> chessRules;'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;undefined > var updatedPosition = chessRules;undefined > consoleWHITE KQkq8 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 > consoleBLACK KQkq8 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'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 offset: 4 x: 4 y: 0 > chessRules offset: 60 x: 4 y: 7 > chessRules offset: 4 x: 4 y: 0 > chessRules offset: 2 x: 2 y: 0 offset: 5 x: 5 y: 0
License
MIT © Yannick Kirschhoffer