"use strict";console.clear();let PIECE_DIR_CALC=0;class Utils{static colToInt(e){return Board.COLS.indexOf(e)}static rowToInt(e){return Board.ROWS.indexOf(e)}static intToCol(e){return Board.COLS[e]}static intToRow(e){return Board.ROWS[e]}static getPositionsFromShortCode(e){let t=Utils.getInitialPiecePositions(),i={},s="X"===e.charAt(0);for(let a in s&&(e=e.slice(1)),e.split(",").forEach(e=>{let t="P"===e.charAt(0);if(t&&(e=e.slice(1)),s){let a=3===e.length,r=e.slice(0,2),o=a?void 0:e.charAt(2),l=a?void 0:e.charAt(3),c=e.charAt(4)||"1";i[r]={col:o,row:l,active:!a,_moves:parseInt(c),_promoted:t}}else{let n=e.length>=4,h=e.slice(0,2),p=e.charAt(n?2:0),d=e.charAt(n?3:1),u=e.charAt(4)||n?"1":"0";i[h]={col:p,row:d,active:!0,_moves:parseInt(u),_promoted:t}}}),t)i[a]?t[a]=i[a]:t[a]=s?t[a]:{active:!1};return t}static getInitialBoardPieces(e,t){let i={},s=document.createElement("div");for(let a in s.className="pieces",e.appendChild(s),t){let r=document.createElement("div");r.className=`piece ${t[a].data.player.toLowerCase()}`,r.innerHTML=t[a].shape(),s.appendChild(r),i[a]=r}return i}static getInitialBoardTiles(e,t){let i={1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{}},s=document.createElement("div");s.className="board",e.appendChild(s);for(let a=0;a<8;a++){let r=document.createElement("div");r.className="row",s.appendChild(r);for(let o=0;o<8;o++){let l=document.createElement("button");l.className="tile";let c=Utils.intToRow(a),n=Utils.intToCol(o);l.addEventListener("click",()=>t({row:c,col:n})),r.appendChild(l),i[c][n]=l}}return i}static getInitialBoardState(e=()=>void 0){let t=()=>({A:e(),B:e(),C:e(),D:e(),E:e(),F:e(),G:e(),H:e()});return{1:Object.assign({},t()),2:Object.assign({},t()),3:Object.assign({},t()),4:Object.assign({},t()),5:Object.assign({},t()),6:Object.assign({},t()),7:Object.assign({},t()),8:Object.assign({},t())}}static getInitialPiecePositions(){return{A8:{active:!0,row:"8",col:"A"},B8:{active:!0,row:"8",col:"B"},C8:{active:!0,row:"8",col:"C"},D8:{active:!0,row:"8",col:"D"},E8:{active:!0,row:"8",col:"E"},F8:{active:!0,row:"8",col:"F"},G8:{active:!0,row:"8",col:"G"},H8:{active:!0,row:"8",col:"H"},A7:{active:!0,row:"7",col:"A"},B7:{active:!0,row:"7",col:"B"},C7:{active:!0,row:"7",col:"C"},D7:{active:!0,row:"7",col:"D"},E7:{active:!0,row:"7",col:"E"},F7:{active:!0,row:"7",col:"F"},G7:{active:!0,row:"7",col:"G"},H7:{active:!0,row:"7",col:"H"},A2:{active:!0,row:"2",col:"A"},B2:{active:!0,row:"2",col:"B"},C2:{active:!0,row:"2",col:"C"},D2:{active:!0,row:"2",col:"D"},E2:{active:!0,row:"2",col:"E"},F2:{active:!0,row:"2",col:"F"},G2:{active:!0,row:"2",col:"G"},H2:{active:!0,row:"2",col:"H"},A1:{active:!0,row:"1",col:"A"},B1:{active:!0,row:"1",col:"B"},C1:{active:!0,row:"1",col:"C"},D1:{active:!0,row:"1",col:"D"},E1:{active:!0,row:"1",col:"E"},F1:{active:!0,row:"1",col:"F"},G1:{active:!0,row:"1",col:"G"},H1:{active:!0,row:"1",col:"H"}}}static getInitialPieces(){return{A8:new Piece({id:"A8",player:"BLACK",type:"ROOK"}),B8:new Piece({id:"B8",player:"BLACK",type:"KNIGHT"}),C8:new Piece({id:"C8",player:"BLACK",type:"BISHOP"}),D8:new Piece({id:"D8",player:"BLACK",type:"QUEEN"}),E8:new Piece({id:"E8",player:"BLACK",type:"KING"}),F8:new Piece({id:"F8",player:"BLACK",type:"BISHOP"}),G8:new Piece({id:"G8",player:"BLACK",type:"KNIGHT"}),H8:new Piece({id:"H8",player:"BLACK",type:"ROOK"}),A7:new Piece({id:"A7",player:"BLACK",type:"PAWN"}),B7:new Piece({id:"B7",player:"BLACK",type:"PAWN"}),C7:new Piece({id:"C7",player:"BLACK",type:"PAWN"}),D7:new Piece({id:"D7",player:"BLACK",type:"PAWN"}),E7:new Piece({id:"E7",player:"BLACK",type:"PAWN"}),F7:new Piece({id:"F7",player:"BLACK",type:"PAWN"}),G7:new Piece({id:"G7",player:"BLACK",type:"PAWN"}),H7:new Piece({id:"H7",player:"BLACK",type:"PAWN"}),A2:new Piece({id:"A2",player:"WHITE",type:"PAWN"}),B2:new Piece({id:"B2",player:"WHITE",type:"PAWN"}),C2:new Piece({id:"C2",player:"WHITE",type:"PAWN"}),D2:new Piece({id:"D2",player:"WHITE",type:"PAWN"}),E2:new Piece({id:"E2",player:"WHITE",type:"PAWN"}),F2:new Piece({id:"F2",player:"WHITE",type:"PAWN"}),G2:new Piece({id:"G2",player:"WHITE",type:"PAWN"}),H2:new Piece({id:"H2",player:"WHITE",type:"PAWN"}),A1:new Piece({id:"A1",player:"WHITE",type:"ROOK"}),B1:new Piece({id:"B1",player:"WHITE",type:"KNIGHT"}),C1:new Piece({id:"C1",player:"WHITE",type:"BISHOP"}),D1:new Piece({id:"D1",player:"WHITE",type:"QUEEN"}),E1:new Piece({id:"E1",player:"WHITE",type:"KING"}),F1:new Piece({id:"F1",player:"WHITE",type:"BISHOP"}),G1:new Piece({id:"G1",player:"WHITE",type:"KNIGHT"}),H1:new Piece({id:"H1",player:"WHITE",type:"ROOK"})}}}class Shape{static shape(e,t){return` `}static shapeBishop(e){return Shape.shape(e,"bishop")}static shapeKing(e){return Shape.shape(e,"king")}static shapeKnight(e){return Shape.shape(e,"knight")}static shapePawn(e){return Shape.shape(e,"pawn")}static shapeQueen(e){return Shape.shape(e,"queen")}static shapeRook(e){return Shape.shape(e,"rook")}}class Constraints{static generate(e,t){let i,{piecePositions:s,piece:a}=e;if(s[a.data.id].active)switch(a.data.type){case"BISHOP":i=Constraints.constraintsBishop;break;case"KING":i=Constraints.constraintsKing;break;case"KNIGHT":i=Constraints.constraintsKnight;break;case"PAWN":i=Constraints.constraintsPawn;break;case"QUEEN":i=Constraints.constraintsQueen;break;case"ROOK":i=Constraints.constraintsRook}let r=i?i(e):{moves:[],captures:[]};if(t){let o=e.moveIndex+1;r.moves=r.moves.filter(e=>!t({piece:a,location:e,capture:!1,moveIndex:o}).length),r.captures=r.captures.filter(e=>!t({piece:a,location:e,capture:!0,moveIndex:o}).length)}return r}static constraintsBishop(e){return Constraints.constraintsDiagonal(e)}static constraintsDiagonal(e){let t={moves:[],captures:[]},{piece:i}=e;return Constraints.runUntil(i.dirNW.bind(i),t,e),Constraints.runUntil(i.dirNE.bind(i),t,e),Constraints.runUntil(i.dirSW.bind(i),t,e),Constraints.runUntil(i.dirSE.bind(i),t,e),t}static constraintsKing(e){let{piece:t,kingCastles:i,piecePositions:s}=e,a=[],r=[],o=[t.dirN(1,s),t.dirNE(1,s),t.dirE(1,s),t.dirSE(1,s),t.dirS(1,s),t.dirSW(1,s),t.dirW(1,s),t.dirNW(1,s),];if(i){let l=i(t);l.forEach(e=>a.push(e))}return o.forEach(t=>{let i=Constraints.relationshipToTile(t,e);"BLANK"===i?a.push(t):"ENEMY"===i&&r.push(t)}),{moves:a,captures:r}}static constraintsKnight(e){let{piece:t,piecePositions:i}=e,s=[],a=[],r=[t.dir(1,2,i),t.dir(1,-2,i),t.dir(2,1,i),t.dir(2,-1,i),t.dir(-1,2,i),t.dir(-1,-2,i),t.dir(-2,1,i),t.dir(-2,-1,i),];return r.forEach(t=>{let i=Constraints.relationshipToTile(t,e);"BLANK"===i?s.push(t):"ENEMY"===i&&a.push(t)}),{moves:s,captures:a}}static constraintsOrthangonal(e){let{piece:t}=e,i={moves:[],captures:[]};return Constraints.runUntil(t.dirN.bind(t),i,e),Constraints.runUntil(t.dirE.bind(t),i,e),Constraints.runUntil(t.dirS.bind(t),i,e),Constraints.runUntil(t.dirW.bind(t),i,e),i}static constraintsPawn(e){let{piece:t,piecePositions:i}=e,s=[],a=[],r=t.dirN(1,i),o=t.dirN(2,i);return"BLANK"!==Constraints.relationshipToTile(r,e)||(s.push(r),t.moves.length||"BLANK"!==Constraints.relationshipToTile(o,e)||s.push(o)),[[t.dirNW(1,i),t.dirW(1,i)],[t.dirNE(1,i),t.dirE(1,i)],].forEach(([i,s])=>{let r=Constraints.relationshipToTile(i,e),o=Constraints.relationshipToTile(s,e);if("ENEMY"===r)a.push(i);else if(t.moves.length>0&&"ENEMY"===o){let l=s.row===(t.playerWhite()?"5":"4"),c=Constraints.locationToPiece(s,e);l&&c&&"PAWN"===c.data.type&&1===c.moves.length&&c.moves[0]===e.moveIndex-1&&(i.capture=Object.assign({},s),a.push(i))}}),{moves:s,captures:a}}static constraintsQueen(e){let t=Constraints.constraintsDiagonal(e),i=Constraints.constraintsOrthangonal(e);return{moves:t.moves.concat(i.moves),captures:t.captures.concat(i.captures)}}static constraintsRook(e){return Constraints.constraintsOrthangonal(e)}static locationToPiece(e,t){if(!e)return;let{state:i,pieces:s}=t,a=i[e.row],r=void 0===a?void 0:a[e.col];return s[r]}static relationshipToTile(e,t){if(!e)return;let{piece:i}=t,s=Constraints.locationToPiece(e,t);return s?s.data.player===i.data.player?"FRIEND":"ENEMY":"BLANK"}static runUntil(e,t,i){let{piecePositions:s}=i,a=1,r=e(a++,s);for(;r;){let o=!1,l=Constraints.relationshipToTile(r,i);"ENEMY"===l?(t.captures.push(r),o=!0):"FRIEND"===l?o=!0:t.moves.push(r),r=o?void 0:e(a++,s)}}}class Piece{constructor(e){this.moves=[],this.promoted=!1,this.updateShape=!1,this.data=e}get orientation(){return"BLACK"===this.data.player?-1:1}dirN(e,t){return this.dir(e,0,t)}dirS(e,t){return this.dir(-e,0,t)}dirW(e,t){return this.dir(0,-e,t)}dirE(e,t){return this.dir(0,e,t)}dirNW(e,t){return this.dir(e,-e,t)}dirNE(e,t){return this.dir(e,e,t)}dirSW(e,t){return this.dir(-e,-e,t)}dirSE(e,t){return this.dir(-e,e,t)}dir(e,t,i){PIECE_DIR_CALC++;let s=Utils.rowToInt(i[this.data.id].row)+this.orientation*e,a=Utils.colToInt(i[this.data.id].col)+this.orientation*t;if(s>=0&&s<=7&&a>=0&&a<=7)return{row:Utils.intToRow(s),col:Utils.intToCol(a)}}move(e){this.moves.push(e)}options(e,t,i,s,a,r){return Constraints.generate({moveIndex:e,state:t,piece:this,pieces:i,piecePositions:s,kingCastles:r},a)}playerBlack(){return"BLACK"===this.data.player}playerWhite(){return"WHITE"===this.data.player}promote(e="QUEEN"){this.data.type=e,this.promoted=!0,this.updateShape=!0}shape(){let e=this.data.player.toLowerCase();switch(this.data.type){case"BISHOP":return Shape.shapeBishop(e);case"KING":return Shape.shapeKing(e);case"KNIGHT":return Shape.shapeKnight(e);case"PAWN":return Shape.shapePawn(e);case"QUEEN":return Shape.shapeQueen(e);case"ROOK":return Shape.shapeRook(e)}}}class Board{constructor(e,t){for(let i in this.checksBlack=[],this.checksWhite=[],this.piecesTilesCaptures={},this.piecesTilesMoves={},this.tilesPiecesBlackCaptures=Utils.getInitialBoardState(()=>[]),this.tilesPiecesBlackMoves=Utils.getInitialBoardState(()=>[]),this.tilesPiecesWhiteCaptures=Utils.getInitialBoardState(()=>[]),this.tilesPiecesWhiteMoves=Utils.getInitialBoardState(()=>[]),this.pieceIdsBlack=[],this.pieceIdsWhite=[],this.state=Utils.getInitialBoardState(),this.pieces=e,e)e[i].playerWhite()?this.pieceIdsWhite.push(i):this.pieceIdsBlack.push(i);this.initializePositions(t)}initializePositions(e){this.piecePositions=e,this.initializeState(),this.piecesUpdate(0)}initializeState(){for(let e in this.pieces){let{row:t,col:i,active:s,_moves:a,_promoted:r}=this.piecePositions[e];a&&delete this.piecePositions[e]._moves,r&&(delete this.piecePositions[e]._promoted,this.pieces[e].promote()),s&&(this.state[t]=this.state[t]||[],this.state[t][i]=e)}}kingCastles(e){let t=[];if(e.moves.length)return t;let i=e.playerWhite(),s=i?this.tilesPiecesBlackMoves:this.tilesPiecesWhiteMoves,a=(e,t,i)=>{let a="A"===t?["D","C","B"]:["F","G"],r=`${t}${e}`,o=this.pieces[r],{active:l}=this.piecePositions[r];if(l&&0===o.moves.length){let c=!0;a.forEach(t=>{this.state[e][t]?c=!1:s[e][t].length&&(c=!1)}),c&&i.push({col:a[1],row:e,castles:t})}},r=i?"1":"8";return this.pieces[`A${r}`].moves.length||a(r,"A",t),this.pieces[`H${r}`].moves.length||a(r,"H",t),t}kingCheckStates(e,t,i){let{col:s,row:a}=e;return t[a][s].map(e=>i[e]).filter(e=>e.active)}pieceCalculateMoves(e,t,i,s,a,r,o,l,c,n){let{captures:h,moves:p}=this.pieces[e].options(t,i,this.pieces,s,c,n);a[e]=Array.from(h),r[e]=Array.from(p),h.forEach(({col:t,row:i})=>o[i][t].push(e)),p.forEach(({col:t,row:i})=>l[i][t].push(e))}pieceCapture(e){let t=e.data.id,{col:i,row:s}=this.piecePositions[t];this.state[s][i]=void 0,delete this.piecePositions[t].col,delete this.piecePositions[t].row,this.piecePositions[t].active=!1}pieceMove(e,t){let i=e.data.id,{row:s,col:a}=this.piecePositions[i];this.state[s][a]=void 0,this.state[t.row][t.col]=i,this.piecePositions[i].row=t.row,this.piecePositions[i].col=t.col,"PAWN"===e.data.type&&("8"===t.row||"1"===t.row)&&e.promote()}piecesUpdate(e){this.tilesPiecesBlackCaptures=Utils.getInitialBoardState(()=>[]),this.tilesPiecesBlackMoves=Utils.getInitialBoardState(()=>[]),this.tilesPiecesWhiteCaptures=Utils.getInitialBoardState(()=>[]),this.tilesPiecesWhiteMoves=Utils.getInitialBoardState(()=>[]),this.pieceIdsBlack.forEach(t=>this.pieceCalculateMoves(t,e,this.state,this.piecePositions,this.piecesTilesCaptures,this.piecesTilesMoves,this.tilesPiecesBlackCaptures,this.tilesPiecesBlackMoves,this.resultingChecks.bind(this),this.kingCastles.bind(this))),this.pieceIdsWhite.forEach(t=>this.pieceCalculateMoves(t,e,this.state,this.piecePositions,this.piecesTilesCaptures,this.piecesTilesMoves,this.tilesPiecesWhiteCaptures,this.tilesPiecesWhiteMoves,this.resultingChecks.bind(this),this.kingCastles.bind(this))),this.checksBlack=this.kingCheckStates(this.piecePositions.E1,this.tilesPiecesBlackCaptures,this.piecePositions),this.checksWhite=this.kingCheckStates(this.piecePositions.E8,this.tilesPiecesWhiteCaptures,this.piecePositions)}resultingChecks({piece:e,location:t,capture:i,moveIndex:s}){let a=Utils.getInitialBoardState(()=>[]),r=Utils.getInitialBoardState(()=>[]),o={},l={},c=JSON.parse(JSON.stringify(this.state)),n=JSON.parse(JSON.stringify(this.piecePositions));if(i){let h=t.capture||t,p=c[h.row][h.col];"KING"===this.pieces[p].data.type||(delete n[p].col,delete n[p].row,n[p].active=!1)}let d=e.data.id,{row:u,col:v}=n[d];c[u][v]=void 0,c[t.row][t.col]=d,n[d].row=t.row,n[d].col=t.col;let P=e.playerWhite()?this.pieceIdsBlack:this.pieceIdsWhite,E=e.playerWhite()?n.E1:n.E8;return P.forEach(e=>this.pieceCalculateMoves(e,s,c,n,o,l,a,r)),this.kingCheckStates(E,a,n)}tileEach(e){Board.ROWS.forEach(t=>{Board.COLS.forEach(i=>{let s=this.tileFind({row:t,col:i}),a=s?this.piecesTilesMoves[s.data.id]:void 0,r=s?this.piecesTilesCaptures[s.data.id]:void 0;e({row:t,col:i},s,a,r)})})}tileFind({row:e,col:t}){let i=this.state[e][t];return this.pieces[i]}toShortCode(){let e=[],t=[];for(let i in this.piecePositions){let{active:s,col:a,row:r}=this.piecePositions[i],o=`${a}${r}`,l=this.pieces[i].moves,c=this.pieces[i].promoted?"P":"",n=l>9?"9":l>1?l.toString():"";s?(e.push(`${c}${i}${i===o?"":o}${n}`),(i!==o||l>0)&&t.push(`${c}${i}${o}${n}`)):"BQ"!==i&&"WQ"!==i&&t.push(`${c}${i}X`)}let h=e.join(","),p=t.join(",");return h.length>p.length?`X${p}`:h}}Board.COLS=["A","B","C","D","E","F","G","H"],Board.ROWS=["1","2","3","4","5","6","7","8"];class Game{constructor(e,t,i="WHITE"){this.active=null,this.activePieceOptions=[],this.moveIndex=0,this.moves=[],this.turn=i,this.board=new Board(e,t)}activate(e){let t=this.board.tileFind(e);if(t&&!this.active&&t.data.player!==this.turn)return this.active=null,{type:"INVALID"};if(this.active){let i=this.active.data.id;this.active=null;let s=this.activePieceOptions.find(t=>t.col===e.col&&t.row===e.row),a=!!s;this.activePieceOptions=[];let r=(null==s?void 0:s.capture)?this.board.tileFind(s.capture):t;if(r){let o=r.data.id;if(o===i)return{type:"CANCEL"};if(a)return this.capture(i,o,e),{type:"CAPTURE",activePieceId:i,capturedPieceId:o,captures:[e]};if(r.data.player!==this.turn)return{type:"CANCEL"}}else{if(!a)return{type:"CANCEL"};let l=this.move(i,e);return{type:"MOVE",activePieceId:i,moves:[e],castledId:l}}}if(!t)return this.activePieceOptions=[],{type:"CANCEL"};{let c=t.data.id,n=this.board.piecesTilesMoves[c],h=this.board.piecesTilesCaptures[c];return n.length||h.length?(this.active=t,this.activePieceOptions=n.concat(h),{type:"TOUCH",captures:h,moves:n,activePieceId:c}):{type:"INVALID"}}}capture(e,t,i){let s=this.board.pieces[t];this.board.pieceCapture(s),this.move(e,i,!0)}handleCastling(e,t){if("KING"===e.data.type&&!e.moves.length&&t.row===(e.playerWhite()?"1":"8")&&("C"===t.col||"G"===t.col))return`${"C"===t.col?"A":"H"}${t.row}`}move(e,t,i=!1){let s=this.board.pieces[e],a=this.handleCastling(s,t);if(s.move(this.moveIndex),a){let r=this.board.pieces[a];r.move(this.moveIndex),this.board.pieceMove(r,{col:"C"===t.col?"D":"F",row:t.row}),this.moves.push(`${e}O${t.col}${t.row}`)}else this.moves.push(`${e}${i?"x":""}${t.col}${t.row}`);this.moveIndex++,this.board.pieceMove(s,t),this.turn="WHITE"===this.turn?"BLACK":"WHITE",this.board.piecesUpdate(this.moveIndex);let o=this.moveResultState();return console.log(o),o.moves||o.captures||alert(o.stalemate?"Stalemate!":`${"WHITE"===this.turn?"Black":"White"} Wins!`),a}moveResultState(){let e=0,t=0,i=0,s=0;this.board.tileEach(({row:a,col:r})=>{e+=this.board.tilesPiecesWhiteMoves[a][r].length,t+=this.board.tilesPiecesWhiteCaptures[a][r].length,i+=this.board.tilesPiecesBlackMoves[a][r].length,s+=this.board.tilesPiecesBlackCaptures[a][r].length});let a=this.board.pieceIdsBlack.filter(e=>this.board.piecePositions[e].active).length,r=this.board.pieceIdsWhite.filter(e=>this.board.piecePositions[e].active).length,o="WHITE"===this.turn?e:i,l="WHITE"===this.turn?t:s,c=e+t+i+s===0,n=!!this.board["WHITE"===this.turn?"checksBlack":"checksWhite"].length,h=this.board.toShortCode();return{turn:this.turn,checked:n,moves:o,captures:l,code:h,stalemate:1===a&&1===r||c||o+l===0&&!n}}randomMove(){if(this.active){if(this.activePieceOptions.length){let{col:e,row:t}=this.activePieceOptions[Math.floor(Math.random()*this.activePieceOptions.length)];return{col:e,row:t}}{let{col:i,row:s}=this.board.piecePositions[this.active.data.id];return{col:i,row:s}}}{let a="WHITE"===this.turn?this.board.pieceIdsWhite:this.board.pieceIdsBlack,r=a.map(e=>{let t=this.board.piecesTilesMoves[e],i=this.board.piecesTilesCaptures[e];return t.length||i.length?this.board.piecePositions[e]:void 0}).filter(e=>null==e?void 0:e.active),o=r[Math.floor(Math.random()*r.length)],{col:l,row:c}=o||{col:"E",row:"1"};return{col:l,row:c}}}}class View{constructor(e,t,i){this.element=e,this.game=t,this.setPerspective(i||this.game.turn),this.tiles=Utils.getInitialBoardTiles(this.element,this.handleTileClick.bind(this)),this.pieces=Utils.getInitialBoardPieces(this.element,this.game.board.pieces),this.drawPiecePositions()}drawActivePiece(e){let{row:t,col:i}=this.game.board.piecePositions[e];this.tiles[t][i].classList.add("highlight-active"),this.pieces[e].classList.add("highlight-active")}drawCapturedPiece(e){let t=this.pieces[e];t.style.setProperty("--transition-delay","var(--transition-duration)"),t.style.removeProperty("--pos-col"),t.style.removeProperty("--pos-row"),t.style.setProperty("--scale","0")}drawPiecePositions(e=[],t=""){document.body.style.setProperty("--color-background",`var(--color-${this.game.turn.toLowerCase()}`);let i="WHITE"===this.game.turn?"turn-black":"turn-white",s="WHITE"===this.game.turn?"turn-white":"turn-black";this.element.classList.add(s),this.element.classList.remove(i),e.length?this.element.classList.add("touching"):this.element.classList.remove("touching");let a=(e,t)=>`${e}-${t}`,r=e.map(({row:e,col:t})=>a(e,t));this.game.board.tileEach(({row:e,col:i},s,o,l)=>{let c=this.tiles[e][i],n=r.includes(a(e,i))?t:"",h=(e,t)=>this.game.board.pieces[e].shape();if(c.innerHTML=`
${n}
${this.game.board.tilesPiecesBlackMoves[e][i].map(e=>h(e,"black")).join("")} ${this.game.board.tilesPiecesWhiteMoves[e][i].map(e=>h(e,"white")).join("")}
${this.game.board.tilesPiecesBlackCaptures[e][i].map(e=>h(e,"black")).join("")} ${this.game.board.tilesPiecesWhiteCaptures[e][i].map(e=>h(e,"white")).join("")}
`,s){c.classList.add("occupied");let p=this.pieces[s.data.id];p.style.setProperty("--pos-col",Utils.colToInt(i).toString()),p.style.setProperty("--pos-row",Utils.rowToInt(e).toString()),p.style.setProperty("--scale","1"),p.classList[(null==o?void 0:o.length)?"add":"remove"]("can-move"),p.classList[(null==l?void 0:l.length)?"add":"remove"]("can-capture"),s.updateShape&&(s.updateShape=!1,p.innerHTML=s.shape())}else c.classList.remove("occupied")})}drawPositions(e,t){null==e||e.forEach(({row:e,col:t})=>{var i,s;this.tiles[e][t].classList.add("highlight-move"),null===(s=this.pieces[null===(i=this.game.board.tileFind({row:e,col:t}))||void 0===i?void 0:i.data.id])||void 0===s||s.classList.add("highlight-move")}),null==t||t.forEach(({row:e,col:t,capture:i})=>{var s,a;i&&(e=i.row,t=i.col),this.tiles[e][t].classList.add("highlight-capture"),null===(a=this.pieces[null===(s=this.game.board.tileFind({row:e,col:t}))||void 0===s?void 0:s.data.id])||void 0===a||a.classList.add("highlight-capture")})}drawResetClassNames(){document.querySelectorAll(".highlight-active").forEach(e=>e.classList.remove("highlight-active")),document.querySelectorAll(".highlight-capture").forEach(e=>e.classList.remove("highlight-capture")),document.querySelectorAll(".highlight-move").forEach(e=>e.classList.remove("highlight-move"))}handleTileClick(e){let{activePieceId:t,capturedPieceId:i,moves:s=[],captures:a=[],type:r}=this.game.activate(e);if(this.drawResetClassNames(),"TOUCH"===r){let o=a.find(e=>!!e.capture),l=o?s.concat([o]):s;this.drawPiecePositions(l,this.game.board.pieces[t].shape())}else this.drawPiecePositions();"CANCEL"!==r&&"INVALID"!==r&&("MOVE"===r||"CAPTURE"===r||this.drawActivePiece(t),"TOUCH"===r?this.drawPositions(s,a):"CAPTURE"===r&&this.drawCapturedPiece(i))}setPerspective(e){this.element.classList.add("WHITE"===e?"perspective-white":"perspective-black"),this.element.classList.remove("WHITE"===e?"perspective-black":"perspective-white")}}class Control{constructor(e,t){this.inputSpeedAsap=document.getElementById("speed-asap"),this.inputSpeedFast=document.getElementById("speed-fast"),this.inputSpeedMedium=document.getElementById("speed-medium"),this.inputSpeedSlow=document.getElementById("speed-slow"),this.inputRandomBlack=document.getElementById("black-random"),this.inputRandomWhite=document.getElementById("white-random"),this.inputPerspectiveBlack=document.getElementById("black-perspective"),this.inputPerspectiveWhite=document.getElementById("white-perspective"),this.game=e,this.view=t,this.inputPerspectiveBlack.addEventListener("change",this.updateViewPerspective.bind(this)),this.inputPerspectiveWhite.addEventListener("change",this.updateViewPerspective.bind(this)),this.updateViewPerspective()}get speed(){return this.inputSpeedAsap.checked?50:this.inputSpeedFast.checked?250:this.inputSpeedMedium.checked?500:this.inputSpeedSlow.checked?1e3:void 0}autoplay(){let e="WHITE"===this.game.turn?this.inputRandomWhite:this.inputRandomBlack;if(!e.checked){setTimeout(this.autoplay.bind(this),this.speed);return}let t=this.game.randomMove();this.view.handleTileClick(t),setTimeout(this.autoplay.bind(this),this.speed)}updateViewPerspective(){this.view.setPerspective(this.inputPerspectiveBlack.checked?"BLACK":"WHITE")}}const DEMOS={castle1:"XD8B3,B1X,C1X,D1X,F1X,G1X",castle2:"XD8B3,B1X,C1X,C2X,D1X,F1X,G1X",castle3:"XD8E3,B1X,C1X,F2X,D1X,F1X,G1X",promote1:"E1,E8,C2C7",promote2:"E1,E8E7,PC2C8",start:"XE7E6,F7F5,D2D4,E2E5",test2:"C8E2,E8,G8H1,D7E4,H7H3,PA2H7,PB2G7,D2D6,E2E39,A1H2,E1B3",test:"C8E2,E8,G8H1,D7E4,H7H3,D1H7,PB2G7,D2D6,E2E39,A1H2,E1B3"},initialPositions=Utils.getInitialPiecePositions(),initialTurn="WHITE",perspective="WHITE",game=new Game(Utils.getInitialPieces(),initialPositions,"WHITE"),view=new View(document.getElementById("board"),game,"WHITE"),control=new Control(game,view);control.autoplay();