chessgroundx
Version:
Extended lichess.org Chess UI
1 lines • 53.1 kB
JavaScript
var Chessground=function(){"use strict";var e;!function(e){e[e.ALGEBRAIC=0]="ALGEBRAIC",e[e.SHOGI_ENGLET=1]="SHOGI_ENGLET",e[e.SHOGI_ARBNUM=2]="SHOGI_ARBNUM",e[e.SHOGI_HANNUM=3]="SHOGI_HANNUM",e[e.JANGGI=4]="JANGGI",e[e.XIANGQI_ARBNUM=5]="XIANGQI_ARBNUM",e[e.XIANGQI_HANNUM=6]="XIANGQI_HANNUM",e[e.THAI_ALGEBRAIC=7]="THAI_ALGEBRAIC"}(e||(e={}));const t=["white","black"],r=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],n=["1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@"],o=[...n].reverse();function c(e){return Array.prototype.concat(...(t=e.width,r.slice(0,t)).map((t=>function(e){return n.slice(0,e)}(e.height).map((e=>t+e)))));var t}function i(e){return c(e).map(a)}const s=e=>r[e[0]]+n[e[1]],a=e=>[e.charCodeAt(0)-97,e.charCodeAt(1)-49];function u(e){return e.replace("+","p").replace("*","_").replace("@","").toLowerCase()+"-piece"}function l(e,t=!1){const r=e.slice(0,e.indexOf("-")),n=(r.length>1?r.replace("p","+"):r).replace("_","*");return t?n.toUpperCase():n}function d(e){return l(e,!0)+"@"}function p(e){return e[0]===e[0].toUpperCase()}function h(e){return"string"==typeof e&&e[0]===e[0].toLowerCase()}function f(e){return"string"!=typeof e}function g(e,t){return f(e)&&f(t)?w(e,t):e===t}function b(e,t,r){var n;e.set(t,(null!==(n=e.get(t))&&void 0!==n?n:0)+r)}const m=()=>{let e;return{start(){e=performance.now()},cancel(){e=void 0},stop(){if(!e)return 0;const t=performance.now()-e;return e=void 0,t}}},v=e=>"white"===e?"black":"white",w=(e,t)=>e.role===t.role&&e.color===t.color&&!!e.promoted==!!t.promoted,k=(e,t)=>`${e.color} ${((e,t)=>e.color===t?"ally":"enemy")(e,t)} ${e.mirror?"mirror":""} ${e.promoted?"promoted ":""}${e.role}`,y=(e,t)=>{const r=e[0]-t[0],n=e[1]-t[1];return r*r+n*n},S=(e,t)=>(r,n)=>[(n?r[0]:t.width-1-r[0])*e.width/t.width,(n?t.height-1-r[1]:r[1])*e.height/t.height],C=(e,t)=>{e.style.transform=`translate(${t[0]}px,${t[1]}px)`},A=(e,t,r=1)=>{e.style.transform=`translate(${t[0]}px,${t[1]}px) scale(${r})`},M=(e,t)=>{e.style.visibility=t?"visible":"hidden"},x=e=>{var t;return e.clientX||0===e.clientX?[e.clientX,e.clientY]:(null===(t=e.targetTouches)||void 0===t?void 0:t[0])?[e.targetTouches[0].clientX,e.targetTouches[0].clientY]:void 0},P=e=>2===e.buttons||2===e.button,N=(e,t)=>{const r=document.createElement(e);return t&&(r.className=t),r};function I(e,t,r,n){const o=a(e);return t||(o[0]=n.width-1-o[0],o[1]=n.height-1-o[1]),[r.left+r.width*(o[0]+.5)/n.width,r.top+r.height*(n.height-o[1]-.5)/n.height]}const L=(e,t)=>Math.abs(e-t),B=e=>(t,r,n,o)=>L(t,n)<2&&("white"===e?o===r+1||r<=1&&o===r+2&&t===n:o===r-1||r>=6&&o===r-2&&t===n),T=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return 1===o&&2===c||2===o&&1===c},O=(e,t,r,n)=>L(e,r)===L(t,n),R=(e,t,r,n)=>e===r||t===n,q=(e,t,r,n)=>O(e,t,r,n)||R(e,t,r,n),G=(e,t,r)=>(n,o,c,i)=>L(n,c)<2&&L(o,i)<2||r&&o===i&&o===("white"===e?0:7)&&(4===n&&(2===c&&t.includes(0)||6===c&&t.includes(7))||t.includes(c));function H(e,t){const r="white"===t?"1":"8",n=[];for(const[o,c]of e)o[1]===r&&c.color===t&&"r-piece"===c.role&&n.push(a(o)[0]);return n}function $(...e){return(t,r,n,o)=>e.map((e=>e(t,r,n,o))).reduce(((e,t)=>e&&t))}const K=function(){const e={};return t=>{const r=`${t}`;return r in e||(e[r]=function(e){return(t,r,n,o)=>Math.max(L(t,n),L(r,o))<=e}(t)),e[r]}}();function E(e){return"white"===e?0:7}const j=(e,t,r,n)=>L(e,r)<2&&L(t,n)<2;function _(e,t,r){return(n,o,c,i)=>j(n,o,c,i)||r&&o===i&&o===E(e)&&t.includes(c)}function D(e){return(t,r,n,o)=>L(t,n)<2&&("white"===e?o===r+1:o===r-1)}function F(e){return(t,r,n,o)=>L(t,n)<2&&("white"===e?o===r+1||r<=2&&o===r+2&&t===n:o===r-1||r>=7&&o===r-2&&t===n)}const U=(e,t,r,n)=>L(e,r)<=2&&L(t,n)<=2,W=(e,t,r,n)=>t===n&&L(e,r)<=1,z=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return 1===o&&0===c||0===o&&1===c},V=(e,t,r,n)=>L(e,r)===L(t,n)&&1===L(e,r),X=(e,t,r,n)=>L(e,r)===L(t,n)&&L(e,r)<3;const Q=(e,t,r,n)=>{const o=L(e,r);return o===L(t,n)&&2===o},Z=(e,t,r,n)=>O(e,t,r,n)||T(e,t,r,n),Y=(e,t,r,n)=>R(e,t,r,n)||T(e,t,r,n),J=(e,t,r,n)=>O(e,t,r,n)||R(e,t,r,n)||T(e,t,r,n),ee=(e,t,r,n)=>O(e,t,r,n)||T(e,t,r,n)||z(e,t,r,n),te=(e,t,r,n)=>R(e,t,r,n)||T(e,t,r,n)||V(e,t,r,n),re=(e,t,r,n)=>j(e,t,r,n)||T(e,t,r,n);function ne(e){return(t,r,n,o)=>n===t&&("white"===e?o>r:o<r)}function oe(e){return(t,r,n,o)=>V(t,r,n,o)||t===n&&("white"===e?o===r+1:o===r-1)}function ce(e){return(t,r,n,o)=>z(t,r,n,o)||L(t,n)<2&&("white"===e?o===r+1:o===r-1)}function ie(e){return(t,r,n,o)=>n===t&&("white"===e?o===r+1:o===r-1)}function se(e){return(t,r,n,o)=>(n===t-1||n===t+1)&&("white"===e?o===r+2:o===r-2)}const ae=(e,t,r,n)=>R(e,t,r,n)||V(e,t,r,n),ue=(e,t,r,n)=>O(e,t,r,n)||z(e,t,r,n),le=(e,t,r,n)=>R(e,t,r,n)||X(e,t,r,n),de=(e,t,r,n)=>O(e,t,r,n)||((e,t,r,n)=>e===r&&L(t,n)<3||t===n&&L(e,r)<3)(e,t,r,n);const pe=function(){const e={};return(t,r)=>{const n=`${t.width}x${t.height}${r.slice(0,1)}`;return n in e||(e[n]=function(e,t){const r=Math.floor(e.width/2),n="white"===t?0:e.height-3;return[[r-1,n+2],[r,n+2],[r+1,n+2],[r-1,n+1],[r,n+1],[r+1,n+1],[r-1,n],[r,n],[r+1,n]]}(t,r)),e[n]}}();function he(e){return(t,r,n,o)=>n===t&&("white"===e?o===r+1:o===r-1)||o===r&&L(t,n)<2}function fe(e,t){const r=pe(t,e);return(e,t,n,o)=>z(e,t,n,o)&&r.some((e=>e[0]===n&&e[1]===o))}const ge=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return 2===o&&3===c||3===o&&2===c};const be=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return!(1!==o&&2!==o||1!==c&&2!==c)},me=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return 0===o&&(2===c||3===c)||0===c&&(2===o||3===o)||o===c&&(2===o||3===o)},ve=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return 1===o||1===c||2===o&&(0===c||2===c)||0===o&&2===c},we=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return o<3&&(c<2||2===c&&0===o)},ke=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return T(e,t,r,n)||1===o&&3===c||3===o&&1===c},ye=(e,t,r,n)=>T(e,t,r,n)||q(e,t,r,n),Se=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return o===c&&o<4||0===c&&2===o||2===c&&o<2},Ce=(e,t,r,n)=>{const o=L(e,r),c=L(t,n);return o<3&&c<3&&!(1===o&&0===c)&&!(0===o&&1===c)};const Ae=(e,t,r,n)=>j(e,t,r,n)&&n!==t;const Me=()=>!1;function xe(e,t,r){const n=function(e,t,r){switch(e){case"ataxx":return(e,t)=>"p-piece"===e.pieces.get(t).role?U:Me;case"xiangqi":case"manchu":return(e,t)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return function(e){return(t,r,n,o)=>n===t&&("white"===e?o===r+1:o===r-1)||o===r&&L(t,n)<2&&("white"===e?r>4:r<5)}(c);case"c-piece":case"r-piece":return R;case"n-piece":return T;case"b-piece":return function(e){return(t,r,n,o)=>Q(t,r,n,o)&&("white"===e?o<5:o>4)}(c);case"a-piece":return function(e,t){const r=pe(t,e);return(e,t,n,o)=>V(e,t,n,o)&&r.some((e=>e[0]===n&&e[1]===o))}(c,r);case"k-piece":return fe(c,r);case"m-piece":return Y;default:return Me}};case"janggi":return(e,t)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return function(e,t){const r=pe(t,v(e));return(t,n,o,c)=>{let i;switch(r.findIndex((e=>e[0]===t&&e[1]===n))){case 0:i=(t,r,n,o)=>n===t+1&&"black"===e&&o===r-1;break;case 2:i=(t,r,n,o)=>n===t-1&&"black"===e&&o===r-1;break;case 4:i=(t,r,n,o)=>1===L(t,n)&&("white"===e?o===r+1:o===r-1);break;case 6:i=(t,r,n,o)=>n===t+1&&"white"===e&&o===r+1;break;case 8:i=(t,r,n,o)=>n===t-1&&"white"===e&&o===r+1;break;default:i=()=>!1}return he(e)(t,n,o,c)||i(t,n,o,c)}}(c,r);case"c-piece":case"r-piece":return function(e){const t=pe(e,"white"),r=pe(e,"black");return(e,n,o,c)=>{let i;const s=t.findIndex((t=>t[0]===e&&t[1]===n)),a=r.findIndex((t=>t[0]===e&&t[1]===n)),u=-1!==s?s:a,l=L(e,o),d=L(n,c);switch(u){case 0:i=(e,t,r,n)=>l===d&&r>e&&r<=e+2&&n<t&&n>=t-2;break;case 2:i=(e,t,r,n)=>l===d&&r<e&&r>=e-2&&n<t&&n>=t-2;break;case 4:i=V;break;case 6:i=(e,t,r,n)=>l===d&&r>e&&r<=e+2&&n>t&&n<=t+2;break;case 8:i=(e,t,r,n)=>l===d&&r<e&&r>=e-2&&n>t&&n<=t+2;break;default:i=()=>!1}return R(e,n,o,c)||i(e,n,o,c)}}(r);case"n-piece":return T;case"b-piece":return ge;case"a-piece":case"k-piece":return function(e,t){const r=pe(t,e);return(e,t,n,o)=>{let c;switch(r.findIndex((r=>r[0]===e&&r[1]===t))){case 0:c=(e,t,r,n)=>r===e+1&&n===t-1;break;case 2:c=(e,t,r,n)=>r===e-1&&n===t-1;break;case 4:c=V;break;case 6:c=(e,t,r,n)=>r===e+1&&n===t+1;break;case 8:c=(e,t,r,n)=>r===e-1&&n===t+1;break;default:c=()=>!1}return(z(e,t,n,o)||c(e,t,n,o))&&r.some((e=>e[0]===n&&e[1]===o))}}(c,r);default:return Me}};case"minixiangqi":return(e,t)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return he(c);case"c-piece":case"r-piece":return R;case"n-piece":return T;case"k-piece":return fe(c,r);default:return Me}};case"shogi":case"minishogi":case"gorogoro":case"gorogoroplus":case"cannonshogi":return(t,r)=>{const n=t.pieces.get(r),o=n.role,c=n.color;switch(o){case"p-piece":return"cannonshogi"===e?he(c):ie(c);case"l-piece":return ne(c);case"n-piece":return se(c);case"k-piece":return j;case"s-piece":return oe(c);case"g-piece":case"pp-piece":case"pl-piece":case"pn-piece":case"ps-piece":return ce(c);case"b-piece":case"i-piece":case"c-piece":return O;case"r-piece":case"u-piece":case"a-piece":return R;case"pr-piece":return ae;case"pb-piece":return ue;case"pu-piece":case"pa-piece":return le;case"pi-piece":case"pc-piece":return de;default:return Me}};case"kyotoshogi":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"l-piece":return ne(o);case"pl-piece":case"pn-piece":return ce(o);case"s-piece":return oe(o);case"ps-piece":return O;case"n-piece":return se(o);case"p-piece":return ie(o);case"pp-piece":return R;case"k-piece":return j;default:return Me}};case"dobutsu":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"c-piece":return ie(o);case"e-piece":return V;case"g-piece":return z;case"l-piece":return j;case"pc-piece":return ce(o);default:return Me}};case"torishogi":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"s-piece":return ie(o);case"ps-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n);return"white"===e?2===c&&o===r+2||0===c&&o===r-2:2===c&&o===r-2||0===c&&o===r+2}}(o);case"l-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n),i=L(r,o);return"white"===e?n===t&&o>r||c===i&&n>t&&o<r||n===t-1&&o===r-1:n===t&&o<r||c===i&&n<t&&o>r||n===t+1&&o===r+1}}(o);case"r-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n),i=L(r,o);return"white"===e?n===t&&o>r||c===i&&n<t&&o<r||n===t+1&&o===r-1:n===t&&o<r||c===i&&n>t&&o>r||n===t-1&&o===r+1}}(o);case"p-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n);return"white"===e?n===t&&o===r+2||1===c&&o===r-1:n===t&&o===r-2||1===c&&o===r+1}}(o);case"c-piece":return Ae;case"f-piece":return function(e){return(t,r,n,o)=>"white"===e?j(t,r,n,o)&&!(n===t&&o===r-1):j(t,r,n,o)&&!(n===t&&o===r+1)}(o);case"pf-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n),i=L(r,o);return"white"===e?j(t,r,n,o)||c===i&&(o>r||o<r&&i<=2)||n===t&&o<r:j(t,r,n,o)||c===i&&(o<r||o>r&&i<=2)||n===t&&o>r}}(o);case"k-piece":return j;default:return Me}};case"makruk":case"makpong":case"sittuyin":case"cambodian":case"asean":return(t,r,n)=>{const o=t.pieces.get(r),c=o.role,i=o.color;switch(c){case"p-piece":return"sittuyin"===e?function(e,t){return(r,n,o,c)=>{let i=("white"===t?n>=4:n<=3)&&(r===n||7-r===n);if(!i){let r=0;for(const n of e.values())"p-piece"===n.role&&n.color===t&&(r+=1);i||(i=1===r)}return D(t)(r,n,o,c)||i&&V(r,n,o,c)}}(t.pieces,i):D(i);case"r-piece":return R;case"n-piece":return T;case"b-piece":case"s-piece":return oe(i);case"q-piece":case"f-piece":case"m-piece":return"cambodian"===e?function(e){return(t,r,n,o)=>V(t,r,n,o)||("white"===e?4===t&&0===r&&4===n&&2===o:3===t&&7===r&&3===n&&5===o)}(i):V;case"k-piece":return"cambodian"===e?function(e,t){return(r,n,o,c)=>j(r,n,o,c)||t&&("white"===e?3===r&&0===n&&(1===o||5===o)&&1===c:4===r&&7===n&&(6===o||2===o)&&6===c)}(i,n):j;default:return Me}};case"shatranj":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"p-piece":return D(o);case"r-piece":return R;case"n-piece":return T;case"b-piece":return Q;case"q-piece":return V;case"k-piece":return j;default:return Me}};case"grand":case"grandhouse":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"p-piece":return F(o);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"c-piece":return Y;case"a-piece":return Z;case"k-piece":return j;default:return Me}};case"shako":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return F(c);case"c-piece":case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"e-piece":return X;case"k-piece":return function(e,t,r){return(n,o,c,i)=>j(n,o,c,i)||r&&o===i&&o===("white"===e?1:8)&&5===n&&(7===c&&t.includes(8)||3===c&&t.includes(1))}(c,function(e,t){const r="white"===t?"2":"9",n=[];for(const[o,c]of e)o[1]===r&&c.color===t&&"r-piece"===c.role&&n.push(a(o)[0]);return n}(e.pieces,c),r);default:return Me}};case"shogun":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"pp-piece":return j;case"r-piece":return R;case"pr-piece":return Y;case"n-piece":return T;case"pn-piece":return re;case"b-piece":return O;case"pb-piece":return Z;case"f-piece":return V;case"pf-piece":return q;case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"orda":case"khans":case"ordamirror":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"l-piece":return Y;case"h-piece":case"t-piece":return re;case"a-piece":return Z;case"y-piece":return oe(c);case"f-piece":return J;case"s-piece":return function(e){return(t,r,n,o)=>n===t&&("white"===e?o===r+1:o===r-1)||(n===t-1||n===t+1)&&("white"===e?o===r+2:o===r-2)||(n===t-2||n===t+2)&&("white"===e?o===r+1:o===r-1)}(c);case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"synochess":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"c-piece":case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"s-piece":return he(c);case"e-piece":return X;case"a-piece":return j;case"k-piece":return G(c,H(e.pieces,c),r&&"white"===c);default:return Me}};case"musketeer":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"l-piece":return be;case"o-piece":return we;case"u-piece":return ke;case"d-piece":return ye;case"c-piece":return Y;case"a-piece":return Z;case"e-piece":return ve;case"h-piece":return me;case"f-piece":return Se;case"s-piece":return Ce;case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"hoppelpoppel":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"r-piece":return R;case"n-piece":case"b-piece":return Z;case"q-piece":return q;case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"mansindam":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"p-piece":return ie(o);case"pp-piece":case"k-piece":return j;case"r-piece":return R;case"pr-piece":return ae;case"n-piece":return T;case"pn-piece":return re;case"b-piece":return O;case"pb-piece":return ue;case"c-piece":return Z;case"pc-piece":return ee;case"m-piece":return Y;case"pm-piece":return te;case"a-piece":return J;case"q-piece":return q;default:return Me}};case"shinobi":case"shinobiplus":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"pl-piece":case"r-piece":return R;case"ph-piece":case"n-piece":return T;case"pm-piece":case"b-piece":return O;case"q-piece":return q;case"pp-piece":case"c-piece":return j;case"l-piece":return ne(c);case"h-piece":return se(c);case"m-piece":return V;case"d-piece":return ae;case"f-piece":return ue;case"j-piece":return Z;case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"empire":return(e,t,r)=>{const n=e.pieces.get(t),o=n.role,c=n.color;switch(o){case"p-piece":return B(c);case"s-piece":return he(c);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":case"d-piece":case"t-piece":case"c-piece":return q;case"e-piece":return J;case"k-piece":return G(c,H(e.pieces,c),r);default:return Me}};case"chak":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"p-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n);return"white"===e?o>=r&&o-r<=1&&c<=1:r>=o&&r-o<=1&&c<=1}}(o);case"pp-piece":return function(e){return(t,r,n,o)=>Ae(t,r,n,o)&&("white"===e?o>=4:o<=4)}(o);case"r-piece":return R;case"v-piece":return T;case"s-piece":return Ae;case"j-piece":return re;case"q-piece":return q;case"k-piece":return j;case"pk-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n),i=L(r,o);return q(t,r,n,o)&&c<=2&&i<=2&&("white"===e?o>=4:o<=4)}}(o);default:return Me}};case"chennis":return(e,t)=>{const r=e.pieces.get(t),n=r.role,o=r.color;switch(n){case"p-piece":return D(o);case"pp-piece":case"pf-piece":return R;case"s-piece":return he(o);case"ps-piece":return O;case"f-piece":return V;case"m-piece":return j;case"pm-piece":return T;case"k-piece":return function(e){return(t,r,n,o)=>j(t,r,n,o)&&n>=1&&n<=5&&("white"===e?o<=3:o>=3)}(o);default:return Me}};case"spartan":return(e,r,n)=>{const o=e.pieces.get(r),c=o.role,i=o.color;switch(c){case"h-piece":return function(e){return(t,r,n,o)=>{const c=L(t,n);return"white"===e?o===r+1&&c<=1||r<=1&&o===r+2&&2===c:o===r-1&&c<=1||r>=6&&o===r-2&&2===c}}(i);case"g-piece":return ae;case"w-piece":return Z;case"c-piece":return $(R,K(2));case"l-piece":return function(...e){return(t,r,n,o)=>e.map((e=>e(t,r,n,o))).reduce(((e,t)=>e||t))}($(O,K(2)),W);case"p-piece":return B(i);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"k-piece":return t?_(i,H(e.pieces,i),n):G(i,H(e.pieces,i),n);default:return Me}};case"capablanca":case"capahouse":return(e,r,n)=>{const o=e.pieces.get(r),c=o.role,i=o.color;switch(c){case"p-piece":return B(i);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"c-piece":return Y;case"a-piece":return Z;case"k-piece":return t?_(i,H(e.pieces,i),n):function(e,t,r){return(n,o,c,i)=>j(n,o,c,i)||r&&o===i&&o===E(e)&&5===n&&(8===c&&t.includes(9)||2===c&&t.includes(0))}(i,H(e.pieces,i),n);default:return Me}};default:return(e,r,n)=>{const o=e.pieces.get(r),c=o.role,i=o.color;switch(c){case"p-piece":return B(i);case"r-piece":return R;case"n-piece":return T;case"b-piece":return O;case"q-piece":return q;case"e-piece":case"c-piece":return Y;case"h-piece":case"a-piece":case"d-piece":return Z;case"k-piece":return t?_(i,H(e.pieces,i),n):G(i,H(e.pieces,i),n);default:return Me}}}}(e,t,r);return(e,t,o)=>{const c=a(t);return i(r).filter((r=>(c[0]!==r[0]||c[1]!==r[1])&&n(e,t,o)(c[0],c[1],r[0],r[1]))).map(s)}}function Pe(e,...t){e&&setTimeout((()=>e(...t)),1)}function Ne(e){e.premovable.current&&(e.premovable.current=void 0,Pe(e.premovable.events.unset))}function Ie(e,t,r){const n=e.boardState.pieces.get(t),o=e.boardState.pieces.get(r);if(t===r||!n)return!1;const c=o&&o.color!==n.color?o:void 0;return r===e.selectable.selected&&qe(e),Pe(e.events.move,t,r,c),function(e,t,r){if(!e.autoCastle)return!1;const n=e.boardState.pieces.get(t);if(!n||"k-piece"!==n.role)return!1;const o=10===e.dimensions.width&&8===e.dimensions.height,c=a(t),i=a(r);if(0!==c[1]&&7!==c[1]||c[1]!==i[1])return!1;c[0]!==(o?5:4)||e.boardState.pieces.has(r)||(i[0]===(o?8:6)?r=s([o?9:7,i[1]]):2===i[0]&&(r=s([0,i[1]])));const u=e.boardState.pieces.get(r);return!(!u||u.color!==n.color||"r-piece"!==u.role||(e.boardState.pieces.delete(t),e.boardState.pieces.delete(r),c[0]<i[0]?(e.boardState.pieces.set(s([o?8:6,i[1]]),n),e.boardState.pieces.set(s([o?7:5,i[1]]),u)):(e.boardState.pieces.set(s([2,i[1]]),n),e.boardState.pieces.set(s([3,i[1]]),u)),0))}(e,t,r)||(e.boardState.pieces.set(r,n),e.boardState.pieces.delete(t)),e.lastMove=[t,r],e.check=void 0,Pe(e.events.change),c||!0}function Le(e,t,r,n,o){if(e.boardState.pieces.has(r)){if(!o)return!1;e.boardState.pieces.delete(r)}return Pe(e.events.dropNewPiece,t,r),e.boardState.pieces.set(r,t),n&&b(e.boardState.pockets[t.color],t.role,-1),e.lastMove=[d(t.role),r],e.check=void 0,Pe(e.events.change),e.movable.dests=void 0,e.turnColor=v(e.turnColor),!0}function Be(e,t,r,n,o){const c=h(t)?Ie(e,t,r):Le(e,t,r,n,o);return c&&(e.movable.dests=void 0,e.turnColor=v(e.turnColor),e.animation.current=void 0),c}function Te(e,t,r,n,o){if($e(e,t,r,n)||o){const c=Be(e,t,r,n,o);if(c){const n=e.hold.stop();qe(e);const o={premove:!1,ctrlKey:e.stats.ctrlKey,holdTime:n};return!0!==c&&(o.captured=c),h(t)?Pe(e.movable.events.after,t,r,o):Pe(e.movable.events.afterNewPiece,t,r,o),!0}}else if(Ee(e,t,r,n))return function(e,t,r,n){e.premovable.current=[t,r],Pe(e.premovable.events.set,t,r,n)}(e,h(t)?t:d(t.role),r,{ctrlKey:e.stats.ctrlKey}),qe(e),!0;return qe(e),!1}function Oe(e,t,r){if(h(t)?Pe(e.events.select,t):Pe(e.events.selectPocket,t),e.selectable.selected){if(g(e.selectable.selected,t)&&!e.draggable.enabled)return qe(e),void e.hold.cancel();if((e.selectable.enabled||r)&&h(t)&&e.selectable.selected!==t&&Te(e,e.selectable.selected,t,!!e.selectable.fromPocket))return void(e.stats.dragged=!1)}(e.selectable.enabled||e.draggable.enabled)&&(He(e,t,!0)||Ke(e,t,!0))&&(Re(e,t,!0),e.hold.start())}function Re(e,t,r){h(t)?function(e,t){e.selectable.selected=t,e.selectable.fromPocket=!1,Ke(e,t,!1)?e.premovable.dests=e.premovable.premoveFunc(e.boardState,t,e.premovable.castle):e.premovable.dests=void 0}(e,t):function(e,t,r){e.selectable.selected=t,e.selectable.fromPocket=r,Ke(e,t,r)?e.premovable.dests=e.premovable.predropFunc(e.boardState,t):e.premovable.dests=void 0}(e,t,!!r)}function qe(e){e.selectable.selected=void 0,e.premovable.dests=void 0,e.hold.cancel()}function Ge(e,t,r){var n,o;let c,i=!1;if(h(t))c=e.boardState.pieces.get(t),i=!!c;else{c=t;const s=null!==(o=null===(n=e.boardState.pockets)||void 0===n?void 0:n[c.color].get(c.role))&&void 0!==o?o:0;i=!r||s>0}return[c,i]}function He(e,t,r){const[n,o]=Ge(e,t,r);return o&&("both"===e.movable.color||e.movable.color===n.color&&e.turnColor===n.color)}const $e=(e,t,r,n)=>{var o,c;return t!==r&&He(e,t,n)&&(e.movable.free||!!(null===(c=null===(o=e.movable.dests)||void 0===o?void 0:o.get(h(t)?t:d(t.role)))||void 0===c?void 0:c.includes(r)))};function Ke(e,t,r){const[n,o]=Ge(e,t,r);return o&&e.premovable.enabled&&e.movable.color===n.color&&e.turnColor!==n.color}const Ee=(e,t,r,n)=>t!==r&&Ke(e,t,n)&&(h(t)?e.premovable.premoveFunc(e.boardState,t,e.premovable.castle).includes(r):e.premovable.predropFunc(e.boardState,t).includes(r));function je(e,t,r){const[n,o]=Ge(e,t,r);return o&&e.draggable.enabled&&("both"===e.movable.color||e.movable.color===n.color&&(e.turnColor===n.color||e.premovable.enabled))}function _e(e){const t=e.premovable.current;if(!t)return!1;const r=h(t[0])?t[0]:{role:u(t[0]),color:e.turnColor},n=t[1];let o=!1;if($e(e,r,n,!0)){const t=Be(e,r,n,!0);if(t){const c={premove:!0};!0!==t&&(c.captured=t),h(r)?Pe(e.movable.events.after,r,n,c):Pe(e.movable.events.afterNewPiece,r,n,c),o=!0}}return Ne(e),o}function De(e){Ne(e),qe(e)}function Fe(e){e.movable.color=e.movable.dests=e.animation.current=void 0,De(e)}function Ue(e,t,r,n){let o=Math.floor(n.width*(e[0]-r.left)/r.width);t||(o=n.width-1-o);let c=n.height-1-Math.floor(n.height*(e[1]-r.top)/r.height);return t||(c=n.height-1-c),o>=0&&o<n.width&&c>=0&&c<n.height?s([o,c]):void 0}const We=e=>"white"===e.orientation,ze="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";function Ve(e,t){const r=e.split(" ")[0],n=r.indexOf("[");let o,c;if(n>-1)o=r.slice(0,n),c=r.slice(n+1,r.indexOf("]"));else{const e=r.split("/");o=e.slice(0,t.height).join("/"),c=e.length>t.height?e[t.height]:void 0}return{pieces:Xe(o),pockets:Qe(c)}}function Xe(e){"start"===e&&(e=ze);const t=new Map;let r=e.split("/").length-1,n=0,o=!1,c=!1,i=0;for(const a of e)switch(a){case" ":case"[":return t;case"/":if(--r,r<0)return t;n=0,i=0;break;case"+":o=!0;break;case"|":c=!0;break;case"~":{const e=t.get(s([n-1,r]));e&&(e.promoted=!0);break}default:{const e=a.charCodeAt(0);if(48<=e&&e<58)i=10*i+e-48;else{n+=i,i=0;const e=a.toLowerCase(),l={role:u(e),color:a===e?"black":"white"};o&&(l.role="p"+l.role,l.promoted=!0,o=!1),c&&(l.mirror=!0,c=!1),t.set(s([n,r]),l),++n}}}return t}function Qe(e){if(void 0!==e){const t=new Map,r=new Map;for(const n of e){const e=u(n);/[A-Z]/.test(n)?b(t,e,1):/[a-z]/.test(n)&&b(r,e,1)}return{white:t,black:r}}}function Ze(e,t){return function(e,t){return o.slice(-t.height).map((n=>r.slice(0,t.width).map((t=>{const r=e.get(t+n);if(r){let e=l(r.role,"white"===r.color);return r.promoted&&"+"!==e.charAt(0)&&(e+="~"),r.mirror&&(e="|"+e),e}return"1"})).join(""))).join("/").replace(/1{2,}/g,(e=>e.length.toString()))}(e.pieces,t)+((n=e.pockets)?"["+Ye(n.white,!0)+Ye(n.black,!1)+"]":"");var n}function Ye(e,t){const r=[];for(const[n,o]of e.entries())r.push(l(n,t).repeat(o));return r.join("")}function Je(e,t){t.animation&&(tt(e.animation,t.animation),(e.animation.duration||0)<70&&(e.animation.enabled=!1))}function et(e,t){var r,n,o;if((null===(r=t.movable)||void 0===r?void 0:r.dests)&&(e.movable.dests=void 0),(null===(n=t.drawable)||void 0===n?void 0:n.autoShapes)&&(e.drawable.autoShapes=[]),tt(e,t),t.fen){const r=Ve(t.fen,e.dimensions),n=e.boardState.pieces.get("a0");void 0!==n&&r.pieces.set("a0",n),e.pocketRoles?r.pockets=null!==(o=r.pockets)&&void 0!==o?o:{white:new Map,black:new Map}:r.pockets=void 0,e.boardState=r,e.drawable.shapes=[]}if(("check"in t||"kingRoles"in t)&&function(e,t){if(Array.isArray(t))e.check=t;else{const r=!0===t?e.turnColor:t;if(e.check=[],r)for(const[t,n]of e.boardState.pieces)e.kingRoles.includes(n.role)&&n.color===r&&e.check.push(t)}}(e,t.check||!1),"lastMove"in t&&!t.lastMove?e.lastMove=void 0:t.lastMove&&(e.lastMove=t.lastMove),e.selectable.selected&&Re(e,e.selectable.selected,e.selectable.fromPocket),Je(e,t),!e.movable.rookCastle&&e.movable.dests){const t="white"===e.movable.color?"1":"8",r="e"+t,n=e.movable.dests.get(r),o=e.boardState.pieces.get(r);if(!n||!o||"k-piece"!==o.role)return;e.movable.dests.set(r,n.filter((e=>!(e==="a"+t&&n.includes("c"+t)||e==="h"+t&&n.includes("g"+t)))))}}function tt(e,t){for(const r in t)Object.prototype.hasOwnProperty.call(t,r)&&(Object.prototype.hasOwnProperty.call(e,r)&&rt(e[r])&&rt(t[r])?tt(e[r],t[r]):e[r]=t[r])}function rt(e){if("object"!=typeof e||null===e)return!1;const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}const nt=(e,t)=>t.animation.enabled?function(e,t){const r=new Map(t.boardState.pieces),n=e(t),o=function(e,t){const r=new Map,n=[],o=new Map,i=[],s=[],a=new Map;let u,l,d;for(const[t,r]of e)a.set(t,ct(t,r));for(const e of c(t.dimensions))u=t.boardState.pieces.get(e),l=a.get(e),u?l?w(u,l.piece)||(i.push(l),s.push(ct(e,u))):s.push(ct(e,u)):l&&i.push(l);for(const e of s)l=it(e,i.filter((t=>w(e.piece,t.piece)))),l&&(d=[l.pos[0]-e.pos[0],l.pos[1]-e.pos[1]],r.set(e.key,d.concat(d)),n.push(l.key));for(const e of i)n.includes(e.key)||o.set(e.key,e.piece);return{anims:r,fadings:o}}(r,t);if(o.anims.size||o.fadings.size){const e=t.animation.current&&t.animation.current.start;t.animation.current={start:performance.now(),frequency:1/t.animation.duration,plan:o},e||st(t,performance.now())}else t.dom.redraw();return n}(e,t):ot(e,t);function ot(e,t){const r=e(t);return t.dom.redraw(),r}const ct=(e,t)=>({key:e,pos:a(e),piece:t}),it=(e,t)=>t.sort(((t,r)=>y(e.pos,t.pos)-y(e.pos,r.pos)))[0];function st(e,t){const r=e.animation.current;if(void 0===r)return void(e.dom.destroyed||e.dom.redrawNow());const n=1-(t-r.start)*r.frequency;if(n<=0)e.animation.current=void 0,e.dom.redrawNow();else{const t=at(n);for(const e of r.plan.anims.values())e[2]=e[0]*t,e[3]=e[1]*t;e.dom.redrawNow(!0),requestAnimationFrame(((t=performance.now())=>st(e,t)))}}const at=e=>e<.5?4*e*e*e:(e-1)*(2*e-2)*(2*e-2)+1,ut=["green","red","blue","yellow"];function lt(e,t){if(t.touches&&t.touches.length>1)return;t.stopPropagation(),t.preventDefault(),t.ctrlKey?qe(e):De(e);const r=x(t),n=Ue(r,We(e),e.dom.bounds(),e.dimensions);n&&(e.drawable.current={orig:n,pos:r,brush:bt(t),snapToValidMove:e.drawable.defaultSnapToValidMove},dt(e))}function dt(e){requestAnimationFrame((()=>{const t=e.drawable.current;if(t){const r=Ue(t.pos,We(e),e.dom.bounds(),e.dimensions);r||(t.snapToValidMove=!1);const n=t.snapToValidMove?function(e,t,r,n,o){const c=a(e),u=i(o).filter((e=>q(c[0],c[1],e[0],e[1])||T(c[0],c[1],e[0],e[1])||9===o.width&&10===o.height&&ge(c[0],c[1],e[0],e[1]))),l=u.map((e=>I(s(e),r,n,o))).map((e=>y(t,e))),[,d]=l.reduce(((e,t,r)=>e[0]<t?e:[t,r]),[l[0],0]);return s(u[d])}(t.orig,t.pos,We(e),e.dom.bounds(),e.dimensions):r;n!==t.mouseSq&&(t.mouseSq=n,t.dest=n!==t.orig?n:void 0,e.dom.redrawNow()),dt(e)}}))}function pt(e,t){e.drawable.current&&(e.drawable.current.pos=x(t))}function ht(e){const t=e.drawable.current;t&&(t.mouseSq&&function(e,t){const r=e=>e.orig===t.orig&&e.dest===t.dest,n=e.shapes.find(r);n&&(e.shapes=e.shapes.filter((e=>!r(e))));n&&n.brush===t.brush||e.shapes.push(t);mt(e)}(e.drawable,t),ft(e))}function ft(e){e.drawable.current&&(e.drawable.current=void 0,e.dom.redraw())}function gt(e){e.drawable.shapes.length&&(e.drawable.shapes=[],e.dom.redraw(),mt(e.drawable))}function bt(e){var t;const r=(e.shiftKey||e.ctrlKey)&&P(e),n=e.altKey||e.metaKey||(null===(t=e.getModifierState)||void 0===t?void 0:t.call(e,"AltGraph"));return ut[(r?1:0)+(n?2:0)]}function mt(e){e.onChange&&e.onChange(e.shapes)}function vt(e,t){if(!t.isTrusted||void 0!==t.button&&0!==t.button)return;if(t.touches&&t.touches.length>1)return;const r=e.dom.bounds(),n=x(t),o=Ue(n,We(e),r,e.dimensions);if(!o)return;const c=e.boardState.pieces.get(o),i=e.selectable.selected;i||!e.drawable.enabled||!e.drawable.eraseOnClick&&c&&c.color===e.turnColor||gt(e),!1!==t.cancelable&&(!t.touches||e.blockTouchScroll||c||i||function(e,t){const r=We(e),n=e.dom.bounds(),o=Math.pow(n.width/e.dimensions.width,2);for(const c of e.boardState.pieces.keys()){const i=I(c,r,n,e.dimensions);if(y(i,t)<=o)return!0}return!1}(e,n))&&t.preventDefault();const s=!!e.premovable.current;e.stats.ctrlKey=t.ctrlKey,e.selectable.selected&&$e(e,e.selectable.selected,o,!!e.selectable.fromPocket)?nt((e=>Oe(e,o)),e):Oe(e,o);const u=e.selectable.selected===o,l=Mt(e,o);if(c&&l&&u&&je(e,o,!1)){e.draggable.current={orig:o,piece:c,origPos:n,pos:n,started:e.draggable.autoDistance&&e.stats.dragged,element:l,previouslySelected:i,originTarget:t.target,keyHasChanged:!1},l.cgDragging=!0,l.classList.add("dragging");const s=e.dom.elements.ghost;s&&(s.className="ghost "+k(c,e.orientation),C(s,S(r,e.dimensions)(a(o),We(e))),M(s,!0)),kt(e)}else s&&Ne(e);e.dom.redraw()}function wt(e,t,r,n,o,c){e.dom.redraw();const i=x(n);e.boardState.pieces.set("a0",t),e.draggable.current={orig:"a0",piece:t,origPos:i,pos:i,started:!0,element:()=>Mt(e,"a0"),previouslySelected:o,originTarget:n.target,fromPocket:r,force:!!c,keyHasChanged:!1},kt(e)}function kt(e){requestAnimationFrame((()=>{var t;const r=e.draggable.current;if(!r)return;(null===(t=e.animation.current)||void 0===t?void 0:t.plan.anims.has(r.orig))&&(e.animation.current=void 0);const n=e.boardState.pieces.get(r.orig);if(w(n,r.piece)){if(!r.started&&y(r.pos,r.origPos)>=Math.pow(e.draggable.distance,2)&&(r.started=!0),r.started){if("function"==typeof r.element){const e=r.element();if(!e)return;e.cgDragging=!0,e.classList.add("dragging"),r.element=e}const t=e.dom.bounds();C(r.element,[r.pos[0]-t.left-t.width/(2*e.dimensions.width),r.pos[1]-t.top-t.height/(2*e.dimensions.height)]),"a0"!==r.orig&&(r.keyHasChanged||(r.keyHasChanged=r.orig!==Ue(r.pos,We(e),t,e.dimensions)))}}else Ct(e);kt(e)}))}function yt(e,t){e.draggable.current&&(!t.touches||t.touches.length<2)&&(e.draggable.current.pos=x(t))}function St(e,t){var r;const n=e.draggable.current;if(!n)return;if("touchend"===t.type&&!1!==t.cancelable&&t.preventDefault(),"touchend"===t.type&&n.originTarget!==t.target)return void(e.draggable.current=void 0);Ne(e);const o=Ue(x(t)||n.pos,We(e),e.dom.bounds(),e.dimensions),c=t.target,i=Number(null!==(r=c.getAttribute("data-nb"))&&void 0!==r?r:-1)>=0?{role:c.getAttribute("data-role"),color:c.getAttribute("data-color")}:void 0;o&&n.started&&n.orig!==o?(e.stats.ctrlKey=t.ctrlKey,Te(e,"a0"!==n.orig?n.orig:n.piece,o,!!n.fromPocket)&&(e.stats.dragged=!0)):e.draggable.deleteOnDropOff&&!o&&(e.boardState.pieces.delete(n.orig),n.fromPocket&&b(e.boardState.pockets[n.piece.color],n.piece.role,-1),Pe(e.events.change)),!(n.previouslySelected&&(n.orig===n.previouslySelected||g(n.piece,n.previouslySelected))||n.keyHasChanged)||n.orig!==o&&o||qe(e),("a0"!==n.orig||i&&w(n.piece,i))&&e.selectable.enabled||qe(e),"a0"===n.orig&&e.boardState.pieces.delete("a0"),At(e),e.draggable.current=void 0,e.dom.redraw()}function Ct(e){e.draggable.current&&(e.draggable.current=void 0,e.boardState.pieces.delete("a0"),qe(e),At(e),e.dom.redraw())}function At(e){const t=e.dom.elements;t.ghost&&M(t.ghost,!1)}function Mt(e,t){let r=e.dom.elements.board.firstChild;for(;r;){if(r.cgKey===t&&"PIECE"===r.tagName)return r;r=r.nextSibling}}function xt(e,t){e.exploding&&(t?e.exploding.stage=t:e.exploding=void 0,e.dom.redraw())}function Pt(e,t){function r(){!function(e){e.orientation=v(e.orientation),e.animation.current=e.draggable.current=e.selectable.selected=void 0}(e),t()}return{set(t){t.orientation&&t.orientation!==e.orientation&&r(),Je(e,t),(t.fen?nt:ot)((e=>et(e,t)),e)},state:e,getFen:()=>Ze(e.boardState,e.dimensions),toggleOrientation:r,setPieces(t){nt((e=>function(e,t){for(const[r,n]of t)n?e.boardState.pieces.set(r,n):e.boardState.pieces.delete(r)}(e,t)),e)},changePocket(t,r){var n;(null===(n=e.pocketRoles)||void 0===n?void 0:n[t.color].includes(t.role))&&(b(e.boardState.pockets[t.color],t.role,r),e.dom.redraw())},selectSquare(t,r){t?nt((e=>Oe(e,t,r)),e):e.selectable.selected&&(qe(e),e.dom.redraw())},selectPocket(t){t?nt((e=>Oe(e,t)),e):e.selectable.selected&&(qe(e),e.dom.redraw())},unselect(){qe(e)},move(t,r){p(t)?Le(e,{role:u(t),color:e.turnColor},r,!0):nt((e=>Ie(e,t,r)),e)},newPiece(t,r,n){nt((e=>Le(e,t,r,n)),e)},playPremove(){if(e.premovable.current){if(nt(_e,e))return!0;e.dom.redraw()}return!1},cancelPremove(){ot(Ne,e)},cancelMove(){ot((e=>{De(e),Ct(e)}),e)},stop(){ot((e=>{Fe(e),Ct(e)}),e)},explode(t){!function(e,t){e.exploding={stage:1,keys:t},e.dom.redraw(),setTimeout((()=>{xt(e,2),setTimeout((()=>xt(e,void 0)),120)}),120)}(e,t)},setAutoShapes(t){ot((e=>e.drawable.autoShapes=t),e)},setShapes(t){ot((e=>e.drawable.shapes=t),e)},getKeyAtDomPos:t=>Ue(t,We(e),e.dom.bounds(),e.dimensions),redrawAll:t,dragNewPiece(t,r,n,o){wt(e,t,r,n,void 0,o)},destroy(){Fe(e),e.dom.unbind&&e.dom.unbind(),e.dom.destroyed=!0}}}const Nt=()=>!0;function It(e,t,r,n){return e<0&&(e+=n.height),t<0&&(t+=n.height),(o,c)=>("black"===r&&(c=n.height-1-c),e<=c&&c<t)}function Lt(e,t){const r=function(e,t){switch(e){case"crazyhouse":case"shouse":case"capahouse":case"gothhouse":return e=>"p-piece"===e.role?It(1,-1,e.color,t):Nt;case"placement":return e=>It(0,1,e.color,t);case"sittuyin":return e=>"r-piece"===e.role?It(0,1,e.color,t):It(0,3,e.color,t);case"shogi":case"minishogi":case"gorogoro":case"gorogoroplus":return e=>{switch(e.role){case"p-piece":case"l-piece":return It(0,-1,e.color,t);case"n-piece":return It(0,-2,e.color,t);default:return Nt}};case"torishogi":return e=>"s-piece"===e.role?It(0,-1,e.color,t):Nt;case"grandhouse":return e=>"p-piece"===e.role?It(1,7,e.color,t):Nt;case"shogun":return e=>It(0,5,e.color,t);case"synochess":return()=>(e,t)=>4===t;case"shinobi":return()=>(e,t)=>t<=3;case"mansindam":return e=>"p-piece"===e.role?It(0,-1,e.color,t):Nt;default:return()=>Nt}}(e,t);return(e,n)=>i(t).filter((t=>{var o;return(null===(o=e.pieces.get(s(t)))||void 0===o?void 0:o.color)!==n.color&&r(n)(t[0],t[1])})).map(s)}function Bt(e,t,r){const n=new Map,o=[];for(const t of e)n.set(t.hash,!1);let c,i=t.firstChild;for(;i;)c=i.getAttribute("cgHash"),n.has(c)?n.set(c,!0):o.push(i),i=i.nextSibling;for(const e of o)t.removeChild(e);for(const o of e)n.get(o.hash)||t.appendChild(r(o))}function Tt(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function Ot(e,t,r){const n=e.drawable,o=n.current,c=o&&o.mouseSq?o:void 0,i=new Map,s=e.dom.bounds(),a=n.autoShapes.filter((e=>!e.piece));for(const e of n.shapes.concat(a).concat(c?[c]:[]))e.dest&&i.set(e.dest,(i.get(e.dest)||0)+1);const u=n.shapes.concat(a).map((e=>({shape:e,current:!1,hash:Rt(e,i,!1,s)})));c&&u.push({shape:c,current:!0,hash:Rt(c,i,!0,s)});const l=u.map((e=>e.hash)).join(";");if(l===e.drawable.prevSvgHash)return;e.drawable.prevSvgHash=l;const d=t.querySelector("defs"),p=t.querySelector("g"),h=r.querySelector("g");!function(e,t,r){const n=new Map;let o;for(const r of t)r.shape.dest&&(o=e.brushes[r.shape.brush],r.shape.modifiers&&(o=jt(o,r.shape.modifiers)),n.set(o.key,o));const c=new Set;let i=r.firstChild;for(;i;)c.add(i.getAttribute("cgKey")),i=i.nextSibling;for(const[e,t]of n.entries())c.has(e)||r.appendChild($t(t))}(n,u,d),Bt(u.filter((e=>!e.shape.customSvg)),p,(t=>Ht(e,t,n.brushes,i,s))),Bt(u.filter((e=>e.shape.customSvg)),h,(t=>Ht(e,t,n.brushes,i,s)))}function Rt({orig:e,dest:t,brush:r,piece:n,modifiers:o,customSvg:c},i,s,a){return[a.width,a.height,s,e,t,r,t&&(i.get(t)||0)>1,n&&qt(n),o&&(u=o,""+(u.lineWidth||"")),c&&Gt(c)].filter((e=>e)).join(",");var u}function qt(e){return[e.color,e.role,e.promoted,e.scale].filter((e=>e)).join(",")}function Gt(e){let t=0;for(let r=0;r<e.length;r++)t=(t<<5)-t+e.charCodeAt(r)>>>0;return"custom-"+t.toString()}function Ht(e,{shape:t,current:r,hash:n},o,c,i){let s;const u=Et(a(t.orig),e.orientation,e.dimensions);if(t.customSvg)s=function(e,t,r,n){const[o,c]=Ft(t,r,n),i=Kt(Tt("g"),{transform:`translate(${o},${c})`}),s=Kt(Tt("svg"),{width:1,height:1,viewBox:"0 0 100 100"});return i.appendChild(s),s.innerHTML=e,i}(t.customSvg,u,i,e.dimensions);else if(t.dest){let n=o[t.brush];t.modifiers&&(n=jt(n,t.modifiers)),s=function(e,t,r,n,o,c,i){const s=function(e){return(e?20:10)/64}(o&&!n),a=Ft(t,c,i),u=Ft(r,c,i),l=u[0]-a[0],d=u[1]-a[1],p=Math.atan2(d,l),h=Math.cos(p)*s,f=Math.sin(p)*s;return Kt(Tt("line"),{stroke:e.color,"stroke-width":_t(e,n),"stroke-linecap":"round","marker-end":"url(#arrowhead-"+e.key+")",opacity:Dt(e,n),x1:a[0],y1:a[1],x2:u[0]-h,y2:u[1]-f})}(n,u,Et(a(t.dest),e.orientation,e.dimensions),r,(c.get(t.dest)||0)>1,i,e.dimensions)}else s=function(e,t,r,n,o){const c=Ft(t,n,o),i=[3/64,4/64],s=Math.min(o.width/(2*o.height),n.width/o.width/(2*n.height/o.height));return Kt(Tt("circle"),{stroke:e.color,"stroke-width":i[r?0:1],fill:"none",opacity:Dt(e,r),cx:c[0],cy:c[1],r:s-i[1]/2})}(o[t.brush],u,r,i,e.dimensions);return s.setAttribute("cgHash",n),s}function $t(e){const t=Kt(Tt("marker"),{id:"arrowhead-"+e.key,orient:"auto",markerWidth:4,markerHeight:8,refX:2.05,refY:2.01});return t.appendChild(Kt(Tt("path"),{d:"M0,0 V4 L3,2 Z",fill:e.color})),t.setAttribute("cgKey",e.key),t}function Kt(e,t){for(const r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.setAttribute(r,t[r]);return e}function Et(e,t,r){return"white"===t?e:[r.width-1-e[0],r.height-1-e[1]]}function jt(e,t){return{color:e.color,opacity:Math.round(10*e.opacity)/10,lineWidth:Math.round(t.lineWidth||e.lineWidth),key:[e.key,t.lineWidth].filter((e=>e)).join("")}}function _t(e,t){return(e.lineWidth||10)*(t?.85:1)/64}function Dt(e,t){return(e.opacity||1)*(t?.9:1)}function Ft(e,t,r){let n,o;return 9===r.width&&10===r.height?(n=Math.max(1,t.width/t.height)*Math.min(1,r.height/r.width),o=Math.max(1,t.height/t.width)*(r.width/r.height)):(n=Math.min(1,t.width/t.height)*Math.max(1,r.height/r.width),o=Math.min(1,t.height/t.width)*Math.max(1,r.width/r.height)),[(e[0]-(r.width-1)/2)*n,((r.height-1)/2-e[1])*o]}const Ut=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],Wt=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"],zt=["一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六"],Vt={[e.ALGEBRAIC]:[{coords:Ut,position:"bottom",direction:"forward"},{coords:Wt,position:"side",direction:"forward"}],[e.SHOGI_ENGLET]:[{coords:Wt,position:"top",direction:"backward"},{coords:Ut,position:"side",direction:"backward"}],[e.SHOGI_ARBNUM]:[{coords:Wt,position:"top",direction:"backward"},{coords:Wt,position:"side",direction:"backward"}],[e.SHOGI_HANNUM]:[{coords:Wt,position:"top",direction:"backward"},{coords:zt,position:"side",direction:"backward"}],[e.JANGGI]:[{coords:Wt,position:"bottom",direction:"forward"},{coords:["1","2","3","4","5","6","7","8","9","0","1","2","3","4","5","6"],position:"side",direction:"backward"}],[e.XIANGQI_ARBNUM]:[{coords:Wt,position:"top",direction:"forward",noBlackReverse:!0},{coords:Wt,position:"bottom",direction:"backward",noBlackReverse:!0}],[e.XIANGQI_HANNUM]:[{coords:Wt,position:"top",direction:"forward",noBlackReverse:!0},{coords:zt,position:"bottom",direction:"backward",noBlackReverse:!0}],[e.THAI_ALGEBRAIC]:[{coords:["ก","ข","ค","ง","จ","ฉ","ช","ญ","ต","ถ","ท","น","ป","ผ","พ","ม"],position:"bottom",direction:"forward"},{coords:["๑","๒","๓","๔","๕","๖","๗","๘","๙","๑๐","๑๑","๑๒","๑๓","๑๔","๑๕","๑๖"],position:"side",direction:"forward"}]};function Xt(e,r){e.innerHTML="",e.classList.add("cg-wrap");for(const n of t)e.classList.toggle("orientation-"+n,r.orientation===n);e.classList.toggle("manipulable",!r.viewOnly);const n=N("cg-container");e.appendChild(n);const o=N("extension");n.appendChild(o);const c=N("cg-board");let i,s;var a;let u,l,d,p;if(n.appendChild(c),a=e,Array.from(a.classList).includes("mini")&&r.boardState.pockets&&(i=N("pocketBottom"),s=N("pocketTop"),n.insertBefore("white"===r.orientation?s:i,c),n.insertBefore("white"===r.orientation?i:s,c.nextSibling)),r.drawable.visible){const e=r.dimensions.width,t=r.dimensions.height;u=Kt(Tt("svg"),{class:"cg-shapes",viewBox:`${-e/2} ${-t/2} ${e} ${t}`,preserveAspectRatio:"xMidYMid slice"}),u.appendChild(Tt("defs")),u.appendChild(Tt("g")),l=Kt(Tt("svg"),{class:"cg-custom-svgs",viewBox:`${-(e-1)/2} ${-(t-1)/2} ${e} ${t}`,preserveAspectRatio:"xMidYMid slice"}),l.appendChild(Tt("g")),d=N("cg-auto-pieces"),n.appendChild(u),n.appendChild(l),n.appendChild(d)}return r.coordinates&&Vt[r.notation].forEach((e=>{const t="side"===e.position?r.dimensions.height:r.dimensions.width,o=e.position,c=e.coords.slice(0,t);n.appendChild(function(e,t){const r=N("coords",t);let n;for(const t of e)n=N("coord"),n.textContent=t,r.appendChild(n);return r}(c,`${o} ${e.direction}${e.noBlackReverse?"":" "+r.orientation}`))})),r.draggable.enabled&&r.draggable.showGhost&&(p=N("piece","ghost"),M(p,!1),n.appendChild(p)),{pocketTop:s,pocketBottom:i,board:c,container:n,wrap:e,ghost:p,svg:u,customSvg:l,autoPieces:d}}function Qt(e,t,r){if(!e.pocketRoles)return;const n="top"===r?v(e.orientation):e.orientation,o=e.pocketRoles[n],c=String(o.length),i=String(e.dimensions.width),s=String(e.dimensions.height);t.setAttribute("style",`--pocketLength: ${c}; --files: ${i}; --ranks: ${s}`),t.classList.add("pocket",r),o.forEach((r=>{const o=k({role:r,color:n},e.orientation),c=N("square"),i=N("piece",o);c.appendChild(i),i.setAttribute("data-color",n),i.setAttribute("data-role",r),Yt(e,c),t.appendChild(c)}))}function Zt(e,t){if(t){let r=t.firstChild;if(r&&r.firstChild){const n=r.firstChild.getAttribute("data-color");for(t.classList.toggle("usable",!e.viewOnly&&(e.movable.free||"both"===e.movable.color||!!n&&e.movable.color===n));r;)Yt(e,r),r=r.nextSibling}}}function Yt(e,t){var r,n,o;const c=t.firstChild,i=c.getAttribute("data-role"),s=c.getAttribute("data-color");c.setAttribute("data-nb",""+(null!==(r=e.boardState.pockets[s].get(i))&&void 0!==r?r:0));const a={role:i,color:s},l=e.selectable.selected;t.classList.toggle("selected-square",!!l&&f(l)&&e.selectable.fromPocket&&w(l,a));const h=null===(n=e.premovable.current)||void 0===n?void 0:n[0];t.classList.toggle("premove",!!h&&p(h)&&u(h)===i&&e.turnColor!==s),t.classList.toggle("last-move",e.highlight.lastMove&&!!(null===(o=e.lastMove)||void 0===o?void 0:o.includes(d(i)))&&e.turnColor!==s)}function Jt(e,t,r,n){return e.addEventListener(t,r,n),()=>e.removeEventListener(t,r,n)}const er=e=>t=>{e.draggable.current?Ct(e):e.drawable.current?ft(e):t.shiftKey||P(t)?e.drawable.enabled&<(e,t):e.viewOnly||vt(e,t)},tr=e=>t=>{e.draggable.current?Ct(e):e.drawable.current?ft(e):t.shiftKey||P(t)?e.drawable.enabled&<(e,t):e.viewOnly||function(e,t){if(!t.isTrusted||void 0!==t.button&&0!==t.button)return;if(t.touches&&t.touches.length>1)return;const r=t.target,n=r.getAttribute("data-role"),o=r.getAttribute("data-color");if(0===Number(r.getAttribute("data-nb")))return;const c={role:n,color:o},i=e.selectable.selected;i||!e.drawable.enabled||!e.drawable.eraseOnClick&&c.color===e.turnColor||gt(e),!1!==t.cancelable&&t.preventDefault();const s=!!e.premovable.current;e.stats.ctrlKey=t.ctrlKey,Oe(e,c);const a=e.selectable.selected;a&&g(a,c)&&je(e,c,!0)?wt(e,c,!0,t,i):s&&Ne(e),e.dom.redraw()}(e,t)},rr=(e,t,r)=>n=>{e.drawable.current?e.drawable.enabled&&r(e,n):e.viewOnly||t(e,n)};function nr(e){const t=We(e),r=S(e.dom.bounds(),e.dimensions),n=e.dom.elements.board,o=e.boardState.pieces,c=e.animation.current,i=c?c.plan.anims:new Map,s=c?c.plan.fadings:new Map,u=e.draggable.current,l=function(e){var t;const r=new Map;if(e.lastMove&&e.highlight.lastMove)for(const t of e.lastMove)h(t)&&"a0"!==t&&ur(r,t,"last-move");if(e.check&&e.highlight.check)for(const t of e.check)ur(r,t,"check");const n=e.selectable.selected;if(n&&(h(n)&&ur(r,n,"selected"),e.movable.showDests)){const o=null===(t=e.movable.dests)||void 0===t?void 0:t.get(h(n)?n:d(n.role));if(o)for(const t of o)ur(r,t,"move-dest"+(e.boardState.pieces.has(t)?" oc":""));const c=e.premovable.dests;if(c)for(const t of c)ur(r,t,"premove-dest"+(e.boardState.pieces.has(t)?" oc":""))}const o=e.premovable.current;if(o)for(const e of o)h(e)&&ur(r,e,"current-premove");const c=e.exploding;if(c)for(const e of c.keys)ur(r,e,"exploding"+c.stage);return r}(e),p=new Set,f=new Set,g=new Map,b=new Map;let m,v,w,y,A,M,x,P,I,L;for(v=n.firstChild;v;){if(m=v.cgKey,cr(v))if(w=o.get(m),A=i.get(m),M=s.get(m),y=v.cgPiece,!v.cgDragging||u&&u.orig===m||(v.classList.remove("dragging"),C(v,r(a(m),t)),v.cgDragging=!1),!M&&v.cgFading&&(v.cgFading=!1,v.classList.remove("fading")),w){if(A&&v.cgAnimating&&y===k(w,e.orientation)){const e=a(m);e[0]+=A[2],e[1]+=A[3],v.classList.add("anim"),C(v,r(e,t))}else v.cgAnimating&&(v.cgAnimating=!1,v.classList.remove("anim"),C(v,r(a(m),t)),e.addPieceZIndex&&(v.style.zIndex=ar(a(m),t)));y!==k(w,e.orientation)||M&&v.cgFading?M&&y===k(M,e.orientation)?(v.classList.add("fading"),v.cgFading=!0):lr(g,y,v):p.add(m)}else lr(g,y,v);else if(ir(v)){const e=v.className;l.get(m)===e?f.add(m):lr(b,e,v)}v=v.nextSibling}for(const[e,o]of l)if(!f.has(e)){I=b.get(o),L=I&&I.pop();const c=r(a(e),t);if(L)L.cgKey=e,C(L,c);else{const t=N("square",o);t.cgKey=e,C(t,c),n.insertBefore(t,n.firstChild)}}for(const[c,s]of o)if(A=i.get(c),!p.has(c))if(x=g.get(k(s,e.orientation)),P=x&&x.pop(),P){P.cgKey=c,P.cgFading&&(P.classList.remove("fading"),P.cgFading=!1);const n=a(c);e.addPieceZIndex&&(P.style.zIndex=ar(n,t)),A&&(P.cgAnimating=!0,P.classList.add("anim"),n[0]+=A[2],n[1]+=A[3]),C(P,r(n,t))}else{const o=k(s,e.orientation),i=N("piece",o),u=a(c);i.cgPiece=o,i.cgKey=c,A&&(i.cgAnimating=!0,u[0]+=A[2],u[1]+=A[3]),C(i,r(u,t)),e.addPieceZIndex&&(i.style.zIndex=ar(u,t)),n.appendChild(i)}for(const t of g.values())sr(e,t);for(const t of b.values())sr(e,t)}function or(e){var t,r,n,o,c,i;const s=e.dom.elements.wrap.getBoundingClientRect(),a=e.dom.elements.container,u=s.height/s.width,l=Math.floor(s.width*window.devicePixelRatio/e.dimensions.width)*e.dimensions.width/window.devicePixelRatio,d=l*u;a.style.width=l+"px",a.style.height=d+"px",e.dom.bounds.clear();const p=e.dimensionsCssVarsSuffix?"-"+e.dimensionsCssVarsSuffix:"";null===(t=e.addDimensionsCssVarsTo)||void 0===t||t.style.setProperty("--cg-width"+p,l+"px"),null===(r=e.addDimensionsCssVarsTo)||void 0===r||r.style.setProperty("--cg-height"+p,d+"px"),null===(n=e.dom.elements.pocketTop)||void 0===n||n.style.setProperty("--cg-width"+p,l+"px"),null===(o=e.dom.elements.pocketTop)||void 0===o||o.style.setProperty("--cg-height"+p,d+"px"),null===(c=e.dom.elements.pocketBottom)||void 0===c||c.style.setProperty("--cg-width"+p,l+"px"),null===(i=e.dom.elements.pocketBottom)||void 0===i||i.style.setProperty("--cg-height"+p,d+"px")}const cr=e=>"PIECE"===e.tagName,ir=e=>"SQUARE"===e.tagName;function sr(e,t){for(const r of t)e.dom.elements.board.removeChild(r)}function ar(e,t){const r=e[1];return`${t?10-r:3+r}`}function ur(e,t,r){const n=e.get(t);n?e.set(t,`${n} ${r}`):e.set(t,r)}function lr(e,t,r){const n=e.get(t);n?n.push(r):e.set(t,[r])}function dr(e,t){Bt(e.drawable.autoShapes.filter((e=>e.piece)).map((e=>({shape:e,hash:pr(e),current:!1}))),t,(t=>function(e,{shape:t,hash:r},n){if(t.piece){const o=t.orig,c=t.piece.scale,i=N("piece",k(t.piece,e.orientation));return i.setAttribute("cgHash",r),i.cgKey=o,i.cgScale=c,A(i,S(n,e.dimensions)(a(o),We(e)),c),i}return N("piece","")}(e,t,e.dom.bounds())))}const pr=e=>{var t,r,n;return[e.orig,null===(t=e.piece)||void 0===t?void 0:t.role,null===(r=e.piece)||void 0===r?void 0:r.color,null===(n=e.piece)||void 0===n?void 0:n.scale].join(",")};function hr(e){let t=!1;return()=>{t||(t=!0,requestAnimationFrame((()=>{e(),t=!1})))}}return function(t,r,n,o){const c={boardState:Ve(ze,{width:8,height:8}),orientation:"white",turnColor:"white",coordinates:!0,ranksPosition:"right",autoCastle:!0,viewOnly:!1,disableContextMenu:!1,addPieceZIndex:!1,blockTouchScroll:!1,pieceKey:!1,highlight:{lastMove:!0,check:!0},animation:{enabled:!0,duration:200},movable:{free:!0,color:"both",showDests:!0,events:{},rookCastle:!0},premovable:{enabled:!0,premoveFunc:xe("chess",!1,{width:8,height:8}),predropFunc:Lt("chess",{width:8,height:8}),castle:!0,events:{}},draggable:{enabled:!0,distance:3,autoDistance:!0,showGhost:!0,deleteOnDropOff:!1},selectable:{enabled:!0},stats:{dragged:!("ontouchs