+ // Inverse operation: augment piece
+ getAugmented(piece) {
+ const knight = this.movesCount <= 1;
+ switch (piece) {
+ case V.ROOK: return (knight ? 'u' : 'v');
+ case V.KNIGHT: return (knight ? 'm' : 'o');
+ case V.BISHOP: return (knight ? 'a' : 'c');
+ case V.QUEEN: return (knight ? 's' : 't');
+ case V.KING: return (knight ? 'j' : 'l');
+ }
+ return '_'; //never reached
+ }
+
+ static IsGoodPosition(position) {
+ if (position.length == 0) return false;
+ const rows = position.split("/");
+ if (rows.length != V.size.x) return false;
+ let kings = { "w": 0, "b": 0 };
+ const allPiecesCodes = V.PIECES.concat(V.AUGMENTED_PIECES);
+ const kingBlackCodes = ['j','k','l'];
+ const kingWhiteCodes = ['J','K','L'];
+ for (let row of rows) {
+ let sumElts = 0;
+ for (let i = 0; i < row.length; i++) {
+ if (kingBlackCodes.includes(row[i])) kings['b']++;
+ else if (kingWhiteCodes.includes(row[i])) kings['w']++;
+ if (allPiecesCodes.includes(row[i].toLowerCase())) sumElts++;
+ else {
+ const num = parseInt(row[i], 10);
+ if (isNaN(num)) return false;
+ sumElts += num;
+ }
+ }
+ if (sumElts != V.size.y) return false;
+ }
+ // Both kings should be on board, only one of each color:
+ if (Object.values(kings).some(v => v != 1)) return false;
+ return true;
+ }
+
+ // Kings may be augmented:
+ scanKings(fen) {
+ this.kingPos = { w: [-1, -1], b: [-1, -1] };
+ const rows = V.ParseFen(fen).position.split("/");
+ for (let i = 0; i < rows.length; i++) {
+ let k = 0; //column index on board
+ for (let j = 0; j < rows[i].length; j++) {
+ const piece = rows[i].charAt(j);
+ if (['j','k','l'].includes(piece.toLowerCase())) {
+ const color = (piece.charCodeAt(0) <= 90 ? 'w' : 'b');
+ this.kingPos[color] = [i, k];
+ }
+ else {
+ const num = parseInt(rows[i].charAt(j), 10);
+ if (!isNaN(num)) k += num - 1;
+ }
+ k++;
+ }
+ }
+ }
+
+ canIplay(side, [x, y]) {
+ if (this.movesCount >= 4) return super.canIplay(side, [x, y]);
+ return (
+ this.turn == side &&
+ (
+ (side == 'w' && x == 7) ||
+ (side == 'b' && x == 0)
+ )
+ );
+ }
+
+ hoverHighlight([x, y]) {
+ const c = this.turn;
+ return (
+ this.movesCount <= 3 &&
+ ((c == 'w' && x == 7) || (c == 'b' && x == 0))
+ );
+ }
+
+ onlyClick([x, y]) {
+ return (
+ this.movesCount <= 3 ||
+ // TODO: next line theoretically shouldn't be required...
+ (this.movesCount == 4 && this.getColor(x, y) != this.turn)
+ );
+ }
+
+ // Special case of move 1 = choose squares, knight first, then bishop
+ doClick(square) {
+ if (this.movesCount >= 4) return null;
+ const color = this.turn;
+ const [x, y] = [square[0], square[1]];
+ if ((color == 'w' && x != 7) || (color == 'b' && x != 0)) return null;
+ const selectedPiece = this.board[x][y][1];
+ // Prevent re-augmenting a chosen piece:
+ if (!ChessRules.PIECES.includes(selectedPiece)) return null;
+ return new Move({
+ appear: [
+ new PiPo({
+ x: x,
+ y: y,
+ c: color,
+ p: this.getAugmented(selectedPiece)
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: x,
+ y: y,
+ c: color,
+ p: selectedPiece
+ })
+ ],
+ start: { x: x, y: y },
+ end: { x: x, y: y }
+ });
+ }
+