Some bugs fixes
[xogo.git] / variants / Benedict / class.js
index 12ef3c8..81fee87 100644 (file)
@@ -6,7 +6,6 @@ export default class BenedictRules extends ChessRules {
   static get Options() {
     return {
       select: C.Options.select,
-      check: [],
       styles: [
         "balance",
         "cylinder",
@@ -22,14 +21,6 @@ export default class BenedictRules extends ChessRules {
     return false;
   }
 
-  get pawnSpecs() {
-    return Object.assign(
-      {},
-      super.pawnSpecs,
-      { canCapture: false }
-    );
-  }
-
   canTake() {
     return false;
   }
@@ -40,59 +31,95 @@ export default class BenedictRules extends ChessRules {
     const [color, piece] = [this.getColor(x, y), this.getPiece(x, y)];
     const oppCol = C.GetOppCol(color);
     let squares = {};
-    const specs = this.pieces(color)[piece];
-    const steps = specs.attack || specs.steps;
-    outerLoop: for (let step of steps) {
-      let [i, j] = [x + step[0], this.computeY(y + step[1])];
-      let nbSteps = 1;
-      while (this.onBoard(i, j) && this.board[i][j] == "") {
-        if (specs.range <= nbSteps++) continue outerLoop;
-        i += step[0];
-        j = this.computeY(j + step[1]);
+    const specs = this.pieces(color, x, y)[piece];
+    const attacks = specs.attack || specs.moves;
+    for (let a of attacks) {
+      outerLoop: for (let step of a.steps) {
+        let [i, j] = [x + step[0], this.getY(y + step[1])];
+        let nbSteps = 1;
+        while (this.onBoard(i, j) && this.board[i][j] == "") {
+          if (a.range <= nbSteps++)
+            continue outerLoop;
+          i += step[0];
+          j = this.getY(j + step[1]);
+        }
+        if (
+          this.onBoard(i, j) && this.getColor(i, j) == oppCol &&
+          (!this.options["zen"] || this.getPieceType(i, j) == "k")
+        ) {
+          squares[C.CoordsToSquare({x: i, y: j})] = true;
+        }
       }
-      if (this.onBoard(i, j) && this.getColor(i, j) == oppCol)
-        squares[C.CoordsToSquare({x: i, y: j})] = true;
     }
     return Object.keys(squares);
   }
 
   postProcessPotentialMoves(moves) {
-    if (moves.length == 0) return moves;
-    const [x, y] = [moves[0].end.x, moves[0].end.y];
-    const color = this.getColor(moves[0].start.x, moves[0].start.y);
-    const oppCol = C.GetOppCol(color);
-    moves = super.postProcessPotentialMoves(moves);
     moves.forEach(m => {
-      this.playOnBoard(m);
-      let attacks;
+      super.playOnBoard(m);
+      let attacks = this.findAttacks([m.end.x, m.end.y])
       if (this.options["zen"]) {
         let endSquares = {};
-        super.getZenCaptures(x, y).forEach(c => {
+        super.findCapturesOn([m.end.x, m.end.y], {zen: true}).forEach(c => {
           endSquares[C.CoordsToSquare(c.end)] = true;
         });
-        attacks = Object.keys(endSquares);
+        Array.prototype.push.apply(attacks, Object.keys(endSquares));
       }
-      else attacks = this.findAttacks([m.end.x, m.end.y])
-      this.undoOnBoard(m);
+      super.undoOnBoard(m);
+      m.flips = [];
       attacks.map(C.SquareToCoords).forEach(a => {
-        const p = this.getPiece(a.x, a.y);
-        m.appear.push(new PiPo({x: a.x, y: a.y, c: color, p: p}));
-        m.vanish.push(new PiPo({x: a.x, y: a.y, c: oppCol, p: p}));
+        m.flips.push({x: a.x, y: a.y});
       });
     });
     return moves;
   }
 
-  // Moves cannot flip our king's color, so (almost) all are valid
+  playOnBoard(move) {
+    super.playOnBoard(move);
+    this.flipColorOf(move.flips);
+  }
+  undoOnBoard(move) {
+    super.undoOnBoard(move);
+    this.flipColorOf(move.flips);
+  }
+
+  flipColorOf(flips) {
+    for (let xy of flips) {
+      const newColor = C.GetOppCol(this.getColor(xy.x, xy.y));
+      this.board[xy.x][xy.y] = newColor + this.board[xy.x][xy.y][1];
+    }
+  }
+
+  postPlay(move) {
+    if (this.options["balance"] && [1, 3].includes(this.movesCount)) {
+      // If enemy king is flipped: game over
+      const oppCol = C.GetOppCol(move.vanish[0].c);
+      const oppKingPos = this.searchKingPos(oppCol);
+      if (oppKingPos[0] < 0) {
+        this.turn = oppCol;
+        this.movesCount++;
+        return;
+      }
+    }
+    super.postPlay(move);
+  }
+
+  // Moves cannot flip our king's color, so all are valid
   filterValid(moves) {
-    if (this.options["balance"] && [1, 3].includes(this.movesCount))
-      return moves.filter(m => m.vanish.every(v => v.p != C.KING));
     return moves;
   }
 
-  // Since it's used just for the king, and there are no captures:
-  underCheck(square, color) {
+  // A king under (regular) check flips color, and the game is over.
+  underCheck() {
     return false;
   }
 
+  playVisual(move, r) {
+    super.playVisual(move, r);
+    move.flips.forEach(f => {
+      this.g_pieces[f.x][f.y].classList.toggle("white");
+      this.g_pieces[f.x][f.y].classList.toggle("black");
+    });
+  }
+
 };