+ // 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].charAt(1)
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: sx,
+ y: sy,
+ c: this.getColor(sx, sy),
+ p: this.board[sx][sy].charAt(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].charAt(1)
+ })
+ );
+ }
+
+ return mv;
+ }
+
+ canIplay(side, [x, y]) {
+ return (
+ (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side) ||
+ (this.subTurn == 2 && x == this.sentryPos.x && y == this.sentryPos.y)
+ );
+ }
+
+ getPotentialMovesFrom([x, y]) {
+ // At subTurn == 2, jailers aren't effective (Jeff K)
+ if (this.subTurn == 1) {
+ const jsq = this.isImmobilized([x, y]);
+ if (!!jsq) {
+ let moves = [];
+ // Special pass move if king:
+ if (this.getPiece(x, y) == V.KING) {
+ moves.push(
+ new Move({
+ appear: [],
+ vanish: [],
+ start: { x: x, y: y },
+ end: { x: jsq[0], y: jsq[1] }
+ })
+ );
+ }
+ return moves;
+ }
+ }
+ if (this.subTurn == 2) {
+ // Temporarily change pushed piece color.
+ // (Not using getPiece() because of lancers)
+ var oppCol = this.getColor(x, y);
+ var color = V.GetOppCol(oppCol);
+ var saveXYstate = this.board[x][y];
+ this.board[x][y] = color + this.board[x][y].charAt(1);
+ }
+ let moves = [];
+ switch (this.getPiece(x, y)) {
+ case V.JAILER:
+ moves = this.getPotentialJailerMoves([x, y]);
+ break;
+ case V.SENTRY:
+ moves = this.getPotentialSentryMoves([x, y]);
+ break;
+ case V.LANCER:
+ moves = this.getPotentialLancerMoves([x, y]);
+ break;
+ default:
+ moves = super.getPotentialMovesFrom([x, y]);
+ break;
+ }
+ 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;
+ });
+ }
+ else if (this.subTurn == 2) {
+ // Don't forget to re-add the sentry on the board:
+ // Also fix color of pushed piece afterward:
+ moves.forEach(m => {
+ m.appear.unshift({x: x, y: y, p: V.SENTRY, c: color});
+ m.appear[1].c = oppCol;
+ m.vanish[0].c = oppCol;
+ });
+ this.board[x][y] = saveXYstate;
+ }
+ return moves;
+ }
+
+ getPotentialPawnMoves([x, y]) {
+ const color = this.getColor(x, y);
+ let moves = [];
+ const [sizeX, sizeY] = [V.size.x, V.size.y];
+ const shiftX = color == "w" ? -1 : 1;
+ const startRank = color == "w" ? sizeX - 2 : 1;
+ const lastRank = color == "w" ? 0 : sizeX - 1;
+
+ const finalPieces =
+ // No promotions after pushes!
+ x + shiftX == lastRank && this.subTurn == 1
+ ?
+ Object.keys(V.LANCER_DIRS).concat(
+ [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.SENTRY, V.JAILER])
+ : [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:
+ const Lep = this.epSquares.length;
+ const epSquare = this.epSquares[Lep - 1];
+ if (
+ !!epSquare &&
+ epSquare.x == x + shiftX &&
+ Math.abs(epSquare.y - y) == 1
+ ) {
+ let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]);
+ enpassantMove.vanish.push({
+ x: x,
+ y: epSquare.y,
+ p: "p",
+ c: this.getColor(x, epSquare.y)
+ });
+ moves.push(enpassantMove);
+ }
+
+ return moves;
+ }
+
+ // Obtain all lancer moves in "step" direction
+ getPotentialLancerMoves_aux([x, y], step, tr) {
+ let moves = [];
+ // Add all moves to vacant squares until opponent is met:
+ const oppCol = V.GetOppCol(this.getColor(x, y));
+ let sq = [x + step[0], y + step[1]];
+ while (V.OnBoard(sq[0], sq[1]) && this.getColor(sq[0], sq[1]) != oppCol) {
+ if (this.board[sq[0]][sq[1]] == V.EMPTY)
+ moves.push(this.getBasicMove([x, y], sq, tr));
+ sq[0] += step[0];
+ sq[1] += step[1];
+ }
+ if (V.OnBoard(sq[0], sq[1]))
+ // Add capturing move
+ moves.push(this.getBasicMove([x, y], sq, tr));
+ return moves;
+ }
+
+ getPotentialLancerMoves([x, y]) {
+ let moves = [];
+ // Add all lancer possible orientations, similar to pawn promotions.
+ // Except if just after a push: allow all movements from init square then
+ const L = this.sentryPush.length;
+ if (!!this.sentryPush[L-1]) {
+ // Maybe I was pushed
+ const pl = this.sentryPush[L-1].length;
+ if (
+ this.sentryPush[L-1][pl-1].x == x &&
+ this.sentryPush[L-1][pl-1].y == y
+ ) {
+ // I was pushed: allow all directions (for this move only), but
+ // do not change direction after moving, *except* if I keep the
+ // same orientation in which I was pushed.
+ const color = this.getColor(x, y);
+ const curDir = V.LANCER_DIRS[this.board[x][x].charAt(1)];
+ Object.values(V.LANCER_DIRS).forEach(step => {
+ const dirCode = Object.keys(V.LANCER_DIRS).find(k => {
+ return (
+ V.LANCER_DIRS[k][0] == step[0] &&
+ V.LANCER_DIRS[k][1] == step[1]
+ );
+ });
+ const dirMoves =
+ this.getPotentialLancerMoves_aux(
+ [x, y],
+ step,
+ { p: dirCode, c: color }
+ );
+ if (curDir[0] == step[0] && curDir[1] == step[1]) {
+ // Keeping same orientation: can choose after
+ let chooseMoves = [];
+ dirMoves.forEach(m => {
+ Object.keys(V.LANCER_DIRS).forEach(k => {
+ let mk = JSON.parse(JSON.stringify(m));
+ mk.appear[0].p = k;
+ moves.push(mk);
+ });
+ });
+ Array.prototype.push.apply(moves, chooseMoves);
+ } else Array.prototype.push.apply(moves, dirMoves);
+ });
+ return moves;
+ }
+ }
+ // I wasn't pushed: standard lancer move
+ const dirCode = this.board[x][y][1];
+ const monodirMoves =
+ this.getPotentialLancerMoves_aux([x, y], V.LANCER_DIRS[dirCode]);
+ // Add all possible orientations aftermove except if I'm being pushed
+ if (this.subTurn == 1) {
+ monodirMoves.forEach(m => {
+ Object.keys(V.LANCER_DIRS).forEach(k => {
+ let mk = JSON.parse(JSON.stringify(m));
+ mk.appear[0].p = k;
+ moves.push(mk);
+ });
+ });
+ return moves;
+ } else {
+ // I'm pushed: add potential nudges
+ let potentialNudges = [];
+ for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
+ if (
+ V.OnBoard(x + step[0], y + step[1]) &&
+ this.board[x + step[0]][y + step[1]] == V.EMPTY
+ ) {
+ potentialNudges.push(
+ this.getBasicMove(
+ [x, y],
+ [x + step[0], y + step[1]]
+ )
+ );
+ }
+ }
+ return monodirMoves.concat(potentialNudges);
+ }
+ }
+
+ getPotentialSentryMoves([x, y]) {
+ // The sentry moves a priori like a bishop:
+ let moves = super.getPotentialBishopMoves([x, y]);
+ // ...but captures are replaced by special move, if and only if
+ // "captured" piece can move now, considered as the capturer unit.
+ // --> except is subTurn == 2, in this case I don't push anything.
+ if (this.subTurn == 2) return moves.filter(m => m.vanish.length == 1);
+ moves.forEach(m => {
+ if (m.vanish.length == 2) {
+ // Temporarily cancel the sentry capture:
+ m.appear.pop();
+ m.vanish.pop();
+ }
+ });
+ // Can the pushed unit make any move? ...resulting in a non-self-check?
+ const color = this.getColor(x, y);
+ const fMoves = moves.filter(m => {
+ // Sentry push?
+ if (m.appear.length == 0) {
+ let res = false;
+ this.play(m);
+ let potentialMoves = this.getPotentialMovesFrom([m.end.x, m.end.y]);
+ // Add nudges (if any a priori possible)
+ for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
+ if (
+ V.OnBoard(m.end.x + step[0], m.end.y + step[1]) &&
+ this.board[m.end.x + step[0]][m.end.y + step[1]] == V.EMPTY
+ ) {
+ potentialMoves.push(
+ this.getBasicMove(
+ [m.end.x, m.end.y],
+ [m.end.x + step[0], m.end.y + step[1]]
+ )
+ );