knightmare / static /js /chess.js
Dennis Vink
Knightmare code
f7b47a4
/**
* Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.39.0.
* Original file: /npm/chess.js@1.0.0-beta.1/dist/chess.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
var t={};!function(t){
/**
* @license
* Copyright (c) 2023, Jeff Hlywa (jhlywa@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
Object.defineProperty(t,"__esModule",{value:!0}),t.Chess=t.validateFen=t.SQUARES=t.DEFAULT_POSITION=t.KING=t.QUEEN=t.ROOK=t.BISHOP=t.KNIGHT=t.PAWN=t.BLACK=t.WHITE=void 0,t.WHITE="w",t.BLACK="b",t.PAWN="p",t.KNIGHT="n",t.BISHOP="b",t.ROOK="r",t.QUEEN="q",t.KING="k",t.DEFAULT_POSITION="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";const e=-1,i={NORMAL:"n",CAPTURE:"c",BIG_PAWN:"b",EP_CAPTURE:"e",PROMOTION:"p",KSIDE_CASTLE:"k",QSIDE_CASTLE:"q"};t.SQUARES=["a8","b8","c8","d8","e8","f8","g8","h8","a7","b7","c7","d7","e7","f7","g7","h7","a6","b6","c6","d6","e6","f6","g6","h6","a5","b5","c5","d5","e5","f5","g5","h5","a4","b4","c4","d4","e4","f4","g4","h4","a3","b3","c3","d3","e3","f3","g3","h3","a2","b2","c2","d2","e2","f2","g2","h2","a1","b1","c1","d1","e1","f1","g1","h1"];const s={NORMAL:1,CAPTURE:2,BIG_PAWN:4,EP_CAPTURE:8,PROMOTION:16,KSIDE_CASTLE:32,QSIDE_CASTLE:64},o={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},r={b:[16,32,17,15],w:[-16,-32,-17,-15]},n={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]},h=[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],a=[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],l={p:1,n:2,b:4,r:8,q:16,k:32},c=[t.KNIGHT,t.BISHOP,t.ROOK,t.QUEEN],_={w:[{square:o.a1,flag:s.QSIDE_CASTLE},{square:o.h1,flag:s.KSIDE_CASTLE}],b:[{square:o.a8,flag:s.QSIDE_CASTLE},{square:o.h8,flag:s.KSIDE_CASTLE}]},f={b:1,w:6},u=["1-0","0-1","1/2-1/2","*"];function d(t){return t>>4}function p(t){return 15&t}function g(t){return-1!=="0123456789".indexOf(t)}function m(t){const e=p(t),i=d(t);return"abcdefgh".substring(e,e+1)+"87654321".substring(i,i+1)}function b(e){return e===t.WHITE?t.BLACK:t.WHITE}function E(t){const e=t.split(/\s+/);if(6!==e.length)return{ok:!1,error:"Invalid FEN: must contain six space-delimited fields"};const i=parseInt(e[5],10);if(isNaN(i)||i<=0)return{ok:!1,error:"Invalid FEN: move number must be a positive integer"};const s=parseInt(e[4],10);if(isNaN(s)||s<0)return{ok:!1,error:"Invalid FEN: half move counter number must be a non-negative integer"};if(!/^(-|[abcdefgh][36])$/.test(e[3]))return{ok:!1,error:"Invalid FEN: en-passant square is invalid"};if(/[^kKqQ-]/.test(e[2]))return{ok:!1,error:"Invalid FEN: castling availability is invalid"};if(!/^(w|b)$/.test(e[1]))return{ok:!1,error:"Invalid FEN: side-to-move is invalid"};const o=e[0].split("/");if(8!==o.length)return{ok:!1,error:"Invalid FEN: piece data does not contain 8 '/'-delimited rows"};for(let t=0;t<o.length;t++){let e=0,i=!1;for(let s=0;s<o[t].length;s++)if(g(o[t][s])){if(i)return{ok:!1,error:"Invalid FEN: piece data is invalid (consecutive number)"};e+=parseInt(o[t][s],10),i=!0}else{if(!/^[prnbqkPRNBQK]$/.test(o[t][s]))return{ok:!1,error:"Invalid FEN: piece data is invalid (invalid piece)"};e+=1,i=!1}if(8!==e)return{ok:!1,error:"Invalid FEN: piece data is invalid (too many squares in rank)"}}if("3"==e[3][1]&&"w"==e[1]||"6"==e[3][1]&&"b"==e[1])return{ok:!1,error:"Invalid FEN: illegal en-passant square"};const r=[{color:"white",regex:/K/g},{color:"black",regex:/k/g}];for(const{color:t,regex:i}of r){if(!i.test(e[0]))return{ok:!1,error:`Invalid FEN: missing ${t} king`};if((e[0].match(i)||[]).length>1)return{ok:!1,error:`Invalid FEN: too many ${t} kings`}}return{ok:!0}}function v(e,i,o,r,n,h=void 0,a=s.NORMAL){const l=d(r);if(n!==t.PAWN||7!==l&&0!==l)e.push({color:i,from:o,to:r,piece:n,captured:h,flags:a});else for(let t=0;t<c.length;t++){const l=c[t];e.push({color:i,from:o,to:r,piece:n,captured:h,promotion:l,flags:a|s.PROMOTION})}}function k(e){let i=e.charAt(0);if(i>="a"&&i<="h"){if(e.match(/[a-h]\d.*[a-h]\d/))return;return t.PAWN}return i=i.toLowerCase(),"o"===i?t.KING:i}function I(t){return t.replace(/=/,"").replace(/[+#]?[?!]*$/,"")}t.validateFen=E;t.Chess=class{constructor(i=t.DEFAULT_POSITION){this._board=new Array(128),this._turn=t.WHITE,this._header={},this._kings={w:e,b:e},this._epSquare=-1,this._halfMoves=0,this._moveNumber=0,this._history=[],this._comments={},this._castling={w:0,b:0},this.load(i)}clear(i=!1){this._board=new Array(128),this._kings={w:e,b:e},this._turn=t.WHITE,this._castling={w:0,b:0},this._epSquare=e,this._halfMoves=0,this._moveNumber=1,this._history=[],this._comments={},this._header=i?this._header:{},this._updateSetup(this.fen())}load(i,r=!1){let n=i.split(/\s+/);if(n.length>=2&&n.length<6){const t=["-","-","0","1"];i=n.concat(t.slice(-(6-n.length))).join(" ")}n=i.split(/\s+/);const{ok:h,error:a}=E(i);if(!h)throw new Error(a);const l=n[0];let c=0;this.clear(r);for(let e=0;e<l.length;e++){const i=l.charAt(e);if("/"===i)c+=8;else if(g(i))c+=parseInt(i,10);else{const e=i<"a"?t.WHITE:t.BLACK;this.put({type:i.toLowerCase(),color:e},m(c)),c++}}this._turn=n[1],n[2].indexOf("K")>-1&&(this._castling.w|=s.KSIDE_CASTLE),n[2].indexOf("Q")>-1&&(this._castling.w|=s.QSIDE_CASTLE),n[2].indexOf("k")>-1&&(this._castling.b|=s.KSIDE_CASTLE),n[2].indexOf("q")>-1&&(this._castling.b|=s.QSIDE_CASTLE),this._epSquare="-"===n[3]?e:o[n[3]],this._halfMoves=parseInt(n[4],10),this._moveNumber=parseInt(n[5],10),this._updateSetup(this.fen())}fen(){var i,r;let n=0,h="";for(let e=o.a8;e<=o.h1;e++){if(this._board[e]){n>0&&(h+=n,n=0);const{color:i,type:s}=this._board[e];h+=i===t.WHITE?s.toUpperCase():s.toLowerCase()}else n++;e+1&136&&(n>0&&(h+=n),e!==o.h1&&(h+="/"),n=0,e+=8)}let a="";this._castling[t.WHITE]&s.KSIDE_CASTLE&&(a+="K"),this._castling[t.WHITE]&s.QSIDE_CASTLE&&(a+="Q"),this._castling[t.BLACK]&s.KSIDE_CASTLE&&(a+="k"),this._castling[t.BLACK]&s.QSIDE_CASTLE&&(a+="q"),a=a||"-";let l="-";if(this._epSquare!==e){const e=this._epSquare+(this._turn===t.WHITE?16:-16),o=[e+1,e-1];for(const e of o){if(136&e)continue;const o=this._turn;if((null===(i=this._board[e])||void 0===i?void 0:i.color)===o&&(null===(r=this._board[e])||void 0===r?void 0:r.type)===t.PAWN){this._makeMove({color:o,from:e,to:this._epSquare,piece:t.PAWN,captured:t.PAWN,flags:s.EP_CAPTURE});const i=!this._isKingAttacked(o);if(this._undoMove(),i){l=m(this._epSquare);break}}}}return[h,this._turn,a,l,this._halfMoves,this._moveNumber].join(" ")}_updateSetup(e){this._history.length>0||(e!==t.DEFAULT_POSITION?(this._header.SetUp="1",this._header.FEN=e):(delete this._header.SetUp,delete this._header.FEN))}reset(){this.load(t.DEFAULT_POSITION)}get(t){return this._board[o[t]]||!1}put({type:i,color:s},r){if(-1==="pnbrqkPNBRQK".indexOf(i.toLowerCase()))return!1;if(!(r in o))return!1;const n=o[r];return(i!=t.KING||this._kings[s]==e||this._kings[s]==n)&&(this._board[n]={type:i,color:s},i===t.KING&&(this._kings[s]=n),this._updateSetup(this.fen()),!0)}remove(i){const s=this.get(i);return delete this._board[o[i]],s&&s.type===t.KING&&(this._kings[s.color]=e),this._updateSetup(this.fen()),s}_attacked(e,i){for(let s=o.a8;s<=o.h1;s++){if(136&s){s+=7;continue}if(void 0===this._board[s]||this._board[s].color!==e)continue;const o=this._board[s],r=s-i;if(0===r)continue;const n=r+119;if(h[n]&l[o.type]){if(o.type===t.PAWN){if(r>0){if(o.color===t.WHITE)return!0}else if(o.color===t.BLACK)return!0;continue}if("n"===o.type||"k"===o.type)return!0;const e=a[n];let h=s+e,l=!1;for(;h!==i;){if(null!=this._board[h]){l=!0;break}h+=e}if(!l)return!0}}return!1}_isKingAttacked(t){return this._attacked(b(t),this._kings[t])}isAttacked(t,e){return this._attacked(e,o[t])}isCheck(){return this._isKingAttacked(this._turn)}inCheck(){return this.isCheck()}isCheckmate(){return this.isCheck()&&0===this._moves().length}isStalemate(){return!this.isCheck()&&0===this._moves().length}isInsufficientMaterial(){const e={b:0,n:0,r:0,q:0,k:0,p:0},i=[];let s=0,r=0;for(let n=o.a8;n<=o.h1;n++){if(r=(r+1)%2,136&n){n+=7;continue}const o=this._board[n];o&&(e[o.type]=o.type in e?e[o.type]+1:1,o.type===t.BISHOP&&i.push(r),s++)}if(2===s)return!0;if(3===s&&(1===e[t.BISHOP]||1===e[t.KNIGHT]))return!0;if(s===e[t.BISHOP]+2){let t=0;const e=i.length;for(let s=0;s<e;s++)t+=i[s];if(0===t||t===e)return!0}return!1}isThreefoldRepetition(){const t=[],e={};let i=!1;for(;;){const e=this._undoMove();if(!e)break;t.push(e)}for(;;){const s=this.fen().split(" ").slice(0,4).join(" ");e[s]=s in e?e[s]+1:1,e[s]>=3&&(i=!0);const o=t.pop();if(!o)break;this._makeMove(o)}return i}isDraw(){return this._halfMoves>=100||this.isStalemate()||this.isInsufficientMaterial()||this.isThreefoldRepetition()}isGameOver(){return this.isCheckmate()||this.isStalemate()||this.isDraw()}moves({verbose:t=!1,square:e}={}){const i=this._moves({square:e});return t?i.map((t=>this._makePretty(t))):i.map((t=>this._moveToSan(t,i)))}_moves({legal:e=!0,piece:i,square:h}={}){var a;const l=h?h.toLowerCase():void 0,c=null==i?void 0:i.toLowerCase(),_=[],u=this._turn,p=b(u);let g=o.a8,m=o.h1,E=!1;if(l){if(!(l in o))return[];g=m=o[l],E=!0}for(let e=g;e<=m;e++){if(136&e){e+=7;continue}if(!this._board[e]||this._board[e].color===p)continue;const{type:i}=this._board[e];let o;if(i===t.PAWN){if(c&&c!==i)continue;o=e+r[u][0],this._board[o]||(v(_,u,e,o,t.PAWN),o=e+r[u][1],f[u]!==d(e)||this._board[o]||v(_,u,e,o,t.PAWN,void 0,s.BIG_PAWN));for(let i=2;i<4;i++)o=e+r[u][i],136&o||((null===(a=this._board[o])||void 0===a?void 0:a.color)===p?v(_,u,e,o,t.PAWN,this._board[o].type,s.CAPTURE):o===this._epSquare&&v(_,u,e,o,t.PAWN,t.PAWN,s.EP_CAPTURE))}else{if(c&&c!==i)continue;for(let r=0,h=n[i].length;r<h;r++){const h=n[i][r];for(o=e;o+=h,!(136&o);){if(this._board[o]){if(this._board[o].color===u)break;v(_,u,e,o,i,this._board[o].type,s.CAPTURE);break}if(v(_,u,e,o,i),i===t.KNIGHT||i===t.KING)break}}}}if(!(void 0!==c&&c!==t.KING||E&&m!==this._kings[u])){if(this._castling[u]&s.KSIDE_CASTLE){const e=this._kings[u],i=e+2;this._board[e+1]||this._board[i]||this._attacked(p,this._kings[u])||this._attacked(p,e+1)||this._attacked(p,i)||v(_,u,this._kings[u],i,t.KING,void 0,s.KSIDE_CASTLE)}if(this._castling[u]&s.QSIDE_CASTLE){const e=this._kings[u],i=e-2;this._board[e-1]||this._board[e-2]||this._board[e-3]||this._attacked(p,this._kings[u])||this._attacked(p,e-1)||this._attacked(p,i)||v(_,u,this._kings[u],i,t.KING,void 0,s.QSIDE_CASTLE)}}if(!e)return _;const k=[];for(let t=0,e=_.length;t<e;t++)this._makeMove(_[t]),this._isKingAttacked(u)||k.push(_[t]),this._undoMove();return k}move(t,{strict:e=!1}={}){let i=null;if("string"==typeof t)i=this._moveFromSan(t,e);else if("object"==typeof t){const e=this._moves();for(let s=0,o=e.length;s<o;s++)if(t.from===m(e[s].from)&&t.to===m(e[s].to)&&(!("promotion"in e[s])||t.promotion===e[s].promotion)){i=e[s];break}}if(!i)throw"string"==typeof t?new Error(`Invalid move: ${t}`):new Error(`Invalid move: ${JSON.stringify(t)}`);const s=this._makePretty(i);return this._makeMove(i),s}_push(t){this._history.push({move:t,kings:{b:this._kings.b,w:this._kings.w},turn:this._turn,castling:{b:this._castling.b,w:this._castling.w},epSquare:this._epSquare,halfMoves:this._halfMoves,moveNumber:this._moveNumber})}_makeMove(i){const o=this._turn,r=b(o);if(this._push(i),this._board[i.to]=this._board[i.from],delete this._board[i.from],i.flags&s.EP_CAPTURE&&(this._turn===t.BLACK?delete this._board[i.to-16]:delete this._board[i.to+16]),i.promotion&&(this._board[i.to]={type:i.promotion,color:o}),this._board[i.to].type===t.KING){if(this._kings[o]=i.to,i.flags&s.KSIDE_CASTLE){const t=i.to-1,e=i.to+1;this._board[t]=this._board[e],delete this._board[e]}else if(i.flags&s.QSIDE_CASTLE){const t=i.to+1,e=i.to-2;this._board[t]=this._board[e],delete this._board[e]}this._castling[o]=0}if(this._castling[o])for(let t=0,e=_[o].length;t<e;t++)if(i.from===_[o][t].square&&this._castling[o]&_[o][t].flag){this._castling[o]^=_[o][t].flag;break}if(this._castling[r])for(let t=0,e=_[r].length;t<e;t++)if(i.to===_[r][t].square&&this._castling[r]&_[r][t].flag){this._castling[r]^=_[r][t].flag;break}i.flags&s.BIG_PAWN?o===t.BLACK?this._epSquare=i.to-16:this._epSquare=i.to+16:this._epSquare=e,i.piece===t.PAWN||i.flags&(s.CAPTURE|s.EP_CAPTURE)?this._halfMoves=0:this._halfMoves++,o===t.BLACK&&this._moveNumber++,this._turn=r}undo(){const t=this._undoMove();return t?this._makePretty(t):null}_undoMove(){const e=this._history.pop();if(void 0===e)return null;const i=e.move;this._kings=e.kings,this._turn=e.turn,this._castling=e.castling,this._epSquare=e.epSquare,this._halfMoves=e.halfMoves,this._moveNumber=e.moveNumber;const o=this._turn,r=b(o);if(this._board[i.from]=this._board[i.to],this._board[i.from].type=i.piece,delete this._board[i.to],i.captured)if(i.flags&s.EP_CAPTURE){let e;e=o===t.BLACK?i.to-16:i.to+16,this._board[e]={type:t.PAWN,color:r}}else this._board[i.to]={type:i.captured,color:r};if(i.flags&(s.KSIDE_CASTLE|s.QSIDE_CASTLE)){let t,e;i.flags&s.KSIDE_CASTLE?(t=i.to+1,e=i.to-1):(t=i.to-2,e=i.to+1),this._board[t]=this._board[e],delete this._board[e]}return i}pgn({newline:t="\n",maxWidth:e=0}={}){const i=[];let s=!1;for(const e in this._header)i.push("["+e+' "'+this._header[e]+'"]'+t),s=!0;s&&this._history.length&&i.push(t);const o=t=>{const e=this._comments[this.fen()];if(void 0!==e){t=`${t}${t.length>0?" ":""}{${e}}`}return t},r=[];for(;this._history.length>0;)r.push(this._undoMove());const n=[];let h="";for(0===r.length&&n.push(o(""));r.length>0;){h=o(h);const t=r.pop();if(!t)break;if(this._history.length||"b"!==t.color)"w"===t.color&&(h.length&&n.push(h),h=this._moveNumber+".");else{const t=`${this._moveNumber}. ...`;h=h?`${h} ${t}`:t}h=h+" "+this._moveToSan(t,this._moves({legal:!0})),this._makeMove(t)}if(h.length&&n.push(o(h)),void 0!==this._header.Result&&n.push(this._header.Result),0===e)return i.join("")+n.join(" ");const a=function(){return i.length>0&&" "===i[i.length-1]&&(i.pop(),!0)},l=function(s,o){for(const r of o.split(" "))if(r){if(s+r.length>e){for(;a();)s--;i.push(t),s=0}i.push(r),s+=r.length,i.push(" "),s++}return a()&&s--,s};let c=0;for(let s=0;s<n.length;s++)c+n[s].length>e&&n[s].includes("{")?c=l(c,n[s]):(c+n[s].length>e&&0!==s?(" "===i[i.length-1]&&i.pop(),i.push(t),c=0):0!==s&&(i.push(" "),c++),i.push(n[s]),c+=n[s].length);return i.join("")}header(...t){for(let e=0;e<t.length;e+=2)"string"==typeof t[e]&&"string"==typeof t[e+1]&&(this._header[t[e]]=t[e+1]);return this._header}loadPgn(t,{strict:e=!1,newlineChar:i="\r?\n"}={}){function s(t){return t.replace(/\\/g,"\\")}t=t.trim();const o=new RegExp("^(\\[((?:"+s(i)+")|.)*\\])((?:\\s*"+s(i)+"){2}|(?:\\s*"+s(i)+")*$)").exec(t),r=o&&o.length>=2?o[1]:"";this.reset();const n=function(t){const e={},o=t.split(new RegExp(s(i)));let r="",n="";for(let t=0;t<o.length;t++){const i=/^\s*\[\s*([A-Za-z]+)\s*"(.*)"\s*\]\s*$/;r=o[t].replace(i,"$1"),n=o[t].replace(i,"$2"),r.trim().length>0&&(e[r]=n)}return e}(r);let h="";for(const t in n)"fen"===t.toLowerCase()&&(h=n[t]),this.header(t,n[t]);if(e){if("1"===n.SetUp){if(!("FEN"in n))throw new Error("Invalid PGN: FEN tag must be supplied with SetUp tag");this.load(n.FEN,!0)}}else h&&this.load(h,!0);const a=function(t){return`{${function(t){return Array.from(t).map((function(t){return t.charCodeAt(0)<128?t.charCodeAt(0).toString(16):encodeURIComponent(t).replace(/%/g,"").toLowerCase()})).join("")}((t=t.replace(new RegExp(s(i),"g")," ")).slice(1,t.length-1))}}`},l=function(t){if(t.startsWith("{")&&t.endsWith("}"))return function(t){return 0==t.length?"":decodeURIComponent("%"+(t.match(/.{1,2}/g)||[]).join("%"))}(t.slice(1,t.length-1))};let c=t.replace(r,"").replace(new RegExp(`({[^}]*})+?|;([^${s(i)}]*)`,"g"),(function(t,e,i){return void 0!==e?a(e):" "+a(`{${i.slice(1)}}`)})).replace(new RegExp(s(i),"g")," ");const _=/(\([^()]+\))+?/g;for(;_.test(c);)c=c.replace(_,"");c=c.replace(/\d+\.(\.\.)?/g,""),c=c.replace(/\.\.\./g,""),c=c.replace(/\$\d+/g,"");let f=c.trim().split(new RegExp(/\s+/));f=f.filter((t=>""!==t));let d="";for(let t=0;t<f.length;t++){const i=l(f[t]);if(void 0!==i){this._comments[this.fen()]=i;continue}const s=this._moveFromSan(f[t],e);if(null==s){if(!(u.indexOf(f[t])>-1))throw new Error(`Invalid move in PGN: ${f[t]}`);d=f[t]}else d="",this._makeMove(s)}d&&Object.keys(this._header).length&&!this._header.Result&&this.header("Result",d)}_moveToSan(e,i){let o="";if(e.flags&s.KSIDE_CASTLE)o="O-O";else if(e.flags&s.QSIDE_CASTLE)o="O-O-O";else{if(e.piece!==t.PAWN){const t=function(t,e){const i=t.from,s=t.to,o=t.piece;let r=0,n=0,h=0;for(let t=0,a=e.length;t<a;t++){const a=e[t].from,l=e[t].to;o===e[t].piece&&i!==a&&s===l&&(r++,d(i)===d(a)&&n++,p(i)===p(a)&&h++)}return r>0?n>0&&h>0?m(i):h>0?m(i).charAt(1):m(i).charAt(0):""}(e,i);o+=e.piece.toUpperCase()+t}e.flags&(s.CAPTURE|s.EP_CAPTURE)&&(e.piece===t.PAWN&&(o+=m(e.from)[0]),o+="x"),o+=m(e.to),e.promotion&&(o+="="+e.promotion.toUpperCase())}return this._makeMove(e),this.isCheck()&&(this.isCheckmate()?o+="#":o+="+"),this._undoMove(),o}_moveFromSan(t,e=!1){const i=I(t);let s,r,n,h,a,l=k(i),c=this._moves({legal:!0,piece:l});for(let t=0,e=c.length;t<e;t++)if(i===I(this._moveToSan(c[t],c)))return c[t];if(e)return null;let _=!1;r=i.match(/([pnbrqkPNBRQK])?([a-h][1-8])x?-?([a-h][1-8])([qrbnQRBN])?/),r?(s=r[1],n=r[2],h=r[3],a=r[4],1==n.length&&(_=!0)):(r=i.match(/([pnbrqkPNBRQK])?([a-h]?[1-8]?)x?-?([a-h][1-8])([qrbnQRBN])?/),r&&(s=r[1],n=r[2],h=r[3],a=r[4],1==n.length&&(_=!0))),l=k(i),c=this._moves({legal:!0,piece:s||l});for(let t=0,e=c.length;t<e;t++)if(n&&h){if(!(s&&s.toLowerCase()!=c[t].piece||o[n]!=c[t].from||o[h]!=c[t].to||a&&a.toLowerCase()!=c[t].promotion))return c[t];if(_){const e=m(c[t].from);if(!(s&&s.toLowerCase()!=c[t].piece||o[h]!=c[t].to||n!=e[0]&&n!=e[1]||a&&a.toLowerCase()!=c[t].promotion))return c[t]}}return null}ascii(){let e=" +------------------------+\n";for(let i=o.a8;i<=o.h1;i++){if(0===p(i)&&(e+=" "+"87654321"[d(i)]+" |"),this._board[i]){const s=this._board[i].type;e+=" "+(this._board[i].color===t.WHITE?s.toUpperCase():s.toLowerCase())+" "}else e+=" . ";i+1&136&&(e+="|\n",i+=8)}return e+=" +------------------------+\n",e+=" a b c d e f g h",e}perft(t){const e=this._moves({legal:!1});let i=0;const s=this._turn;for(let o=0,r=e.length;o<r;o++)this._makeMove(e[o]),this._isKingAttacked(s)||(t-1>0?i+=this.perft(t-1):i++),this._undoMove();return i}_makePretty(t){const{color:e,piece:o,from:r,to:n,flags:h,captured:a,promotion:l}=t;let c="";for(const t in s)s[t]&h&&(c+=i[t]);const _=m(r),f=m(n),u={color:e,piece:o,from:_,to:f,san:this._moveToSan(t,this._moves({legal:!0})),flags:c,lan:_+f};return a&&(u.captured=a),l&&(u.promotion=l,u.lan+=l),u}turn(){return this._turn}board(){const t=[];let e=[];for(let i=o.a8;i<=o.h1;i++)null==this._board[i]?e.push(null):e.push({square:m(i),type:this._board[i].type,color:this._board[i].color}),i+1&136&&(t.push(e),e=[],i+=8);return t}squareColor(t){if(t in o){const e=o[t];return(d(e)+p(e))%2==0?"light":"dark"}return null}history({verbose:t=!1}={}){const e=[],i=[];for(;this._history.length>0;)e.push(this._undoMove());for(;;){const s=e.pop();if(!s)break;t?i.push(Object.assign({fen:this.fen()},this._makePretty(s))):i.push(this._moveToSan(s,this._moves())),this._makeMove(s)}return i}_pruneComments(){const t=[],e={},i=t=>{t in this._comments&&(e[t]=this._comments[t])};for(;this._history.length>0;)t.push(this._undoMove());for(i(this.fen());;){const e=t.pop();if(!e)break;this._makeMove(e),i(this.fen())}this._comments=e}getComment(){return this._comments[this.fen()]}setComment(t){this._comments[this.fen()]=t.replace("{","[").replace("}","]")}deleteComment(){const t=this._comments[this.fen()];return delete this._comments[this.fen()],t}getComments(){return this._pruneComments(),Object.keys(this._comments).map((t=>({fen:t,comment:this._comments[t]})))}deleteComments(){return this._pruneComments(),Object.keys(this._comments).map((t=>{const e=this._comments[t];return delete this._comments[t],{fen:t,comment:e}}))}}}(t);var e=t.BISHOP,i=t.BLACK,s=t.Chess,o=t.DEFAULT_POSITION,r=t.KING,n=t.KNIGHT,h=t.PAWN,a=t.QUEEN,l=t.ROOK,c=t.SQUARES,_=t.WHITE,f=t.__esModule,u=t.validateFen;export{e as BISHOP,i as BLACK,s as Chess,o as DEFAULT_POSITION,r as KING,n as KNIGHT,h as PAWN,a as QUEEN,l as ROOK,c as SQUARES,_ as WHITE,f as __esModule,t as default,u as validateFen};