+ getEnpassantCaptures(sq, shiftX) {
+ // HACK: when artificially change turn, do not consider en-passant
+ const mcMod2 = this.movesCount % 2;
+ const c = this.turn;
+ if ((c == 'w' && mcMod2 == 1) || (c == 'b' && mcMod2 == 0)) return [];
+ return super.getEnpassantCaptures(sq, shiftX);
+ }
+
+ isAttacked_aux(files, color, positions, fromSquare, released) {
+ // "positions" = array of FENs to detect infinite loops. Example:
+ // r1q1k2r/p1Pb1ppp/5n2/1f1p4/AV5P/P1eDP3/3B1PP1/R3K1NR,
+ // Bxd2 Bxc3 Bxb4 Bxc3 Bxb4 etc.
+ const newPos = {
+ fen: super.getBaseFen(),
+ piece: released,
+ from: fromSquare
+ };
+ if (
+ positions.some(p => {
+ return (
+ p.piece == newPos.piece &&
+ p.fen == newPos.fen &&
+ p.from == newPos.from
+ );
+ })
+ ) {
+ // Start of an infinite loop: exit
+ return false;
+ }
+ positions.push(newPos);
+ const rank = (color == 'w' ? 0 : 7);
+ const moves = this.getPotentialMovesFrom(fromSquare);
+ if (moves.some(m => m.end.x == rank && files.includes(m.end.y)))
+ // Found an attack!
+ return true;
+ for (let m of moves) {
+ if (!!m.end.released) {
+ // Turn won't change since !!m.released
+ this.play(m);
+ const res = this.isAttacked_aux(
+ files, color, positions, [m.end.x, m.end.y], m.end.released);
+ this.undo(m);
+ if (res) return true;
+ }
+ }
+ return false;
+ }
+
+ isAttacked(files, color) {
+ const rank = (color == 'w' ? 0 : 7);
+ // Since it's too difficult (impossible?) to search from the square itself,
+ // let's adopt a suboptimal but working strategy: find all attacks.
+ const c = this.turn;
+ // Artificial turn change is required:
+ this.turn = color;
+ let res = false;
+ outerLoop: for (let i=0; i<8; i++) {
+ for (let j=0; j<8; j++) {
+ // Attacks must start from a normal piece, not an union.
+ // Therefore, the following test is correct.
+ if (
+ this.board[i][j] != V.EMPTY &&
+ // Do not start with king (irrelevant, and lead to infinite calls)
+ [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN].includes(
+ this.board[i][j].charAt(1)) &&
+ this.board[i][j].charAt(0) == color
+ ) {
+ // Try from here.
+ const moves = this.getPotentialMovesFrom([i, j]);
+ if (moves.some(m => m.end.x == rank && files.includes(m.end.y))) {
+ res = true;
+ break outerLoop;
+ }
+ for (let m of moves) {
+ if (!!m.end.released) {
+ // Turn won't change since !!m.released
+ this.play(m);
+ let positions = [];
+ res = this.isAttacked_aux(
+ files, color, positions, [m.end.x, m.end.y], m.end.released);
+ this.undo(m);
+ if (res) break outerLoop;
+ }
+ }
+ }
+ }
+ }
+ this.turn = c;
+ return res;
+ }
+
+ isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
+ for (let step of steps) {
+ let rx = x + step[0],
+ ry = y + step[1];
+ while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
+ rx += step[0];
+ ry += step[1];
+ }
+ if (
+ V.OnBoard(rx, ry) &&
+ this.board[rx][ry] != V.EMPTY &&
+ this.getPiece(rx, ry) == piece &&
+ this.getColor(rx, ry) == color &&
+ this.canTake([rx, ry], [x, y]) //TODO: necessary line?
+ //If not, generic method is OK
+ ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ // Do not consider checks, except to forbid castling
+ getCheckSquares() {
+ return [];
+ }
+
+ filterValid(moves) {
+ if (moves.length == 0) return [];
+ const L = this.umoves.length; //at least 1: init from FEN
+ return moves.filter(m => {
+ if (this.oppositeMoves(this.umoves[L - 1], m)) return false;
+ if (!m.end.released) return true;
+ // Check for repetitions:
+ V.PlayOnBoard(this.board, m);
+ const newState = {
+ piece: m.end.released,
+ square: { x: m.end.x, y: m.end.y },
+ position: this.getBaseFen()
+ };
+ const repet =
+ this.repetitions.some(r => {
+ return (
+ r.piece == newState.piece &&
+ (
+ r.square.x == newState.square.x &&
+ r.square.y == newState.square.y
+ ) &&
+ r.position == newState.position
+ );
+ });
+ V.UndoOnBoard(this.board, m);
+ return !repet;
+ });
+ }
+
+ updateCastleFlags(move, piece) {
+ const c = this.turn;
+ const firstRank = (c == "w" ? 7 : 0);
+ if (piece == V.KING && move.appear.length > 0)
+ this.castleFlags[c] = [V.size.y, V.size.y];
+ else if (
+ move.start.x == firstRank &&
+ 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;
+ }
+ else if (
+ move.end.x == firstRank &&
+ this.castleFlags[c].includes(move.end.y)
+ ) {
+ // Move to our rook: necessary normal piece, to union, releasing
+ // (or the rook was moved before!)
+ const flagIdx = (move.end.y == this.castleFlags[c][0] ? 0 : 1);
+ this.castleFlags[c][flagIdx] = V.size.y;
+ }
+ }
+
+ prePlay(move) {
+ // Easier before move is played in this case (flags are saved)
+ const c = this.turn;
+ const L = this.lastMoveEnd.length;
+ const lm = this.lastMoveEnd[L-1];
+ // NOTE: lm.p != V.KING, always.
+ const piece =
+ !!lm
+ ? lm.p
+ : this.getPiece(move.vanish[0].x, move.vanish[0].y);
+ if (piece == V.KING)
+ this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
+ this.updateCastleFlags(move, piece);
+ const pawnFirstRank = (c == 'w' ? 6 : 1);
+ if (
+ move.start.x == pawnFirstRank &&
+ piece == V.PAWN &&
+ Math.abs(move.end.x - move.start.x) == 2
+ ) {
+ // This move turns off a 2-squares pawn flag
+ this.pawnFlags[c][move.start.y] = false;
+ }
+ }
+