Comparing version 0.10.3 to 0.11.0
182
chess.js
@@ -28,10 +28,2 @@ /* | ||
/* minified license below */ | ||
/* @license | ||
* Copyright (c) 2018, Jeff Hlywa (jhlywa@gmail.com) | ||
* Released under the BSD license | ||
* https://github.com/jhlywa/chess.js/blob/master/LICENSE | ||
*/ | ||
var Chess = function(fen) { | ||
@@ -171,2 +163,3 @@ var BLACK = 'b' | ||
var header = {} | ||
var comments = {} | ||
@@ -196,5 +189,25 @@ /* if the user passes in a fen string, load it, else default to | ||
if (!keep_headers) header = {} | ||
comments = {} | ||
update_setup(generate_fen()) | ||
} | ||
function prune_comments() { | ||
var reversed_history = []; | ||
var current_comments = {}; | ||
var copy_comment = function(fen) { | ||
if (fen in comments) { | ||
current_comments[fen] = comments[fen]; | ||
} | ||
}; | ||
while (history.length > 0) { | ||
reversed_history.push(undo_move()); | ||
} | ||
copy_comment(generate_fen()); | ||
while (reversed_history.length > 0) { | ||
make_move(reversed_history.pop()); | ||
copy_comment(generate_fen()); | ||
} | ||
comments = current_comments; | ||
} | ||
function reset() { | ||
@@ -1406,2 +1419,11 @@ load(DEFAULT_POSITION) | ||
var append_comment = function(move_string) { | ||
var comment = comments[generate_fen()] | ||
if (typeof comment !== 'undefined') { | ||
var delimiter = move_string.length > 0 ? ' ' : ''; | ||
move_string = `${move_string}${delimiter}{${comment}}` | ||
} | ||
return move_string | ||
} | ||
/* pop all of history onto reversed_history */ | ||
@@ -1416,4 +1438,10 @@ var reversed_history = [] | ||
/* special case of a commented starting position with no moves */ | ||
if (reversed_history.length === 0) { | ||
moves.push(append_comment('')) | ||
} | ||
/* build the list of moves. a move_string looks like: "3. e3 e6" */ | ||
while (reversed_history.length > 0) { | ||
move_string = append_comment(move_string) | ||
var move = reversed_history.pop() | ||
@@ -1438,3 +1466,3 @@ | ||
if (move_string.length) { | ||
moves.push(move_string) | ||
moves.push(append_comment(move_string)) | ||
} | ||
@@ -1447,3 +1475,3 @@ | ||
/* history should be back to what is was before we started generating PGN, | ||
/* history should be back to what it was before we started generating PGN, | ||
* so join together moves | ||
@@ -1455,5 +1483,43 @@ */ | ||
var strip = function() { | ||
if (result.length > 0 && result[result.length - 1] === ' ') { | ||
result.pop(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
/* NB: this does not preserve comment whitespace. */ | ||
var wrap_comment = function(width, move) { | ||
for (var token of move.split(' ')) { | ||
if (!token) { | ||
continue; | ||
} | ||
if (width + token.length > max_width) { | ||
while (strip()) { | ||
width--; | ||
} | ||
result.push(newline); | ||
width = 0; | ||
} | ||
result.push(token); | ||
width += token.length; | ||
result.push(' '); | ||
width++; | ||
} | ||
if (strip()) { | ||
width--; | ||
} | ||
return width; | ||
}; | ||
/* wrap the PGN output at max_width */ | ||
var current_width = 0 | ||
for (var i = 0; i < moves.length; i++) { | ||
if (current_width + moves[i].length > max_width) { | ||
if (moves[i].includes('{')) { | ||
current_width = wrap_comment(current_width, moves[i]); | ||
continue; | ||
} | ||
} | ||
/* if the current move will push past max_width */ | ||
@@ -1511,3 +1577,3 @@ if (current_width + moves[i].length > max_width && i !== 0) { | ||
key = headers[i].replace(/^\[([A-Z][A-Za-z]*)\s.*\]$/, '$1') | ||
value = headers[i].replace(/^\[[A-Za-z]+\s"(.*)"\]$/, '$1') | ||
value = headers[i].replace(/^\[[A-Za-z]+\s"(.*)"\ *\]$/, '$1') | ||
if (trim(key).length > 0) { | ||
@@ -1561,10 +1627,56 @@ header_obj[key] = value | ||
/* NB: the regexes below that delete move numbers, recursive | ||
* annotations, and numeric annotation glyphs may also match | ||
* text in comments. To prevent this, we transform comments | ||
* by hex-encoding them in place and decoding them again after | ||
* the other tokens have been deleted. | ||
* | ||
* While the spec states that PGN files should be ASCII encoded, | ||
* we use {en,de}codeURIComponent here to support arbitrary UTF8 | ||
* as a convenience for modern users */ | ||
var to_hex = function(string) { | ||
return Array | ||
.from(string) | ||
.map(function(c) { | ||
/* encodeURI doesn't transform most ASCII characters, | ||
* so we handle these ourselves */ | ||
return c.charCodeAt(0) < 128 | ||
? c.charCodeAt(0).toString(16) | ||
: encodeURIComponent(c).replace(/\%/g, '').toLowerCase() | ||
}) | ||
.join('') | ||
} | ||
var from_hex = function(string) { | ||
return string.length == 0 | ||
? '' | ||
: decodeURIComponent('%' + string.match(/.{1,2}/g).join('%')) | ||
} | ||
var encode_comment = function(string) { | ||
string = string.replace(new RegExp(mask(newline_char), 'g'), ' ') | ||
return `{${to_hex(string.slice(1, string.length - 1))}}` | ||
} | ||
var decode_comment = function(string) { | ||
if (string.startsWith('{') && string.endsWith('}')) { | ||
return from_hex(string.slice(1, string.length - 1)) | ||
} | ||
} | ||
/* delete header to get the moves */ | ||
var ms = pgn | ||
.replace(header_string, '') | ||
.replace( | ||
/* encode comments so they don't get deleted below */ | ||
new RegExp(`(\{[^}]*\})+?|;([^${mask(newline_char)}]*)`, 'g'), | ||
function(match, bracket, semicolon) { | ||
return bracket !== undefined | ||
? encode_comment(bracket) | ||
: ' ' + encode_comment(`{${semicolon.slice(1)}}`) | ||
} | ||
) | ||
.replace(new RegExp(mask(newline_char), 'g'), ' ') | ||
/* delete comments */ | ||
ms = ms.replace(/(\{[^}]+\})+?/g, '') | ||
/* delete recursive annotation variations */ | ||
@@ -1596,2 +1708,7 @@ var rav_regex = /(\([^\(\)]+\))+?/g | ||
for (var half_move = 0; half_move < moves.length - 1; half_move++) { | ||
var comment = decode_comment(moves[half_move]) | ||
if (comment !== undefined) { | ||
comments[generate_fen()] = comment | ||
continue | ||
} | ||
move = move_from_san(moves[half_move], sloppy) | ||
@@ -1609,2 +1726,8 @@ | ||
comment = decode_comment(moves[moves.length - 1]) | ||
if (comment !== undefined) { | ||
comments[generate_fen()] = comment | ||
moves.pop() | ||
} | ||
/* examine last move */ | ||
@@ -1750,2 +1873,33 @@ move = moves[moves.length - 1] | ||
return move_history | ||
}, | ||
get_comment: function() { | ||
return comments[generate_fen()]; | ||
}, | ||
set_comment: function(comment) { | ||
comments[generate_fen()] = comment.replace('{', '[').replace('}', ']'); | ||
}, | ||
delete_comment: function() { | ||
var comment = comments[generate_fen()]; | ||
delete comments[generate_fen()]; | ||
return comment; | ||
}, | ||
get_comments: function() { | ||
prune_comments(); | ||
return Object.keys(comments).map(function(fen) { | ||
return {fen: fen, comment: comments[fen]}; | ||
}); | ||
}, | ||
delete_comments: function() { | ||
prune_comments(); | ||
return Object.keys(comments) | ||
.map(function(fen) { | ||
var comment = comments[fen]; | ||
delete comments[fen]; | ||
return {fen: fen, comment: comment}; | ||
}); | ||
} | ||
@@ -1752,0 +1906,0 @@ } |
Special thanks to the following developers for their patches and contributions | ||
(alphabetically): | ||
- [Steve Bragg](https://github.com/2sb18) | ||
- [Ngoc Dao](https://github.com/ngocdaothanh) | ||
- [Matt Flaschen](https://github.com/mattflaschen) | ||
- [E. Azer Koçulu](https://github.com/azer) | ||
- [Falco Nogatz](https://github.com/fnogatz) | ||
- [jdponomarev](https://github.com/jdponomarev) | ||
- [Tom Offermann](https://github.com/toffer) | ||
- [David Moises Paz Reyes](https://github.com/davidmpaz) | ||
- [Raminder Singh](https://github.com/imor) | ||
- [Stiff](https://github.com/stiff) | ||
- [Seb Vincent](https://github.com/sebv) | ||
- [Linmiao Xu](https://github.com/linrock) | ||
- [Jonathan Zacsh](https://github.com/jzacsh) | ||
- [Steve Bragg](https://github.com/2sb18) | ||
- [Ngoc Dao](https://github.com/ngocdaothanh) | ||
- [Matt Flaschen](https://github.com/mattflaschen) | ||
- [E. Azer Koçulu](https://github.com/azer) | ||
- [Falco Nogatz](https://github.com/fnogatz) | ||
- [jdponomarev](https://github.com/jdponomarev) | ||
- [Tom Offermann](https://github.com/toffer) | ||
- [David Moises Paz Reyes](https://github.com/davidmpaz) | ||
- [Raminder Singh](https://github.com/imor) | ||
- [Stiff](https://github.com/stiff) | ||
- [Seb Vincent](https://github.com/sebv) | ||
- [Linmiao Xu](https://github.com/linrock) | ||
- [Jonathan Zacsh](https://github.com/jzacsh) |
{ | ||
"name": "chess.js", | ||
"version": "0.10.3", | ||
"version": "0.11.0", | ||
"license": "BSD-2-Clause", | ||
@@ -5,0 +5,0 @@ "main": "chess.js", |
549
README.md
@@ -15,48 +15,33 @@ # chess.js | ||
```sh | ||
npm install --save chess.js | ||
``` | ||
# NPM | ||
npm install chess.js | ||
chess.js is also available via [CDNJS](https://cdnjs.com/libraries/chess.js): | ||
```html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script> | ||
# Yarn | ||
yarn add chess.js | ||
``` | ||
## Example Code | ||
The code below plays a complete game of chess ... randomly. | ||
The code below plays a random game of chess: | ||
```js | ||
var Chess = require('./chess').Chess; | ||
var chess = new Chess(); | ||
const { Chess } = require('./chess.js') | ||
const chess = new Chess() | ||
while (!chess.game_over()) { | ||
var moves = chess.moves(); | ||
var move = moves[Math.floor(Math.random() * moves.length)]; | ||
chess.move(move); | ||
const moves = chess.moves() | ||
const move = moves[Math.floor(Math.random() * moves.length)] | ||
chess.move(move) | ||
} | ||
console.log(chess.pgn()); | ||
console.log(chess.pgn()) | ||
``` | ||
## Sites Using chess.js | ||
## User Interface | ||
- [chess.com](http://www.chess.com/) | ||
- [The Internet Chess Club (ICC)](http://www.chessclub.com/) | ||
- [lichess](http://lichess.org/tv) | ||
- [Redbull - Battle for the Queen](http://battleforthequeen.redbull.com/) | ||
- [Asm.js Chess Battle](https://developer.microsoft.com/en-us/microsoft-edge/testdrive/demos/chess/) | ||
- [3D Hartwig Chess](http://creativejs.com/2012/12/3d-hartwig-chess/) | ||
- [Scene VR](http://client.scenevr.com/?connect=chess.scenevr.hosting/chess.xml) | ||
- [Multiplayer Chess](http://chessapp.com/) | ||
- [Reti Chess](http://retichess.nodejitsu.com/) | ||
- [Chess Fork](http://www.chessfork.com/) | ||
- [Lozza](http://op12no2.me/posts/1641) | ||
- [angular-chess](http://theborakompanioni.github.io/angular-chess) | ||
- [Chessable](https://www.chessable.com) | ||
- [SlimChess](https://slimchess.com/now) | ||
Need a user interface? Try Chris Oakman's excellent | ||
[chessboard.js](http://chessboardjs.com) library. See | ||
By design, chess.js is headless and does not include user interface. Many | ||
developers have have had success integrating chess.js with the | ||
[chessboard.js](http://chessboardjs.com) library. See | ||
[chessboard.js - Random vs Random](http://chessboardjs.com/examples#5002) for | ||
an example integration of chess.js with chessboard.js. | ||
an example. | ||
@@ -66,2 +51,3 @@ ## API | ||
### Constructor: Chess([ fen ]) | ||
The Chess() constructor takes an optional parameter which specifies the board configuration | ||
@@ -72,20 +58,23 @@ in [Forsyth-Edwards Notation](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation). | ||
// board defaults to the starting position when called with no parameters | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
// pass in a FEN string to load a particular position | ||
var chess = new Chess('r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1 b - c3 0 19'); | ||
const chess = new Chess( | ||
'r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1 b - c3 0 19' | ||
) | ||
``` | ||
### .ascii() | ||
Returns a string containing an ASCII diagram of the current position. | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
// make some moves | ||
chess.move('e4'); | ||
chess.move('e5'); | ||
chess.move('f4'); | ||
chess.move('e4') | ||
chess.move('e5') | ||
chess.move('f4') | ||
chess.ascii(); | ||
chess.ascii() | ||
// -> ' +------------------------+ | ||
@@ -104,11 +93,11 @@ // 8 | r n b q k b n r | | ||
### .board() | ||
### .board() | ||
Returns an 2D array representation of the current position. Empty squares are | ||
Returns an 2D array representation of the current position. Empty squares are | ||
represented by `null`. | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
chess.board(); | ||
chess.board() | ||
// -> [[{type: 'r', color: 'b'}, | ||
@@ -137,24 +126,68 @@ {type: 'n', color: 'b'}, | ||
### .clear() | ||
### .clear() | ||
Clears the board. | ||
```js | ||
chess.clear(); | ||
chess.fen(); | ||
chess.clear() | ||
chess.fen() | ||
// -> '8/8/8/8/8/8/8/8 w - - 0 1' <- empty board | ||
``` | ||
### .delete_comment() | ||
Delete and return the comment for the current position, if it exists. | ||
```js | ||
const chess = new Chess() | ||
chess.load_pgn("1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *") | ||
chess.get_comment() | ||
// -> "giuoco piano" | ||
chess.delete_comment() | ||
// -> "giuoco piano" | ||
chess.get_comment() | ||
// -> undefined | ||
``` | ||
### .delete_comments() | ||
Delete and return comments for all positions. | ||
```js | ||
const chess = new Chess() | ||
chess.load_pgn("1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *") | ||
chess.delete_comments() | ||
// -> [ | ||
// { | ||
// fen: "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", | ||
// comment: "king's pawn opening" | ||
// }, | ||
// { | ||
// fen: "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3", | ||
// comment: "giuoco piano" | ||
// } | ||
// ] | ||
chess.get_comments() | ||
// -> [] | ||
``` | ||
### .fen() | ||
Returns the FEN string for the current position. | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
// make some moves | ||
chess.move('e4'); | ||
chess.move('e5'); | ||
chess.move('f4'); | ||
chess.move('e4') | ||
chess.move('e5') | ||
chess.move('f4') | ||
chess.fen(); | ||
chess.fen() | ||
// -> 'rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b KQkq f3 0 2' | ||
@@ -164,47 +197,108 @@ ``` | ||
### .game_over() | ||
Returns true if the game has ended via checkmate, stalemate, draw, threefold repetition, or insufficient material. Otherwise, returns false. | ||
```js | ||
var chess = new Chess(); | ||
chess.game_over(); | ||
const chess = new Chess() | ||
chess.game_over() | ||
// -> false | ||
chess.load('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78'); | ||
chess.game_over(); | ||
// -> true (stalemate) | ||
// stalemate | ||
chess.load('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78') | ||
chess.game_over() | ||
// -> true | ||
chess.load('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3'); | ||
chess.game_over(); | ||
// -> true (checkmate) | ||
// checkmate | ||
chess.load('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3') | ||
chess.game_over() | ||
// -> true | ||
``` | ||
### .get(square) | ||
Returns the piece on the square: | ||
```js | ||
chess.clear(); | ||
chess.clear() | ||
chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.get('a5'); | ||
chess.get('a5') | ||
// -> { type: 'p', color: 'b' }, | ||
chess.get('a6'); | ||
chess.get('a6') | ||
// -> null | ||
``` | ||
### .get_comment() | ||
Retrieve the comment for the current position, if it exists. | ||
```js | ||
const chess = new Chess() | ||
chess.load_pgn("1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *") | ||
chess.get_comment() | ||
// -> "giuoco piano" | ||
``` | ||
### .get_comments() | ||
Retrieve comments for all positions. | ||
```js | ||
const chess = new Chess() | ||
chess.load_pgn("1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *") | ||
chess.get_comments() | ||
// -> [ | ||
// { | ||
// fen: "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", | ||
// comment: "king's pawn opening" | ||
// }, | ||
// { | ||
// fen: "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3", | ||
// comment: "giuoco piano" | ||
// } | ||
// ] | ||
``` | ||
### .header() | ||
Allows header information to be added to PGN output. Any number of key/value | ||
pairs can be passed to .header(). | ||
```js | ||
chess.header('White', 'Robert James Fischer') | ||
chess.header('Black', 'Mikhail Tal') | ||
// or | ||
chess.header('White', 'Morphy', 'Black', 'Anderssen', 'Date', '1858-??-??') | ||
``` | ||
Calling .header() without any arguments returns the header information as an object. | ||
```js | ||
chess.header() | ||
// -> { White: 'Morphy', Black: 'Anderssen', Date: '1858-??-??' } | ||
``` | ||
### .history([ options ]) | ||
Returns a list containing the moves of the current game. Options is an optional | ||
parameter which may contain a 'verbose' flag. See .moves() for a description of the | ||
Returns a list containing the moves of the current game. Options is an optional | ||
parameter which may contain a 'verbose' flag. See .moves() for a description of the | ||
verbose move fields. | ||
```js | ||
var chess = new Chess(); | ||
chess.move('e4'); | ||
chess.move('e5'); | ||
chess.move('f4'); | ||
chess.move('exf4'); | ||
const chess = new Chess() | ||
chess.move('e4') | ||
chess.move('e5') | ||
chess.move('f4') | ||
chess.move('exf4') | ||
chess.history(); | ||
chess.history() | ||
// -> ['e4', 'e5', 'f4', 'exf4'] | ||
chess.history({ verbose: true }); | ||
chess.history({ verbose: true }) | ||
// -> [{ color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' }, | ||
@@ -217,7 +311,10 @@ // { color: 'b', from: 'e7', to: 'e5', flags: 'b', piece: 'p', san: 'e5' }, | ||
### .in_check() | ||
Returns true or false if the side to move is in check. | ||
```js | ||
var chess = new Chess('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3'); | ||
chess.in_check(); | ||
const chess = new Chess( | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3' | ||
) | ||
chess.in_check() | ||
// -> true | ||
@@ -227,7 +324,10 @@ ``` | ||
### .in_checkmate() | ||
Returns true or false if the side to move has been checkmated. | ||
```js | ||
var chess = new Chess('rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3'); | ||
chess.in_checkmate(); | ||
const chess = new Chess( | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3' | ||
) | ||
chess.in_checkmate() | ||
// -> true | ||
@@ -237,7 +337,8 @@ ``` | ||
### .in_draw() | ||
Returns true or false if the game is drawn (50-move rule or insufficient material). | ||
```js | ||
var chess = new Chess('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78'); | ||
chess.in_draw(); | ||
const chess = new Chess('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78') | ||
chess.in_draw() | ||
// -> true | ||
@@ -247,7 +348,8 @@ ``` | ||
### .in_stalemate() | ||
Returns true or false if the side to move has been stalemated. | ||
```js | ||
var chess = new Chess('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78'); | ||
chess.in_stalemate(); | ||
const chess = new Chess('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78') | ||
chess.in_stalemate() | ||
// -> true | ||
@@ -257,2 +359,3 @@ ``` | ||
### .in_threefold_repetition() | ||
Returns true or false if the current board position has occurred three or more | ||
@@ -262,45 +365,26 @@ times. | ||
```js | ||
var chess = new Chess('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'); | ||
const chess = new Chess('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') | ||
// -> true | ||
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq occurs 1st time | ||
chess.in_threefold_repetition(); | ||
chess.in_threefold_repetition() | ||
// -> false | ||
chess.move('Nf3'); chess.move('Nf6'); chess.move('Ng1'); chess.move('Ng8'); | ||
chess.move('Nf3') chess.move('Nf6') chess.move('Ng1') chess.move('Ng8') | ||
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq occurs 2nd time | ||
chess.in_threefold_repetition(); | ||
chess.in_threefold_repetition() | ||
// -> false | ||
chess.move('Nf3'); chess.move('Nf6'); chess.move('Ng1'); chess.move('Ng8'); | ||
chess.move('Nf3') chess.move('Nf6') chess.move('Ng1') chess.move('Ng8') | ||
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq occurs 3rd time | ||
chess.in_threefold_repetition(); | ||
chess.in_threefold_repetition() | ||
// -> true | ||
``` | ||
### .header() | ||
Allows header information to be added to PGN output. Any number of key/value | ||
pairs can be passed to .header(). | ||
### .insufficient_material() | ||
```js | ||
chess.header('White', 'Robert James Fischer'); | ||
chess.header('Black', 'Mikhail Tal'); | ||
// or | ||
chess.header('White', 'Morphy', 'Black', 'Anderssen', 'Date', '1858-??-??'); | ||
``` | ||
Calling .header() without any arguments returns the header information as an object. | ||
```js | ||
chess.header(); | ||
// -> { White: 'Morphy', Black: 'Anderssen', Date: '1858-??-??' } | ||
``` | ||
### .insufficient_material() | ||
Returns true if the game is drawn due to insufficient material (K vs. K, | ||
K vs. KB, or K vs. KN); otherwise false. | ||
K vs. KB, or K vs. KN) otherwise false. | ||
```js | ||
var chess = new Chess('k7/8/n7/8/8/8/8/7K b - - 0 1'); | ||
const chess = new Chess('k7/8/n7/8/8/8/8/7K b - - 0 1') | ||
chess.insufficient_material() | ||
@@ -311,11 +395,12 @@ // -> true | ||
### .load(fen) | ||
The board is cleared, and the FEN string is loaded. Returns true if the position was | ||
The board is cleared, and the FEN string is loaded. Returns true if the position was | ||
successfully loaded, otherwise false. | ||
```js | ||
var chess = new Chess(); | ||
chess.load('4r3/8/2p2PPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45'); | ||
const chess = new Chess() | ||
chess.load('4r3/8/2p2PPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
// -> true | ||
chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45'); | ||
chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
// -> false, bad piece X | ||
@@ -325,2 +410,3 @@ ``` | ||
### .load_pgn(pgn, [ options ]) | ||
Load the moves of a game stored in | ||
@@ -334,3 +420,3 @@ [Portable Game Notation](http://en.wikipedia.org/wiki/Portable_Game_Notation). | ||
should not be pre-escaped, but any literal special characters should be escaped | ||
as is normal for a RegExp. Keep in mind that backslashes in JavaScript strings | ||
as is normal for a RegExp. Keep in mind that backslashes in JavaScript strings | ||
must themselves be escaped (see `sloppy_pgn` example below). Avoid using | ||
@@ -347,29 +433,31 @@ a `newline_char` that may occur elsewhere in a PGN, such as `.` or `x`, as this | ||
```js | ||
var chess = new Chess(); | ||
pgn = ['[Event "Casual Game"]', | ||
'[Site "Berlin GER"]', | ||
'[Date "1852.??.??"]', | ||
'[EventDate "?"]', | ||
'[Round "?"]', | ||
'[Result "1-0"]', | ||
'[White "Adolf Anderssen"]', | ||
'[Black "Jean Dufresne"]', | ||
'[ECO "C52"]', | ||
'[WhiteElo "?"]', | ||
'[BlackElo "?"]', | ||
'[PlyCount "47"]', | ||
'', | ||
'1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 4.b4 Bxb4 5.c3 Ba5 6.d4 exd4 7.O-O', | ||
'd3 8.Qb3 Qf6 9.e5 Qg6 10.Re1 Nge7 11.Ba3 b5 12.Qxb5 Rb8 13.Qa4', | ||
'Bb6 14.Nbd2 Bb7 15.Ne4 Qf5 16.Bxd3 Qh5 17.Nf6+ gxf6 18.exf6', | ||
'Rg8 19.Rad1 Qxf3 20.Rxe7+ Nxe7 21.Qxd7+ Kxd7 22.Bf5+ Ke8', | ||
'23.Bd7+ Kf8 24.Bxe7# 1-0']; | ||
const chess = new Chess() | ||
const pgn = [ | ||
'[Event "Casual Game"]', | ||
'[Site "Berlin GER"]', | ||
'[Date "1852.??.??"]', | ||
'[EventDate "?"]', | ||
'[Round "?"]', | ||
'[Result "1-0"]', | ||
'[White "Adolf Anderssen"]', | ||
'[Black "Jean Dufresne"]', | ||
'[ECO "C52"]', | ||
'[WhiteElo "?"]', | ||
'[BlackElo "?"]', | ||
'[PlyCount "47"]', | ||
'', | ||
'1.e4 e5 2.Nf3 Nc6 3.Bc4 Bc5 4.b4 Bxb4 5.c3 Ba5 6.d4 exd4 7.O-O', | ||
'd3 8.Qb3 Qf6 9.e5 Qg6 10.Re1 Nge7 11.Ba3 b5 12.Qxb5 Rb8 13.Qa4', | ||
'Bb6 14.Nbd2 Bb7 15.Ne4 Qf5 16.Bxd3 Qh5 17.Nf6+ gxf6 18.exf6', | ||
'Rg8 19.Rad1 Qxf3 20.Rxe7+ Nxe7 21.Qxd7+ Kxd7 22.Bf5+ Ke8', | ||
'23.Bd7+ Kf8 24.Bxe7# 1-0' | ||
] | ||
chess.load_pgn(pgn.join('\n')); | ||
chess.load_pgn(pgn.join('\n')) | ||
// -> true | ||
chess.fen(); | ||
chess.fen() | ||
// -> 1r3kr1/pbpBBp1p/1b3P2/8/8/2P2q2/P4PPP/3R2K1 b - - 0 24 | ||
chess.ascii(); | ||
chess.ascii() | ||
// -> ' +------------------------+ | ||
@@ -387,33 +475,33 @@ // 8 | . r . . . k r . | | ||
// Parse non-standard move formats and unusual line separators | ||
var sloppy_pgn = ['[Event "Wijk aan Zee (Netherlands)"]', | ||
'[Date "1971.01.26"]', | ||
'[Result "1-0"]', | ||
'[White "Tigran Vartanovich Petrosian"]', | ||
'[Black "Hans Ree"]', | ||
'[ECO "A29"]', | ||
'', | ||
'1. Pc2c4 Pe7e5', // non-standard | ||
'2. Nc3 Nf6', | ||
'3. Nf3 Nc6', | ||
'4. g2g3 Bb4', // non-standard | ||
'5. Nd5 Nxd5', | ||
'6. c4xd5 e5-e4', // non-standard | ||
'7. dxc6 exf3', | ||
'8. Qb3 1-0' | ||
].join('|'); | ||
const sloppy_pgn = [ | ||
'[Event "Wijk aan Zee (Netherlands)"]', | ||
'[Date "1971.01.26"]', | ||
'[Result "1-0"]', | ||
'[White "Tigran Vartanovich Petrosian"]', | ||
'[Black "Hans Ree"]', | ||
'[ECO "A29"]', | ||
'', | ||
'1. Pc2c4 Pe7e5', // non-standard | ||
'2. Nc3 Nf6', | ||
'3. Nf3 Nc6', | ||
'4. g2g3 Bb4', // non-standard | ||
'5. Nd5 Nxd5', | ||
'6. c4xd5 e5-e4', // non-standard | ||
'7. dxc6 exf3', | ||
'8. Qb3 1-0' | ||
].join('|') | ||
var options = { | ||
newline_char: '\\|', // Literal '|' character escaped | ||
sloppy: true | ||
}; | ||
const options = { | ||
newline_char: '\\|', // Literal '|' character escaped | ||
sloppy: true | ||
} | ||
chess.load_pgn(sloppy_pgn); | ||
chess.load_pgn(sloppy_pgn) | ||
// -> false | ||
chess.load_pgn(sloppy_pgn, options); | ||
chess.load_pgn(sloppy_pgn, options) | ||
// -> true | ||
chess.fen(); | ||
chess.fen() | ||
// -> 'r1bqk2r/pppp1ppp/2P5/8/1b6/1Q3pP1/PP1PPP1P/R1B1KB1R b KQkq - 1 8' | ||
@@ -423,8 +511,9 @@ ``` | ||
### .move(move, [ options ]) | ||
Attempts to make a move on the board, returning a move object if the move was | ||
legal, otherwise null. The .move function can be called two ways, by passing | ||
legal, otherwise null. The .move function can be called two ways, by passing | ||
a string in Standard Algebraic Notation (SAN): | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
@@ -445,5 +534,5 @@ chess.move('e4') | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
chess.move({ from: 'g2', to: 'g3' }); | ||
chess.move({ from: 'g2', to: 'g3' }) | ||
// -> { color: 'w', from: 'g2', to: 'g3', flags: 'n', piece: 'p', san: 'g3' } | ||
@@ -456,41 +545,43 @@ ``` | ||
```js | ||
const chess = new Chess() | ||
var chess = new Chess(); | ||
// various forms of Long Algebraic Notation | ||
chess.move('e2e4', {sloppy: true}); | ||
chess.move('e2e4', { sloppy: true }) | ||
// -> { color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' } | ||
chess.move('e7-e5', {sloppy: true}); | ||
chess.move('e7-e5', { sloppy: true }) | ||
// -> { color: 'b', from: 'e7', to: 'e5', flags: 'b', piece: 'p', san: 'e5' } | ||
chess.move('Pf2f4', {sloppy: true}); | ||
chess.move('Pf2f4', { sloppy: true }) | ||
// -> { color: 'w', from: 'f2', to: 'f4', flags: 'b', piece: 'p', san: 'f4' } | ||
chess.move('Pe5xf4', {sloppy: true}); | ||
chess.move('Pe5xf4', { sloppy: true }) | ||
// -> { color: 'b', from: 'e5', to: 'f4', flags: 'c', piece: 'p', captured: 'p', san: 'exf4' } | ||
// correctly parses incorrectly disambiguated moves | ||
chess = new Chess('r2qkbnr/ppp2ppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R b KQkq - 3 7'); | ||
chess = new Chess( | ||
'r2qkbnr/ppp2ppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R b KQkq - 3 7' | ||
) | ||
chess.move('Nge7'); // Ne7 is unambiguous because the knight on c6 is pinned | ||
chess.move('Nge7') // Ne7 is unambiguous because the knight on c6 is pinned | ||
// -> null | ||
chess.move('Nge7', {sloppy: true}); | ||
chess.move('Nge7', { sloppy: true }) | ||
// -> { color: 'b', from: 'g8', to: 'e7', flags: 'n', piece: 'n', san: 'Ne7' } | ||
``` | ||
### .moves([ options ]) | ||
Returns a list of legal moves from the current position. The function takes an optional parameter which controls the single-square move generation and verbosity. | ||
Returns a list of legal moves from the current position. The function takes an optional parameter which controls the single-square move generation and verbosity. | ||
```js | ||
var chess = new Chess(); | ||
chess.moves(); | ||
const chess = new Chess() | ||
chess.moves() | ||
// -> ['a3', 'a4', 'b3', 'b4', 'c3', 'c4', 'd3', 'd4', 'e3', 'e4', | ||
// 'f3', 'f4', 'g3', 'g4', 'h3', 'h4', 'Na3', 'Nc3', 'Nf3', 'Nh3'] | ||
chess.moves({square: 'e2'}); | ||
chess.moves({ square: 'e2' }) | ||
// -> ['e3', 'e4'] | ||
chess.moves({square: 'e9'}); // invalid square | ||
chess.moves({ square: 'e9' }) // invalid square | ||
// -> [] | ||
chess.moves({ verbose: true }); | ||
chess.moves({ verbose: true }) | ||
// -> [{ color: 'w', from: 'a2', to: 'a3', | ||
@@ -510,9 +601,9 @@ // flags: 'n', piece: 'p', san 'a3' | ||
- 'n' - a non-capture | ||
- 'b' - a pawn push of two squares | ||
- 'e' - an en passant capture | ||
- 'c' - a standard capture | ||
- 'p' - a promotion | ||
- 'k' - kingside castling | ||
- 'q' - queenside castling | ||
- 'n' - a non-capture | ||
- 'b' - a pawn push of two squares | ||
- 'e' - an en passant capture | ||
- 'c' - a standard capture | ||
- 'p' - a promotion | ||
- 'k' - kingside castling | ||
- 'q' - queenside castling | ||
@@ -522,2 +613,3 @@ A flag of 'pc' would mean that a pawn captured a piece on the 8th rank and promoted. | ||
### .pgn([ options ]) | ||
Returns the game in PGN format. Options is an optional parameter which may include | ||
@@ -527,10 +619,10 @@ max width and/or a newline character settings. | ||
```js | ||
var chess = new Chess(); | ||
chess.header('White', 'Plunky', 'Black', 'Plinkie'); | ||
chess.move('e4'); | ||
chess.move('e5'); | ||
chess.move('Nc3'); | ||
chess.move('Nc6'); | ||
const chess = new Chess() | ||
chess.header('White', 'Plunky', 'Black', 'Plinkie') | ||
chess.move('e4') | ||
chess.move('e5') | ||
chess.move('Nc3') | ||
chess.move('Nc6') | ||
chess.pgn({ max_width: 5, newline_char: '<br />' }); | ||
chess.pgn({ max_width: 5, newline_char: '<br />' }) | ||
// -> '[White "Plunky"]<br />[Black "Plinkie"]<br /><br />1. e4 e5<br />2. Nc3 Nc6' | ||
@@ -540,5 +632,6 @@ ``` | ||
### .put(piece, square) | ||
Place a piece on the square where piece is an object with the form | ||
{ type: ..., color: ... }. Returns true if the piece was successfully placed, | ||
otherwise, the board remains unchanged and false is returned. `put()` will fail | ||
{ type: ..., color: ... }. Returns true if the piece was successfully placed, | ||
otherwise, the board remains unchanged and false is returned. `put()` will fail | ||
when passed an invalid piece or square, or when two or more kings of the | ||
@@ -548,3 +641,3 @@ same color are placed. | ||
```js | ||
chess.clear(); | ||
chess.clear() | ||
@@ -556,3 +649,3 @@ chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.fen(); | ||
chess.fen() | ||
// -> '8/8/8/p7/8/8/8/7K w - - 0 0' | ||
@@ -563,3 +656,3 @@ | ||
chess.clear(); | ||
chess.clear() | ||
@@ -571,18 +664,18 @@ chess.put({ type: 'k', color: 'w' }, 'a1') | ||
// -> false | ||
``` | ||
### .remove(square) | ||
Remove and return the piece on _square_. | ||
```js | ||
chess.clear(); | ||
chess.clear() | ||
chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.put({ type: chess.KING, color: chess.WHITE }, 'h1') // put a white king on h1 | ||
chess.remove('a5'); | ||
chess.remove('a5') | ||
// -> { type: 'p', color: 'b' }, | ||
chess.remove('h1'); | ||
chess.remove('h1') | ||
// -> { type: 'k', color: 'w' }, | ||
chess.remove('e1'); | ||
chess.remove('e1') | ||
// -> null | ||
@@ -592,9 +685,25 @@ ``` | ||
### .reset() | ||
Reset the board to the initial starting position. | ||
### .set_comment(comment) | ||
Comment on the current position. | ||
```js | ||
const chess = new Chess() | ||
chess.move("e4") | ||
chess.set_comment("king's pawn opening") | ||
chess.pgn() | ||
// -> "1. e4 {king's pawn opening}" | ||
``` | ||
### .square_color(square) | ||
Returns the color of the square ('light' or 'dark'). | ||
```js | ||
var chess = Chess(); | ||
const chess = Chess() | ||
chess.square_color('h1') | ||
@@ -609,2 +718,3 @@ // -> 'light' | ||
### .turn() | ||
Returns the current side to move. | ||
@@ -619,18 +729,19 @@ | ||
### .undo() | ||
Takeback the last half-move, returning a move object if successful, otherwise null. | ||
```js | ||
var chess = new Chess(); | ||
const chess = new Chess() | ||
chess.fen(); | ||
chess.fen() | ||
// -> 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' | ||
chess.move('e4'); | ||
chess.fen(); | ||
chess.move('e4') | ||
chess.fen() | ||
// -> 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1' | ||
chess.undo(); | ||
chess.undo() | ||
// -> { color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' } | ||
chess.fen(); | ||
chess.fen() | ||
// -> 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' | ||
chess.undo(); | ||
chess.undo() | ||
// -> null | ||
@@ -640,2 +751,3 @@ ``` | ||
### .validate_fen(fen): | ||
Returns a validation object specifying validity or the errors found within the | ||
@@ -645,6 +757,6 @@ FEN string. | ||
```js | ||
chess.validate_fen('2n1r3/p1k2pp1/B1p3b1/P7/5bP1/2N1B3/1P2KP2/2R5 b - - 4 25'); | ||
chess.validate_fen('2n1r3/p1k2pp1/B1p3b1/P7/5bP1/2N1B3/1P2KP2/2R5 b - - 4 25') | ||
// -> { valid: true, error_number: 0, error: 'No errors.' } | ||
chess.validate_fen('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45'); | ||
chess.validate_fen('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
// -> { valid: false, error_number: 9, | ||
@@ -658,14 +770,7 @@ // error: '1st field (piece positions) is invalid [invalid piece].' } | ||
- [The Grateful Dead](https://www.youtube.com/watch?feature=player_detailpage&v=ANF6qanEB7s#t=2999) | ||
- [Umphrey's McGee](http://www.youtube.com/watch?v=jh-1fFWkSdw) | ||
- [The Grateful Dead](https://www.youtube.com/watch?v=z-D9rdJWfWs) | ||
- [Umphrey's McGee](https://www.youtube.com/watch?v=auEfZVcYp64) | ||
## BUGS | ||
- The en passant square and castling flags aren't adjusted when using the put/remove functions (workaround: use .load() instead) | ||
## TODO | ||
- Investigate the use of piece lists (this may shave a few cycles off | ||
generate_moves() and attacked()). | ||
- Refactor API to use camelCase - yuck. | ||
- Add more robust FEN validation. | ||
- The en passant square and castling flags aren't adjusted when using the put/remove functions (workaround: use .load() instead) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
146578
3149
743
0