- canTake([x1,y1], [x2,y2])
- {
- const color1 = this.getColor(x1,y1);
- const color2 = this.getColor(x2,y2);
- // Checkered aren't captured
- return color1 != color2 && color2 != 'c' && (color1 != 'c' || color2 != this.turn);
- }
-
- // Post-processing: apply "checkerization" of standard moves
- getPotentialMovesFrom([x,y])
- {
- let standardMoves = super.getPotentialMovesFrom([x,y]);
- const lastRank = this.turn == "w" ? 0 : 7;
- if (this.getPiece(x,y) == V.KING)
- return standardMoves; //king has to be treated differently (for castles)
- let moves = [];
- standardMoves.forEach(m => {
- if (m.vanish[0].p == V.PAWN)
- {
- if (Math.abs(m.end.x-m.start.x)==2 && !this.pawnFlags[this.turn][m.start.y])
- return; //skip forbidden 2-squares jumps
- if (this.board[m.end.x][m.end.y] == V.EMPTY && m.vanish.length==2
- && this.getColor(m.start.x,m.start.y) == 'c')
- {
- return; //checkered pawns cannot take en-passant
- }
- }
- if (m.vanish.length == 1)
- moves.push(m); //no capture
- else
- {
- // A capture occured (m.vanish.length == 2)
- m.appear[0].c = "c";
- moves.push(m);
- if (m.appear[0].p != m.vanish[1].p //avoid promotions (already treated):
- && (m.vanish[0].p != V.PAWN || m.end.x != lastRank))
- {
- // Add transformation into captured piece
- let m2 = JSON.parse(JSON.stringify(m));
- m2.appear[0].p = m.vanish[1].p;
- moves.push(m2);
- }
- }
- });
- return moves;
- }
-
- canIplay(side, [x,y])
- {
- return (side == this.turn && [side,'c'].includes(this.getColor(x,y)));
- }
-
- // Does m2 un-do m1 ? (to disallow undoing checkered moves)
- oppositeMoves(m1, m2)
- {
- return (!!m1 && m2.appear[0].c == 'c'
- && m2.appear.length == 1 && m2.vanish.length == 1
- && m1.start.x == m2.end.x && m1.end.x == m2.start.x
- && m1.start.y == m2.end.y && m1.end.y == m2.start.y);
- }
-
- filterValid(moves)
- {
- if (moves.length == 0)
- return [];
- const color = this.turn;
- return moves.filter(m => {
- const L = this.cmoves.length; //at least 1: init from FEN
- if (this.oppositeMoves(this.cmoves[L-1], m))
- return false;
- this.play(m);
- const res = !this.underCheck(color);
- this.undo(m);
- return res;
- });
- }
-
- isAttackedByPawn([x,y], colors)
- {
- for (let c of colors)
- {
- const color = (c=="c" ? this.turn : c);
- let pawnShift = (color=="w" ? 1 : -1);
- if (x+pawnShift>=0 && x+pawnShift<8)
- {
- for (let i of [-1,1])
- {
- if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==V.PAWN
- && this.getColor(x+pawnShift,y+i)==c)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
-
- underCheck(color)
- {
- return this.isAttacked(this.kingPos[color], [V.GetOppCol(color),'c']);
- }
-
- getCheckSquares(color)
- {
- // Artifically change turn, for checkered pawns
- this.turn = V.GetOppCol(color);
- const kingAttacked = this.isAttacked(
- this.kingPos[color], [V.GetOppCol(color),'c']);
- let res = kingAttacked
- ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
- : [];
- this.turn = color;
- return res;
- }
-
- updateVariables(move)
- {
- super.updateVariables(move);
- // Does this move turn off a 2-squares pawn flag?
- const secondRank = [1,6];
- if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN)
- this.pawnFlags[move.start.x==6 ? "w" : "b"][move.start.y] = false;
- }
-
- getCurrentScore()
- {
- if (this.atLeastOneMove()) // game not over
+ canTake([x1, y1], [x2, y2]) {
+ const color1 = this.getColor(x1, y1);
+ const color2 = this.getColor(x2, y2);
+ // Checkered aren't captured
+ return (
+ color1 != color2 &&
+ color2 != "c" &&
+ (color1 != "c" || color2 != this.turn)
+ );
+ }
+
+ // Post-processing: apply "checkerization" of standard moves
+ getPotentialMovesFrom([x, y]) {
+ let standardMoves = super.getPotentialMovesFrom([x, y]);
+ const lastRank = this.turn == "w" ? 0 : 7;
+ if (this.getPiece(x, y) == V.KING) return standardMoves; //king has to be treated differently (for castles)
+ let moves = [];
+ standardMoves.forEach(m => {
+ if (m.vanish[0].p == V.PAWN) {
+ if (
+ Math.abs(m.end.x - m.start.x) == 2 &&
+ !this.pawnFlags[this.turn][m.start.y]
+ )
+ return; //skip forbidden 2-squares jumps
+ if (
+ this.board[m.end.x][m.end.y] == V.EMPTY &&
+ m.vanish.length == 2 &&
+ this.getColor(m.start.x, m.start.y) == "c"
+ ) {
+ return; //checkered pawns cannot take en-passant
+ }
+ }
+ if (m.vanish.length == 1) moves.push(m);
+ // No capture
+ else {
+ // A capture occured (m.vanish.length == 2)
+ m.appear[0].c = "c";
+ moves.push(m);
+ if (
+ m.appear[0].p != m.vanish[1].p && //avoid promotions (already treated):
+ (m.vanish[0].p != V.PAWN || m.end.x != lastRank)
+ ) {
+ // Add transformation into captured piece
+ let m2 = JSON.parse(JSON.stringify(m));
+ m2.appear[0].p = m.vanish[1].p;
+ moves.push(m2);
+ }
+ }
+ });
+ return moves;
+ }
+
+ canIplay(side, [x, y]) {
+ return side == this.turn && [side, "c"].includes(this.getColor(x, y));
+ }
+
+ // Does m2 un-do m1 ? (to disallow undoing checkered moves)
+ oppositeMoves(m1, m2) {
+ return (
+ m1 &&
+ m2.appear[0].c == "c" &&
+ m2.appear.length == 1 &&
+ m2.vanish.length == 1 &&
+ m1.start.x == m2.end.x &&
+ m1.end.x == m2.start.x &&
+ m1.start.y == m2.end.y &&
+ m1.end.y == m2.start.y
+ );
+ }
+
+ filterValid(moves) {
+ if (moves.length == 0) return [];
+ const color = this.turn;
+ const L = this.cmoves.length; //at least 1: init from FEN
+ return moves.filter(m => {
+ if (this.oppositeMoves(this.cmoves[L - 1], m)) return false;
+ this.play(m);
+ const res = !this.underCheck(color);
+ this.undo(m);
+ return res;
+ });
+ }
+
+ isAttackedByPawn([x, y], colors) {
+ for (let c of colors) {
+ const color = c == "c" ? this.turn : c;
+ let pawnShift = color == "w" ? 1 : -1;
+ if (x + pawnShift >= 0 && x + pawnShift < 8) {
+ for (let i of [-1, 1]) {
+ if (
+ y + i >= 0 &&
+ y + i < 8 &&
+ this.getPiece(x + pawnShift, y + i) == V.PAWN &&
+ this.getColor(x + pawnShift, y + i) == c
+ ) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+ }
+
+ underCheck(color) {
+ return this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]);
+ }
+
+ getCheckSquares(color) {
+ // Artifically change turn, for checkered pawns
+ this.turn = V.GetOppCol(color);
+ const kingAttacked = this.isAttacked(this.kingPos[color], [
+ V.GetOppCol(color),
+ "c"
+ ]);
+ let res = kingAttacked
+ ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
+ : [];
+ this.turn = color;
+ return res;
+ }
+
+ updateVariables(move) {
+ super.updateVariables(move);
+ // Does this move turn off a 2-squares pawn flag?
+ const secondRank = [1, 6];
+ if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN)
+ this.pawnFlags[move.start.x == 6 ? "w" : "b"][move.start.y] = false;
+ }
+
+ getCurrentScore() {
+ if (this.atLeastOneMove())
+ // game not over