Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Fullcavalry.js
index 672ebc8..07951f6 100644 (file)
@@ -88,66 +88,32 @@ export class FullcavalryRules extends ChessRules {
     );
   }
 
-  static GenRandInitFen(randomness) {
-    if (randomness == 0)
+  static GenRandInitFen(options) {
+    if (options.randomness == 0)
       // Deterministic:
-      return "efbqkbnm/pppppppp/8/8/8/8/PPPPPPPP/EDBQKBNM w 0 ahah -";
+      return "enbqkbnm/pppppppp/8/8/8/8/PPPPPPPP/ENBQKBNM w 0 ahah -";
 
-    const baseFen = ChessRules.GenRandInitFen(randomness);
+    const baseFen = ChessRules.GenRandInitFen(options);
     // Replace rooks by lancers with expected orientation:
     const firstBlackRook = baseFen.indexOf('r'),
           lastBlackRook = baseFen.lastIndexOf('r'),
           firstWhiteRook = baseFen.indexOf('R'),
           lastWhiteRook = baseFen.lastIndexOf('R');
     return (
-      baseFen.substring(0, firstBlackRook) + 'e' +
-      baseFen.substring(firstBlackRook + 1, lastBlackRook) + 'm' +
-      baseFen.substring(lastBlackRook + 1, firstWhiteRook) + 'E' +
-      baseFen.substring(firstWhiteRook + 1, lastWhiteRook) + 'M' +
+      baseFen.substring(0, firstBlackRook) +
+        (firstBlackRook <= 3 ? 'e' : 'm') +
+      baseFen.substring(firstBlackRook + 1, lastBlackRook) +
+        (lastBlackRook >= 5 ? 'm' : 'e') +
+      // Subtract 35 = total number of characters before last FEN row:
+      // 8x3 (full rows) + 4 (empty rows) + 7 (separators)
+      baseFen.substring(lastBlackRook + 1, firstWhiteRook) +
+        (firstWhiteRook - 35 <= 3 ? 'E' : 'M') +
+      baseFen.substring(firstWhiteRook + 1, lastWhiteRook) +
+        (lastWhiteRook - 35 >= 5 ? 'M' : 'E') +
       baseFen.substring(lastWhiteRook + 1)
     );
   }
 
-  // Because of the lancers, getPiece() could be wrong:
-  // use board[x][y][1] instead (always valid).
-  // TODO: base implementation now uses this too (no?)
-  getBasicMove([sx, sy], [ex, ey], tr) {
-    const initColor = this.getColor(sx, sy);
-    const initPiece = this.board[sx][sy].charAt(1);
-    let mv = new Move({
-      appear: [
-        new PiPo({
-          x: ex,
-          y: ey,
-          c: tr ? tr.c : initColor,
-          p: tr ? tr.p : initPiece
-        })
-      ],
-      vanish: [
-        new PiPo({
-          x: sx,
-          y: sy,
-          c: initColor,
-          p: initPiece
-        })
-      ]
-    });
-
-    // The opponent piece disappears if we take it
-    if (this.board[ex][ey] != V.EMPTY) {
-      mv.vanish.push(
-        new PiPo({
-          x: ex,
-          y: ey,
-          c: this.getColor(ex, ey),
-          p: this.board[ex][ey].charAt(1)
-        })
-      );
-    }
-
-    return mv;
-  }
-
   getPotentialMovesFrom([x, y]) {
     if (this.getPiece(x, y) == V.LANCER)
       return this.getPotentialLancerMoves([x, y]);
@@ -156,12 +122,8 @@ export class FullcavalryRules extends ChessRules {
 
   getPotentialPawnMoves([x, y]) {
     const color = this.getColor(x, y);
-    let moves = [];
-    const [sizeX, sizeY] = [V.size.x, V.size.y];
     let shiftX = (color == "w" ? -1 : 1);
-    const startRank = color == "w" ? sizeX - 2 : 1;
-    const lastRank = color == "w" ? 0 : sizeX - 1;
-
+    const lastRank = (color == "w" ? 0 : 7);
     let finalPieces = [V.PAWN];
     if (x + shiftX == lastRank) {
       // Only allow direction facing inside board:
@@ -171,46 +133,7 @@ export class FullcavalryRules extends ChessRules {
           : ['c', 'd', 'e', 'm', 'o'];
       finalPieces = allowedLancerDirs.concat([V.KNIGHT, V.BISHOP, V.QUEEN]);
     }
-    if (this.board[x + shiftX][y] == V.EMPTY) {
-      // One square forward
-      for (let piece of finalPieces) {
-        moves.push(
-          this.getBasicMove([x, y], [x + shiftX, y], {
-            c: color,
-            p: piece
-          })
-        );
-      }
-      if (x == startRank && this.board[x + 2 * shiftX][y] == V.EMPTY)
-        // Two squares jump
-        moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
-    }
-    // Captures
-    for (let shiftY of [-1, 1]) {
-      if (
-        y + shiftY >= 0 &&
-        y + shiftY < sizeY &&
-        this.board[x + shiftX][y + shiftY] != V.EMPTY &&
-        this.canTake([x, y], [x + shiftX, y + shiftY])
-      ) {
-        for (let piece of finalPieces) {
-          moves.push(
-            this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
-              c: color,
-              p: piece
-            })
-          );
-        }
-      }
-    }
-
-    // Add en-passant captures
-    Array.prototype.push.apply(
-      moves,
-      this.getEnpassantCaptures([x, y], shiftX)
-    );
-
-    return moves;
+    return super.getPotentialPawnMoves([x, y], finalPieces);
   }
 
   // Obtain all lancer moves in "step" direction
@@ -369,12 +292,8 @@ export class FullcavalryRules extends ChessRules {
           this.getColor(coord.x, coord.y) == color
         )
       ) {
-        if (
-          this.getPiece(coord.x, coord.y) == V.LANCER &&
-          !this.isImmobilized([coord.x, coord.y])
-        ) {
+        if (this.getPiece(coord.x, coord.y) == V.LANCER)
           lancerPos.push({x: coord.x, y: coord.y});
-        }
         coord.x += step[0];
         coord.y += step[1];
       }
@@ -409,10 +328,14 @@ export class FullcavalryRules extends ChessRules {
 
   filterValid(moves) {
     // At move 1, forbid captures (in case of...):
-    if (this.movesCount >= 2) return moves;
+    if (this.movesCount >= 2) return super.filterValid(moves);
     return moves.filter(m => m.vanish.length == 1);
   }
 
+  static get SEARCH_DEPTH() {
+    return 2;
+  }
+
   getNotation(move) {
     let notation = super.getNotation(move);
     if (Object.keys(V.LANCER_DIRNAMES).includes(move.vanish[0].p))