Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Pacosako.js
index b88f2e3..0462d2d 100644 (file)
@@ -29,24 +29,30 @@ export class PacosakoRules extends ChessRules {
       w: ['b', 'q'],
       x: ['b', 'k'],
       y: ['q', 'q'],
-      z: ['q', 'k']
+      z: ['q', 'k'],
+      '@': ['k', 'k']
     };
   }
 
+  static fen2board(f) {
+    // Arobase is character 64
+    return f.charCodeAt() <= 90 ? "w" + f.toLowerCase() : "b" + f;
+  }
+
   static IsGoodPosition(position) {
     if (position.length == 0) return false;
     const rows = position.split("/");
     if (rows.length != V.size.x) return false;
-    let kingSymb = ['k', 'g', 'm', 'u', 'x'];
+    let kingSymb = ['k', 'g', 'm', 'u', 'x', 'z', '@'];
     let kings = { 'k': 0, 'K': 0 };
     for (let row of rows) {
       let sumElts = 0;
       for (let i = 0; i < row.length; i++) {
-        const lowR = row[i].toLowerCase
-        if (!!(row[i].toLowerCase().match(/[a-z]/))) {
+        if (!!(row[i].toLowerCase().match(/[a-z@]/))) {
           sumElts++;
           if (kingSymb.includes(row[i])) kings['k']++;
-          else if (kingSymb.some(s => row[i] == s.toUpperCase())) kings['K']++;
+          // Not "else if", if two kings dancing together
+          if (kingSymb.some(s => row[i] == s.toUpperCase())) kings['K']++;
         }
         else {
           const num = parseInt(row[i], 10);
@@ -98,15 +104,18 @@ export class PacosakoRules extends ChessRules {
     this.kingPos = { w: [-1, -1], b: [-1, -1] };
     const fenRows = V.ParseFen(fen).position.split("/");
     const startRow = { 'w': V.size.x - 1, 'b': 0 };
-    const kingSymb = ['k', 'g', 'm', 'u', 'x'];
+    const kingSymb = ['k', 'g', 'm', 'u', 'x', 'z', '@'];
     for (let i = 0; i < fenRows.length; i++) {
       let k = 0;
       for (let j = 0; j < fenRows[i].length; j++) {
         const c = fenRows[i].charAt(j);
-        if (kingSymb.includes(c))
-          this.kingPos["b"] = [i, k];
-        else if (kingSymb.some(s => c == s.toUpperCase()))
-          this.kingPos["w"] = [i, k];
+        if (!!(c.toLowerCase().match(/[a-z@]/))) {
+          if (kingSymb.includes(c))
+            this.kingPos["b"] = [i, k];
+          // Not "else if", in case of two kings dancing together
+          if (kingSymb.some(s => c == s.toUpperCase()))
+            this.kingPos["w"] = [i, k];
+        }
         else {
           const num = parseInt(fenRows[i].charAt(j), 10);
           if (!isNaN(num)) k += num - 1;
@@ -130,6 +139,8 @@ export class PacosakoRules extends ChessRules {
         end: ChessRules.SquareToCoords(umove.substr(2))
       });
     }
+    // Local stack of positions to avoid redundant moves:
+    this.repetitions = [];
   }
 
   static IsGoodFen(fen) {
@@ -141,10 +152,38 @@ export class PacosakoRules extends ChessRules {
     return true;
   }
 
+  static IsGoodFlags(flags) {
+    // 4 for castle + 16 for pawns
+    return !!flags.match(/^[a-z]{4,4}[01]{16,16}$/);
+  }
+
+  setFlags(fenflags) {
+    super.setFlags(fenflags); //castleFlags
+    this.pawnFlags = {
+      w: [...Array(8)], //pawns can move 2 squares?
+      b: [...Array(8)]
+    };
+    const flags = fenflags.substr(4); //skip first 4 letters, for castle
+    for (let c of ["w", "b"]) {
+      for (let i = 0; i < 8; i++)
+        this.pawnFlags[c][i] = flags.charAt((c == "w" ? 0 : 8) + i) == "1";
+    }
+  }
+
+  aggregateFlags() {
+    return [this.castleFlags, this.pawnFlags];
+  }
+
+  disaggregateFlags(flags) {
+    this.castleFlags = flags[0];
+    this.pawnFlags = flags[1];
+  }
+
   getUmove(move) {
     if (
       move.vanish.length == 1 &&
-      !(ChessRules.PIECES.includes(move.appear[0].p))
+      !(ChessRules.PIECES.includes(move.appear[0].p)) &&
+      move.appear[0].p == move.vanish[0].p //not a promotion
     ) {
       // An union moving
       return { start: move.start, end: move.end };
@@ -160,9 +199,18 @@ export class PacosakoRules extends ChessRules {
     );
   }
 
-  static GenRandInitFen(randomness) {
-    // Add empty umove
-    return ChessRules.GenRandInitFen(randomness) + " -";
+  static GenRandInitFen(options) {
+    // Add 16 pawns flags + empty umove:
+    return ChessRules.GenRandInitFen(options)
+      .slice(0, -2) + "1111111111111111 - -";
+  }
+
+  getFlagsFen() {
+    let fen = super.getFlagsFen();
+    // Add pawns flags
+    for (let c of ["w", "b"])
+      for (let i = 0; i < 8; i++) fen += (this.pawnFlags[c][i] ? "1" : "0");
+    return fen;
   }
 
   getUmoveFen() {
@@ -308,7 +356,8 @@ export class PacosakoRules extends ChessRules {
         p: cp.p
       })
     ];
-    mv.released = up[c];
+    // In move.end, to be sent to the server
+    mv.end.released = up[c];
     return mv;
   }
 
@@ -322,10 +371,28 @@ export class PacosakoRules extends ChessRules {
       this.board[x][y] = this.turn + piece;
     }
     let baseMoves = [];
-    switch (piece || this.getPiece(x, y)) {
-      case V.PAWN:
-        baseMoves = this.getPotentialPawnMoves([x, y]);
+    const c = this.turn;
+    switch (piece) {
+      case V.PAWN: {
+        const firstRank = (c == 'w' ? 7 : 0);
+        baseMoves = this.getPotentialPawnMoves([x, y]).filter(m => {
+          // Skip forbidden 2-squares jumps (except from first rank)
+          // Also skip unions capturing en-passant (not allowed).
+          return (
+            (
+              m.start.x == firstRank ||
+              Math.abs(m.end.x - m.start.x) == 1 ||
+              this.pawnFlags[c][m.start.y]
+            )
+            &&
+            (
+              this.board[x][y].charAt(1) == V.PAWN ||
+              m.start.y == m.end.y
+            )
+          );
+        });
         break;
+      }
       case V.ROOK:
         baseMoves = this.getPotentialRookMoves([x, y]);
         break;
@@ -345,7 +412,6 @@ export class PacosakoRules extends ChessRules {
     // When a pawn in an union reaches final rank with a non-standard
     // promotion move: apply promotion anyway
     let moves = [];
-    const c = this.turn;
     const oppCol = V.GetOppCol(c);
     const oppLastRank = (c == 'w' ? 7 : 0);
     baseMoves.forEach(m => {
@@ -380,7 +446,6 @@ export class PacosakoRules extends ChessRules {
             return;
           // Fix en-passant capture: union type, maybe released piece too
           const cs = [m.end.x + (c == 'w' ? 1 : -1), m.end.y];
-          const color = this.board[cs[0]][cs[1]].charAt(0);
           const code = this.board[cs[0]][cs[1]].charAt(1);
           if (code == V.PAWN) {
             // Simple en-passant capture (usual: just form union)
@@ -388,9 +453,10 @@ export class PacosakoRules extends ChessRules {
             m.appear[0].p = 'a';
           }
           else {
-            // An union pawn + something juste moved two squares
+            // An union pawn + something just moved two squares
+            const color = this.board[cs[0]][cs[1]].charAt(0);
             const up = this.getUnionPieces(color, code);
-            m.released = up[c];
+            m.end.released = up[c];
             let args = [V.PAWN, up[oppCol]];
             if (c == 'b') args = args.reverse();
             const cp = this.getUnionCode(args[0], args[1]);
@@ -405,23 +471,6 @@ export class PacosakoRules extends ChessRules {
     return moves;
   }
 
-  getPotentialKingMoves(sq) {
-    let moves = this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      "oneStep"
-    );
-    const c = this.turn;
-    const oppCol = V.GetOppCol(c);
-    if (
-      !this.isAttacked(this.kingPos[c], oppCol) &&
-      this.castleFlags[c].some(v => v < V.size.y)
-    ) {
-      moves = moves.concat(super.getCastleMoves(sq, null, true));
-    }
-    return moves;
-  }
-
   getEpSquare(moveOrSquare) {
     if (typeof moveOrSquare === "string") {
       const square = moveOrSquare;
@@ -431,11 +480,10 @@ export class PacosakoRules extends ChessRules {
     const move = moveOrSquare;
     const s = move.start,
           e = move.end;
-    const oppCol = V.GetOppCol(this.turn);
     if (
       s.y == e.y &&
       Math.abs(s.x - e.x) == 2 &&
-      this.getPiece(s.x, s.y, oppCol) == V.PAWN
+      this.getPiece(s.x, s.y, this.turn) == V.PAWN
     ) {
       return {
         x: (s.x + e.x) / 2,
@@ -451,6 +499,7 @@ export class PacosakoRules extends ChessRules {
       !!m1 &&
       !(ChessRules.PIECES.includes(m2.appear[0].p)) &&
       m2.vanish.length == 1 &&
+      !m2.end.released &&
       m1.start.x == m2.end.x &&
       m1.end.x == m2.start.x &&
       m1.start.y == m2.end.y &&
@@ -458,54 +507,326 @@ export class PacosakoRules extends ChessRules {
     );
   }
 
-  // Do not consider checks for now (TODO)
-  underCheck() {
+  getCastleMoves([x, y]) {
+    const c = this.getColor(x, y);
+    const oppCol = V.GetOppCol(c);
+    let moves = [];
+    const finalSquares = [ [2, 3], [6, 5] ];
+    castlingCheck: for (let castleSide = 0; castleSide < 2; castleSide++) {
+      if (this.castleFlags[c][castleSide] >= 8) continue;
+      const rookPos = this.castleFlags[c][castleSide];
+      const castlingColor = this.board[x][rookPos].charAt(0);
+      const castlingPiece = this.board[x][rookPos].charAt(1);
+
+      // Nothing on the path of the king ?
+      const finDist = finalSquares[castleSide][0] - y;
+      let step = finDist / Math.max(1, Math.abs(finDist));
+      let i = y;
+      let kingSquares = [y];
+      do {
+        if (
+          (
+            this.board[x][i] != V.EMPTY &&
+            (this.getColor(x, i) != c || ![y, rookPos].includes(i))
+          )
+        ) {
+          continue castlingCheck;
+        }
+        i += step;
+        kingSquares.push(i);
+      } while (i != finalSquares[castleSide][0]);
+      // No checks on the path of the king ?
+      if (this.isAttacked(kingSquares, oppCol)) continue castlingCheck;
+
+      // Nothing on the path to the rook?
+      step = castleSide == 0 ? -1 : 1;
+      for (i = y + step; i != rookPos; i += step) {
+        if (this.board[x][i] != V.EMPTY) continue castlingCheck;
+      }
+
+      // Nothing on final squares, except maybe king and castling rook?
+      for (i = 0; i < 2; i++) {
+        if (
+          finalSquares[castleSide][i] != rookPos &&
+          this.board[x][finalSquares[castleSide][i]] != V.EMPTY &&
+          (
+            finalSquares[castleSide][i] != y ||
+            this.getColor(x, finalSquares[castleSide][i]) != c
+          )
+        ) {
+          continue castlingCheck;
+        }
+      }
+
+      moves.push(
+        new Move({
+          appear: [
+            new PiPo({
+              x: x,
+              y: finalSquares[castleSide][0],
+              p: V.KING,
+              c: c
+            }),
+            new PiPo({
+              x: x,
+              y: finalSquares[castleSide][1],
+              p: castlingPiece,
+              c: castlingColor
+            })
+          ],
+          vanish: [
+            // King might be initially disguised (Titan...)
+            new PiPo({ x: x, y: y, p: V.KING, c: c }),
+            new PiPo({ x: x, y: rookPos, p: castlingPiece, c: castlingColor })
+          ],
+          end:
+            Math.abs(y - rookPos) <= 2
+              ? { x: x, y: rookPos }
+              : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) }
+        })
+      );
+    }
+
+    return moves;
+  }
+
+  getEnpassantCaptures(sq, shiftX) {
+    // HACK: when artificially change turn, do not consider en-passant
+    const mcMod2 = this.movesCount % 2;
+    const c = this.turn;
+    if ((c == 'w' && mcMod2 == 1) || (c == 'b' && mcMod2 == 0)) return [];
+    return super.getEnpassantCaptures(sq, shiftX);
+  }
+
+  isAttacked_aux(files, color, positions, fromSquare, released) {
+    // "positions" = array of FENs to detect infinite loops. Example:
+    // r1q1k2r/p1Pb1ppp/5n2/1f1p4/AV5P/P1eDP3/3B1PP1/R3K1NR,
+    // Bxd2 Bxc3 Bxb4 Bxc3 Bxb4 etc.
+    const newPos = {
+      fen: super.getBaseFen(),
+      piece: released,
+      from: fromSquare
+    };
+    if (
+      positions.some(p => {
+        return (
+          p.piece == newPos.piece &&
+          p.fen == newPos.fen &&
+          p.from == newPos.from
+        );
+      })
+    ) {
+      // Start of an infinite loop: exit
+      return false;
+    }
+    positions.push(newPos);
+    const rank = (color == 'w' ? 0 : 7);
+    const moves = this.getPotentialMovesFrom(fromSquare);
+    if (moves.some(m => m.end.x == rank && files.includes(m.end.y)))
+      // Found an attack!
+      return true;
+    for (let m of moves) {
+      if (!!m.end.released) {
+        // Turn won't change since !!m.released
+        this.play(m);
+        const res = this.isAttacked_aux(
+          files, color, positions, [m.end.x, m.end.y], m.end.released);
+        this.undo(m);
+        if (res) return true;
+      }
+    }
+    return false;
+  }
+
+  isAttacked(files, color) {
+    const rank = (color == 'w' ? 0 : 7);
+    // Since it's too difficult (impossible?) to search from the square itself,
+    // let's adopt a suboptimal but working strategy: find all attacks.
+    const c = this.turn;
+    // Artificial turn change is required:
+    this.turn = color;
+    let res = false;
+    outerLoop: for (let i=0; i<8; i++) {
+      for (let j=0; j<8; j++) {
+        // Attacks must start from a normal piece, not an union.
+        // Therefore, the following test is correct.
+        if (
+          this.board[i][j] != V.EMPTY &&
+          // Do not start with king (irrelevant, and lead to infinite calls)
+          [V.PAWN, V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN].includes(
+            this.board[i][j].charAt(1)) &&
+          this.board[i][j].charAt(0) == color
+        ) {
+          // Try from here.
+          const moves = this.getPotentialMovesFrom([i, j]);
+          if (moves.some(m => m.end.x == rank && files.includes(m.end.y))) {
+            res = true;
+            break outerLoop;
+          }
+          for (let m of moves) {
+            if (!!m.end.released) {
+              // Turn won't change since !!m.released
+              this.play(m);
+              let positions = [];
+              res = this.isAttacked_aux(
+                files, color, positions, [m.end.x, m.end.y], m.end.released);
+              this.undo(m);
+              if (res) break outerLoop;
+            }
+          }
+        }
+      }
+    }
+    this.turn = c;
+    return res;
+  }
+
+  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.board[rx][ry] != V.EMPTY &&
+        this.getPiece(rx, ry) == piece &&
+        this.getColor(rx, ry) == color &&
+        this.canTake([rx, ry], [x, y]) //TODO: necessary line?
+                                       //If not, generic method is OK
+      ) {
+        return true;
+      }
+    }
     return false;
   }
+
+  // Do not consider checks, except to forbid castling
   getCheckSquares() {
     return [];
   }
+
   filterValid(moves) {
     if (moves.length == 0) return [];
     const L = this.umoves.length; //at least 1: init from FEN
-    return moves.filter(m => !this.oppositeMoves(this.umoves[L - 1], m));
+    return moves.filter(m => {
+      if (this.oppositeMoves(this.umoves[L - 1], m)) return false;
+      if (!m.end.released) return true;
+      // Check for repetitions:
+      V.PlayOnBoard(this.board, m);
+      const newState = {
+        piece: m.end.released,
+        square: { x: m.end.x, y: m.end.y },
+        position: this.getBaseFen()
+      };
+      const repet =
+        this.repetitions.some(r => {
+          return (
+            r.piece == newState.piece &&
+            (
+              r.square.x == newState.square.x &&
+              r.square.y == newState.square.y
+            ) &&
+            r.position == newState.position
+          );
+        });
+      V.UndoOnBoard(this.board, m);
+      return !repet;
+    });
+  }
+
+  updateCastleFlags(move, piece) {
+    const c = this.turn;
+    const firstRank = (c == "w" ? 7 : 0);
+    if (piece == V.KING && move.appear.length > 0)
+      this.castleFlags[c] = [V.size.y, V.size.y];
+    else if (
+      move.start.x == firstRank &&
+      this.castleFlags[c].includes(move.start.y)
+    ) {
+      const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
+      this.castleFlags[c][flagIdx] = V.size.y;
+    }
+    else if (
+      move.end.x == firstRank &&
+      this.castleFlags[c].includes(move.end.y)
+    ) {
+      // Move to our rook: necessary normal piece, to union, releasing
+      // (or the rook was moved before!)
+      const flagIdx = (move.end.y == this.castleFlags[c][0] ? 0 : 1);
+      this.castleFlags[c][flagIdx] = V.size.y;
+    }
+  }
+
+  prePlay(move) {
+    // Easier before move is played in this case (flags are saved)
+    const c = this.turn;
+    const L = this.lastMoveEnd.length;
+    const lm = this.lastMoveEnd[L-1];
+    // NOTE: lm.p != V.KING, always.
+    const piece =
+      !!lm
+        ? lm.p
+        : this.getPiece(move.vanish[0].x, move.vanish[0].y);
+    if (piece == V.KING)
+      this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
+    this.updateCastleFlags(move, piece);
+    const pawnFirstRank = (c == 'w' ? 6 : 1);
+    if (
+      move.start.x == pawnFirstRank &&
+      piece == V.PAWN &&
+      Math.abs(move.end.x - move.start.x) == 2
+    ) {
+      // This move turns off a 2-squares pawn flag
+      this.pawnFlags[c][move.start.y] = false;
+    }
   }
 
   play(move) {
+    move.flags = JSON.stringify(this.aggregateFlags());
+    this.prePlay(move);
     this.epSquares.push(this.getEpSquare(move));
     // Check if the move is the last of the turn: all cases except releases
-    if (!move.released) {
+    if (!move.end.released) {
       // No more union releases available
       this.turn = V.GetOppCol(this.turn);
       this.movesCount++;
       this.lastMoveEnd.push(null);
     }
-    else this.lastMoveEnd.push(Object.assign({ p: move.released }, move.end));
+    else {
+      this.lastMoveEnd.push({
+        p: move.end.released,
+        x: move.end.x,
+        y: move.end.y
+      });
+    }
     V.PlayOnBoard(this.board, move);
     this.umoves.push(this.getUmove(move));
-    this.postPlay(move);
-  }
-
-  postPlay(move) {
-    if (move.vanish.length == 0)
-      // A piece released just moved. Cannot be the king.
-      return;
-    const c = move.vanish[0].c;
-    const piece = move.vanish[0].p;
-    if (piece == V.KING)
-      this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
-    this.updateCastleFlags(move, piece);
+    if (!move.end.released) this.repetitions = [];
+    else {
+      this.repetitions.push(
+        {
+          piece: move.end.released,
+          square: { x: move.end.x, y: move.end.y },
+          position: this.getBaseFen()
+        }
+      );
+    }
   }
 
   undo(move) {
     this.epSquares.pop();
+    this.disaggregateFlags(JSON.parse(move.flags));
     V.UndoOnBoard(this.board, move);
     this.lastMoveEnd.pop();
-    if (!move.released) {
+    if (!move.end.released) {
       this.turn = V.GetOppCol(this.turn);
       this.movesCount--;
     }
     this.umoves.pop();
+    if (!!move.end.released) this.repetitions.pop();
     this.postUndo(move);
   }
 
@@ -541,14 +862,15 @@ export class PacosakoRules extends ChessRules {
         mv = moves[randInt(moves.length)];
         mvArray.push(mv);
         this.play(mv);
-        if (!!mv.released)
+        if (!!mv.end.released)
           // A piece was just released from an union
           moves = this.getPotentialMovesFrom([mv.end.x, mv.end.y]);
         else break;
       }
       for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]);
-      if (!mv.released) return (mvArray.length > 1 ? mvArray : mvArray[0]);
+      if (!mv.end.released) return (mvArray.length > 1 ? mvArray : mvArray[0]);
     }
+    return null; //never reached
   }
 
   // NOTE: evalPosition() is wrong, but unused since bot plays at random
@@ -586,8 +908,13 @@ export class PacosakoRules extends ChessRules {
     // Add potential promotion indications:
     const firstLastRank = (c == 'w' ? [7, 0] : [0, 7]);
     if (move.end.x == firstLastRank[1] && piece == V.PAWN) {
-      const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p);
-      notation += "=" + up[c].toUpperCase();
+      notation += "=";
+      if (ChessRules.PIECES.includes(move.appear[0].p))
+        notation += move.appear[0].p.toUpperCase();
+      else {
+        const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p);
+        notation += up[c].toUpperCase();
+      }
     }
     else if (
       move.end.x == firstLastRank[0] &&