- static UndoOnBoard(board, move)
- {
- for (let psq of move.appear)
- board[psq.x][psq.y] = V.EMPTY;
- for (let psq of move.vanish)
- board[psq.x][psq.y] = psq.c + psq.p;
+ static UndoOnBoard(board, move) {
+ for (let psq of move.appear) board[psq.x][psq.y] = V.EMPTY;
+ for (let psq of move.vanish) board[psq.x][psq.y] = psq.c + psq.p;
+ }
+
+ prePlay() {}
+
+ play(move) {
+ // DEBUG:
+// if (!this.states) this.states = [];
+// const stateFen = this.getFen() + JSON.stringify(this.kingPos);
+// this.states.push(stateFen);
+
+ this.prePlay(move);
+ // Save flags (for undo)
+ if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags());
+ if (V.HasEnpassant) this.epSquares.push(this.getEpSquare(move));
+ V.PlayOnBoard(this.board, move);
+ this.turn = V.GetOppCol(this.turn);
+ this.movesCount++;
+ this.postPlay(move);
+ }
+
+ updateCastleFlags(move, piece, color) {
+ // TODO: check flags. If already off, no need to always re-evaluate
+ const c = color || V.GetOppCol(this.turn);
+ const firstRank = (c == "w" ? V.size.x - 1 : 0);
+ // Update castling flags if rooks are moved
+ const oppCol = this.turn;
+ const oppFirstRank = V.size.x - 1 - firstRank;
+ if (piece == V.KING && move.appear.length > 0)
+ this.castleFlags[c] = [V.size.y, V.size.y];
+ else if (
+ move.start.x == firstRank && //our rook moves?
+ this.castleFlags[c].includes(move.start.y)
+ ) {
+ const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
+ this.castleFlags[c][flagIdx] = V.size.y;
+ }
+ // NOTE: not "else if" because a rook could take an opposing rook
+ if (
+ move.end.x == oppFirstRank && //we took opponent rook?
+ this.castleFlags[oppCol].includes(move.end.y)
+ ) {
+ const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
+ this.castleFlags[oppCol][flagIdx] = V.size.y;
+ }