+ // Scan kings, rooks and jailers
+ scanKingsRooks(fen) {
+ this.INIT_COL_KING = { w: -1, b: -1 };
+ this.INIT_COL_ROOK = { w: -1, b: -1 };
+ this.INIT_COL_JAILER = { w: -1, b: -1 };
+ this.kingPos = { w: [-1, -1], b: [-1, -1] };
+ const fenRows = V.ParseFen(fen).position.split("/");
+ const startRow = { 'w': V.size.x - 1, 'b': 0 };
+ for (let i = 0; i < fenRows.length; i++) {
+ let k = 0;
+ for (let j = 0; j < fenRows[i].length; j++) {
+ switch (fenRows[i].charAt(j)) {
+ case "k":
+ this.kingPos["b"] = [i, k];
+ this.INIT_COL_KING["b"] = k;
+ break;
+ case "K":
+ this.kingPos["w"] = [i, k];
+ this.INIT_COL_KING["w"] = k;
+ break;
+ case "r":
+ if (i == startRow['b'] && this.INIT_COL_ROOK["b"] < 0)
+ this.INIT_COL_ROOK["b"] = k;
+ break;
+ case "R":
+ if (i == startRow['w'] && this.INIT_COL_ROOK["w"] < 0)
+ this.INIT_COL_ROOK["w"] = k;
+ break;
+ case "j":
+ if (i == startRow['b'] && this.INIT_COL_JAILER["b"] < 0)
+ this.INIT_COL_JAILER["b"] = k;
+ break;
+ case "J":
+ if (i == startRow['w'] && this.INIT_COL_JAILER["w"] < 0)
+ this.INIT_COL_JAILER["w"] = k;
+ break;
+ default: {
+ const num = parseInt(fenRows[i].charAt(j));
+ if (!isNaN(num)) k += num - 1;
+ }
+ }
+ k++;
+ }
+ }
+ }
+
+ // Is piece on square (x,y) immobilized?
+ isImmobilized([x, y]) {
+ const color = this.getColor(x, y);
+ const oppCol = V.GetOppCol(color);
+ for (let step of V.steps[V.ROOK]) {
+ const [i, j] = [x + step[0], y + step[1]];
+ if (
+ V.OnBoard(i, j) &&
+ this.board[i][j] != V.EMPTY &&
+ this.getColor(i, j) == oppCol
+ ) {
+ const oppPiece = this.getPiece(i, j);
+ if (oppPiece == V.JAILER) return [i, j];
+ }
+ }
+ return null;
+ }
+
+ // Because of the lancers, getPiece() could be wrong:
+ // use board[x][y][1] instead (always valid).
+ getBasicMove([sx, sy], [ex, ey], tr) {
+ let mv = new Move({
+ appear: [
+ new PiPo({
+ x: ex,
+ y: ey,
+ c: tr ? tr.c : this.getColor(sx, sy),
+ p: tr ? tr.p : this.board[sx][sy][1]
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: sx,
+ y: sy,
+ c: this.getColor(sx, sy),
+ p: this.board[sx][sy][1]
+ })
+ ]
+ });
+
+ // The opponent piece disappears if we take it
+ if (this.board[ex][ey] != V.EMPTY) {
+ mv.vanish.push(
+ new PiPo({
+ x: ex,
+ y: ey,
+ c: this.getColor(ex, ey),
+ p: this.board[ex][ey][1]
+ })
+ );
+ }
+
+ return mv;
+ }
+
+ getPotentialMovesFrom_aux([x, y]) {
+ switch (this.getPiece(x, y)) {
+ case V.JAILER:
+ return this.getPotentialJailerMoves([x, y]);
+ case V.SENTRY:
+ return this.getPotentialSentryMoves([x, y]);
+ case V.LANCER:
+ return this.getPotentialLancerMoves([x, y]);
+ default:
+ return super.getPotentialMovesFrom([x, y]);
+ }
+ }
+
+ getPotentialMovesFrom([x,y]) {
+ if (this.subTurn == 1) {
+ if (!!this.isImmobilized([x, y])) return [];
+ let moves = this.getPotentialMovesFrom_aux([x, y]);
+ const L = this.sentryPush.length;
+ if (!!this.sentryPush[L-1]) {
+ // Delete moves walking back on sentry push path
+ moves = moves.filter(m => {
+ if (
+ m.vanish[0].p != V.PAWN &&
+ this.sentryPush[L-1].some(sq => sq.x == m.end.x && sq.y == m.end.y)
+ ) {
+ return false;
+ }
+ return true;
+ });
+ }
+ return moves;
+ }
+ // subTurn == 2: only the piece pushed by the sentry is allowed to move,
+ // as if the sentry didn't exist
+ if (x != this.sentryPos.x && y != this.sentryPos.y) return [];
+ const moves2 = this.getPotentialMovesFrom_aux([x, y]);
+ // Don't forget to re-add the sentry on the board:
+ const oppCol = V.GetOppCol(this.turn);
+ return moves2.map(m => {
+ m.appear.push({x: x, y: y, p: V.SENTRY, c: oppCol});
+ return m;
+ });
+ }
+
+ getPotentialPawnMoves([x, y]) {
+ const color = this.turn;
+ let moves = [];
+ const [sizeX, sizeY] = [V.size.x, V.size.y];
+ let shiftX = color == "w" ? -1 : 1;
+ // Special case of a sentry push: pawn goes in the capturer direction
+ if (this.subTurn == 2) shiftX *= -1;
+ const startRank = color == "w" ? sizeX - 2 : 1;
+ const lastRank = color == "w" ? 0 : sizeX - 1;
+
+ const finalPieces =
+ x + shiftX == lastRank
+ ?
+ [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.SENTRY, V.JAILER]
+ .concat(Object.keys(V.LANCER_DIRS))
+ : [V.PAWN];
+ if (this.board[x + shiftX][y] == V.EMPTY) {
+ // One square forward
+ for (let piece of finalPieces) {
+ moves.push(
+ this.getBasicMove([x, y], [x + shiftX, y], {
+ c: color,
+ p: piece
+ })
+ );
+ }
+ if (
+ x == startRank &&
+ this.board[x + 2 * shiftX][y] == V.EMPTY
+ ) {
+ // Two squares jump
+ moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
+ }
+ }
+ // Captures
+ for (let shiftY of [-1, 1]) {
+ if (
+ y + shiftY >= 0 &&
+ y + shiftY < sizeY &&
+ this.board[x + shiftX][y + shiftY] != V.EMPTY &&
+ this.canTake([x, y], [x + shiftX, y + shiftY])
+ ) {
+ for (let piece of finalPieces) {
+ moves.push(
+ this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
+ c: color,
+ p: piece
+ })
+ );
+ }
+ }
+ }
+
+ // En passant: no subTurn consideration here (always == 1)
+ const Lep = this.epSquares.length;
+ const epSquare = this.epSquares[Lep - 1]; //always at least one element