+ // subTurn == 2 only when a sentry moved, and is about to push something
+ this.subTurn = 1;
+ // Stack pieces' forbidden squares after a sentry move at each turn
+ const parsedFen = V.ParseFen(fen);
+ if (parsedFen.sentrypush == "-") this.sentryPush = [null];
+ else {
+ this.sentryPush = [
+ parsedFen.sentrypush.split(",").map(sq => {
+ return V.SquareToCoords(sq);
+ })
+ ];
+ }
+ }
+
+ canTake([x1,y1], [x2, y2]) {
+ if (this.subTurn == 2)
+ // Sentry push: pieces can capture own color (only)
+ return this.getColor(x1, y1) == this.getColor(x2, y2);
+ return super.canTake([x1,y1], [x2, y2]);
+ }
+
+ static GenRandInitFen(randomness) {
+ // TODO: special conditions for 960
+ return "jsfqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JSDQKBNR w 0 1111 - -";
+ }
+
+ // 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;
+ }
+
+ 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 [];
+ return this.getPotentialMovesFrom_aux([x, y]);
+ }
+ // 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 [];
+ return this.getPotentialMovesFrom_aux([x,y]);