Advance on Chakart
[vchess.git] / client / src / variants / Chakart.js
index a1caed4..a2106e4 100644 (file)
-import { ChessRules } from "@/base_rules";
+import { ChessRules, Move, PiPo } from "@/base_rules";
+import { SuicideRules } from "@/variants/Suicide";
 
 export class ChakartRules extends ChessRules {
-  // NOTE: getBasicMove, ajouter les bonus à vanish array
-  // + déterminer leur effet (si cavalier) ou case (si banane ou bombe)
-  // (L'effet doit être caché au joueur : devrait être OK)
-  //
-  // Saut possible par dessus bonus ou champis mais pas bananes ou bombes
+  static get PawnSpecs() {
+    return SuicideRules.PawnSpecs;
+  }
+
+  static get HasCastle() {
+    return false;
+  }
+
+  static get CorrConfirm() {
+    // Because of bonus effects
+    return false;
+  }
+
+  static get CanAnalyze() {
+    return false;
+  }
+
+  hoverHighlight(x, y) {
+    if (this.subTurn == 1) return false;
+    const L = this.firstMove.length;
+    const fm = this.firstMove[L-1];
+    if (fm.end.effect != 0) return false;
+    const deltaX = Math.abs(fm.end.x - x);
+    const deltaY = Math.abs(fm.end.y - y);
+    return (
+      (deltaX == 0 && deltaY == 0) ||
+      (
+        this.board[x][y] == V.EMPTY &&
+        (
+          (fm.vanish[0].p == V.ROOK && deltaX == 1 && deltaY == 1) ||
+          (fm.vanish[0].p == V.BISHOP && deltaX + deltaY == 1)
+        )
+      )
+    );
+  }
+
+  static get IMMOBILIZE_CODE() {
+    return {
+      'p': 's',
+      'r': 'u',
+      'n': 'o',
+      'b': 'c',
+      'q': 't',
+      'k': 'l'
+    };
+  }
+
+  static get IMMOBILIZE_DECODE() {
+    return {
+      's': 'p',
+      'u': 'r',
+      'o': 'n',
+      'c': 'b',
+      't': 'q',
+      'l': 'k'
+    };
+  }
+
+  static get INVISIBLE_QUEEN() {
+    return 'i';
+  }
+
+  // Fictive color 'a', bomb banana mushroom egg
+  static get BOMB() {
+    // Doesn't collide with bishop because color 'a'
+    return 'b';
+  }
+  static get BANANA() {
+    return 'n';
+  }
+  static get EGG() {
+    return 'e';
+  }
+  static get MUSHROOM() {
+    return 'm';
+  }
+
+  static get PIECES() {
+    return (
+      ChessRules.PIECES.concat(
+      Object.keys(V.IMMOBILIZE_DECODE)).concat(
+      [V.BANANA, V.BOMB, V.EGG, V.MUSHROOM, V.INVISIBLE_QUEEN])
+    );
+  }
+
+  getPpath(b) {
+    let prefix = "";
+    if (
+      b[0] == 'a' ||
+      b[1] == V.INVISIBLE_QUEEN ||
+      Object.keys(V.IMMOBILIZE_DECODE).includes(b[1])
+    ) {
+      prefix = "Chakart/";
+    }
+    return prefix + b;
+  }
+
+  static ParseFen(fen) {
+    const fenParts = fen.split(" ");
+    return Object.assign(
+      ChessRules.ParseFen(fen),
+      { captured: fenParts[5] }
+    );
+  }
+
+  // King can be l or L (immobilized) --> similar to Alice variant
+  static IsGoodPosition(position) {
+    if (position.length == 0) return false;
+    const rows = position.split("/");
+    if (rows.length != V.size.x) return false;
+    let kings = { "k": 0, "K": 0, 'l': 0, 'L': 0 };
+    for (let row of rows) {
+      let sumElts = 0;
+      for (let i = 0; i < row.length; i++) {
+        if (['K','k','L','l'].includes(row[i])) kings[row[i]]++;
+        if (V.PIECES.includes(row[i].toLowerCase())) sumElts++;
+        else {
+          const num = parseInt(row[i]);
+          if (isNaN(num)) return false;
+          sumElts += num;
+        }
+      }
+      if (sumElts != V.size.y) return false;
+    }
+    if (kings['k'] + kings['l'] != 1 || kings['K'] + kings['L'] != 1)
+      return false;
+    return true;
+  }
+
+  static IsGoodFlags(flags) {
+    // 4 for Peach + Mario w, b
+    return !!flags.match(/^[01]{4,4}$/);
+  }
 
-  // FEN : immobilized (pas flag), castle flags + flags peach (power used?)
-  // "pièces" supplémentaires : bananes, bombes, champis, bonus --> + couleur ?
-  //   (Semble mieux sans couleur => couleur spéciale indiquant que c'est pas jouable)
-  // (Attention: pas jouables cf. getPotentialMoves...)
+  setFlags(fenflags) {
+    // King can send shell? Queen can be invisible?
+    this.powerFlags = {
+      w: [{ 'k': false, 'q': false }],
+      b: [{ 'k': false, 'q': false }]
+    };
+    for (let c of ["w", "b"]) {
+      for (let p of ['k', 'q']) {
+        this.powerFlags[c][p] =
+          fenFlags.charAt((c == "w" ? 0 : 2) + (p == 'k' ? 0 : 1)) == "1";
+      }
+    }
+  }
+
+  aggregateFlags() {
+    return this.powerFlags;
+  }
+
+  disaggregateFlags(flags) {
+    this.powerFlags = flags;
+  }
+
+  getFen() {
+    return super.getFen() + " " + this.getCapturedFen();
+  }
+
+  getFenForRepeat() {
+    return super.getFenForRepeat() + "_" + this.getCapturedFen();
+  }
+
+  getCapturedFen() {
+    let counts = [...Array(10).fill(0)];
+    let i = 0;
+    for (let p of [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN, V.PAWN]) {
+      counts[i] = this.captured["w"][p];
+      counts[5 + i] = this.captured["b"][p];
+      i++;
+    }
+    return counts.join("");
+  }
 
   setOtherVariables(fen) {
-    super.setOtherVariables(fen);
+    const fenParsed = V.ParseFen(fen);
+    // Initialize captured pieces' counts from FEN
+    this.captured = {
+      w: {
+        [V.ROOK]: parseInt(fenParsed.captured[0]),
+        [V.KNIGHT]: parseInt(fenParsed.captured[1]),
+        [V.BISHOP]: parseInt(fenParsed.captured[2]),
+        [V.QUEEN]: parseInt(fenParsed.captured[3]),
+        [V.PAWN]: parseInt(fenParsed.captured[4]),
+      },
+      b: {
+        [V.ROOK]: parseInt(fenParsed.captured[5]),
+        [V.KNIGHT]: parseInt(fenParsed.captured[6]),
+        [V.BISHOP]: parseInt(fenParsed.captured[7]),
+        [V.QUEEN]: parseInt(fenParsed.captured[8]),
+        [V.PAWN]: parseInt(fenParsed.captured[9]),
+      }
+    };
+    this.firstMove = [];
     this.subTurn = 1;
   }
 
+  getFlagsFen() {
+    let fen = "";
+    // Add power flags
+    for (let c of ["w", "b"])
+      for (let p of ['k', 'q']) fen += (this.powerFlags[c][p] ? "1" : "0");
+    return fen;
+  }
+
+  static get RESERVE_PIECES() {
+    return [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN];
+  }
+
+  getReserveMoves([x, y]) {
+    const color = this.turn;
+    const p = V.RESERVE_PIECES[y];
+    if (this.reserve[color][p] == 0) return [];
+    let moves = [];
+    const start = (color == 'w' && p == V.PAWN ? 1 : 0);
+    const end = (color == 'b' && p == V.PAWN ? 7 : 8);
+    for (let i = start; i < end; i++) {
+      for (let j = 0; j < V.size.y; j++) {
+        if (this.board[i][j] == V.EMPTY) {
+          let mv = new Move({
+            appear: [
+              new PiPo({
+                x: i,
+                y: j,
+                c: color,
+                p: p
+              })
+            ],
+            vanish: [],
+            start: { x: x, y: y }, //a bit artificial...
+            end: { x: i, y: j }
+          });
+          moves.push(mv);
+        }
+      }
+    }
+    return moves;
+  }
+
   getPotentialMovesFrom([x, y]) {
-    // TODO: bananes et bombes limitent les déplacements (agissent comme un mur "capturable")
-    // bananes jaunes et rouges ?! (agissant sur une seule couleur ?) --> mauvaise idée.
+    if (this.subTurn == 1) return super.getPotentialMovesFrom([x, y]);
     if (this.subTurn == 2) {
-      // TODO: coup compatible avec firstMove
+      let moves = [];
+      const L = this.firstMove.length;
+      const fm = this.firstMove[L-1];
+      switch (fm.end.effect) {
+        // case 0: a click is required (banana or bomb)
+        case 1:
+          // Exchange position with any piece
+          for (let i=0; i<8; i++) {
+            for (let j=0; j<8; j++) {
+              const colIJ = this.getColor(i, j);
+              if (
+                i != x &&
+                j != y &&
+                this.board[i][j] != V.EMPTY &&
+                colIJ != 'a'
+              ) {
+                const movedUnit = new PiPo({
+                  x: x,
+                  y: y,
+                  c: colIJ,
+                  p: this.getPiece(i, j)
+                });
+                let mMove = this.getBasicMove([x, y], [i, j]);
+                mMove.appear.push(movedUnit);
+                moves.push(mMove);
+              }
+            }
+          }
+          break;
+        case 2:
+          // Resurrect a captured piece
+          if (x >= V.size.x) moves = this.getReserveMoves([x, y]);
+          break;
+        case 3:
+          // Play again with the same piece
+          if (fm.end.x == x && fm.end.y == y)
+            moves = super.getPotentialMovesFrom([x, y]);
+          break;
+      }
+      return moves;
     }
   }
 
-  getPotentialPawnMoves(sq) {
-    //Toad: pion
-    //  laisse sur sa case de départ un champi turbo permettant à Peach d'aller
-    //  une case plus loin, et aux pièces arrivant sur cette case de sauter par
-    //  dessus une pièce immédiatement adjacente (en atterissant juste derrière).
+  getBasicMove([x1, y1], [x2, y2], tr) {
+    // TODO: if this.subTurn == 2 :: no mushroom effect
+    // (first, transformation. then:)
+    // Apply mushroom, bomb or banana effect (hidden to the player).
+    // Determine egg effect, too, and apply its first part if possible.
+    // add egg + add mushroom for pawns.
+    let move = super.getBasicMove([x1, y1], [x2, y2]);
+    // TODO
+    return move;
+    // Infer move type based on its effects (used to decide subTurn 1 --> 2)
+    // --> impossible étant donné juste first part (egg --> effect?)
+    // => stocker l'effet (i, ii ou iii) dans le coup directement,
+    // Pas terrible, mais y'aura pas 36 variantes comme ça. Disons end.effect == 0, 1, 2 ou 3
+    // 0 => tour ou fou, pose potentielle.
+    // If queen can be invisible, add move same start + end but final type changes
+    // set move.end.effect (if subTurn --> 2)
   }
 
-  // Coups en 2 temps (si pose)
-  getPotentialRookMoves(sq) {
-    //Donkey : tour
-    //  pose une banane (optionnel) sur une case adjacente (diagonale) à celle d'arrivée
-    //  Si une pièce arrive sur la peau de banane, alors elle effectue un déplacement
-    //  aléatoire d'une (2?) case (vertical ou horizontal) depuis sa position finale.
+  getEnpassantCaptures([x, y], shiftX) {
+    const Lep = this.epSquares.length;
+    const epSquare = this.epSquares[Lep - 1]; //always at least one element
+    let enpassantMove = null;
+    if (
+      !!epSquare &&
+      epSquare.x == x + shiftX &&
+      Math.abs(epSquare.y - y) == 1
+    ) {
+      // Not using this.getBasicMove() because the mushroom has no effect
+      enpassantMove = super.getBasicMove([x, y], [epSquare.x, epSquare.y]);
+      enpassantMove.vanish.push({
+        x: x,
+        y: epSquare.y,
+        p: V.PAWN,
+        c: this.getColor(x, epSquare.y)
+      });
+    }
+    return !!enpassantMove ? [enpassantMove] : [];
   }
 
-  // Coups en 2 temps (si pose)
-  getPotentialBishopMoves([x, y]) {
-    //Wario: fou
-    //  pose une bombe (optionnel) sur une case orthogonalement adjacente à la case d'arrivée
-    //  Si une pièce arrive sur une bombe, alors elle effectue un déplacement diagonal
-    //  aléatoire d'une (2?) case depuis sa position finale (juste une case si impossible).
+  getPotentialQueenMoves(sq) {
+    const normalMoves = super.getPotentialQueenMoves(sq);
+    // If flag allows it, add 'invisible movements'
+    let invisibleMoves = [];
+    if (this.powerFlags[this.turn][V.QUEEN]) {
+      normalMoves.forEach(m => {
+        if (m.vanish.length == 1) {
+          let im = JSON.parse(JSON.stringify(m));
+          m.appear[0].p = V.INVISIBLE_QUEEN;
+          invisibleMoves.push(im);
+        }
+      });
+    }
+    return normalMoves.concat(invisibleMoves);
   }
 
-  getPotentialKnightMoves([x, y]) {
-    //Yoshi: cavalier
-    //  laisse sur sa case de départ un bonus aléatoire
-    //  (NOTE: certains bonus pourraient ne pas être applicables ==> pion bloqué par exemple)
-    //    - i) roi boo(*E*) : échange avec n'importe quelle pièce (choix du joueur, type et/ou couleur différents)
-    //                       à condition que la position résultante ne soit pas un auto-échec
-    //    - i*) koopa(*B*) : ramène sur la case initiale
-    //    - ii) toadette(*R*) : permet de poser une pièce capturée sur le plateau
-    //                         (n'importe où sauf 8eme rangée pour les pions)
-    //    - ii*) chomp(*W*) : mange la pièce ; si c'est Peach, c'est perdu
-    //    - iii) daisy(*T*) : permet de rejouer un coup avec la même pièce --> cumulable si ensuite coup sur bonus Daisy.
-    //    - iii*) bowser(*M*) : immobilise la pièce (marquée jaune/rouge), qui ne pourra pas jouer au tour suivant
-    //    - iv) luigi(*L*) : fait changer de camp une pièce adverse (aléatoire ?) (sauf le roi)
-    //    - iv*) waluigi(*D*) : fait changer de camp une de nos pièces (aléatoire ?) (sauf le roi)
-    //  --> i, ii, iii en deux temps (subTurn 1 & 2)
-    //      iii* : indication dans FEN (immobilized)
+  getPotentialKingMoves([x, y]) {
+    let moves = super.getPotentialKingMoves([x, y]);
+    const color = this.turn;
+    // If flag allows it, add 'remote shell captures'
+    if (this.powerFlags[this.turn][V.KING]) {
+      V.steps[V.ROOK].concat(V.steps[V.BISHOP]).forEach(step => {
+        let [i, j] = [x + 2 * step[0], y + 2 * step[1]];
+        while (
+          V.OnBoard(i, j) &&
+          (
+            this.board[i][j] == V.EMPTY ||
+            (
+              this.getColor(i, j) == 'a' &&
+              [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
+            )
+          )
+        ) {
+          i += step[0];
+          j += step[1];
+        }
+        if (V.OnBoard(i, j) && this.getColor(i, j) != color)
+          // May just destroy a bomb or banana:
+          moves.push(this.getBasicMove([x, y], [i, j]));
+      });
+    }
+    return moves;
   }
 
-  getPotentialQueenMoves(sq) {
-    //Mario: dame
-    //  pouvoir "casquette ailée" (à chaque coup?) : peut sauter par dessus n'importe quelle pièce (une seule), sans la capturer.
+  getSlideNJumpMoves([x, y], steps, oneStep) {
+    let moves = [];
+    outerLoop: for (let step of steps) {
+      let i = x + step[0];
+      let j = y + step[1];
+      while (
+        V.OnBoard(i, j) &&
+        (
+          this.board[i][j] == V.EMPTY ||
+          (
+            this.getColor(i, j) == 'a' &&
+            [V.EGG, V.MUSHROOM].includes(this.getPiece(i, j))
+          )
+        )
+      ) {
+        moves.push(this.getBasicMove([x, y], [i, j]));
+        if (oneStep) continue outerLoop;
+        i += step[0];
+        j += step[1];
+      }
+      if (V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
+        moves.push(this.getBasicMove([x, y], [i, j]));
+    }
+    return moves;
+  }
+
+  getAllPotentialMoves() {
+    if (this.subTurn == 1) return super.getAllPotentialMoves();
+    let moves = [];
+    const L = this.firstMove.length;
+    const fm = this.firstMove[L-1];
+    //switch (fm.end.effect) {
+    //  case 0: //...
   }
 
-  getPotentialKingMoves(sq) {
-    //Peach: roi
-    //  Carapace rouge (disons ^^) jouable une seule fois dans la partie,
-    //  au lieu de se déplacer. Capture un ennemi au choix parmi les plus proches,
-    //  à condition qu'ils soient visible (suivant les directions de déplacement d'une dame).
-    //  Profite des accélérateurs posés par les pions (+ 1 case : obligatoire).
+  doClick(square) {
+    if (isNaN(square[0])) return null;
+    if (this.subTurn == 1) return null;
+    const L = this.firstMove.length;
+    const fm = this.firstMove[L-1];
+    if (fm.end.effect != 0) return null;
+    const [x, y] = [square[0], square[1]];
+    const deltaX = Math.abs(fm.end.x - x);
+    const deltaY = Math.abs(fm.end.y - y);
+    if (deltaX == 0 && deltaY == 0) {
+      // Empty move:
+      return {
+        start: { x: -1, y: -1 },
+        end: { x: -1, y: -1 },
+        appear: [],
+        vanish: []
+      };
+    }
+    if (
+      this.board[x][y] == V.EMPTY &&
+      (
+        (fm.vanish[0].p == V.ROOK && deltaX == 1 && deltaY == 1) ||
+        (fm.vanish[0].p == V.BISHOP && deltaX + deltaY == 1)
+      )
+    ) {
+      return new Move({
+        start: { x: -1, y: -1 },
+        end: { x: x, y: y },
+        appear: [
+          new PiPo({
+            x: x,
+            y: y,
+            c: 'a',
+            p: (fm.vanish[0].p == V.ROOK ? V.BANANA : V.BOMB)
+          })
+        ],
+        vanish: []
+      });
+    }
+    return null;
   }
 
   play(move) {
-    // TODO: subTurn passe à 2 si potentiellement pose (tour, fou) ou si choix (reconnaître i (ok), ii (ok) et iii (si coup normal + pas immobilisé) ?)
-    // voire +2 si plusieurs daisy...
+    move.flags = JSON.stringify(this.aggregateFlags());
+    this.epSquares.push(this.getEpSquare(move));
+    V.PlayOnBoard(this.board, move);
+    if (move.end.effect !== undefined) {
+      this.firstMove.push(move);
+      this.subTurn = 2;
+      if (move.end.effect == 2) this.reserve = this.captured;
+    }
+    else {
+      this.turn = V.GetOppCol(this.turn);
+      this.subTurn = 1;
+      this.reserve = null;
+    }
+  }
+
+  postPlay(move) {
+    if (move.vanish[0].p == V.KING) { }
+    //si roi et delta >= 2 ou dame et appear invisible queen : turn flag off
+    if (move.vanish.length == 2 && move.vanish[1].c != 'a')
+      // Capture: update this.captured
+      this.captured[move.vanish[1].c][move.vanish[1].p]++;
+    else if (move.vanish.length == 0) {
+      // A piece is back on board
+      this.captured[move.vanish[1].c][move.vanish[1].p]++;
+      this.reserve = null;
+    }
+    // si pièce immobilisée de ma couleur : elle redevient utilisable (changer status fin de play)
+    // TODO: un-immobilize my formerly immobilized piece, if any.
+    // Make invisible queen visible again, if any opponent invisible queen.
   }
 
   undo(move) {
-    // TODO: reconnaissance inverse si subTurn == 1 --> juste impossible ==> marquer pendant play (comme DoubleMove1 : move.turn = ...)
+    // TODO: should be easy once end.effect is set in getBasicMove()
+    if (move.end.effect !== undefined)
+      this.firstMove.pop();
   }
 
-  //atLeastOneMove() should be OK
+  postUndo(move) {
+    if (move.vanish.length == 2 && move.vanish[1].c != 'a')
+      this.captured[move.vanish[1].c][move.vanish[1].p]--;
+  }
 
-  doClick(square) {
-    // A click to promote a piece on subTurn 2 would trigger this.
-    // For now it would then return [NaN, NaN] because surrounding squares
-    // have no IDs in the promotion modal. TODO: improve this?
-    if (isNaN(square[0])) return null;
-    // If subTurn == 2:
-    // if square is empty && firstMove is compatible,
-    // complete the move (banana or bomb).
-    // if square not empty, just complete with empty move
-    const Lf = this.firstMove.length;
-    if (this.subTurn == 2) {
-      if (
-        this.board[square[0]][square[1]] == V.EMPTY &&
-        !this.underCheck(this.turn) &&
-        (La == 0 || !this.oppositeMoves(this.amoves[La-1], this.firstMove[Lf-1]))
-      ) {
-        return {
-          start: { x: -1, y: -1 },
-          end: { x: -1, y: -1 },
-          appear: [],
-          vanish: []
-        };
+  getCheckSquares() {
+    return [];
+  }
+
+  getCurrentScore() {
+    // Find kings (not tracked in this variant)
+    let kingThere = { w: false, b: false };
+    for (let i=0; i<8; i++) {
+      for (let j=0; j<8; j++) {
+        if (this.board[i][j] != V.EMPTY && this.getPiece(i, j) == V.KING)
+          kingThere[this.getColor(i, j)] = true;
       }
     }
-    return null;
+    if (!kingThere['w']) return "0-1";
+    if (!kingThere['b']) return "1-0";
+    return "*";
   }
 
-  postPlay(move) {
-    // TODO: king may also be "chomped"
-    super.updateCastleFlags(move, piece);
+  static GenRandInitFen(randomness) {
+    return (
+      SuicideRules.GenRandInitFen(randomness).slice(0, -1) +
+      // Add Peach + Mario flags, re-add en-passant + capture counts
+      "0000 - 0000000000"
+    );
   }
 
-  getCurrentScore() {
-    if (this.kingPos[this.turn][0] < 0)
-      // King captured (or "chomped")
-      return this.turn == "w" ? "0-1" : "1-0";
-    const color = V.GetOppCol(this.turn);
-    const lastRank = (color == 'w' ? 0 : 7);
-    if (this.kingPos[color][0] == lastRank)
-      // The opposing edge is reached!
-      return color == "w" ? "1-0" : "0-1";
-    if (this.atLeastOneMove()) return "*";
-    // Game over
-    const oppCol = this.turn;
-    if (!this.underCheck(oppCol)) return "1/2";
-    return (oppCol == "w" ? "0-1" : "1-0");
-    //TODO: But = capturer la princesse adverse. Variante : but = la princesse arrive de l'autre côté de l'échiquier.
-    //==> On peut mixer ces deux conditions : arriver au bout du plateau ou capturer la princesse adverse.
-    return '*';
+  filterValid(moves) {
+    return moves;
   }
 
   getComputerMove() {
-    // TODO: random mover
+    // Random mover:
+    const moves = this.getAllValidMoves();
+    let move1 = moves[randInt(movs.length)];
+    this.play(move1);
+    let move2 = undefined;
+    if (this.subTurn == 2) {
+      const moves2 = this.getAllValidMoves();
+      move2 = moves2[randInt(moves2.length)];
+    }
+    this.undo(move1);
+    if (!move2) return move1;
+    return [move1, move2];
   }
 
-  //Détails :
-  //Si une pièce pose quelque chose sur une case ça remplace ce qui y était déjà.
-  //Pas de condition de pat, puisque l'objectif est la capture de la reine :)
+  getNotation(move) {
+    // TODO: invisibility used => move notation Q??
+    // Also, bonus should be clearly indicated + bomb/bananas locations
+    return super.getNotation(move);
+  }
 };