+ }
+ 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();
+ }
+ });
+ const color = this.getColor(x, y);
+ const fMoves = moves.filter(m => {
+ // Can the pushed unit make any move? ...resulting in a non-self-check?
+ if (m.appear.length == 0) {
+ let res = false;
+ this.play(m);
+ let moves2 = this.getPotentialMovesFrom([m.end.x, m.end.y]);
+ for (let m2 of moves2) {
+ this.play(m2);
+ res = !this.underCheck(color);
+ this.undo(m2);
+ if (res) break;
+ }
+ this.undo(m);
+ return res;
+ }
+ return true;
+ });
+ return fMoves;
+ }
+
+ getPotentialJailerMoves([x, y]) {
+ return super.getPotentialRookMoves([x, y]).filter(m => {
+ // Remove jailer captures
+ return m.vanish[0].p != V.JAILER || m.vanish.length == 1;
+ });
+ }
+
+ getPotentialKingMoves(sq) {
+ const moves = this.getSlideNJumpMoves(
+ sq,
+ V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
+ "oneStep"
+ );
+ return (
+ this.subTurn == 1
+ ? moves.concat(this.getCastleMoves(sq))
+ : moves
+ );
+ }
+
+ atLeastOneMove() {
+ // If in second-half of a move, we already know that a move is possible
+ if (this.subTurn == 2) return true;
+ return super.atLeastOneMove();
+ }
+
+ filterValid(moves) {
+ if (moves.length == 0) return [];
+ const basicFilter = (m, c) => {
+ this.play(m);
+ const res = !this.underCheck(c);
+ this.undo(m);
+ return res;
+ };
+ // Disable check tests for sentry pushes,
+ // because in this case the move isn't finished
+ let movesWithoutSentryPushes = [];
+ let movesWithSentryPushes = [];
+ moves.forEach(m => {
+ // Second condition below for special king "pass" moves
+ if (m.appear.length > 0 || m.vanish.length == 0)
+ movesWithoutSentryPushes.push(m);
+ else movesWithSentryPushes.push(m);
+ });
+ const color = this.turn;
+ const oppCol = V.GetOppCol(color);
+ const filteredMoves =
+ movesWithoutSentryPushes.filter(m => basicFilter(m, color));
+ // If at least one full move made, everything is allowed.
+ // Else: forbid checks and captures.
+ return (
+ this.movesCount >= 2
+ ? filteredMoves
+ : filteredMoves.filter(m => {
+ return (m.vanish.length <= 1 && basicFilter(m, oppCol));
+ })
+ ).concat(movesWithSentryPushes);
+ }
+
+ getAllValidMoves() {
+ if (this.subTurn == 1) return super.getAllValidMoves();
+ // Sentry push:
+ const sentrySq = [this.sentryPos.x, this.sentryPos.y];
+ return this.filterValid(this.getPotentialMovesFrom(sentrySq));
+ }
+
+ isAttacked(sq, color) {
+ return (
+ super.isAttacked(sq, color) ||
+ this.isAttackedByLancer(sq, color) ||
+ this.isAttackedBySentry(sq, color)
+ // The jailer doesn't capture.
+ );
+ }
+
+ isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
+ for (let step of steps) {
+ let rx = x + step[0],
+ ry = y + step[1];
+ while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
+ rx += step[0];
+ ry += step[1];
+ }
+ if (
+ V.OnBoard(rx, ry) &&
+ this.getPiece(rx, ry) == piece &&
+ this.getColor(rx, ry) == color &&
+ !this.isImmobilized([rx, ry])
+ ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ isAttackedByPawn([x, y], color) {
+ const pawnShift = (color == "w" ? 1 : -1);
+ if (x + pawnShift >= 0 && x + pawnShift < V.size.x) {
+ for (let i of [-1, 1]) {