/* @license

* Copyright (c) 2016, Jeff Hlywa (jhlywa@gmail.com)
* Released under the BSD license
* https://github.com/jhlywa/chess.js/blob/master/LICENSE
*/

var Chess=function®{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®{var n=r.split(/s+/),t=n,f=0;if(!o®.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,n.indexOf(“K”)>-1&&(pr.w|=er.KSIDE_CASTLE),n.indexOf(“Q”)>-1&&(pr.w|=er.QSIDE_CASTLE),n.indexOf(“k”)>-1&&(pr.b|=er.KSIDE_CASTLE),n.indexOf(“q”)>-1&&(pr.b|=er.QSIDE_CASTLE),cr=“-”===n?x:fr[n],vr=parseInt(n,10),gr=parseInt(n,10),a(i()),!0}function o®{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};if(isNaN(n)||parseInt(n,10)<=0)return{valid:!1,error_number:2,error:e};if(isNaN(n)||parseInt(n,10)<0)return{valid:!1,error_number:3,error:e};if(!/^(-|[36])$/.test(n))return{valid:!1,error_number:4,error:e};if(!/^(KQ?k?q?|Qk?q?|kq?|q|-)$/.test(n))return{valid:!1,error_number:5,error:e};if(!/^(w|b)$/.test(n))return{valid:!1,error_number:6,error:e};var t=n.split(“/”);if(8!==t.length)return{valid:!1,error_number:7,error:e};for(var o=0;o<t.length;o++){for(var i=0,f=!1,a=0;a.length;a++)if(isNaN(t[a])){if(!/^$/.test(t[a]))return{valid:!1,error_number:9,error:e};i+=1,f=!1}else{if(f)return{valid:!1,error_number:8,error:e};i+=parseInt(t[a],10),f=!0}if(8!==i)return{valid:!1,error_number:10,error:e}}return“3”==n[1]&&“w”==n||“6”==n[1]&&“b”==n?{valid:!1,error_number:11,error:e}:{valid:!0,error_number:0,error:e}}function i(){for(var r=0,e=“”,n=fr.a8;n<=fr.h1;n++){if(null==ur)r++;else{r>0&&(e+=r,r=0);var t=ur.color,o=ur.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&er.KSIDE_CASTLE&&(i+=“K”),pr&er.QSIDE_CASTLE&&(i+=“Q”),pr&er.KSIDE_CASTLE&&(i+=“k”),pr&er.QSIDE_CASTLE&&(i+=“q”),i=i||“-”;var f=cr===x?“-”:R(cr);return.join(“ ”)}function f®{for(var e=0;e<r.length;e+=2)“string”==typeof r&&“string”==typeof r&&(dr[r]=r);return dr}function a®{hr.length>0||(r!==H?(dr.SetUp=“1”,dr.FEN=r):(delete dr.SetUp,delete dr.FEN))}function u®{var e=ur[fr];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;return r.type==F&&lr!=x&&lr!=n?!1:(ur={type:r.type,color:r.color},r.type===F&&(lr=n),a(i()),!0)}function s®{var e=u®;return ur[fr]=null,e&&e.type===F&&(lr=x),a(i()),e}function p(r,e,n,t,o){var i={color:sr,from:e,to:n,flags:t,piece:r.type};return o&&(i.flags|=er.PROMOTION,i.promotion=o),r?i.captured=r.type:t&er.EP_CAPTURE&&(i.captured=$),i}function c®{function e(r,e,n,t,o){if(r.type!==$||w(t)!==ir&&w(t)!==nr)e.push(p(r,n,t,o));else for(var i=,f=0,a=i.length;a>f;f++)e.push(p(r,n,t,o,i))}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,u=!0}for(var s=f;a>=s;s++)if(136&s)s+=7;else{var c=ur;if(null!=c&&c.color===t)if(c.type===$){var v=s+z[0];if(null==ur){e(ur,n,s,v,er.NORMAL);var v=s+z[1];i===w(s)&&null==ur&&e(ur,n,s,v,er.BIG_PAWN)}for(g=2;4>g;g++){var v=s+z[g];136&v||(null!=ur&&ur.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.length;E>g;g++)for(var b=J[g],v=s;;){if(v+=b,136&v)break;if(null!=ur){if(ur.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){if(pr&er.KSIDE_CASTLE){var _=lr,y=_+2;null!=ur||null!=ur||h(o,lr)||h(o,_+1)||h(o,y)||e(ur,n,lr,y,er.KSIDE_CASTLE)}if(pr&er.QSIDE_CASTLE){var _=lr,y=_-2;null!=ur||null!=ur||null!=ur||h(o,lr)||h(o,_-1)||h(o,y)||e(ur,n,lr,y,er.QSIDE_CASTLE)}}if(!l)return n;for(var m=[],s=0,E=n.length;E>s;s++)S(n),d(t)||m.push(n),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)),n+=“x”),n+=R(r.to),r.flags&er.PROMOTION&&(n+=“=”+r.promotion.toUpperCase())}return S®,E()&&(n+=b()?“#”:“+”),C(),n}function g®{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&&ur.color===r){var t=ur,o=n-e,i=o+119;if(V&1<){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,a=n+f,u=!1;a!==e;){if(null!=ur){u=!0;break}a+=f}if(!u)return!0}}return!1}function d®{return h(N®,lr)}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;i&&(r=i.type in r?r+1:1,i.type===B&&e.push(t),n++)}if(2===n)return!0;if(3===n&&(1===r||1===r))return!0;if(n===r+2){for(var f=0,a=e.length,o=0;a>o;o++)f+=e;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 in e?e+1:1,e>=3&&(n=!0),!r.length)break;S(r.pop())}return n}function A®{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®{var e=sr,n=N(e);if(A®,ur=ur,ur=null,r.flags&er.EP_CAPTURE&&(sr===Q?ur=null:ur=null),r.flags&er.PROMOTION&&(ur={type:r.promotion,color:e}),ur.type===F){if(lr[ur.color]=r.to,r.flags&er.KSIDE_CASTLE){var t=r.to-1,o=r.to+1;ur=ur,ur=null}else if(r.flags&er.QSIDE_CASTLE){var t=r.to+1,o=r.to-2;ur=ur,ur=null}pr=“”}if(pr)for(var i=0,f=ar.length;f>i;i++)if(r.from===ar[i].square&&pr&ar[i].flag){pr^=ar[i].flag;break}if(pr)for(var i=0,f=ar.length;f>i;i++)if(r.to===ar[i].square&&pr&ar[i].flag){pr^=ar[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=ur,ur.type=e.piece,ur=null,e.flags&er.CAPTURE)ur={type:e.captured,color:t};else if(e.flags&er.EP_CAPTURE){var o;o=n===Q?e.to-16:e.to+16,ur={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=ur,ur=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.from,v=n.to,g=n.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”+“ |”),null==ur)r+=“ . ”;else{var n=ur.type,t=ur.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 hn”}function P(r,e){var n=g®;if(e){var t=n.match(/()?([a-h])x?-?([1-8])()?/);if(t)var o=t,i=t,f=t,a=t}for(var u=c(),l=0,s=u.length;s>l;l++){if(n===g(v(u))||e&&n===g(v(u,!0)))return u;if(!(!t||o&&o.toLowerCase()!=u.piece||fr!=u.from||fr!=u.to||a&&a.toLowerCase()!=u.promotion))return u}return null}function w®{return r>>4}function L®{return 15&r}function R®{var e=L®,n=w®;return“abcdefgh”.substring(e,e+1)+“87654321”.substring(n,n+1)}function N®{return r===U?Q:U}function O®{return-1!==“0123456789”.indexOf®}function k®{var e=q®;e.san=v(e,!1),e.to=R(e.to),e.from=R(e.from);var n=“”;for(var t in er)er&e.flags&&(n+=rr);return e.flags=n,e}function q®{var e=r instanceof Array?[]:{};for(var n in r)“object”==typeof n?e=q(r):e=r;return e}function D®{return r.replace(/^s+|s+$/g,“”)}function K®{for(var e=c({legal:!1}),n=0,t=sr,o=0,i=e.length;i>o;o++){if(S(e),!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=,z={b:,w:},J={n:,b:,r:,q:,k:},V=,X=,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:,b:},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®{return t®},reset:function(){return n()},moves:function®{for(var e=c®,n=[],t=0,o=e.length;o>t;t++)n.push(“undefined”!=typeof r&&“verbose”in r&&r.verbose?k(e):v(e,!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®{return o®},fen:function(){return i()},pgn:function®{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.length>n&&0!==i?(“ ”===t&&t.pop(),t.push(e),s=0):0!==i&&(t.push(“ ”),s++),t.push(a),s+=a.length;return t.join(“”)},load_pgn:function(r,e){function o®{return r.replace(/\/g,“\”)}function i®{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.replace(/^[([A-Za-z]*)s.*]$/,“$1”),a=i.replace(/^[+s“(.*)”]$/,“$1”),D(f).length>0&&(t=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)+“)*1.(”+o(l)+“|.)*$”,“g”),p=r.replace(s,“$1”);“[”!==p&&(p=“”),n();var c=a(p,e);for(var v in c)f([v,c]);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,u),null==E)return!1;S(E)}if(E=d,Z.indexOf(E)>-1)i(dr)&&“undefined”==typeof dr.Result&&f();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.from)||r.to!==R(o.to)||“promotion”in o&&r.promotion!==o.promotion)){t=o;break}if(!t)return null;var a=k(t);return S(t),a},undo:function(){var r=C();return r?k®:null},clear:function(){return e()},put:function(r,e){return l(r,e)},get:function®{return u®},remove:function®{return s®},perft:function®{return K®},square_color:function®{if(r in fr){var e=fr;return(w(e)+L(e))%2===0?“light”:“dark”}return null},history:function®{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});