+ const sqCol = this.getColor(x, y);
+ const pawnShift = (color == 'w' ? -1 : 1);
+ const pawnStartRank = (color == 'w' ? 6 : 1);
+ const getMoveHash = (m) => {
+ return V.CoordsToSquare(m.start) + V.CoordsToSquare(m.end);
+ };
+ if (this.subTurn == 1) {
+ const addMoves = (dir, nbSteps) => {
+ const newMoves =
+ this.getMovesInDirection([x, y], [-dir[0], -dir[1]], nbSteps)
+ .filter(m => !movesHash[getMoveHash(m)]);
+ newMoves.forEach(m => { movesHash[getMoveHash(m)] = true; });
+ Array.prototype.push.apply(moves, newMoves);
+ };
+ // Free to play any move (if piece of my color):
+ let moves =
+ sqCol == color
+ ? super.getPotentialMovesFrom([x, y])
+ : [];
+ // There may be several suicide moves: keep only one
+ let hasExit = false;
+ moves = moves.filter(m => {
+ const suicide = (m.appear.length == 0);
+ if (suicide) {
+ if (hasExit) return false;
+ hasExit = true;
+ }
+ return true;
+ });
+ // Structure to avoid adding moves twice (can be action & move)
+ let movesHash = {};
+ moves.forEach(m => { movesHash[getMoveHash(m)] = true; });
+ // [x, y] is pushed by 'color'
+ for (let step of V.steps[V.KNIGHT]) {
+ const [i, j] = [x + step[0], y + step[1]];
+ if (
+ V.OnBoard(i, j) &&
+ this.board[i][j] != V.EMPTY &&
+ this.getColor(i, j) == color &&
+ this.getPiece(i, j) == V.KNIGHT
+ ) {
+ addMoves(step, 1);
+ }
+ }
+ for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
+ let [i, j] = [x + step[0], y + step[1]];
+ while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
+ i += step[0];
+ j += step[1];
+ }
+ if (
+ V.OnBoard(i, j) &&
+ this.board[i][j] != V.EMPTY &&
+ this.getColor(i, j) == color
+ ) {
+ const deltaX = Math.abs(i - x);
+ const deltaY = Math.abs(j - y);
+ switch (this.getPiece(i, j)) {
+ case V.PAWN:
+ if (
+ (x - i) / deltaX == pawnShift &&
+ deltaX <= 2 &&
+ deltaY <= 1
+ ) {
+ if (sqCol == color && deltaY == 0) {
+ // Pushed forward
+ const maxSteps = (i == pawnStartRank && deltaX == 1 ? 2 : 1);
+ addMoves(step, maxSteps);
+ }
+ else if (sqCol != color && deltaY == 1 && deltaX == 1)
+ // Pushed diagonally
+ addMoves(step, 1);
+ }
+ break;
+ case V.ROOK:
+ if (deltaX == 0 || deltaY == 0) addMoves(step);
+ break;
+ case V.BISHOP:
+ if (deltaX == deltaY) addMoves(step);
+ break;
+ case V.QUEEN:
+ // All steps are valid for a queen:
+ addMoves(step);
+ break;
+ case V.KING:
+ if (deltaX <= 1 && deltaY <= 1) addMoves(step, 1);
+ break;
+ }
+ }
+ }
+ return moves;
+ }
+ // If subTurn == 2 then we should have a first move,
+ // which restrict what we can play now: only in the first move direction
+ const L = this.firstMove.length;
+ const fm = this.firstMove[L-1];
+ if (
+ (fm.appear.length == 2 && fm.vanish.length == 2) ||
+ (fm.vanish[0].c == sqCol && sqCol != color)
+ ) {
+ // Castle or again opponent color: no move playable then.
+ return [];
+ }
+ const piece = this.getPiece(x, y);
+ const getPushExit = () => {
+ // Piece at subTurn 1 exited: can I have caused the exit?
+ if (
+ this.isAprioriValidExit(
+ [x, y],
+ [fm.start.x, fm.start.y],
+ fm.vanish[0].c
+ )
+ ) {
+ // Seems so:
+ const dir = this.getNormalizedDirection(
+ [fm.start.x - x, fm.start.y - y]);
+ const nbSteps =
+ [V.PAWN, V.KING, V.KNIGHT].includes(piece)
+ ? 1
+ : null;
+ return this.getMovesInDirection([x, y], dir, nbSteps);
+ }
+ return [];
+ }
+ const getPushMoves = () => {
+ // Piece from subTurn 1 is still on board:
+ const dirM = this.getNormalizedDirection(
+ [fm.end.x - fm.start.x, fm.end.y - fm.start.y]);
+ const dir = this.getNormalizedDirection(
+ [fm.start.x - x, fm.start.y - y]);
+ // Normalized directions should match
+ if (dir[0] == dirM[0] && dir[1] == dirM[1]) {
+ // We don't know if first move is a pushed piece or normal move,
+ // so still must check if the push is valid.
+ const deltaX = Math.abs(fm.start.x - x);
+ const deltaY = Math.abs(fm.start.y - y);
+ switch (piece) {
+ case V.PAWN:
+ if (x == pawnStartRank) {
+ if (
+ (fm.start.x - x) * pawnShift < 0 ||
+ deltaX >= 3 ||
+ deltaY >= 2 ||
+ (fm.vanish[0].c == color && deltaY > 0) ||
+ (fm.vanish[0].c != color && deltaY == 0) ||
+ Math.abs(fm.end.x - fm.start.x) > deltaX ||
+ fm.end.y - fm.start.y != fm.start.y - y
+ ) {
+ return [];
+ }
+ }
+ else {
+ if (
+ fm.start.x - x != pawnShift ||
+ deltaY >= 2 ||
+ (fm.vanish[0].c == color && deltaY == 1) ||
+ (fm.vanish[0].c != color && deltaY == 0) ||
+ fm.end.x - fm.start.x != pawnShift ||
+ fm.end.y - fm.start.y != fm.start.y - y
+ ) {
+ return [];
+ }
+ }
+ break;
+ case V.KNIGHT:
+ if (
+ (deltaX + deltaY != 3 || (deltaX == 0 && deltaY == 0)) ||
+ (fm.end.x - fm.start.x != fm.start.x - x) ||
+ (fm.end.y - fm.start.y != fm.start.y - y)
+ ) {
+ return [];
+ }
+ break;
+ case V.KING:
+ if (
+ (deltaX >= 2 || deltaY >= 2) ||
+ (fm.end.x - fm.start.x != fm.start.x - x) ||
+ (fm.end.y - fm.start.y != fm.start.y - y)
+ ) {
+ return [];
+ }
+ break;
+ case V.BISHOP:
+ if (deltaX != deltaY) return [];
+ break;
+ case V.ROOK:
+ if (deltaX != 0 && deltaY != 0) return [];
+ break;
+ case V.QUEEN:
+ if (deltaX != deltaY && deltaX != 0 && deltaY != 0) return [];
+ break;
+ }
+ // Nothing should stand between [x, y] and the square fm.start
+ let [i, j] = [x + dir[0], y + dir[1]];
+ while (
+ (i != fm.start.x || j != fm.start.y) &&
+ this.board[i][j] == V.EMPTY
+ ) {
+ i += dir[0];
+ j += dir[1];
+ }
+ if (i == fm.start.x && j == fm.start.y)
+ return this.getMovesInDirection([x, y], dir);
+ }
+ return [];
+ }
+ const getPullExit = () => {
+ // Piece at subTurn 1 exited: can I be pulled?
+ // Note: kings cannot suicide, so fm.vanish[0].p is not KING.
+ // Could be PAWN though, if a pawn was pushed out of board.
+ if (
+ fm.vanish[0].p != V.PAWN && //pawns cannot pull
+ this.isAprioriValidExit(
+ [x, y],
+ [fm.start.x, fm.start.y],
+ fm.vanish[0].c,
+ fm.vanish[0].p
+ )
+ ) {
+ // Seems so:
+ const dir = this.getNormalizedDirection(
+ [fm.start.x - x, fm.start.y - y]);
+ const nbSteps = (fm.vanish[0].p == V.KNIGHT ? 1 : null);
+ return this.getMovesInDirection([x, y], dir, nbSteps);
+ }
+ return [];
+ };
+ const getPullMoves = () => {
+ if (fm.vanish[0].p == V.PAWN)
+ // pawns cannot pull
+ return [];
+ const dirM = this.getNormalizedDirection(
+ [fm.end.x - fm.start.x, fm.end.y - fm.start.y]);
+ const dir = this.getNormalizedDirection(
+ [fm.start.x - x, fm.start.y - y]);
+ // Normalized directions should match
+ if (dir[0] == dirM[0] && dir[1] == dirM[1]) {
+ // Am I at the right distance?
+ const deltaX = Math.abs(x - fm.start.x);
+ const deltaY = Math.abs(y - fm.start.y);
+ if (
+ (fm.vanish[0].p == V.KING && (deltaX > 1 || deltaY > 1)) ||
+ (fm.vanish[0].p == V.KNIGHT &&
+ (deltaX + deltaY != 3 || deltaX == 0 || deltaY == 0))
+ ) {
+ return [];
+ }
+ // Nothing should stand between [x, y] and the square fm.start
+ let [i, j] = [x + dir[0], y + dir[1]];
+ while (
+ (i != fm.start.x || j != fm.start.y) &&
+ this.board[i][j] == V.EMPTY
+ ) {
+ i += dir[0];
+ j += dir[1];
+ }
+ if (i == fm.start.x && j == fm.start.y)
+ return this.getMovesInDirection([x, y], dir);
+ }
+ return [];
+ };
+ if (fm.vanish[0].c != color) {
+ // Only possible action is a push:
+ if (fm.appear.length == 0) return getPushExit();
+ return getPushMoves();
+ }
+ else if (sqCol != color) {
+ // Only possible action is a pull, considering moving piece abilities
+ if (fm.appear.length == 0) return getPullExit();
+ return getPullMoves();
+ }