SPeed-up checkmate and king capture for Magnetic chess
[vchess.git] / public / javascripts / variants / Magnetic.js
index d1436c0..e126f1c 100644 (file)
@@ -212,4 +212,23 @@ class MagneticRules extends ChessRules
                // No valid move: our king disappeared
                return this.turn == "w" ? "0-1" : "1-0";
        }
+
+       static get THRESHOLD_MATE() {
+               return 500; //checkmates evals may be slightly below 1000
+       }
+
+       getComputerMove()
+       {
+               let moves1 = this.getAllValidMoves();
+               // Can I mate in 1 ?
+               for (let i of _.shuffle(_.range(moves1.length)))
+               {
+                       this.play(moves1[i]);
+                       const finish = (Math.abs(this.evalPosition()) >= VariantRules.THRESHOLD_MATE);
+                       this.undo(moves1[i]);
+                       if (finish)
+                               return moves1[i];
+               }
+               return super.getComputerMove(moves1);
+       }
 }