+ // 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: pawns and kings cannot suicide,
+ // so fm.vanish[0].p is neither PAWN or KING
+ if (
+ 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);