gungi.js 3.3.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +896 -821
- package/dist/index.d.ts +12 -0
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -66,6 +66,7 @@ declare function isSquareAttacked(square: string, byColor: Color, board: Board):
|
|
|
66
66
|
type PGNOptions = {
|
|
67
67
|
maxWidth?: number;
|
|
68
68
|
newline?: string;
|
|
69
|
+
withComments?: boolean;
|
|
69
70
|
};
|
|
70
71
|
|
|
71
72
|
declare class Gungi {
|
|
@@ -108,6 +109,17 @@ declare class Gungi {
|
|
|
108
109
|
reset(): void;
|
|
109
110
|
turn(): Color;
|
|
110
111
|
undo(): Move | null;
|
|
112
|
+
setComment(comment: string): void;
|
|
113
|
+
getComment(): string | undefined;
|
|
114
|
+
removeComment(): string | undefined;
|
|
115
|
+
getComments(): {
|
|
116
|
+
fen: string;
|
|
117
|
+
comment: string;
|
|
118
|
+
}[];
|
|
119
|
+
removeComments(): {
|
|
120
|
+
fen: string;
|
|
121
|
+
comment: string;
|
|
122
|
+
}[];
|
|
111
123
|
validateFen(fen: string): {
|
|
112
124
|
ok: boolean;
|
|
113
125
|
error?: string;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import D from"picocolors";var G=t=>Object.entries(t).reduce((e,[r,o])=>({...e,[o]:r}),{}),k={intro:0,beginner:1,intermediate:2,advanced:3},K=G(k),he="w",me="b",ye="\u4ED8",ge="\u53D6",be="\u8FD4",Pe="\u65B0",Ne="\u5E25",Ee="\u5927",Te="\u4E2D",Ce="\u5C0F",$e="\u4F8D",xe="\u69CD",ke="\u99AC",we="\u5FCD",Ae="\u7826",Fe="\u5175",Ie="\u7832",Me="\u5F13",ve="\u7B52",Re="\u8B00",F=Array.from({length:9},(t,e)=>e+1).flatMap(t=>Array.from({length:9},(e,r)=>`${t}-${9-r}`)),N={marshal:"\u5E25",general:"\u5927",lieutenant_general:"\u4E2D",major_general:"\u5C0F",warrior:"\u4F8D",lancer:"\u69CD",rider:"\u99AC",spy:"\u5FCD",fortress:"\u7826",soldier:"\u5175",cannon:"\u7832",archer:"\u5F13",musketeer:"\u7B52",tactician:"\u8B00"},w=G(N),$={marshal:"m",general:"g",lieutenant_general:"i",major_general:"j",warrior:"w",lancer:"n",rider:"r",spy:"s",fortress:"f",soldier:"d",cannon:"c",archer:"a",musketeer:"k",tactician:"t"},J=G($),H=(t,[e,r,o])=>{let n=t===t.toLowerCase()?"b":"w",i=J[t.toLowerCase()];return{square:`${e}-${r}`,tier:o,type:N[i],color:n}},V=(t,e)=>{let r=t===t.toLowerCase()?"b":"w",o=J[t.toLowerCase()];return{type:N[o],count:e,color:r}},Se={\u5E25:"sui",\u5927:"taishou",\u4E2D:"chuujou",\u5C0F:"shoushou",\u4F8D:"samurai",\u69CD:"yari",\u99AC:"kiba",\u5FCD:"shinobi",\u7826:"toride",\u5175:"hyou",\u7832:"oodzutsu",\u5F13:"yumi",\u7B52:"tsutsu",\u8B00:"boushou"},Oe={\u5E25:"marshal",\u5927:"general",\u4E2D:"lieutenant general",\u5C0F:"major general",\u4F8D:"warrior",\u69CD:"lancer",\u99AC:"rider",\u5FCD:"spy",\u7826:"fortress",\u5175:"soldier",\u7832:"cannon",\u5F13:"archer",\u7B52:"musketeer",\u8B00:"tactician"},qe={\u5E25:"m",\u5927:"g",\u4E2D:"i",\u5C0F:"j",\u4F8D:"w",\u69CD:"n",\u99AC:"r",\u5FCD:"s",\u7826:"f",\u5175:"d",\u7832:"c",\u5F13:"a",\u7B52:"k",\u8B00:"t"};function _(t,e,r=!1){return t.forEach(o=>{let n=r?o.color==="b"?"w":"b":o.color,i=e.find(a=>a.type===o.type&&a.color===n),s=e.findIndex(a=>a.type===o.type&&a.color===n);i&&(i.count--,i.count===0&&e.splice(s,1))}),e}function Y(t,e,r){let[o,n]=t.split("-").map(Number),i=x(t,r);i&&(r[o-1][9-n]=i.map(s=>(e.find(c=>JSON.stringify(c)===JSON.stringify(s))&&(s.color=s.color==="b"?"w":"b"),s)))}function z(t,e,r){let[o,n]=t.split("-").map(Number),i=x(t,r);if(!i)return;let s=i.filter(a=>!e.some(c=>JSON.stringify(c)===JSON.stringify(a)));s.length===0?r[o-1][9-n]=[null]:r[o-1][9-n]=s}function R(t,e){let[r,o]=t.split("-").map(Number),n=x(t,e);n&&(n.pop(),n.length===0&&(e[r-1][9-o]=[null]))}function Q(t,e){let[r,o]=t.square.split("-").map(Number);e[r-1][9-o][0]?e[r-1][9-o].push(t):e[r-1][9-o]=[t]}function E(t,e){let r=x(t,e);return r?r.at(-1):void 0}function x(t,e){let[r,o]=t.split("-").map(Number);if(r<1||r>9||o<1||o>9)return;let n=e[r-1][9-o];if(n[0])return n}function S(t){return t.flat().flat().filter(r=>r?.type==="\u5E25").length!==2}var X="3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1",De="3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1",Ge="9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 2 wb 1",L="9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 3 wb 1";function C(t){let e=W(t);if(!e.ok)throw new Error(`Invalid FEN: ${e.error}`);let[r,o,n,i,s,a]=t.split(" "),c=r.split("/").map((l,d)=>l.split("|").reduce((u,p)=>{if(p.includes(":")){let m=p.split(":").map((h,y)=>H(h,[d+1,9-u.length,y+1]));u.push(m)}else{let m=p.split("").reduce((h,y)=>(Number.isInteger(+y)?h=h.concat(Array(+y).fill(null)):h.push(H(y,[d+1,9-u.length-h.length,1])),h),[]).map(h=>[h]);u=u.concat(m)}return u},[])),f=o.split("/").flatMap(l=>{let d=[];if(l!=="-")for(let u=0;u<l.length;u+=2){let p=l[u],m=+l[u+1];d.push(V(p,m))}return d});return{board:c,hand:f,turn:n,mode:K[+i],drafting:{w:s.includes("w"),b:s.includes("b")},moveNumber:+a}}function O(t){let e="",r=0;for(let a=0;a<9;a++){for(let c=0;c<9;c++){let f=t.board[a][c];if(f[0]===null){r++;continue}if(r&&(e+=r,r=0),f.length>1)e+="|"+f.map(l=>{if(!l)return null;let d=$[w[l.type]];return l.color==="w"?d.toUpperCase():d}).join(":")+"|";else{let{type:l,color:d}=f[0],u=$[w[l]];e+=d==="w"?u.toUpperCase():u}}e+=r?`${r}/`:"/",r=0}let o="",n=t.hand.filter(a=>a.color==="w"),i=t.hand.filter(a=>a.color==="b");n.forEach(a=>{let c=$[w[a.type]];o+=`${a.color==="w"?c.toUpperCase():c}${a.count}`}),o+=n.length>0?"/":"-/",i.forEach(a=>{let c=$[w[a.type]];o+=`${a.color==="w"?c.toUpperCase():c}${a.count}`}),o+=i.length>0?"":"-";let s=Object.keys(t.drafting).filter(a=>t.drafting[a]);return`${e.slice(0,-1)} ${o} ${t.turn} ${k[t.mode]} ${s.length>0?s.join(""):"-"} ${t.moveNumber}`}function W(t){let e=t.split(" ");if(e.length!==6)return{ok:!1,error:`expected 6 fields, received ${e.length}`};let[r,o,n,i,s,a]=e,c=r.split("/");if(c.length!==9)return{ok:!1,error:`1st field (piece positions) is invalid [expected 9 ranks, received ${c.length}]`};for(let u=0;u<c.length;u++){let m=c[u].split("|"),h=0;for(let y=0;y<m.length;y++){let g=m[y];if(g.includes(":")){h++;let b=g.split(":");if(b.length>3)return{ok:!1,error:`1st field (piece positions) is invalid [tower size] @(${u+1}-${h})`};if(!b.every(P=>"mgijwnrsfdcaktMGIJWNRSFDCAKT".includes(P)))return{ok:!1,error:`1st field (piece positions) is invalid [invalid piece] @(${u+1}-${h})`}}else{let b=g.split("");for(let P=0;P<b.length;P++){let v=b[P];if(Number.isInteger(+v))h+=+v;else if(h++,!"mgijwnrsfdcaktMGIJWNRSFDCAKT".includes(v))return{ok:!1,error:`1st field (piece positions) is invalid [invalid piece] @(${u+1}-${h})`}}}}if(h!==9)return{ok:!1,error:`1st field (piece positions) is invalid [expected 9 squares, received ${h}] in rank: ${u+1}`}}let f=o.split("/");if(f.length!==2)return{ok:!1,error:`2nd field (hand pieces) is invalid [expected 2 hands, received ${f.length}]`};let[l,d]=f;if(l.length===1&&l!=="-"||d.length===1&&d!=="-")return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(l.length>1&&l.length%2!==0||d.length>1&&d.length%2!==0)return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece count]"};if(l.length>1)for(let u=0;u<l.length;u+=2){if(!"MGIJWNRSFDCAKT".includes(l[u]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(!Number.isInteger(+l[u+1]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid count]"}}if(d.length>1)for(let u=0;u<d.length;u+=2){if(!"mgijwnrsfdcakt".includes(d[u]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(!Number.isInteger(+d[u+1]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid count]"}}return n!=="w"&&n!=="b"?{ok:!1,error:`3rd field (active player) is invalid [expected 'w' or 'b', received ${n}]`}:i.length!==1||!"0123".includes(i)?{ok:!1,error:`4th field (setup mode) is invalid [expected '0', '1', '2', or '3', received ${i}]`}:s!=="-"&&!"wb".includes(s)?{ok:!1,error:`5th field (drafting availability) is invalid [expected '-', 'w', 'b', or 'wb', received ${s}]`}:Number.isInteger(+a)?{ok:!0}:{ok:!1,error:`6th field (full move number) is invalid [expected an integer, received ${a}]`}}var Z=[[-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]],ee={\u5E25:[1,1,1,1,1,1,1,1],\u5927:[1,1/0,1,1/0,1/0,1,1/0,1],\u4E2D:[1/0,1,1/0,1,1,1/0,1,1/0],\u5C0F:[1,1,1,1,1,0,1,0],\u4F8D:[1,1,1,0,0,0,1,0],\u69CD:[1,[1,2],1,0,0,0,1,0],\u99AC:[0,[1,2],0,1,1,0,[1,2],0],\u5FCD:[[1,2],0,[1,2],0,0,[1,2],0,[1,2]],\u7826:[0,1,0,1,1,1,0,1],\u5175:[0,1,0,0,0,0,1,0],\u7832:[0,3,0,1,1,0,1,0],\u5F13:[2,2,2,0,0,0,1,0],\u7B52:[0,2,0,0,0,1,0,1],\u8B00:[1,0,1,0,0,0,1,0]};function re(t,e,r,o,n){let[i,s]=e,[a,c]=r,[f,l]=t,d=E(`${a}-${c}`,n),u=[],p={x:s,y:i};for(;p.x!==c||p.y!==a;){p.x-=l,p.y-=f;let y=E(`${p.y}-${p.x}`,n);if(y&&y.tier>d.tier)return[];let g=d.color==="b"?-1:1,b=E(`${p.y+g}-${p.x}`,n);if(b&&b.square===`${a}-${c}`)break}let m={x:s,y:i},h=0;for(;h<o&&!(m.x<1||m.x>9||m.y<1||m.y>9);){let y=E(`${m.y}-${m.x}`,n);if(y&&y.tier>d.tier)break;u.push(`${m.y}-${m.x}`);let g=[N.cannon,N.musketeer,N.archer];if(y&&!g.includes(d.type))break;m.x+=l,m.y+=f,h++}return u}function q(t,e){let{board:r,turn:o,mode:n,hand:i}=C(e),s=E(t,r);if(!s||o!==s.color)return[];let[a,c]=t.split("-").map(Number);return ee[s.type].flatMap((d,u)=>{let p=typeof d=="number"?d:d[0],m=typeof d=="number"?1:d[1];if(p<1)return[];let[h,y]=Z[u];s.color==="b"&&(h*=-1,y*=-1);let[g,b]=p===1/0?[a+h,c+y]:[a+p*h,c+y],P=p===1/0?1/0:s.tier+m-1;return re([h,y],[g,b],[a,c],P,r)}).reduce((d,u)=>{let p=E(u,r),m=x(u,r),h=n==="advanced"?3:2,y=["advanced","intermediate"].includes(n);if(!p||!m)d.push(I(s,`${u}-1`,e,"route"));else{if(p.tier<h&&p.type!==N.marshal&&((s.type!==N.marshal||y)&&d.push(I(s,`${u}-${p.tier+1}`,e,"tsuke")),s.type===N.tactician&&p.color!==s.color)){let g=m.filter(T=>T.color!==s.color),b=i.filter(T=>T.color===s.color),P=g.reduce((T,A)=>(T.set(A.type,(T.get(A.type)??0)+1),T),new Map),v=Array.from(P.entries()).filter(([T,A])=>b.some(B=>B.type===T&&B.count>=A)).flatMap(([T])=>g.filter(A=>A.type===T)),pe=_e(v);d.push(...pe.map(T=>I(s,`${u}-${p.tier+1}`,e,"betray",T)))}if(p.color!==s.color){let g=m.filter(P=>P.color===s.color).length+1,b=m.filter(P=>P.color!==s.color);d.push(I(s,`${u}-${g}`,e,"capture",b))}}return d},[])}function M(t,e){let{board:r,turn:o,mode:n,hand:i,drafting:s}=C(e);if(!t||o!==t.color)return[];if(!!i.some(p=>p.type===N.marshal&&p.color===t.color)&&t.type!==N.marshal)return[];let c=s.b||s.w,f=n==="advanced"?3:2,l=[],d=[];if(c)l=t.color==="w"?[7,8,9]:[1,2,3];else{let p=t.color==="b"?1:9,m=t.color==="b"?9:1,h=t.color==="b"?1:-1;for(let y=p;t.color==="b"?y<=m:y>=m;y+=h)Array.from({length:9}).map((b,P)=>E(`${y}-${P+1}`,r)).some(b=>b&&b.color===t.color)?(l.push(...d,y),d=[]):d.push(y)}return l.flatMap(p=>Array.from({length:9},(m,h)=>`${p}-${h+1}`)).reduce((p,m)=>{let h=E(m,r),y={type:t.type,color:t.color,square:m,tier:0};if(!h||h.color===y.color&&h.tier<f&&h.type!==N.marshal){let g=(h?.tier??0)+1;p.push(I(y,`${m}-${g}`,e,"arata")),s[t.color]&&p.push(I(y,`${m}-${g}`,e,"arata",[],!0))}return p},[])}function I(t,e,r,o,n,i){let s=t.tier!==0?`(${t.square}-${t.tier})`:"",a=+(e.split("-").at(-1)??0),c=o==="arata"?"\u65B0":"",f=o==="capture"?"\u53D6":"",l=o==="tsuke"||a!==1&&a-t.tier>0?"\u4ED8":"",d=o==="betray"?`\u8FD4${n?.map(y=>y.type).join("")}`:"",u=i?"\u7D42":"",p={piece:t.type,color:t.color,from:s.slice(1,-1),to:e,type:o,san:`${c}${t.type}${s}${f}(${e})${d||l}${u}`,before:r,after:"",draftFinished:i,captured:n},{after:m,gameOverSAN:h}=He(p,p.before);return p.after=m,p.san+=h,p}function He(t,e){let{board:r,hand:o,mode:n,turn:i,drafting:s,moveNumber:a}=C(e),[c,f,l]=t.to.split("-"),d={type:t.piece,color:t.color,square:`${c}-${f}`,tier:+l};t.type==="route"||t.type==="tsuke"?R(t.from,r):t.type==="capture"?(R(t.from,r),z(`${c}-${f}`,t.captured,r)):t.type==="betray"?(R(t.from,r),Y(`${c}-${f}`,t.captured,r),_(t.captured,o,!0)):t.type==="arata"&&(_([d],o),t.draftFinished&&(s[t.color]=!1)),Q(d,r),i==="b"&&(s.w||!s.b)&&a++,i==="w"&&!s.b&&!s.w&&t.draftFinished&&a++,s.w===s.b?(!t.draftFinished||i!=="w")&&(i=i==="b"?"w":"b"):!s.b&&i==="b"?i="w":!s.w&&i==="w"&&(i="b");let u="";return t.captured?.some(p=>p.type===N.marshal)&&(u="#"),{after:O({board:r,hand:o,mode:n,turn:i,drafting:s,moveNumber:a}),gameOverSAN:u}}function _e(t){let e=[];function r(o,n){n.length>0&&e.push([...n]);for(let i=o;i<t.length;i++)n.push(t[i]),r(i+1,n),n.pop()}return r(0,[]),e}function Le(t,e){let r=E(t,e);if(!r)return[];let[o,n]=t.split("-").map(Number);return ee[r.type].flatMap((s,a)=>{let c=typeof s=="number"?s:s[0],f=typeof s=="number"?1:s[1];if(c<1)return[];let[l,d]=Z[a];r.color==="b"&&(l*=-1,d*=-1);let[u,p]=c===1/0?[o+l,n+d]:[o+c*l,n+d],m=c===1/0?1/0:r.tier+f-1;return re([l,d],[u,p],[o,n],m,e)})}function te(t,e,r){let[o,n]=t.split("-").map(Number),i=`${o}-${n}`;for(let s of F){let a=E(s,r);if(!a||a.color!==e)continue;if(Le(s,r).some(f=>f.startsWith(i)))return!0}return!1}function oe(t,e){let{board:r}=C(e),o=t==="w"?"b":"w";for(let n of F){let i=E(n,r);if(i?.type===N.marshal&&i.color===t)return te(n,o,r)}return!1}function ne(t,e){let r=e?.maxWidth??6,o=e?.newline??`
|
|
2
|
-
`,n="",
|
|
3
|
-
`;return t.split(r).flatMap(n=>n.replace(/\d+\.+/g,"").split(" ")).filter(n=>n!=="")}function se(t,e,r){let o=
|
|
1
|
+
import D from"picocolors";var G=t=>Object.entries(t).reduce((e,[r,o])=>({...e,[o]:r}),{}),w={intro:0,beginner:1,intermediate:2,advanced:3},K=G(w),he="w",me="b",ge="\u4ED8",ye="\u53D6",be="\u8FD4",Pe="\u65B0",Ne="\u5E25",Ce="\u5927",Ee="\u4E2D",Te="\u5C0F",$e="\u4F8D",xe="\u69CD",we="\u99AC",ke="\u5FCD",Ae="\u7826",Me="\u5175",Fe="\u7832",Ie="\u5F13",ve="\u7B52",Re="\u8B00",M=Array.from({length:9},(t,e)=>e+1).flatMap(t=>Array.from({length:9},(e,r)=>`${t}-${9-r}`)),N={marshal:"\u5E25",general:"\u5927",lieutenant_general:"\u4E2D",major_general:"\u5C0F",warrior:"\u4F8D",lancer:"\u69CD",rider:"\u99AC",spy:"\u5FCD",fortress:"\u7826",soldier:"\u5175",cannon:"\u7832",archer:"\u5F13",musketeer:"\u7B52",tactician:"\u8B00"},k=G(N),$={marshal:"m",general:"g",lieutenant_general:"i",major_general:"j",warrior:"w",lancer:"n",rider:"r",spy:"s",fortress:"f",soldier:"d",cannon:"c",archer:"a",musketeer:"k",tactician:"t"},J=G($),H=(t,[e,r,o])=>{let n=t===t.toLowerCase()?"b":"w",i=J[t.toLowerCase()];return{square:`${e}-${r}`,tier:o,type:N[i],color:n}},V=(t,e)=>{let r=t===t.toLowerCase()?"b":"w",o=J[t.toLowerCase()];return{type:N[o],count:e,color:r}},Se={\u5E25:"sui",\u5927:"taishou",\u4E2D:"chuujou",\u5C0F:"shoushou",\u4F8D:"samurai",\u69CD:"yari",\u99AC:"kiba",\u5FCD:"shinobi",\u7826:"toride",\u5175:"hyou",\u7832:"oodzutsu",\u5F13:"yumi",\u7B52:"tsutsu",\u8B00:"boushou"},Oe={\u5E25:"marshal",\u5927:"general",\u4E2D:"lieutenant general",\u5C0F:"major general",\u4F8D:"warrior",\u69CD:"lancer",\u99AC:"rider",\u5FCD:"spy",\u7826:"fortress",\u5175:"soldier",\u7832:"cannon",\u5F13:"archer",\u7B52:"musketeer",\u8B00:"tactician"},qe={\u5E25:"m",\u5927:"g",\u4E2D:"i",\u5C0F:"j",\u4F8D:"w",\u69CD:"n",\u99AC:"r",\u5FCD:"s",\u7826:"f",\u5175:"d",\u7832:"c",\u5F13:"a",\u7B52:"k",\u8B00:"t"};function _(t,e,r=!1){return t.forEach(o=>{let n=r?o.color==="b"?"w":"b":o.color,i=e.find(a=>a.type===o.type&&a.color===n),s=e.findIndex(a=>a.type===o.type&&a.color===n);i&&(i.count--,i.count===0&&e.splice(s,1))}),e}function Y(t,e,r){let[o,n]=t.split("-").map(Number),i=x(t,r);i&&(r[o-1][9-n]=i.map(s=>(e.find(c=>JSON.stringify(c)===JSON.stringify(s))&&(s.color=s.color==="b"?"w":"b"),s)))}function z(t,e,r){let[o,n]=t.split("-").map(Number),i=x(t,r);if(!i)return;let s=i.filter(a=>!e.some(c=>JSON.stringify(c)===JSON.stringify(a)));s.length===0?r[o-1][9-n]=[null]:r[o-1][9-n]=s}function R(t,e){let[r,o]=t.split("-").map(Number),n=x(t,e);n&&(n.pop(),n.length===0&&(e[r-1][9-o]=[null]))}function Q(t,e){let[r,o]=t.square.split("-").map(Number);e[r-1][9-o][0]?e[r-1][9-o].push(t):e[r-1][9-o]=[t]}function C(t,e){let r=x(t,e);return r?r.at(-1):void 0}function x(t,e){let[r,o]=t.split("-").map(Number);if(r<1||r>9||o<1||o>9)return;let n=e[r-1][9-o];if(n[0])return n}function S(t){return t.flat().flat().filter(r=>r?.type==="\u5E25").length!==2}var X="3img3/1s2n2s1/d1fwdwf1d/9/9/9/D1FWDWF1D/1S2N2S1/3GMI3 J2N2R2D1/j2n2r2d1 w 0 - 1",De="3img3/1ra1n1as1/d1fwdwf1d/9/9/9/D1FWDWF1D/1SA1N1AR1/3GMI3 J2N2S1R1D1/j2n2s1r1d1 w 1 - 1",Ge="9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 2 wb 1",L="9/9/9/9/9/9/9/9/9 M1G1I1J2W2N3R2S2F2D4C1A2K1T1/m1g1i1j2w2n3r2s2f2d4c1a2k1t1 w 3 wb 1";function T(t){let e=W(t);if(!e.ok)throw new Error(`Invalid FEN: ${e.error}`);let[r,o,n,i,s,a]=t.split(" "),c=r.split("/").map((l,d)=>l.split("|").reduce((u,p)=>{if(p.includes(":")){let m=p.split(":").map((h,g)=>H(h,[d+1,9-u.length,g+1]));u.push(m)}else{let m=p.split("").reduce((h,g)=>(Number.isInteger(+g)?h=h.concat(Array(+g).fill(null)):h.push(H(g,[d+1,9-u.length-h.length,1])),h),[]).map(h=>[h]);u=u.concat(m)}return u},[])),f=o.split("/").flatMap(l=>{let d=[];if(l!=="-")for(let u=0;u<l.length;u+=2){let p=l[u],m=+l[u+1];d.push(V(p,m))}return d});return{board:c,hand:f,turn:n,mode:K[+i],drafting:{w:s.includes("w"),b:s.includes("b")},moveNumber:+a}}function O(t){let e="",r=0;for(let a=0;a<9;a++){for(let c=0;c<9;c++){let f=t.board[a][c];if(!f[0]){r++;continue}if(r&&(e+=r,r=0),f.length>1)e+="|"+f.map(l=>{if(!l)return null;let d=$[k[l.type]];return l.color==="w"?d.toUpperCase():d}).join(":")+"|";else{let{type:l,color:d}=f[0],u=$[k[l]];e+=d==="w"?u.toUpperCase():u}}e+=r?`${r}/`:"/",r=0}let o="",n=t.hand.filter(a=>a.color==="w"),i=t.hand.filter(a=>a.color==="b");n.forEach(a=>{let c=$[k[a.type]];o+=`${a.color==="w"?c.toUpperCase():c}${a.count}`}),o+=n.length>0?"/":"-/",i.forEach(a=>{let c=$[k[a.type]];o+=`${a.color==="w"?c.toUpperCase():c}${a.count}`}),o+=i.length>0?"":"-";let s=Object.keys(t.drafting).filter(a=>t.drafting[a]);return`${e.slice(0,-1)} ${o} ${t.turn} ${w[t.mode]} ${s.length>0?s.join(""):"-"} ${t.moveNumber}`}function W(t){let e=t.split(" ");if(e.length!==6)return{ok:!1,error:`expected 6 fields, received ${e.length}`};let[r,o,n,i,s,a]=e,c=r.split("/");if(c.length!==9)return{ok:!1,error:`1st field (piece positions) is invalid [expected 9 ranks, received ${c.length}]`};for(let u=0;u<c.length;u++){let m=c[u].split("|"),h=0;for(let g=0;g<m.length;g++){let y=m[g];if(y.includes(":")){h++;let b=y.split(":");if(b.length>3)return{ok:!1,error:`1st field (piece positions) is invalid [tower size] @(${u+1}-${h})`};if(!b.every(P=>"mgijwnrsfdcaktMGIJWNRSFDCAKT".includes(P)))return{ok:!1,error:`1st field (piece positions) is invalid [invalid piece] @(${u+1}-${h})`}}else{let b=y.split("");for(let P=0;P<b.length;P++){let v=b[P];if(Number.isInteger(+v))h+=+v;else if(h++,!"mgijwnrsfdcaktMGIJWNRSFDCAKT".includes(v))return{ok:!1,error:`1st field (piece positions) is invalid [invalid piece] @(${u+1}-${h})`}}}}if(h!==9)return{ok:!1,error:`1st field (piece positions) is invalid [expected 9 squares, received ${h}] in rank: ${u+1}`}}let f=o.split("/");if(f.length!==2)return{ok:!1,error:`2nd field (hand pieces) is invalid [expected 2 hands, received ${f.length}]`};let[l,d]=f;if(l.length===1&&l!=="-"||d.length===1&&d!=="-")return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(l.length>1&&l.length%2!==0||d.length>1&&d.length%2!==0)return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece count]"};if(l.length>1)for(let u=0;u<l.length;u+=2){if(!"MGIJWNRSFDCAKT".includes(l[u]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(!Number.isInteger(+l[u+1]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid count]"}}if(d.length>1)for(let u=0;u<d.length;u+=2){if(!"mgijwnrsfdcakt".includes(d[u]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid piece]"};if(!Number.isInteger(+d[u+1]))return{ok:!1,error:"2nd field (hand pieces) is invalid [invalid count]"}}return n!=="w"&&n!=="b"?{ok:!1,error:`3rd field (active player) is invalid [expected 'w' or 'b', received ${n}]`}:i.length!==1||!"0123".includes(i)?{ok:!1,error:`4th field (setup mode) is invalid [expected '0', '1', '2', or '3', received ${i}]`}:s!=="-"&&!"wb".includes(s)?{ok:!1,error:`5th field (drafting availability) is invalid [expected '-', 'w', 'b', or 'wb', received ${s}]`}:Number.isInteger(+a)?{ok:!0}:{ok:!1,error:`6th field (full move number) is invalid [expected an integer, received ${a}]`}}var Z=[[-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]],ee={\u5E25:[1,1,1,1,1,1,1,1],\u5927:[1,1/0,1,1/0,1/0,1,1/0,1],\u4E2D:[1/0,1,1/0,1,1,1/0,1,1/0],\u5C0F:[1,1,1,1,1,0,1,0],\u4F8D:[1,1,1,0,0,0,1,0],\u69CD:[1,[1,2],1,0,0,0,1,0],\u99AC:[0,[1,2],0,1,1,0,[1,2],0],\u5FCD:[[1,2],0,[1,2],0,0,[1,2],0,[1,2]],\u7826:[0,1,0,1,1,1,0,1],\u5175:[0,1,0,0,0,0,1,0],\u7832:[0,3,0,1,1,0,1,0],\u5F13:[2,2,2,0,0,0,1,0],\u7B52:[0,2,0,0,0,1,0,1],\u8B00:[1,0,1,0,0,0,1,0]};function re(t,e,r,o,n){let[i,s]=e,[a,c]=r,[f,l]=t,d=C(`${a}-${c}`,n),u=[],p={x:s,y:i};for(;p.x!==c||p.y!==a;){p.x-=l,p.y-=f;let g=C(`${p.y}-${p.x}`,n);if(g&&g.tier>d.tier)return[];let y=d.color==="b"?-1:1,b=C(`${p.y+y}-${p.x}`,n);if(b&&b.square===`${a}-${c}`)break}let m={x:s,y:i},h=0;for(;h<o&&!(m.x<1||m.x>9||m.y<1||m.y>9);){let g=C(`${m.y}-${m.x}`,n);if(g&&g.tier>d.tier)break;u.push(`${m.y}-${m.x}`);let y=[N.cannon,N.musketeer,N.archer];if(g&&!y.includes(d.type))break;m.x+=l,m.y+=f,h++}return u}function q(t,e){let{board:r,turn:o,mode:n,hand:i}=T(e),s=C(t,r);if(!s||o!==s.color)return[];let[a,c]=t.split("-").map(Number);return ee[s.type].flatMap((d,u)=>{let p=typeof d=="number"?d:d[0],m=typeof d=="number"?1:d[1];if(p<1)return[];let[h,g]=Z[u];s.color==="b"&&(h*=-1,g*=-1);let[y,b]=p===1/0?[a+h,c+g]:[a+p*h,c+g],P=p===1/0?1/0:s.tier+m-1;return re([h,g],[y,b],[a,c],P,r)}).reduce((d,u)=>{let p=C(u,r),m=x(u,r),h=n==="advanced"?3:2,g=["advanced","intermediate"].includes(n);if(!p||!m)d.push(F(s,`${u}-1`,e,"route"));else{if(p.tier<h&&p.type!==N.marshal&&((s.type!==N.marshal||g)&&d.push(F(s,`${u}-${p.tier+1}`,e,"tsuke")),s.type===N.tactician&&p.color!==s.color)){let y=m.filter(E=>E.color!==s.color),b=i.filter(E=>E.color===s.color),P=y.reduce((E,A)=>(E.set(A.type,(E.get(A.type)??0)+1),E),new Map),v=Array.from(P.entries()).filter(([E,A])=>b.some(B=>B.type===E&&B.count>=A)).flatMap(([E])=>y.filter(A=>A.type===E)),pe=_e(v);d.push(...pe.map(E=>F(s,`${u}-${p.tier+1}`,e,"betray",E)))}if(p.color!==s.color){let y=m.filter(P=>P.color===s.color).length+1,b=m.filter(P=>P.color!==s.color);d.push(F(s,`${u}-${y}`,e,"capture",b))}}return d},[])}function I(t,e){let{board:r,turn:o,mode:n,hand:i,drafting:s}=T(e);if(!t||o!==t.color)return[];if(!!i.some(p=>p.type===N.marshal&&p.color===t.color)&&t.type!==N.marshal)return[];let c=s.b||s.w,f=n==="advanced"?3:2,l=[],d=[];if(c)l=t.color==="w"?[7,8,9]:[1,2,3];else{let p=t.color==="b"?1:9,m=t.color==="b"?9:1,h=t.color==="b"?1:-1;for(let g=p;t.color==="b"?g<=m:g>=m;g+=h)Array.from({length:9}).map((b,P)=>C(`${g}-${P+1}`,r)).some(b=>b&&b.color===t.color)?(l.push(...d,g),d=[]):d.push(g)}return l.flatMap(p=>Array.from({length:9},(m,h)=>`${p}-${h+1}`)).reduce((p,m)=>{let h=C(m,r),g={type:t.type,color:t.color,square:m,tier:0};if(!h||h.color===g.color&&h.tier<f&&h.type!==N.marshal){let y=(h?.tier??0)+1;p.push(F(g,`${m}-${y}`,e,"arata")),s[t.color]&&p.push(F(g,`${m}-${y}`,e,"arata",[],!0))}return p},[])}function F(t,e,r,o,n,i){let s=t.tier!==0?`(${t.square}-${t.tier})`:"",a=+(e.split("-").at(-1)??0),c=o==="arata"?"\u65B0":"",f=o==="capture"?"\u53D6":"",l=o==="tsuke"||a!==1&&a-t.tier>0?"\u4ED8":"",d=o==="betray"?`\u8FD4${n?.map(g=>g.type).join("")}`:"",u=i?"\u7D42":"",p={piece:t.type,color:t.color,from:s.slice(1,-1),to:e,type:o,san:`${c}${t.type}${s}${f}(${e})${d||l}${u}`,before:r,after:"",draftFinished:i,captured:n},{after:m,gameOverSAN:h}=He(p,p.before);return p.after=m,p.san+=h,p}function He(t,e){let{board:r,hand:o,mode:n,turn:i,drafting:s,moveNumber:a}=T(e),[c,f,l]=t.to.split("-"),d={type:t.piece,color:t.color,square:`${c}-${f}`,tier:+l};t.type==="route"||t.type==="tsuke"?R(t.from,r):t.type==="capture"?(R(t.from,r),z(`${c}-${f}`,t.captured,r)):t.type==="betray"?(R(t.from,r),Y(`${c}-${f}`,t.captured,r),_(t.captured,o,!0)):t.type==="arata"&&(_([d],o),t.draftFinished&&(s[t.color]=!1)),Q(d,r),i==="b"&&(s.w||!s.b)&&a++,i==="w"&&!s.b&&!s.w&&t.draftFinished&&a++,s.w===s.b?(!t.draftFinished||i!=="w")&&(i=i==="b"?"w":"b"):!s.b&&i==="b"?i="w":!s.w&&i==="w"&&(i="b");let u="";return t.captured?.some(p=>p.type===N.marshal)&&(u="#"),{after:O({board:r,hand:o,mode:n,turn:i,drafting:s,moveNumber:a}),gameOverSAN:u}}function _e(t){let e=[];function r(o,n){n.length>0&&e.push([...n]);for(let i=o;i<t.length;i++)n.push(t[i]),r(i+1,n),n.pop()}return r(0,[]),e}function Le(t,e){let r=C(t,e);if(!r)return[];let[o,n]=t.split("-").map(Number);return ee[r.type].flatMap((s,a)=>{let c=typeof s=="number"?s:s[0],f=typeof s=="number"?1:s[1];if(c<1)return[];let[l,d]=Z[a];r.color==="b"&&(l*=-1,d*=-1);let[u,p]=c===1/0?[o+l,n+d]:[o+c*l,n+d],m=c===1/0?1/0:r.tier+f-1;return re([l,d],[u,p],[o,n],m,e)})}function te(t,e,r){let[o,n]=t.split("-").map(Number),i=`${o}-${n}`;for(let s of M){let a=C(s,r);if(!a||a.color!==e)continue;if(Le(s,r).some(f=>f.startsWith(i)))return!0}return!1}function oe(t,e){let{board:r}=T(e),o=t==="w"?"b":"w";for(let n of M){let i=C(n,r);if(i?.type===N.marshal&&i.color===t)return te(n,o,r)}return!1}function ne(t,e){let r=e?.maxWidth??6,o=e?.newline??`
|
|
2
|
+
`,n=e?.withComments??!1,i=e?.comments??new Map,s="",a=0;return t.forEach((c,f)=>{let l=T(c.before);if(l.moveNumber!==a){let d=c.color==="w"?".":"...";s+=`${l.moveNumber}${d} `,a=l.moveNumber}s+=`${c.san} `,n&&i.has(c.after)&&(s+=`{${i.get(c.after)}} `),(f+1)%r===0&&(s+=o)}),s.replaceAll(" "," ").trim()}function ie(t,e){let r=e?.newline??`
|
|
3
|
+
`;return t.split(r).flatMap(n=>n.replace(/\d+\.+/g,"").split(" ")).filter(n=>n!=="")}function se(t,e,r){let o=T(t);return e?r?je(o,e,r):ze(o,e):We(o)}function We(t){let e=ue(t),r=w[t.mode],o=de(e);return Object.entries(o).forEach(([n,i])=>{let[s,a]=n.split("|");Qe(i).forEach((f,l)=>{f.id=`${r}-${s}-${a}-${l+1}`})}),e}function je(t,e,r){let o=Xe(t),n=w[t.mode],i=Ue(r);return Be(o,e,i),Ke(o,e,r,n),ae(o,n),o}function Ue(t){let e=[],r=[],o,n;switch(t.type){case"route":case"tsuke":if(t.from){let[i,s,a]=t.from.split("-").map(Number);o={square:`${i}-${s}`,tier:a,type:t.piece,color:t.color}}break;case"capture":if(t.from){let[i,s,a]=t.from.split("-").map(Number);o={square:`${i}-${s}`,tier:a,type:t.piece,color:t.color}}t.captured&&t.captured.forEach(i=>{e.push({square:t.to,tier:i.tier,type:i.type,color:i.color})});break;case"arata":r.push({color:t.color,type:t.piece}),n=t.to;break;case"betray":if(t.from){let[i,s,a]=t.from.split("-").map(Number);o={square:`${i}-${s}`,tier:a,type:t.piece,color:t.color}}t.captured&&(t.captured.forEach(i=>{e.push({square:t.to,tier:i.tier,type:i.type,color:i.color})}),t.captured.forEach(i=>{r.push({color:t.color,type:i.type})}));break}return{movingPiece:o,capturedPieces:e,placementSquare:n,handPieceTypes:r}}function Be(t,e,r){let o=(n,i,s,a)=>!!(r.movingPiece&&r.movingPiece.square===n&&r.movingPiece.tier===i&&r.movingPiece.type===s&&r.movingPiece.color===a||r.capturedPieces.some(c=>c.square===n&&c.tier===i&&c.type===s&&c.color===a)||r.placementSquare===n);for(let n=0;n<9;n++)for(let i=0;i<9;i++){let s=`${n+1}-${9-i}`,a=t.board[n][i],c=e.board[n][i];for(let f=0;f<Math.min(a.length,c.length);f++){let l=a[f],d=c[f];l&&d&&l.type===d.type&&l.color===d.color&&d.id&&(o(s,f+1,l.type,l.color)||(l.id=d.id))}}t.hand.forEach(n=>{if(!r.handPieceTypes.some(s=>s.color===n.color&&s.type===n.type)){let s=e.hand.find(a=>a.type===n.type&&a.color===n.color);s?.id&&(n.id=s.id)}})}function Ke(t,e,r,o){switch(r.type){case"route":case"tsuke":U(t,e,r);break;case"capture":Je(t,e,r);break;case"arata":Ve(t,e,r,o);break;case"betray":Ye(t,e,r,o);break}}function U(t,e,r){if(!r.from)return;let[o,n,i]=r.from.split("-").map(Number),s=`${o}-${n}`,a=j(e.board,s,r.piece,r.color,i);if(!a?.id)return;let[c,f,l]=r.to.split("-").map(Number),d=`${c}-${f}`,u=j(t.board,d,r.piece,r.color,l);u&&(u.id=a.id)}function Je(t,e,r){U(t,e,r)}function Ve(t,e,r,o){let n=e.hand.find(a=>a.type===r.piece&&a.color===r.color);if(!n?.id)return;let i=t.hand.find(a=>a.type===r.piece&&a.color===r.color);i&&(i.id=n.id);let s=j(t.board,r.to,r.piece,r.color);if(s){let a=ce(t,o,r.color,r.piece);s.id=a}}function Ye(t,e,r,o){U(t,e,r),r.captured&&r.captured.forEach(n=>{let i=t.hand.find(s=>s.type===n.type&&s.color===r.color);if(i&&!i.id){let s=ce(t,o,r.color,n.type);i.id=s}})}function ze(t,e){let r=ue(t),o=w[t.mode];for(let n=0;n<9;n++)for(let i=0;i<9;i++){let s=r.board[n][i],a=e.board[n][i];for(let c=0;c<Math.min(s.length,a.length);c++){let f=s[c],l=a[c];f&&l&&f.type===l.type&&f.color===l.color&&l.id&&(f.id=l.id)}}return r.hand.forEach(n=>{let i=e.hand.find(s=>s.type===n.type&&s.color===n.color);i?.id&&(n.id=i.id)}),ae(r,o),r}function ae(t,e){let r=new Set(le(t)),o=de(t);Object.entries(o).forEach(([n,i])=>{let[s,a]=n.split("|");i.forEach(c=>{if(!c.id){let f=1,l=`${e}-${s}-${a}-${f}`;for(;r.has(l);)f++,l=`${e}-${s}-${a}-${f}`;c.id=l,r.add(l)}})})}function ce(t,e,r,o){let n=new Set(le(t)),i=1,s=`${e}-${r}-${o}-${i}`;for(;n.has(s);)i++,s=`${e}-${r}-${o}-${i}`;return s}function le(t){let e=[];for(let r of t.board)for(let o of r)for(let n of o)n?.id&&e.push(n.id);for(let r of t.hand)r.id&&e.push(r.id);return e}function de(t){let e={};for(let r of t.board)for(let o of r)for(let n of o)if(n){let i=`${n.color}|${n.type}`;e[i]||(e[i]=[]),e[i].push(n)}for(let r of t.hand){let o=`${r.color}|${r.type}`;e[o]||(e[o]=[]),e[o].push(r)}return e}function Qe(t){return t.sort((e,r)=>{let o="square"in e,n="square"in r;if(o&&!n)return-1;if(!o&&n)return 1;if(o&&n){let i=e,s=r;if(i.square!==s.square){let[a,c]=i.square.split("-").map(Number),[f,l]=s.square.split("-").map(Number);return a!==f?a-f:c-l}return i.tier-s.tier}return 0})}function j(t,e,r,o,n){let[i,s]=e.split("-").map(Number);if(i<1||i>9||s<1||s>9)return null;let a=t[i-1][9-s];for(let c of a)if(c&&(!r||c.type===r)&&(!o||c.color===o)&&(!n||c.tier===n))return c;return null}function ue(t){return{board:t.board.map(e=>e.map(r=>r.map(o=>o&&{...o}))),hand:t.hand.map(e=>({...e})),turn:t.turn,mode:t.mode,drafting:{...t.drafting},moveNumber:t.moveNumber}}function Xe(t){return{board:t.board.map(e=>e.map(r=>r.map(o=>o&&{...o,id:void 0}))),hand:t.hand.map(e=>({...e,id:void 0})),turn:t.turn,mode:t.mode,drafting:{...t.drafting},moveNumber:t.moveNumber}}var fe=class{#t;#n;#s;#a;#i;#d;#e=[];#u=null;#r=new Map;#c;#o(e,r){let o=se(e,this.#u,r);this.#t=o.board,this.#n=o.hand,this.#s=o.turn,this.#a=o.moveNumber,this.#i=o.drafting,this.#d=o.mode,this.#u=o}constructor(e){this.#c=e??X,this.#o(this.#c)}ascii(e){let r={1:D.yellow,2:D.blue,3:D.green},o=`\u3000\uFF19\uFF18\uFF17\uFF16\uFF15\uFF14\uFF13\uFF12\uFF11\u3000
|
|
4
4
|
`;o+=`\uFF0B\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\uFF0B
|
|
5
|
-
`;for(let n=1;n<=9;n++){o+="\uFF5C";for(let i=9;i>0;i--){let s=this.getTop(`${n}-${i}`);if(!s){o+="\u30FB";continue}let a=$[
|
|
5
|
+
`;for(let n=1;n<=9;n++){o+="\uFF5C";for(let i=9;i>0;i--){let s=this.getTop(`${n}-${i}`);if(!s){o+="\u30FB";continue}let a=$[k[s.type]],c=s.color==="w"?a.toUpperCase():a,f=r[s.tier](e?.english?String.fromCharCode(c.charCodeAt(0)+65248):s.type);o+=s.color==="w"?f:D.dim(f)}o+=`\uFF5C${String.fromCharCode(n-1+65297)}
|
|
6
6
|
`}return o+=`\uFF0B\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\u30FC\uFF0B
|
|
7
|
-
`,o}board(){return this.#
|
|
7
|
+
`,o}board(){return this.#t}captured(e){let o=this.#e.flatMap(n=>n.captured??[]).reduce((n,i)=>{let s=n.find(a=>a.type===i.type&&a.color===i.color);return s?s.count++:n.push({type:i.type,color:i.color,count:1}),n},[]);return e?o.filter(n=>n.color===e):o}clear(){this.#o(L),this.#e=[],this.#r.clear()}fen(){return O({board:this.#t,hand:this.#n,turn:this.#s,mode:this.#d,drafting:this.#i,moveNumber:this.#a})}get(e){return x(e,this.#t)}getDraftingRights(e){return e?this.#i[e]:this.#i}getTop(e){return C(e,this.#t)}hand(e){return e?this.#n.filter(r=>r.color===e):this.#n}history(e){return e?.verbose?this.#e:this.#e.map(r=>r.san)}inDraft(){return this.#i.b||this.#i.w}isFourfoldRepetition(){let e=this.#e.map(o=>o.before.split(" ")[0]);e.length&&e.push(this.#e.at(-1).after.split(" ")[0]);let r=e.reduce((o,n)=>(o.set(n,(o.get(n)||0)+1),o),new Map);return Math.max(...r.values())===4}#l(){let e=this.fen();if(this.inDraft())return this.#n.every(r=>I(r,e).length===0);for(let r of M)if(q(r,e).length>0)return!1;for(let r of this.#n)if(I(r,e).length>0)return!1;return!0}inCheck(e){let r=e??this.turn();return oe(r,this.fen())}isCheckmate(){return this.inDraft()||S(this.#t)?!1:this.#l()&&this.inCheck()}isStalemate(){return this.inDraft()||S(this.#t)?!1:this.#l()&&!this.inCheck()}isDraw(){return this.isStalemate()||this.isFourfoldRepetition()}isGameOver(){return this.inDraft()?!1:S(this.#t)||this.isFourfoldRepetition()?!0:this.#l()}load(e){this.#o(e)}loadPgn(e,r=L,o){this.#o(r),this.#e=[],ie(e,o).forEach(i=>this.move(i))}move(e){let r=this.moves({verbose:!0}),o=typeof e=="string"?r.find(n=>n.san===e):r.find(n=>e.piece===n.piece&&e.type===n.type&&e.from===n.from&&e.to===n.to&&e.captured===n.captured&&e.draftFinished===n.draftFinished);if(!o)throw new Error(`Illegal move: ${e}`);return this.#e.push(o),this.#o(o.after,o),this.isCheckmate()?this.#e.at(-1).san+="#":(this.isStalemate()||this.isFourfoldRepetition())&&(this.#e.at(-1).san+="="),o}moves(e){if(this.isGameOver())return[];if(e?.square){let r=q(e.square,this.fen());return e.verbose?r:r.map(o=>o.san)}else if(e?.arata){let r=I(e.arata,this.fen());return e.verbose?r:r.map(o=>o.san)}else if(this.inDraft()){let r=this.hand().flatMap(o=>I(o,this.fen()));return e?.verbose?r:r.map(o=>o.san)}else{let r=M.flatMap(i=>q(i,this.fen())),o=this.hand().flatMap(i=>I(i,this.fen())),n=[...r,...o];return e?.verbose?n:n.map(i=>i.san)}}moveNumber(){return this.#a}pgn(e){return ne(this.#e,{...e,comments:this.#r})}print(e){console.log(this.ascii(e))}reset(){this.#o(this.#c),this.#e=[],this.#r.clear()}turn(){return this.#s}undo(){let e=this.#e.pop();return e?(this.#o(e.before),e):null}setComment(e){this.#r.set(this.fen(),e)}getComment(){return this.#r.get(this.fen())}removeComment(){let e=this.fen(),r=this.#r.get(e);return this.#r.delete(e),r}getComments(){let e=[];for(let[r,o]of this.#r)e.push({fen:r,comment:o});return e}removeComments(){let e=this.getComments();return this.#r.clear(),e}validateFen(e){return W(e)}};export{L as ADVANCED_POSITION,Pe as ARATA,Ie as ARCHER,De as BEGINNER_POSITION,be as BETRAY,me as BLACK,Fe as CANNON,Se as CANONICAL_NAMES,Oe as ENGLISH_NAMES,qe as FEN_CODES,Ae as FORTRESS,Ce as GENERAL,fe as Gungi,Ge as INTERMEDIATE_POSITION,X as INTRO_POSITION,xe as LANCER,Ee as LIEUTENANT_GENERAL,Te as MAJOR_GENERAL,Ne as MARSHAL,ve as MUSKETEER,we as RIDER,Me as SOLDIER,ke as SPY,M as SQUARES,Re as TACTICIAN,ye as TAKE,ge as TSUKE,$e as WARRIOR,he as WHITE,te as isSquareAttacked,W as validateFen};
|
|
8
8
|
//# sourceMappingURL=index.js.map
|