Fix some bugs
[xogo.git] / variants / Chakart / class.js
index 815be74..5202b48 100644 (file)
@@ -312,7 +312,7 @@ export default class ChakartRules extends ChessRules {
         moves.push(this.getBasicMove([x, y], [x + shiftX, nextY]));
       }
     }
-    this.pawnPostProcess(moves, color, oppCol);
+    moves = super.pawnPostProcess(moves, color, oppCol);
     // Add mushroom on before-last square (+ potential segments)
     moves.forEach(m => {
       let [mx, my] = [x, y];
@@ -387,25 +387,6 @@ export default class ChakartRules extends ChessRules {
   play(move) {
     const color = this.turn;
     const oppCol = C.GetOppCol(color);
-    if (
-      move.appear.length > 0 &&
-      move.appear[0].p == 'p' &&
-      (
-        (color == 'w' && move.end.x == 0) ||
-        (color == 'b' && move.end.x == this.size.x - 1)
-      )
-    ) {
-      // "Forgotten" promotion, which occurred after some effect
-      let moves = [move];
-      super.pawnPostProcess(moves, color, oppCol);
-      super.showChoices(moves);
-      return false;
-    }
-    this.postPlay(move, color, oppCol);
-    return true;
-  }
-
-  postPlay(move, color, oppCol) {
     this.egg = move.egg;
     if (move.egg == "toadette") {
       this.reserve = { w: {}, b: {} };
@@ -480,7 +461,27 @@ export default class ChakartRules extends ChessRules {
       this.displayBonus(move);
   }
 
+  buildMoveStack(move, r) {
+    const color = this.turn;
+    if (
+      move.appear.length > 0 &&
+      move.appear[0].p == 'p' &&
+      (
+        (color == 'w' && move.end.x == 0) ||
+        (color == 'b' && move.end.x == this.size.x - 1)
+      )
+    ) {
+      // "Forgotten" promotion, which occurred after some effect
+      let moves = super.pawnPostProcess([move], color, C.GetOppCol(color));
+      super.showChoices(moves, r);
+    }
+    else
+      super.buildMoveStack(move, r);
+  }
+
   computeNextMove(move) {
+    if (move.koopa)
+      return null;
     // Set potential random effects, so that play() is deterministic
     // from opponent viewpoint:
     const endPiece = this.getPiece(move.end.x, move.end.y);
@@ -559,8 +560,17 @@ export default class ChakartRules extends ChessRules {
       let bagOfPieces = [];
       for (let i=0; i<this.size.x; i++) {
         for (let j=0; j<this.size.y; j++) {
-          if (this.getColor(i, j) == c && this.getPiece(i, j) != 'k')
+          const pieceIJ = this.getPiece(i, j);
+          if (
+            this.getColor(i, j) == c && pieceIJ != 'k' &&
+            (
+              // The color will change, so pawns on first rank are ineligible
+              pieceIJ != 'p' ||
+              (c == 'w' && i < this.size.x - 1) || (c == 'b' && i > 0)
+            )
+          ) {
             bagOfPieces.push([i, j]);
+          }
         }
       }
       if (bagOfPieces.length >= 1)
@@ -632,6 +642,7 @@ export default class ChakartRules extends ChessRules {
             p: this.getPiece(move.start.x, move.start.y)
           }));
         }
+        em.koopa = true; //avoid applying effect
         break;
       case "chomp":
         // Eat piece
@@ -690,12 +701,7 @@ export default class ChakartRules extends ChessRules {
   }
 
   displayBonus(move) {
-    let divBonus = document.createElement("div");
-    divBonus.classList.add("bonus-text");
-    divBonus.innerHTML = move.egg;
-    let container = document.getElementById(this.containerId);
-    container.appendChild(divBonus);
-    setTimeout(() => container.removeChild(divBonus), 2000);
+    super.displayMessage(null, move.egg, "bonus-text", 2000);
   }
 
   atLeastOneMove() {