Socket
Socket
Sign inDemoInstall

chess.js

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chess.js - npm Package Compare versions

Comparing version 0.10.1 to 0.10.2

103

chess.js

@@ -685,2 +685,7 @@ /*

// parses all of the decorators out of a SAN string
function stripped_san(move) {
return move.replace(/=/,'').replace(/[+#]?[?!]*$/,'');
}
function attacked(color, square) {

@@ -1057,2 +1062,41 @@ for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {

// convert a move from Standard Algebraic Notation (SAN) to 0x88 coordinates
function move_from_san(move, sloppy) {
// strip off any move decorations: e.g Nf3+?!
var clean_move = stripped_san(move);
// if we're using the sloppy parser run a regex to grab piece, to, and from
// this should parse invalid SAN like: Pe2-e4, Rc1c4, Qf3xf7
if (sloppy) {
var matches = clean_move.match(/([pnbrqkPNBRQK])?([a-h][1-8])x?-?([a-h][1-8])([qrbnQRBN])?/);
if (matches) {
var piece = matches[1];
var from = matches[2];
var to = matches[3];
var promotion = matches[4];
}
}
var moves = generate_moves();
for (var i = 0, len = moves.length; i < len; i++) {
// try the strict parser first, then the sloppy parser if requested
// by the user
if ((clean_move === stripped_san(move_to_san(moves[i]))) ||
(sloppy && clean_move === stripped_san(move_to_san(moves[i], true)))) {
return moves[i];
} else {
if (matches &&
(!piece || piece.toLowerCase() == moves[i].piece) &&
SQUARES[from] == moves[i].from &&
SQUARES[to] == moves[i].to &&
(!promotion || promotion.toLowerCase() == moves[i].promotion)) {
return moves[i];
}
}
}
return null;
}
/*****************************************************************************

@@ -1356,29 +1400,2 @@ * UTILITY FUNCTIONS

/* convert a move from Standard Algebraic Notation (SAN) to 0x88
* coordinates
*/
function move_from_san(move) {
/* strip off any move decorations: e.g Nf3+?! */
var move_replaced = move.replace(/=/,'').replace(/[+#]?[?!]*$/,'');
var moves = generate_moves();
for (var i = 0, len = moves.length; i < len; i++) {
// try the strict parser first, then the sloppy parser if requested
// by the user
if ((move_replaced ===
move_to_san(moves[i]).replace(/=/,'').replace(/[+#]?[?!]*$/,'')) ||
(sloppy &&
move_replaced ===
move_to_san(moves[i], sloppy).replace(/=/,'').replace(/[+#]?[?!]*$/,''))) {
return moves[i];
}
}
return null;
}
function get_move_obj(move) {
return move_from_san(trim(move));
}
function has_keys(object) {

@@ -1414,5 +1431,5 @@ for (var key in object) {

options.newline_char : '\r?\n';
var regex = new RegExp('^(\\[(.|' + mask(newline_char) + ')*\\])' +
'(' + mask(newline_char) + ')*' +
'1.(' + mask(newline_char) + '|.)*$', 'g');
var regex = new RegExp('^(\\[(.|' + mask(newline_char) + ')*\\])' +
'(' + mask(newline_char) + ')*' +
'1.(' + mask(newline_char) + '|.)*$', 'g');

@@ -1427,3 +1444,3 @@ /* get header part of the PGN file */

reset();
reset();

@@ -1473,3 +1490,3 @@ /* parse PGN header */

for (var half_move = 0; half_move < moves.length - 1; half_move++) {
move = get_move_obj(moves[half_move]);
move = move_from_san(moves[half_move], sloppy);

@@ -1494,3 +1511,3 @@ /* move not possible! (don't clear the board to examine to show the

else {
move = get_move_obj(move);
move = move_from_san(move, sloppy);
if (move == null) {

@@ -1534,22 +1551,8 @@ return false;

var move_obj = null;
var moves = generate_moves();
if (typeof move === 'string') {
/* convert the move string to a move object */
/* strip off any move decorations: e.g Nf3+?! */
var move_replaced = move.replace(/=/,'').replace(/[+#]?[?!]*$/,'');
for (var i = 0, len = moves.length; i < len; i++) {
move_obj = move_from_san(move, sloppy);
} else if (typeof move === 'object') {
var moves = generate_moves();
// try the strict parser first, then the sloppy parser if requested
// by the user
if ((move_replaced ===
move_to_san(moves[i]).replace(/=/,'').replace(/[+#]?[?!]*$/,'')) ||
(sloppy &&
move_replaced ===
move_to_san(moves[i], sloppy).replace(/=/,'').replace(/[+#]?[?!]*$/,''))) {
move_obj = moves[i];
break;
}
}
} else if (typeof move === 'object') {
/* convert the pretty move object to an ugly move object */

@@ -1556,0 +1559,0 @@ for (var i = 0, len = moves.length; i < len; i++) {

@@ -6,2 +6,2 @@ /* @license

*/
var Chess=function(r){function e(){fr=new Array(128),ar={w:Q,b:Q},lr=K,ur={w:0,b:0},pr=Q,sr=0,cr=1,vr=[],gr={},a(i())}function n(){t(F)}function t(r){var n=r.split(/\s+/),t=n[0],f=0;if(!o(r).valid)return!1;e();for(var l=0;l<t.length;l++){var p=t.charAt(l);if("/"===p)f+=8;else if(R(p))f+=parseInt(p,10);else{var s="a">p?K:D;u({type:p.toLowerCase(),color:s},w(f)),f++}}return lr=n[1],n[2].indexOf("K")>-1&&(ur.w|=Y.KSIDE_CASTLE),n[2].indexOf("Q")>-1&&(ur.w|=Y.QSIDE_CASTLE),n[2].indexOf("k")>-1&&(ur.b|=Y.KSIDE_CASTLE),n[2].indexOf("q")>-1&&(ur.b|=Y.QSIDE_CASTLE),pr="-"===n[3]?Q:or[n[3]],sr=parseInt(n[4],10),cr=parseInt(n[5],10),a(i()),!0}function o(r){var e={0:"No errors.",1:"FEN string must contain six space-delimited fields.",2:"6th field (move number) must be a positive integer.",3:"5th field (half move counter) must be a non-negative integer.",4:"4th field (en-passant square) is invalid.",5:"3rd field (castling availability) is invalid.",6:"2nd field (side to move) is invalid.",7:"1st field (piece positions) does not contain 8 '/'-delimited rows.",8:"1st field (piece positions) is invalid [consecutive numbers].",9:"1st field (piece positions) is invalid [invalid piece].",10:"1st field (piece positions) is invalid [row too large].",11:"Illegal en-passant square"},n=r.split(/\s+/);if(6!==n.length)return{valid:!1,error_number:1,error:e[1]};if(isNaN(n[5])||parseInt(n[5],10)<=0)return{valid:!1,error_number:2,error:e[2]};if(isNaN(n[4])||parseInt(n[4],10)<0)return{valid:!1,error_number:3,error:e[3]};if(!/^(-|[abcdefgh][36])$/.test(n[3]))return{valid:!1,error_number:4,error:e[4]};if(!/^(KQ?k?q?|Qk?q?|kq?|q|-)$/.test(n[2]))return{valid:!1,error_number:5,error:e[5]};if(!/^(w|b)$/.test(n[1]))return{valid:!1,error_number:6,error:e[6]};var t=n[0].split("/");if(8!==t.length)return{valid:!1,error_number:7,error:e[7]};for(var o=0;o<t.length;o++){for(var i=0,f=!1,a=0;a<t[o].length;a++)if(isNaN(t[o][a])){if(!/^[prnbqkPRNBQK]$/.test(t[o][a]))return{valid:!1,error_number:9,error:e[9]};i+=1,f=!1}else{if(f)return{valid:!1,error_number:8,error:e[8]};i+=parseInt(t[o][a],10),f=!0}if(8!==i)return{valid:!1,error_number:10,error:e[10]}}return"3"==n[3][1]&&"w"==n[1]||"6"==n[3][1]&&"b"==n[1]?{valid:!1,error_number:11,error:e[11]}:{valid:!0,error_number:0,error:e[0]}}function i(){for(var r=0,e="",n=or.a8;n<=or.h1;n++){if(null==fr[n])r++;else{r>0&&(e+=r,r=0);var t=fr[n].color,o=fr[n].type;e+=t===K?o.toUpperCase():o.toLowerCase()}n+1&136&&(r>0&&(e+=r),n!==or.h1&&(e+="/"),r=0,n+=8)}var i="";ur[K]&Y.KSIDE_CASTLE&&(i+="K"),ur[K]&Y.QSIDE_CASTLE&&(i+="Q"),ur[D]&Y.KSIDE_CASTLE&&(i+="k"),ur[D]&Y.QSIDE_CASTLE&&(i+="q"),i=i||"-";var f=pr===Q?"-":w(pr);return[e,lr,i,f,sr,cr].join(" ")}function f(r){for(var e=0;e<r.length;e+=2)"string"==typeof r[e]&&"string"==typeof r[e+1]&&(gr[r[e]]=r[e+1]);return gr}function a(r){vr.length>0||(r!==F?(gr.SetUp="1",gr.FEN=r):(delete gr.SetUp,delete gr.FEN))}function l(r){var e=fr[or[r]];return e?{type:e.type,color:e.color}:null}function u(r,e){if(!("type"in r&&"color"in r))return!1;if(-1===G.indexOf(r.type.toLowerCase()))return!1;if(!(e in or))return!1;var n=or[e];return r.type==M&&ar[r.color]!=Q&&ar[r.color]!=n?!1:(fr[n]={type:r.type,color:r.color},r.type===M&&(ar[r.color]=n),a(i()),!0)}function p(r){var e=l(r);return fr[or[r]]=null,e&&e.type===M&&(ar[e.color]=Q),a(i()),e}function s(r,e,n,t,o){var i={color:lr,from:e,to:n,flags:t,piece:r[e].type};return o&&(i.flags|=Y.PROMOTION,i.promotion=o),r[n]?i.captured=r[n].type:t&Y.EP_CAPTURE&&(i.captured=U),i}function c(r){function e(r,e,n,t,o){if(r[n].type!==U||I(t)!==tr&&I(t)!==rr)e.push(s(r,n,t,o));else for(var i=[B,j,x,$],f=0,a=i.length;a>f;f++)e.push(s(r,n,t,o,i[f]))}var n=[],t=lr,o=L(t),i={b:nr,w:er},f=or.a8,a=or.h1,l=!1,u="undefined"!=typeof r&&"legal"in r?r.legal:!0;if("undefined"!=typeof r&&"square"in r){if(!(r.square in or))return[];f=a=or[r.square],l=!0}for(var p=f;a>=p;p++)if(136&p)p+=7;else{var c=fr[p];if(null!=c&&c.color===t)if(c.type===U){var v=p+H[t][0];if(null==fr[v]){e(fr,n,p,v,Y.NORMAL);var v=p+H[t][1];i[t]===I(p)&&null==fr[v]&&e(fr,n,p,v,Y.BIG_PAWN)}for(d=2;4>d;d++){var v=p+H[t][d];136&v||(null!=fr[v]&&fr[v].color===o?e(fr,n,p,v,Y.CAPTURE):v===pr&&e(fr,n,p,pr,Y.EP_CAPTURE))}}else for(var d=0,E=Z[c.type].length;E>d;d++)for(var b=Z[c.type][d],v=p;;){if(v+=b,136&v)break;if(null!=fr[v]){if(fr[v].color===t)break;e(fr,n,p,v,Y.CAPTURE);break}if(e(fr,n,p,v,Y.NORMAL),"n"===c.type||"k"===c.type)break}}if(!l||a===ar[t]){if(ur[t]&Y.KSIDE_CASTLE){var _=ar[t],y=_+2;null!=fr[_+1]||null!=fr[y]||g(o,ar[t])||g(o,_+1)||g(o,y)||e(fr,n,ar[t],y,Y.KSIDE_CASTLE)}if(ur[t]&Y.QSIDE_CASTLE){var _=ar[t],y=_-2;null!=fr[_-1]||null!=fr[_-2]||null!=fr[_-3]||g(o,ar[t])||g(o,_-1)||g(o,y)||e(fr,n,ar[t],y,Y.QSIDE_CASTLE)}}if(!u)return n;for(var A=[],p=0,E=n.length;E>p;p++)S(n[p]),h(t)||A.push(n[p]),m();return A}function v(r,e){var n="";if(r.flags&Y.KSIDE_CASTLE)n="O-O";else if(r.flags&Y.QSIDE_CASTLE)n="O-O-O";else{var t=C(r,e);r.piece!==U&&(n+=r.piece.toUpperCase()+t),r.flags&(Y.CAPTURE|Y.EP_CAPTURE)&&(r.piece===U&&(n+=w(r.from)[0]),n+="x"),n+=w(r.to),r.flags&Y.PROMOTION&&(n+="="+r.promotion.toUpperCase())}return S(r),d()&&(n+=E()?"#":"+"),m(),n}function g(r,e){for(var n=or.a8;n<=or.h1;n++)if(136&n)n+=7;else if(null!=fr[n]&&fr[n].color===r){var t=fr[n],o=n-e,i=o+119;if(z[i]&1<<V[t.type]){if(t.type===U){if(o>0){if(t.color===K)return!0}else if(t.color===D)return!0;continue}if("n"===t.type||"k"===t.type)return!0;for(var f=J[i],a=n+f,l=!1;a!==e;){if(null!=fr[a]){l=!0;break}a+=f}if(!l)return!0}}return!1}function h(r){return g(L(r),ar[r])}function d(){return h(lr)}function E(){return d()&&0===c().length}function b(){return!d()&&0===c().length}function _(){for(var r={},e=[],n=0,t=0,o=or.a8;o<=or.h1;o++)if(t=(t+1)%2,136&o)o+=7;else{var i=fr[o];i&&(r[i.type]=i.type in r?r[i.type]+1:1,i.type===x&&e.push(t),n++)}if(2===n)return!0;if(3===n&&(1===r[x]||1===r[$]))return!0;if(n===r[x]+2){for(var f=0,a=e.length,o=0;a>o;o++)f+=e[o];if(0===f||f===a)return!0}return!1}function y(){for(var r=[],e={},n=!1;;){var t=m();if(!t)break;r.push(t)}for(;;){var o=i().split(" ").slice(0,4).join(" ");if(e[o]=o in e?e[o]+1:1,e[o]>=3&&(n=!0),!r.length)break;S(r.pop())}return n}function A(r){vr.push({move:r,kings:{b:ar.b,w:ar.w},turn:lr,castling:{b:ur.b,w:ur.w},ep_square:pr,half_moves:sr,move_number:cr})}function S(r){var e=lr,n=L(e);if(A(r),fr[r.to]=fr[r.from],fr[r.from]=null,r.flags&Y.EP_CAPTURE&&(lr===D?fr[r.to-16]=null:fr[r.to+16]=null),r.flags&Y.PROMOTION&&(fr[r.to]={type:r.promotion,color:e}),fr[r.to].type===M){if(ar[fr[r.to].color]=r.to,r.flags&Y.KSIDE_CASTLE){var t=r.to-1,o=r.to+1;fr[t]=fr[o],fr[o]=null}else if(r.flags&Y.QSIDE_CASTLE){var t=r.to+1,o=r.to-2;fr[t]=fr[o],fr[o]=null}ur[e]=""}if(ur[e])for(var i=0,f=ir[e].length;f>i;i++)if(r.from===ir[e][i].square&&ur[e]&ir[e][i].flag){ur[e]^=ir[e][i].flag;break}if(ur[n])for(var i=0,f=ir[n].length;f>i;i++)if(r.to===ir[n][i].square&&ur[n]&ir[n][i].flag){ur[n]^=ir[n][i].flag;break}pr=r.flags&Y.BIG_PAWN?"b"===lr?r.to-16:r.to+16:Q,r.piece===U?sr=0:r.flags&(Y.CAPTURE|Y.EP_CAPTURE)?sr=0:sr++,lr===D&&cr++,lr=L(lr)}function m(){var r=vr.pop();if(null==r)return null;var e=r.move;ar=r.kings,lr=r.turn,ur=r.castling,pr=r.ep_square,sr=r.half_moves,cr=r.move_number;var n=lr,t=L(lr);if(fr[e.from]=fr[e.to],fr[e.from].type=e.piece,fr[e.to]=null,e.flags&Y.CAPTURE)fr[e.to]={type:e.captured,color:t};else if(e.flags&Y.EP_CAPTURE){var o;o=n===D?e.to-16:e.to+16,fr[o]={type:U,color:t}}if(e.flags&(Y.KSIDE_CASTLE|Y.QSIDE_CASTLE)){var i,f;e.flags&Y.KSIDE_CASTLE?(i=e.to+1,f=e.to-1):e.flags&Y.QSIDE_CASTLE&&(i=e.to-2,f=e.to+1),fr[i]=fr[f],fr[f]=null}return e}function C(r,e){for(var n=c({legal:!e}),t=r.from,o=r.to,i=r.piece,f=0,a=0,l=0,u=0,p=n.length;p>u;u++){var s=n[u].from,v=n[u].to,g=n[u].piece;i===g&&t!==s&&o===v&&(f++,I(t)===I(s)&&a++,P(t)===P(s)&&l++)}return f>0?a>0&&l>0?w(t):w(t).charAt(l>0?1:0):""}function T(){for(var r=" +------------------------+\n",e=or.a8;e<=or.h1;e++){if(0===P(e)&&(r+=" "+"87654321"[I(e)]+" |"),null==fr[e])r+=" . ";else{var n=fr[e].type,t=fr[e].color,o=t===K?n.toUpperCase():n.toLowerCase();r+=" "+o+" "}e+1&136&&(r+="|\n",e+=8)}return r+=" +------------------------+\n",r+=" a b c d e f g h\n"}function I(r){return r>>4}function P(r){return 15&r}function w(r){var e=P(r),n=I(r);return"abcdefgh".substring(e,e+1)+"87654321".substring(n,n+1)}function L(r){return r===K?D:K}function R(r){return-1!=="0123456789".indexOf(r)}function O(r){var e=N(r);e.san=v(e,!1),e.to=w(e.to),e.from=w(e.from);var n="";for(var t in Y)Y[t]&e.flags&&(n+=X[t]);return e.flags=n,e}function N(r){var e=r instanceof Array?[]:{};for(var n in r)"object"==typeof n?e[n]=N(r[n]):e[n]=r[n];return e}function k(r){return r.replace(/^\s+|\s+$/g,"")}function q(r){for(var e=c({legal:!1}),n=0,t=lr,o=0,i=e.length;i>o;o++){if(S(e[o]),!h(t))if(r-1>0){var f=q(r-1);n+=f}else n++;m()}return n}var D="b",K="w",Q=-1,U="p",$="n",x="b",j="r",B="q",M="k",G="pnbrqkPNBRQK",F="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",W=["1-0","0-1","1/2-1/2","*"],H={b:[16,32,17,15],w:[-16,-32,-17,-15]},Z={n:[-18,-33,-31,-14,18,33,31,14],b:[-17,-15,17,15],r:[-16,1,16,-1],q:[-17,-16,-15,1,17,16,15,-1],k:[-17,-16,-15,1,17,16,15,-1]},z=[20,0,0,0,0,0,0,24,0,0,0,0,0,0,20,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,24,24,24,24,24,24,56,0,56,24,24,24,24,24,24,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,20,0,0,0,0,0,0,24,0,0,0,0,0,0,20],J=[17,0,0,0,0,0,0,16,0,0,0,0,0,0,15,0,0,17,0,0,0,0,0,16,0,0,0,0,0,15,0,0,0,0,17,0,0,0,0,16,0,0,0,0,15,0,0,0,0,0,0,17,0,0,0,16,0,0,0,15,0,0,0,0,0,0,0,0,17,0,0,16,0,0,15,0,0,0,0,0,0,0,0,0,0,17,0,16,0,15,0,0,0,0,0,0,0,0,0,0,0,0,17,16,15,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,-15,-16,-17,0,0,0,0,0,0,0,0,0,0,0,0,-15,0,-16,0,-17,0,0,0,0,0,0,0,0,0,0,-15,0,0,-16,0,0,-17,0,0,0,0,0,0,0,0,-15,0,0,0,-16,0,0,0,-17,0,0,0,0,0,0,-15,0,0,0,0,-16,0,0,0,0,-17,0,0,0,0,-15,0,0,0,0,0,-16,0,0,0,0,0,-17,0,0,-15,0,0,0,0,0,0,-16,0,0,0,0,0,0,-17],V={p:0,n:1,b:2,r:3,q:4,k:5},X={NORMAL:"n",CAPTURE:"c",BIG_PAWN:"b",EP_CAPTURE:"e",PROMOTION:"p",KSIDE_CASTLE:"k",QSIDE_CASTLE:"q"},Y={NORMAL:1,CAPTURE:2,BIG_PAWN:4,EP_CAPTURE:8,PROMOTION:16,KSIDE_CASTLE:32,QSIDE_CASTLE:64},rr=7,er=6,nr=1,tr=0,or={a8:0,b8:1,c8:2,d8:3,e8:4,f8:5,g8:6,h8:7,a7:16,b7:17,c7:18,d7:19,e7:20,f7:21,g7:22,h7:23,a6:32,b6:33,c6:34,d6:35,e6:36,f6:37,g6:38,h6:39,a5:48,b5:49,c5:50,d5:51,e5:52,f5:53,g5:54,h5:55,a4:64,b4:65,c4:66,d4:67,e4:68,f4:69,g4:70,h4:71,a3:80,b3:81,c3:82,d3:83,e3:84,f3:85,g3:86,h3:87,a2:96,b2:97,c2:98,d2:99,e2:100,f2:101,g2:102,h2:103,a1:112,b1:113,c1:114,d1:115,e1:116,f1:117,g1:118,h1:119},ir={w:[{square:or.a1,flag:Y.QSIDE_CASTLE},{square:or.h1,flag:Y.KSIDE_CASTLE}],b:[{square:or.a8,flag:Y.QSIDE_CASTLE},{square:or.h8,flag:Y.KSIDE_CASTLE}]},fr=new Array(128),ar={w:Q,b:Q},lr=K,ur={w:0,b:0},pr=Q,sr=0,cr=1,vr=[],gr={};return t("undefined"==typeof r?F:r),{WHITE:K,BLACK:D,PAWN:U,KNIGHT:$,BISHOP:x,ROOK:j,QUEEN:B,KING:M,SQUARES:function(){for(var r=[],e=or.a8;e<=or.h1;e++)136&e?e+=7:r.push(w(e));return r}(),FLAGS:X,load:function(r){return t(r)},reset:function(){return n()},moves:function(r){for(var e=c(r),n=[],t=0,o=e.length;o>t;t++)n.push("undefined"!=typeof r&&"verbose"in r&&r.verbose?O(e[t]):v(e[t],!1));return n},in_check:function(){return d()},in_checkmate:function(){return E()},in_stalemate:function(){return b()},in_draw:function(){return sr>=100||b()||_()||y()},insufficient_material:function(){return _()},in_threefold_repetition:function(){return y()},game_over:function(){return sr>=100||E()||b()||_()||y()},validate_fen:function(r){return o(r)},fen:function(){return i()},pgn:function(r){var e="object"==typeof r&&"string"==typeof r.newline_char?r.newline_char:"\n",n="object"==typeof r&&"number"==typeof r.max_width?r.max_width:0,t=[],o=!1;for(var i in gr)t.push("["+i+' "'+gr[i]+'"]'+e),o=!0;o&&vr.length&&t.push(e);for(var f=[];vr.length>0;)f.push(m());for(var a=[],l="";f.length>0;){var u=f.pop();vr.length||"b"!==u.color?"w"===u.color&&(l.length&&a.push(l),l=cr+"."):l=cr+". ...",l=l+" "+v(u,!1),S(u)}if(l.length&&a.push(l),"undefined"!=typeof gr.Result&&a.push(gr.Result),0===n)return t.join("")+a.join(" ");for(var p=0,i=0;i<a.length;i++)p+a[i].length>n&&0!==i?(" "===t[t.length-1]&&t.pop(),t.push(e),p=0):0!==i&&(t.push(" "),p++),t.push(a[i]),p+=a[i].length;return t.join("")},load_pgn:function(r,e){function o(r){return r.replace(/\\/g,"\\")}function i(r){for(var e=r.replace(/=/,"").replace(/[+#]?[?!]*$/,""),n=c(),t=0,o=n.length;o>t;t++)if(e===v(n[t]).replace(/=/,"").replace(/[+#]?[?!]*$/,"")||p&&e===v(n[t],p).replace(/=/,"").replace(/[+#]?[?!]*$/,""))return n[t];return null}function a(r){return i(k(r))}function l(r){for(var e in r)return!0;return!1}function u(r,e){for(var n="object"==typeof e&&"string"==typeof e.newline_char?e.newline_char:"\r?\n",t={},i=r.split(new RegExp(o(n))),f="",a="",l=0;l<i.length;l++)f=i[l].replace(/^\[([A-Z][A-Za-z]*)\s.*\]$/,"$1"),a=i[l].replace(/^\[[A-Za-z]+\s"(.*)"\]$/,"$1"),k(f).length>0&&(t[f]=a);return t}var p="undefined"!=typeof e&&"sloppy"in e?e.sloppy:!1,s="object"==typeof e&&"string"==typeof e.newline_char?e.newline_char:"\r?\n",g=new RegExp("^(\\[(.|"+o(s)+")*\\])("+o(s)+")*1.("+o(s)+"|.)*$","g"),h=r.replace(g,"$1");"["!==h[0]&&(h=""),n();var d=u(h,e);for(var E in d)f([E,d[E]]);if("1"===d.SetUp&&!("FEN"in d&&t(d.FEN)))return!1;var b=r.replace(h,"").replace(new RegExp(o(s),"g")," ");b=b.replace(/(\{[^}]+\})+?/g,"");for(var _=/(\([^\(\)]+\))+?/g;_.test(b);)b=b.replace(_,"");b=b.replace(/\d+\.(\.\.)?/g,""),b=b.replace(/\.\.\./g,""),b=b.replace(/\$\d+/g,"");var y=k(b).split(new RegExp(/\s+/));y=y.join(",").replace(/,,+/g,",").split(",");for(var A="",m=0;m<y.length-1;m++){if(A=a(y[m]),null==A)return!1;S(A)}if(A=y[y.length-1],W.indexOf(A)>-1)l(gr)&&"undefined"==typeof gr.Result&&f(["Result",A]);else{if(A=a(A),null==A)return!1;S(A)}return!0},header:function(){return f(arguments)},ascii:function(){return T()},turn:function(){return lr},move:function(r,e){var n="undefined"!=typeof e&&"sloppy"in e?e.sloppy:!1,t=null,o=c();if("string"==typeof r){for(var i=r.replace(/=/,"").replace(/[+#]?[?!]*$/,""),f=0,a=o.length;a>f;f++)if(i===v(o[f]).replace(/=/,"").replace(/[+#]?[?!]*$/,"")||n&&i===v(o[f],n).replace(/=/,"").replace(/[+#]?[?!]*$/,"")){t=o[f];break}}else if("object"==typeof r)for(var f=0,a=o.length;a>f;f++)if(!(r.from!==w(o[f].from)||r.to!==w(o[f].to)||"promotion"in o[f]&&r.promotion!==o[f].promotion)){t=o[f];break}if(!t)return null;var l=O(t);return S(t),l},undo:function(){var r=m();return r?O(r):null},clear:function(){return e()},put:function(r,e){return u(r,e)},get:function(r){return l(r)},remove:function(r){return p(r)},perft:function(r){return q(r)},square_color:function(r){if(r in or){var e=or[r];return(I(e)+P(e))%2===0?"light":"dark"}return null},history:function(r){for(var e=[],n=[],t=("undefined"!=typeof r&&"verbose"in r&&r.verbose);vr.length>0;)e.push(m());for(;e.length>0;){var o=e.pop();n.push(t?O(o):v(o)),S(o)}return n}}};"undefined"!=typeof exports&&(exports.Chess=Chess),"undefined"!=typeof define&&define(function(){return Chess});
var Chess=function(r){function e(){ur=new Array(128),lr={w:x,b:x},sr=U,pr={w:0,b:0},cr=x,vr=0,gr=1,hr=[],dr={},a(i())}function n(){t(H)}function t(r){var n=r.split(/\s+/),t=n[0],f=0;if(!o(r).valid)return!1;e();for(var u=0;u<t.length;u++){var s=t.charAt(u);if("/"===s)f+=8;else if(O(s))f+=parseInt(s,10);else{var p="a">s?U:Q;l({type:s.toLowerCase(),color:p},R(f)),f++}}return sr=n[1],n[2].indexOf("K")>-1&&(pr.w|=er.KSIDE_CASTLE),n[2].indexOf("Q")>-1&&(pr.w|=er.QSIDE_CASTLE),n[2].indexOf("k")>-1&&(pr.b|=er.KSIDE_CASTLE),n[2].indexOf("q")>-1&&(pr.b|=er.QSIDE_CASTLE),cr="-"===n[3]?x:fr[n[3]],vr=parseInt(n[4],10),gr=parseInt(n[5],10),a(i()),!0}function o(r){var e={0:"No errors.",1:"FEN string must contain six space-delimited fields.",2:"6th field (move number) must be a positive integer.",3:"5th field (half move counter) must be a non-negative integer.",4:"4th field (en-passant square) is invalid.",5:"3rd field (castling availability) is invalid.",6:"2nd field (side to move) is invalid.",7:"1st field (piece positions) does not contain 8 '/'-delimited rows.",8:"1st field (piece positions) is invalid [consecutive numbers].",9:"1st field (piece positions) is invalid [invalid piece].",10:"1st field (piece positions) is invalid [row too large].",11:"Illegal en-passant square"},n=r.split(/\s+/);if(6!==n.length)return{valid:!1,error_number:1,error:e[1]};if(isNaN(n[5])||parseInt(n[5],10)<=0)return{valid:!1,error_number:2,error:e[2]};if(isNaN(n[4])||parseInt(n[4],10)<0)return{valid:!1,error_number:3,error:e[3]};if(!/^(-|[abcdefgh][36])$/.test(n[3]))return{valid:!1,error_number:4,error:e[4]};if(!/^(KQ?k?q?|Qk?q?|kq?|q|-)$/.test(n[2]))return{valid:!1,error_number:5,error:e[5]};if(!/^(w|b)$/.test(n[1]))return{valid:!1,error_number:6,error:e[6]};var t=n[0].split("/");if(8!==t.length)return{valid:!1,error_number:7,error:e[7]};for(var o=0;o<t.length;o++){for(var i=0,f=!1,a=0;a<t[o].length;a++)if(isNaN(t[o][a])){if(!/^[prnbqkPRNBQK]$/.test(t[o][a]))return{valid:!1,error_number:9,error:e[9]};i+=1,f=!1}else{if(f)return{valid:!1,error_number:8,error:e[8]};i+=parseInt(t[o][a],10),f=!0}if(8!==i)return{valid:!1,error_number:10,error:e[10]}}return"3"==n[3][1]&&"w"==n[1]||"6"==n[3][1]&&"b"==n[1]?{valid:!1,error_number:11,error:e[11]}:{valid:!0,error_number:0,error:e[0]}}function i(){for(var r=0,e="",n=fr.a8;n<=fr.h1;n++){if(null==ur[n])r++;else{r>0&&(e+=r,r=0);var t=ur[n].color,o=ur[n].type;e+=t===U?o.toUpperCase():o.toLowerCase()}n+1&136&&(r>0&&(e+=r),n!==fr.h1&&(e+="/"),r=0,n+=8)}var i="";pr[U]&er.KSIDE_CASTLE&&(i+="K"),pr[U]&er.QSIDE_CASTLE&&(i+="Q"),pr[Q]&er.KSIDE_CASTLE&&(i+="k"),pr[Q]&er.QSIDE_CASTLE&&(i+="q"),i=i||"-";var f=cr===x?"-":R(cr);return[e,sr,i,f,vr,gr].join(" ")}function f(r){for(var e=0;e<r.length;e+=2)"string"==typeof r[e]&&"string"==typeof r[e+1]&&(dr[r[e]]=r[e+1]);return dr}function a(r){hr.length>0||(r!==H?(dr.SetUp="1",dr.FEN=r):(delete dr.SetUp,delete dr.FEN))}function u(r){var e=ur[fr[r]];return e?{type:e.type,color:e.color}:null}function l(r,e){if(!("type"in r&&"color"in r))return!1;if(-1===W.indexOf(r.type.toLowerCase()))return!1;if(!(e in fr))return!1;var n=fr[e];return r.type==F&&lr[r.color]!=x&&lr[r.color]!=n?!1:(ur[n]={type:r.type,color:r.color},r.type===F&&(lr[r.color]=n),a(i()),!0)}function s(r){var e=u(r);return ur[fr[r]]=null,e&&e.type===F&&(lr[e.color]=x),a(i()),e}function p(r,e,n,t,o){var i={color:sr,from:e,to:n,flags:t,piece:r[e].type};return o&&(i.flags|=er.PROMOTION,i.promotion=o),r[n]?i.captured=r[n].type:t&er.EP_CAPTURE&&(i.captured=$),i}function c(r){function e(r,e,n,t,o){if(r[n].type!==$||w(t)!==ir&&w(t)!==nr)e.push(p(r,n,t,o));else for(var i=[G,M,B,j],f=0,a=i.length;a>f;f++)e.push(p(r,n,t,o,i[f]))}var n=[],t=sr,o=N(t),i={b:or,w:tr},f=fr.a8,a=fr.h1,u=!1,l="undefined"!=typeof r&&"legal"in r?r.legal:!0;if("undefined"!=typeof r&&"square"in r){if(!(r.square in fr))return[];f=a=fr[r.square],u=!0}for(var s=f;a>=s;s++)if(136&s)s+=7;else{var c=ur[s];if(null!=c&&c.color===t)if(c.type===$){var v=s+z[t][0];if(null==ur[v]){e(ur,n,s,v,er.NORMAL);var v=s+z[t][1];i[t]===w(s)&&null==ur[v]&&e(ur,n,s,v,er.BIG_PAWN)}for(g=2;4>g;g++){var v=s+z[t][g];136&v||(null!=ur[v]&&ur[v].color===o?e(ur,n,s,v,er.CAPTURE):v===cr&&e(ur,n,s,cr,er.EP_CAPTURE))}}else for(var g=0,E=J[c.type].length;E>g;g++)for(var b=J[c.type][g],v=s;;){if(v+=b,136&v)break;if(null!=ur[v]){if(ur[v].color===t)break;e(ur,n,s,v,er.CAPTURE);break}if(e(ur,n,s,v,er.NORMAL),"n"===c.type||"k"===c.type)break}}if(!u||a===lr[t]){if(pr[t]&er.KSIDE_CASTLE){var _=lr[t],y=_+2;null!=ur[_+1]||null!=ur[y]||h(o,lr[t])||h(o,_+1)||h(o,y)||e(ur,n,lr[t],y,er.KSIDE_CASTLE)}if(pr[t]&er.QSIDE_CASTLE){var _=lr[t],y=_-2;null!=ur[_-1]||null!=ur[_-2]||null!=ur[_-3]||h(o,lr[t])||h(o,_-1)||h(o,y)||e(ur,n,lr[t],y,er.QSIDE_CASTLE)}}if(!l)return n;for(var m=[],s=0,E=n.length;E>s;s++)S(n[s]),d(t)||m.push(n[s]),C();return m}function v(r,e){var n="";if(r.flags&er.KSIDE_CASTLE)n="O-O";else if(r.flags&er.QSIDE_CASTLE)n="O-O-O";else{var t=T(r,e);r.piece!==$&&(n+=r.piece.toUpperCase()+t),r.flags&(er.CAPTURE|er.EP_CAPTURE)&&(r.piece===$&&(n+=R(r.from)[0]),n+="x"),n+=R(r.to),r.flags&er.PROMOTION&&(n+="="+r.promotion.toUpperCase())}return S(r),E()&&(n+=b()?"#":"+"),C(),n}function g(r){return r.replace(/=/,"").replace(/[+#]?[?!]*$/,"")}function h(r,e){for(var n=fr.a8;n<=fr.h1;n++)if(136&n)n+=7;else if(null!=ur[n]&&ur[n].color===r){var t=ur[n],o=n-e,i=o+119;if(V[i]&1<<Y[t.type]){if(t.type===$){if(o>0){if(t.color===U)return!0}else if(t.color===Q)return!0;continue}if("n"===t.type||"k"===t.type)return!0;for(var f=X[i],a=n+f,u=!1;a!==e;){if(null!=ur[a]){u=!0;break}a+=f}if(!u)return!0}}return!1}function d(r){return h(N(r),lr[r])}function E(){return d(sr)}function b(){return E()&&0===c().length}function _(){return!E()&&0===c().length}function y(){for(var r={},e=[],n=0,t=0,o=fr.a8;o<=fr.h1;o++)if(t=(t+1)%2,136&o)o+=7;else{var i=ur[o];i&&(r[i.type]=i.type in r?r[i.type]+1:1,i.type===B&&e.push(t),n++)}if(2===n)return!0;if(3===n&&(1===r[B]||1===r[j]))return!0;if(n===r[B]+2){for(var f=0,a=e.length,o=0;a>o;o++)f+=e[o];if(0===f||f===a)return!0}return!1}function m(){for(var r=[],e={},n=!1;;){var t=C();if(!t)break;r.push(t)}for(;;){var o=i().split(" ").slice(0,4).join(" ");if(e[o]=o in e?e[o]+1:1,e[o]>=3&&(n=!0),!r.length)break;S(r.pop())}return n}function A(r){hr.push({move:r,kings:{b:lr.b,w:lr.w},turn:sr,castling:{b:pr.b,w:pr.w},ep_square:cr,half_moves:vr,move_number:gr})}function S(r){var e=sr,n=N(e);if(A(r),ur[r.to]=ur[r.from],ur[r.from]=null,r.flags&er.EP_CAPTURE&&(sr===Q?ur[r.to-16]=null:ur[r.to+16]=null),r.flags&er.PROMOTION&&(ur[r.to]={type:r.promotion,color:e}),ur[r.to].type===F){if(lr[ur[r.to].color]=r.to,r.flags&er.KSIDE_CASTLE){var t=r.to-1,o=r.to+1;ur[t]=ur[o],ur[o]=null}else if(r.flags&er.QSIDE_CASTLE){var t=r.to+1,o=r.to-2;ur[t]=ur[o],ur[o]=null}pr[e]=""}if(pr[e])for(var i=0,f=ar[e].length;f>i;i++)if(r.from===ar[e][i].square&&pr[e]&ar[e][i].flag){pr[e]^=ar[e][i].flag;break}if(pr[n])for(var i=0,f=ar[n].length;f>i;i++)if(r.to===ar[n][i].square&&pr[n]&ar[n][i].flag){pr[n]^=ar[n][i].flag;break}cr=r.flags&er.BIG_PAWN?"b"===sr?r.to-16:r.to+16:x,r.piece===$?vr=0:r.flags&(er.CAPTURE|er.EP_CAPTURE)?vr=0:vr++,sr===Q&&gr++,sr=N(sr)}function C(){var r=hr.pop();if(null==r)return null;var e=r.move;lr=r.kings,sr=r.turn,pr=r.castling,cr=r.ep_square,vr=r.half_moves,gr=r.move_number;var n=sr,t=N(sr);if(ur[e.from]=ur[e.to],ur[e.from].type=e.piece,ur[e.to]=null,e.flags&er.CAPTURE)ur[e.to]={type:e.captured,color:t};else if(e.flags&er.EP_CAPTURE){var o;o=n===Q?e.to-16:e.to+16,ur[o]={type:$,color:t}}if(e.flags&(er.KSIDE_CASTLE|er.QSIDE_CASTLE)){var i,f;e.flags&er.KSIDE_CASTLE?(i=e.to+1,f=e.to-1):e.flags&er.QSIDE_CASTLE&&(i=e.to-2,f=e.to+1),ur[i]=ur[f],ur[f]=null}return e}function T(r,e){for(var n=c({legal:!e}),t=r.from,o=r.to,i=r.piece,f=0,a=0,u=0,l=0,s=n.length;s>l;l++){var p=n[l].from,v=n[l].to,g=n[l].piece;i===g&&t!==p&&o===v&&(f++,w(t)===w(p)&&a++,L(t)===L(p)&&u++)}return f>0?a>0&&u>0?R(t):R(t).charAt(u>0?1:0):""}function I(){for(var r=" +------------------------+\n",e=fr.a8;e<=fr.h1;e++){if(0===L(e)&&(r+=" "+"87654321"[w(e)]+" |"),null==ur[e])r+=" . ";else{var n=ur[e].type,t=ur[e].color,o=t===U?n.toUpperCase():n.toLowerCase();r+=" "+o+" "}e+1&136&&(r+="|\n",e+=8)}return r+=" +------------------------+\n",r+=" a b c d e f g h\n"}function P(r,e){var n=g(r);if(e){var t=n.match(/([pnbrqkPNBRQK])?([a-h][1-8])x?-?([a-h][1-8])([qrbnQRBN])?/);if(t)var o=t[1],i=t[2],f=t[3],a=t[4]}for(var u=c(),l=0,s=u.length;s>l;l++){if(n===g(v(u[l]))||e&&n===g(v(u[l],!0)))return u[l];if(!(!t||o&&o.toLowerCase()!=u[l].piece||fr[i]!=u[l].from||fr[f]!=u[l].to||a&&a.toLowerCase()!=u[l].promotion))return u[l]}return null}function w(r){return r>>4}function L(r){return 15&r}function R(r){var e=L(r),n=w(r);return"abcdefgh".substring(e,e+1)+"87654321".substring(n,n+1)}function N(r){return r===U?Q:U}function O(r){return-1!=="0123456789".indexOf(r)}function k(r){var e=q(r);e.san=v(e,!1),e.to=R(e.to),e.from=R(e.from);var n="";for(var t in er)er[t]&e.flags&&(n+=rr[t]);return e.flags=n,e}function q(r){var e=r instanceof Array?[]:{};for(var n in r)"object"==typeof n?e[n]=q(r[n]):e[n]=r[n];return e}function D(r){return r.replace(/^\s+|\s+$/g,"")}function K(r){for(var e=c({legal:!1}),n=0,t=sr,o=0,i=e.length;i>o;o++){if(S(e[o]),!d(t))if(r-1>0){var f=K(r-1);n+=f}else n++;C()}return n}var Q="b",U="w",x=-1,$="p",j="n",B="b",M="r",G="q",F="k",W="pnbrqkPNBRQK",H="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",Z=["1-0","0-1","1/2-1/2","*"],z={b:[16,32,17,15],w:[-16,-32,-17,-15]},J={n:[-18,-33,-31,-14,18,33,31,14],b:[-17,-15,17,15],r:[-16,1,16,-1],q:[-17,-16,-15,1,17,16,15,-1],k:[-17,-16,-15,1,17,16,15,-1]},V=[20,0,0,0,0,0,0,24,0,0,0,0,0,0,20,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,24,24,24,24,24,24,56,0,56,24,24,24,24,24,24,0,0,0,0,0,0,2,53,56,53,2,0,0,0,0,0,0,0,0,0,0,0,20,2,24,2,20,0,0,0,0,0,0,0,0,0,0,20,0,0,24,0,0,20,0,0,0,0,0,0,0,0,20,0,0,0,24,0,0,0,20,0,0,0,0,0,0,20,0,0,0,0,24,0,0,0,0,20,0,0,0,0,20,0,0,0,0,0,24,0,0,0,0,0,20,0,0,20,0,0,0,0,0,0,24,0,0,0,0,0,0,20],X=[17,0,0,0,0,0,0,16,0,0,0,0,0,0,15,0,0,17,0,0,0,0,0,16,0,0,0,0,0,15,0,0,0,0,17,0,0,0,0,16,0,0,0,0,15,0,0,0,0,0,0,17,0,0,0,16,0,0,0,15,0,0,0,0,0,0,0,0,17,0,0,16,0,0,15,0,0,0,0,0,0,0,0,0,0,17,0,16,0,15,0,0,0,0,0,0,0,0,0,0,0,0,17,16,15,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,-15,-16,-17,0,0,0,0,0,0,0,0,0,0,0,0,-15,0,-16,0,-17,0,0,0,0,0,0,0,0,0,0,-15,0,0,-16,0,0,-17,0,0,0,0,0,0,0,0,-15,0,0,0,-16,0,0,0,-17,0,0,0,0,0,0,-15,0,0,0,0,-16,0,0,0,0,-17,0,0,0,0,-15,0,0,0,0,0,-16,0,0,0,0,0,-17,0,0,-15,0,0,0,0,0,0,-16,0,0,0,0,0,0,-17],Y={p:0,n:1,b:2,r:3,q:4,k:5},rr={NORMAL:"n",CAPTURE:"c",BIG_PAWN:"b",EP_CAPTURE:"e",PROMOTION:"p",KSIDE_CASTLE:"k",QSIDE_CASTLE:"q"},er={NORMAL:1,CAPTURE:2,BIG_PAWN:4,EP_CAPTURE:8,PROMOTION:16,KSIDE_CASTLE:32,QSIDE_CASTLE:64},nr=7,tr=6,or=1,ir=0,fr={a8:0,b8:1,c8:2,d8:3,e8:4,f8:5,g8:6,h8:7,a7:16,b7:17,c7:18,d7:19,e7:20,f7:21,g7:22,h7:23,a6:32,b6:33,c6:34,d6:35,e6:36,f6:37,g6:38,h6:39,a5:48,b5:49,c5:50,d5:51,e5:52,f5:53,g5:54,h5:55,a4:64,b4:65,c4:66,d4:67,e4:68,f4:69,g4:70,h4:71,a3:80,b3:81,c3:82,d3:83,e3:84,f3:85,g3:86,h3:87,a2:96,b2:97,c2:98,d2:99,e2:100,f2:101,g2:102,h2:103,a1:112,b1:113,c1:114,d1:115,e1:116,f1:117,g1:118,h1:119},ar={w:[{square:fr.a1,flag:er.QSIDE_CASTLE},{square:fr.h1,flag:er.KSIDE_CASTLE}],b:[{square:fr.a8,flag:er.QSIDE_CASTLE},{square:fr.h8,flag:er.KSIDE_CASTLE}]},ur=new Array(128),lr={w:x,b:x},sr=U,pr={w:0,b:0},cr=x,vr=0,gr=1,hr=[],dr={};return t("undefined"==typeof r?H:r),{WHITE:U,BLACK:Q,PAWN:$,KNIGHT:j,BISHOP:B,ROOK:M,QUEEN:G,KING:F,SQUARES:function(){for(var r=[],e=fr.a8;e<=fr.h1;e++)136&e?e+=7:r.push(R(e));return r}(),FLAGS:rr,load:function(r){return t(r)},reset:function(){return n()},moves:function(r){for(var e=c(r),n=[],t=0,o=e.length;o>t;t++)n.push("undefined"!=typeof r&&"verbose"in r&&r.verbose?k(e[t]):v(e[t],!1));return n},in_check:function(){return E()},in_checkmate:function(){return b()},in_stalemate:function(){return _()},in_draw:function(){return vr>=100||_()||y()||m()},insufficient_material:function(){return y()},in_threefold_repetition:function(){return m()},game_over:function(){return vr>=100||b()||_()||y()||m()},validate_fen:function(r){return o(r)},fen:function(){return i()},pgn:function(r){var e="object"==typeof r&&"string"==typeof r.newline_char?r.newline_char:"\n",n="object"==typeof r&&"number"==typeof r.max_width?r.max_width:0,t=[],o=!1;for(var i in dr)t.push("["+i+' "'+dr[i]+'"]'+e),o=!0;o&&hr.length&&t.push(e);for(var f=[];hr.length>0;)f.push(C());for(var a=[],u="";f.length>0;){var l=f.pop();hr.length||"b"!==l.color?"w"===l.color&&(u.length&&a.push(u),u=gr+"."):u=gr+". ...",u=u+" "+v(l,!1),S(l)}if(u.length&&a.push(u),"undefined"!=typeof dr.Result&&a.push(dr.Result),0===n)return t.join("")+a.join(" ");for(var s=0,i=0;i<a.length;i++)s+a[i].length>n&&0!==i?(" "===t[t.length-1]&&t.pop(),t.push(e),s=0):0!==i&&(t.push(" "),s++),t.push(a[i]),s+=a[i].length;return t.join("")},load_pgn:function(r,e){function o(r){return r.replace(/\\/g,"\\")}function i(r){for(var e in r)return!0;return!1}function a(r,e){for(var n="object"==typeof e&&"string"==typeof e.newline_char?e.newline_char:"\r?\n",t={},i=r.split(new RegExp(o(n))),f="",a="",u=0;u<i.length;u++)f=i[u].replace(/^\[([A-Z][A-Za-z]*)\s.*\]$/,"$1"),a=i[u].replace(/^\[[A-Za-z]+\s"(.*)"\]$/,"$1"),D(f).length>0&&(t[f]=a);return t}var u="undefined"!=typeof e&&"sloppy"in e?e.sloppy:!1,l="object"==typeof e&&"string"==typeof e.newline_char?e.newline_char:"\r?\n",s=new RegExp("^(\\[(.|"+o(l)+")*\\])("+o(l)+")*1.("+o(l)+"|.)*$","g"),p=r.replace(s,"$1");"["!==p[0]&&(p=""),n();var c=a(p,e);for(var v in c)f([v,c[v]]);if("1"===c.SetUp&&!("FEN"in c&&t(c.FEN)))return!1;var g=r.replace(p,"").replace(new RegExp(o(l),"g")," ");g=g.replace(/(\{[^}]+\})+?/g,"");for(var h=/(\([^\(\)]+\))+?/g;h.test(g);)g=g.replace(h,"");g=g.replace(/\d+\.(\.\.)?/g,""),g=g.replace(/\.\.\./g,""),g=g.replace(/\$\d+/g,"");var d=D(g).split(new RegExp(/\s+/));d=d.join(",").replace(/,,+/g,",").split(",");for(var E="",b=0;b<d.length-1;b++){if(E=P(d[b],u),null==E)return!1;S(E)}if(E=d[d.length-1],Z.indexOf(E)>-1)i(dr)&&"undefined"==typeof dr.Result&&f(["Result",E]);else{if(E=P(E,u),null==E)return!1;S(E)}return!0},header:function(){return f(arguments)},ascii:function(){return I()},turn:function(){return sr},move:function(r,e){var n="undefined"!=typeof e&&"sloppy"in e?e.sloppy:!1,t=null;if("string"==typeof r)t=P(r,n);else if("object"==typeof r)for(var o=c(),i=0,f=o.length;f>i;i++)if(!(r.from!==R(o[i].from)||r.to!==R(o[i].to)||"promotion"in o[i]&&r.promotion!==o[i].promotion)){t=o[i];break}if(!t)return null;var a=k(t);return S(t),a},undo:function(){var r=C();return r?k(r):null},clear:function(){return e()},put:function(r,e){return l(r,e)},get:function(r){return u(r)},remove:function(r){return s(r)},perft:function(r){return K(r)},square_color:function(r){if(r in fr){var e=fr[r];return(w(e)+L(e))%2===0?"light":"dark"}return null},history:function(r){for(var e=[],n=[],t=("undefined"!=typeof r&&"verbose"in r&&r.verbose);hr.length>0;)e.push(C());for(;e.length>0;){var o=e.pop();n.push(t?k(o):v(o)),S(o)}return n}}};"undefined"!=typeof exports&&(exports.Chess=Chess),"undefined"!=typeof define&&define(function(){return Chess});
{
"name": "chess.js",
"version": "0.10.1",
"version": "0.10.2",
"description": "A Javascript chess library for chess move generation/validation, piece placement/movement, and check/checkmate/draw detection",

@@ -5,0 +5,0 @@ "author": "Jeff Hlywa <jhlywa@gmail.com> (https://github.com/jhlywa)",

@@ -22,3 +22,3 @@ # chess.js

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.1/chess.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
```

@@ -281,5 +281,6 @@

string representation of a RegExp (and should not be pre-escaped) and defaults
to `\r?\n`). Options may also contain the `sloppy` flag to handle overly
disambiguated moves (see `.move` documentation for more information). Returns
true if the PGN was parsed successfully, otherwise false.
to `\r?\n`). Options may also contain a `sloppy` flag which allows chess.js
to parse moves in various non-standard notations (see `.move` documentation
for more information). Returns true if the PGN was parsed successfully,
otherwise false.

@@ -355,18 +356,26 @@ ```js

Some chess applications (e.g. Fritz and Chessbase) overly disambiguate SAN. To
handle these situations, a sloppy flag can be passed to .move to allow slightly
relaxed parsing:
An optional sloppy flag can be used to parse a variety of non-standard move
notations:
```js
// there are two black knights in the postion below, one on c6 (pinned) and one
// one g8
var chess = new Chess('r2qkbnr/ppp2ppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R b KQkq - 3 7');
// Ne7 doesn't need disambiguation because only one knight can legally move to
// e7, so chess.js will reject any attempts at disambiguation (it violates SAN
// spec)
chess.move('Nge7');
var chess = new Chess();
// various forms of Long Algebraic Notation
chess.move('e2e4', {sloppy: true});
// -> { color: 'w', from: 'e2', to: 'e4', flags: 'b', piece: 'p', san: 'e4' }
chess.move('e7-e5', {sloppy: true});
// -> { color: 'b', from: 'e7', to: 'e5', flags: 'b', piece: 'p', san: 'e5' }
chess.move('Pf2f4', {sloppy: true});
// -> { color: 'w', from: 'f2', to: 'f4', flags: 'b', piece: 'p', san: 'f4' }
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.move('Nge7'); // Ne7 is unambiguous because the knight on c6 is pinned
// -> null
// to relax the overly disambiguation constraint, use the sloppy flag
chess.move('Nge7', {sloppy: true});

@@ -373,0 +382,0 @@ // -> { color: 'b', from: 'g8', to: 'e7', flags: 'n', piece: 'n', san: 'Ne7' }

@@ -621,2 +621,105 @@ if (typeof require != "undefined") {

// the sloppy parser should handle lazy disambiguation (e.g. Rc1c4 below)
{pgn: [
'1.e4 e5 2. Nf3 d5 3. Nxe5 f6 4. Bb5+ c6 5. Qh5+ Ke7',
'Qf7+ Kd6 7. d3 Kxe5 8. Qh5+ g5 9. g3 cxb5 10. Bf4+ Ke6',
'exd5+ Qxd5 12. Qe8+ Kf5 13. Rg1 gxf4 14. Nc3 Qc5 15. Ne4 Qxf2+',
'Kxf2 fxg3+ 17. Rxg3 Nd7 18. Qh5+ Ke6 19. Qe8+ Kd5 20. Rg4 Rb8',
'c4+ Kc6 22. Qe6+ Kc7 23. cxb5 Ne7 24. Rc1+ Kd8 25. Nxf6 Ra8',
'Kf1 Rb8 27. Rc1c4 b6 28. Rc4-d4 Rb7 29. Qf7 Rc7 30. Qe8# 1-0'],
fen: '2bkQb1r/p1rnn2p/1p3N2/1P6/3R2R1/3P4/PP5P/5K2 b - - 5 30',
expect: true,
sloppy: true
},
// sloppy parse should parse long algebraic notation
{pgn: [
'e2e4 d7d5 e4d5 d8d5 d2d4 g8f6 c2c4 d5d8 g1f3 c8g4 f1e2 e7e6 b1c3 f8e7',
'c1e3 e8g8 d1b3 b8c6 a1d1 a8b8 e1g1 d8c8 h2h3 g4h5 d4d5 e6d5 c4d5 h5f3',
'e2f3 c6e5 f3e2 a7a6 e3a7 b8a8 a7d4 e7d6 b3c2 f8e8 f2f4 e5d7 e2d3 c7c5',
'd4f2 d6f4 c3e4 f6d5 e4d6 f4d6 d3h7'],
fen: 'r1q1r1k1/1p1n1ppB/p2b4/2pn4/8/7P/PPQ2BP1/3R1RK1 b - - 0 25',
expect: true,
sloppy: true,
},
// sloppy parse should parse extended long algebraic notation w/ en passant
{pgn: [
'1. d2d4 f7f5 2. b2b3 e7e6 3. c1b2 d7d5 4. g1f3 f8d6 5. e2e3 g8f6 6. b1d2',
'e8g8 7. c2c4 c7c6 8. f1d3 b8d7 9. e1g1 f6e4 10. a1c1 g7g5 11. h2h3 d8e8 12.',
'd3e4 d5e4 13. f3g5 e8g6 14. h3h4 h7h6 15. g5h3 d7f6 16. f2f4 e4f3 17. d2f3',
'f6g4 18. d1e2 d6g3 19. h3f4 g6g7 20. d4d5 g7f7 21. d5e6 c8e6 22. f3e5 g4e5',
'23. b2e5 g8h7 24. h4h5 f8g8 25. e2f3 g3f4 26. e5f4 g8g4 27. g2g3 a8g8 28.',
'c1c2 b7b5 29. c4b5 e6d5 30. f3d1 f7h5 31. c2h2 g4g3+ 32. f4g3 g8g3+ 33.',
'g1f2 h5h2+ 34. f2e1 g3g2 35. d1d3 d5e4 36. d3d7+ h7g6 37. b5c6 g2e2+ 38.',
'e1d1 e2a2 0-1'],
fen: '8/p2Q4/2P3kp/5p2/4b3/1P2P3/r6q/3K1R2 w - - 0 39',
expect: true,
sloppy: true
},
// sloppy parse should parse long algebraic notation w/ underpromotions
{pgn: [
'1. e2e4 c7c5 2. g1f3 d7d6 3. d2d4 c5d4 4. f3d4 g8f6 5. f1d3 a7a6 6. c1e3',
'e7e5 7. d4f5 c8f5 8. e4f5 d6d5 9. e3g5 f8e7 10. d1e2 e5e4 11. g5f6 e7f6 12.',
'd3e4 d5e4 13. e2e4+ d8e7 14. e4e7+ f6e7 15. e1g1 e8g8 16. f1e1 e7f6 17.',
'c2c3 b8c6 18. b1d2 a8d8 19. d2e4 f8e8 20. e1e3 c6e5 21. a1e1 e5d3 22. e4f6+',
'g7f6 23. e3e8+ d8e8 24. e1e8+ g8g7 25. b2b4 d3e5 26. a2a4 b7b5 27. a4b5',
'a6b5 28. e8b8 e5g4 29. b8b5 g4e5 30. b5c5 g7f8 31. b4b5 f8e7 32. f2f4 e5d7',
'33. c5c7 e7d6 34. c7c8 d7b6 35. c8c6+ d6d7 36. c6b6 h7h5 37. b6f6 h5h4 38.',
'f6f7+ d7d6 39. f7h7 h4h3 40. h7h3 d6e7 41. b5b6 e7f6 42. h3h5 f6g7 43. b6b7',
'g7g8 44. b7b8N g8g7 45. c3c4 g7f6 46. c4c5 f6e7 47. c5c6 e7f6 48. c6c7 f6e7',
'49. c7c8B e7d6 50. b8a6 d6e7 51. c8e6 e7f6 52. a6c5 f6g7 53. c5e4 g7f8 54.',
'h5h8+ f8g7 55. h8g8+ g7h6 56. g8g6+ h6h7 57. e4f6+ h7h8 58. f6e4 h8h7 59.',
'f5f6 h7g6 60. f6f7 g6h5 61. f7f8R h5h6 62. f4f5 h6h7 63. f8f7+ h7h6 64.',
'f5f6 h6g6 65. f7g7+ g6h5 66. f6f7 h5h4 67. f7f8Q h4h5 68. f8h8# 1-0'],
fen: '7Q/6R1/4B3/7k/4N3/8/6PP/6K1 b - - 2 68',
expect: true,
sloppy: true
},
// sloppy parse should parse abbreviated long algebraic notation
{pgn: [
'1. d2d4 f7f5 2. Bc1g5 d7d6 3. e2e3 Nb8d7 4. c2c4 Ng8f6 5. Nb1c3 e7e5 6.',
'd4e5 d6e5 7. g2g3 Bf8e7 8. Bf1h3 h7h6 9. Bg5f6 Nd7f6 10. Qd1d8+ Be7d8 11.',
'Ng1f3 e5e4 12. Nf3d4 g7g6 13. e1g1 c7c5 14. Nd4b5 e8g8 15. Nb5d6 Bd8c7 16.',
'Nd6c8 Ra8c8 17. Rf1d1 Rc8d8 18. Bh3f1 b7b6 19. Nc3d5 Nf6d5 20. c4d5 Rf8e8',
'21. Bf1b5 Re8e5 22. Bb5c6 Kg8f7 23. Kg1f1 Kf7f6 24. h2h4 g6g5 25. h4g5+',
'h6g5 26. Kf1e2 Rd8h8 27. Rd1h1 Rh8h1 28. Ra1h1 Kf6g7 29. Rh1h5 Kg7g6 30.',
'Rh5h8 Re5e7 31. Rh8a8 a7a5 32. Ra8a7 Kg6f6 33. Ra7b7 Kf6e5 34. Ke2d2 f5f4',
'35. g3f4+ g5f4 36. Kd2c3 f4e3 37. f2e3 Re7f7 38. Kc3c4 Ke5d6 39. a2a3 Rf7f3',
'40. b2b4 a5b4 41. a3b4 c5b4 42. Kc4b4 Rf3e3 43. Kb4c4 Re3a3 44. Kc4b4 e4e3',
'45. Bc6b5 Ra3a1 46. Kb4c3 Ra1a3+ 47. Kc3d4 Ra3b3 48. Bb5e2 Rb3b4+ 49. Kd4e3',
'Rb4h4 50. Be2f3 Rh4h3 51. Rb7a7 Rh3f3+ 52. Ke3f3 b6b5 53. Kf3e4 Kd6c5 54.',
'Ra7b7 Bc7b6 55. Ke4e5 b5b4 56. d5d6 b4b3 57. Rb7b6 Kc5b6 58. d6d7 Kb6c7 59.',
'Ke5e6 1-0'],
fen: '8/2kP4/4K3/8/8/1p6/8/8 b - - 2 59',
expect: true,
sloppy: true
},
// sloppy parse should parse extended long algebraic notation
{pgn: [
'1. e2-e4 c7-c5 2. Ng1-f3 d7-d6 3. d2-d4 c5xd4 4. Nf3xd4 Ng8-f6 5. Bf1-d3',
'a7-a6 6. Bc1-e3 e7-e5 7. Nd4-f5 Bc8xf5 8. e4xf5 d6-d5 9. Be3-g5 Bf8-e7 10.',
'Qd1-e2 e5-e4 11. Bg5xf6 Be7xf6 12. Bd3xe4 d5xe4 13. Qe2xe4+ Qd8-e7 14.',
'Qe4xe7+ Bf6xe7 15. e1-g1 e8-g8 16. Rf1-e1 Be7-f6 17. c2-c3 Nb8-c6 18.',
'Nb1-d2 Ra8-d8 19. Nd2-e4 Rf8-e8 20. Re1-e3 Nc6-e5 21. Ra1-e1 Ne5-d3 22.',
'Ne4xf6+ g7xf6 23. Re3xe8+ Rd8xe8 24. Re1xe8+ Kg8-g7 25. b2-b4 Nd3-e5 26.',
'a2-a4 b7-b5 27. a4xb5 a6xb5 28. Re8-b8 Ne5-g4 29. Rb8xb5 Ng4-e5 30. Rb5-c5',
'Kg7-f8 31. b4-b5 Kf8-e7 32. f2-f4 Ne5-d7 33. Rc5-c7 Ke7-d6 34. Rc7-c8',
'Nd7-b6 35. Rc8-c6+ Kd6-d7 36. Rc6xb6 h7-h5 37. Rb6xf6 h5-h4 38. Rf6xf7+',
'Kd7-d6 39. Rf7-h7 h4-h3 40. Rh7xh3 Kd6-e7 41. b5-b6 Ke7-f6 42. Rh3-h5',
'Kf6-g7 43. b6-b7 Kg7-g8 44. b7-b8N Kg8-g7 45. c3-c4 Kg7-f6 46. c4-c5 Kf6-e7',
'47. c5-c6 Ke7-f6 48. c6-c7 Kf6-e7 49. c7-c8B Ke7-d6 50. Nb8-a6 Kd6-e7 51.',
'Bc8-e6 Ke7-f6 52. Na6-c5 Kf6-g7 53. Nc5-e4 Kg7-f8 54. Rh5-h8+ Kf8-g7 55.',
'Rh8-g8+ Kg7-h6 56. Rg8-g6+ Kh6-h7 57. Ne4-f6+ Kh7-h8 58. Nf6-e4 Kh8-h7 59.',
'f5-f6 Kh7xg6 60. f6-f7 Kg6-h5 61. f7-f8R Kh5-h6 62. f4-f5 Kh6-h7 63.',
'Rf8-f7+ Kh7-h6 64. f5-f6 Kh6-g6 65. Rf7-g7+ Kg6-h5 66. f6-f7 Kh5-h4 67.',
'f7-f8Q Kh4-h5 68. Qf8-h8# 1-0'],
fen: '7Q/6R1/4B3/7k/4N3/8/6PP/6K1 b - - 2 68',
expect: true,
sloppy: true
},
];

@@ -646,2 +749,3 @@

}
} else {

@@ -740,4 +844,3 @@ /* this test should fail, so make sure it does */

move: 'Ne7',
next: 'r2qkb1r/ppp1nppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R w KQkq - 4 8'},
next: 'r2qkb1r/ppp1nppp/2n5/1B2pQ2/4P3/8/PPP2PPP/RNB1K2R w KQkq - 4 8'}
];

@@ -744,0 +847,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc