- // After move is played, update variables + flags
- updateVariables(move) {
- let piece = undefined;
- // TODO: update variables before move is played, and just use this.turn ?
- // (doesn't work in general, think MarseilleChess)
- let c = undefined;
- if (move.vanish.length >= 1) {
- // Usual case, something is moved
- piece = move.vanish[0].p;
- c = move.vanish[0].c;
- } else {
- // Crazyhouse-like variants
- piece = move.appear[0].p;
- c = move.appear[0].c;
- }
- if (!['w','b'].includes(c)) {
- // Checkered, for example
- c = V.GetOppCol(this.turn);
- }
- const firstRank = c == "w" ? V.size.x - 1 : 0;
-
- // Update king position + flags
- if (piece == V.KING && move.appear.length > 0) {
- this.kingPos[c][0] = move.appear[0].x;
- this.kingPos[c][1] = move.appear[0].y;
- if (V.HasFlags) this.castleFlags[c] = [false, false];
- return;
- }
- if (V.HasFlags) {
- // Update castling flags if rooks are moved
- const oppCol = V.GetOppCol(c);
- const oppFirstRank = V.size.x - 1 - firstRank;
- if (
- move.start.x == firstRank && //our rook moves?
- this.INIT_COL_ROOK[c].includes(move.start.y)
- ) {
- const flagIdx = move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1;
- this.castleFlags[c][flagIdx] = false;
- } else if (
- move.end.x == oppFirstRank && //we took opponent rook?
- this.INIT_COL_ROOK[oppCol].includes(move.end.y)
- ) {
- const flagIdx = move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1;
- this.castleFlags[oppCol][flagIdx] = false;
- }
- }
- }
-
- // After move is undo-ed *and flags resetted*, un-update other variables
- // TODO: more symmetry, by storing flags increment in move (?!)
- unupdateVariables(move) {
- // (Potentially) Reset king position
- const c = this.getColor(move.start.x, move.start.y);
- if (this.getPiece(move.start.x, move.start.y) == V.KING)
- this.kingPos[c] = [move.start.x, move.start.y];
- }