+ 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();
+ }
+ else {
+ // My color + my color: both actions possible
+ // Structure to avoid adding moves twice (can be action & move)
+ let movesHash = {};
+ if (fm.appear.length == 0) {
+ const pushes = getPushExit();
+ pushes.forEach(m => { movesHash[getMoveHash(m)] = true; });
+ return (
+ pushes.concat(getPullExit().filter(m => !movesHash[getMoveHash(m)]))
+ );
+ }
+ const pushes = getPushMoves();
+ pushes.forEach(m => { movesHash[getMoveHash(m)] = true; });
+ return (
+ pushes.concat(getPullMoves().filter(m => !movesHash[getMoveHash(m)]))
+ );