Fix Koopa promotions with captures, and Balakhlava: pawns move forward
[vchess.git] / client / src / variants / Koopa.js
index 27c147a..57d19b9 100644 (file)
@@ -118,7 +118,17 @@ export class KoopaRules extends ChessRules {
   }
 
   getPotentialMovesFrom([x, y]) {
-    let moves = super.getPotentialMovesFrom([x, y]);
+    let moves = super.getPotentialMovesFrom([x, y]).filter(m => {
+      if (
+        m.vanish[0].p != V.PAWN ||
+        m.appear[0].p == V.PAWN ||
+        m.vanish.length == 1
+      ) {
+        return true;
+      }
+      // Pawn promotion, "capturing": remove duplicates
+      return m.appear[0].p == V.QUEEN;
+    });
     // Complete moves: stuns & kicks
     let promoteAfterStun = [];
     const color = this.turn;
@@ -219,14 +229,14 @@ export class KoopaRules extends ChessRules {
     // Forbid kicking own king out
     const color = this.turn;
     return moves.filter(m => {
-      const kingAppear = m.appear.some(a => a.c == color && a.p == V.KING);
-      return m.vanish.every(v => {
-        return (
-          v.c != color ||
-          !["k", "l"].includes(v.p) ||
-          (v.p == "k" && kingAppear)
-        );
-      });
+      const kingVanish =
+        m.vanish.some(v => v.c == color && ['k', 'l'].includes(v.p));
+      if (kingVanish) {
+        const kingAppear =
+          m.appear.some(a => a.c == color && ['k', 'l'].includes(a.p));
+        return kingAppear;
+      }
+      return true;
     });
   }