Flip knights for variants with knightriders (waiting for a better image)
[vchess.git] / client / src / variants / Monochrome.js
index 4a24e60..2d5a912 100644 (file)
@@ -37,6 +37,10 @@ export class MonochromeRules extends ChessRules {
     return true;
   }
 
+  getPpath(b) {
+    return (b[1] == V.KNIGHT ? "Enpassant/" : "") + b;
+  }
+
   canIplay(side, [x, y]) {
     const xBounds = side == 'w' ? [4,7] : [0,3];
     return this.turn == side && x >= xBounds[0] && x <= xBounds[1];
@@ -47,79 +51,21 @@ export class MonochromeRules extends ChessRules {
     return ((x1 <= 3 && x2 >= 4) || (x1 >= 4 && x2 <= 3));
   }
 
-  // follow steps from x,y until something is met.
-  // if met piece is opponent and same movement (asA): eat it!
-  findCaptures_aux([x, y], asA) {
-    let moves = [];
-    const steps =
-      asA != V.PAWN
-        ? [V.QUEEN, V.KING].includes(asA)
-          ? V.steps[V.ROOK].concat(V.steps[V.BISHOP])
-          : V.steps[asA]
-        : this.turn == "w"
-          ? [
-            [-1, -1],
-            [-1, 1]
-          ]
-          : [
-            [1, -1],
-            [1, 1]
-          ];
-    const oneStep = [V.KNIGHT, V.PAWN, V.KING].includes(asA);
-    outerLoop: for (let loop = 0; loop < steps.length; loop++) {
-      const step = steps[loop];
-      let i = x + step[0];
-      let j = y + step[1];
-      while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
-        if (oneStep) continue outerLoop;
-        i += step[0];
-        j += step[1];
-      }
-      if (
-        V.OnBoard(i, j) &&
-        this.getPiece(i, j) == asA &&
-        this.canTake([i, j], [x, y])
-      ) {
-        // eat!
-        moves.push(this.getBasicMove([x, y], [i, j]));
-      }
-    }
-    return moves;
-  }
-
-  // Find possible captures from a square: look in every direction!
-  findCaptures(sq) {
-    let moves = [];
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN));
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.ROOK));
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KNIGHT));
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.BISHOP));
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.QUEEN));
-    Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KING));
-    return moves;
-  }
-
   // Trim all non-capturing moves
   static KeepCaptures(moves) {
     return moves.filter(m => m.vanish.length == 2);
   }
 
-  getPotentialMovesFrom(sq) {
-    const moves = super.getPotentialMovesFrom(sq);
-    const zenCaptures = this.findCaptures(sq);
-    // Remove duplicate captures in a lazy way (TODO: more efficient...)
-    let hashMoves = {};
-    moves.forEach(m => {
-      if (m.vanish.length == 2) {
-        const hash =
-          m.start.x + "." + m.start.y + "." + m.end.x + "." + m.end.y;
-        hashMoves[hash] = true;
-      }
-    }
-    return moves.concat(zenCaptures.filter(m => {
-      const hash = m.start.x + "." + m.start.y + "." + m.end.x + "." + m.end.y;
-      return !hashMoves[hash];
-    }));
+  getPotentialKnightMoves(sq) {
+    // Knight becomes knightrider:
+    return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT]);
+  }
+
+  getPotentialKingMoves(sq) {
+    // King become queen:
+    return (
+      this.getSlideNJumpMoves(sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP]))
+    );
   }
 
   getAllPotentialMoves() {
@@ -211,12 +157,18 @@ export class MonochromeRules extends ChessRules {
 
   static GenRandInitFen(randomness) {
     // Remove the en-passant + castle part of the FEN
-    const fen = ChessRules.GenRandInitFen(randomness).slice(0, -6);
+    let fen = ChessRules.GenRandInitFen(randomness).slice(0, -6);
+    // Replace kings with queens
+    fen = fen.replace("k", "q").replace("K", "Q");
+    // Move pawns up:
+    fen = fen.replace("pppppppp/8","8/pppppppp")
+             .replace("8/PPPPPPPP","PPPPPPPP/8");
     const firstSpace = fen.indexOf(' ');
-    return (
+    // Paint it black:
+    fen =
       fen.substr(0, firstSpace).replace(/[A-Z]/g, (c) => c.toLowerCase()) +
-      fen.substr(firstSpace)
-    );
+      fen.substr(firstSpace);
+    return fen;
   }
 
   static get SEARCH_DEPTH() {