Simplify Game logic a bit + some advances on Chakart
[vchess.git] / client / src / variants / Teleport.js
index 2379692..f5422c7 100644 (file)
@@ -3,8 +3,27 @@ import { randInt } from "@/utils/alea";
 
 export class TeleportRules extends ChessRules {
   hoverHighlight(x, y) {
-    // TODO: only highlight if the move is legal
-    return (this.subTurn == 2 && this.board[x][y] == V.EMPTY);
+    if (this.subTurn == 1 || this.board[x][y] != V.EMPTY)
+      return false;
+    // Only highlight if the move is legal
+    const color = this.turn;
+    const tMove = new Move({
+      appear: [
+        new PiPo({
+          x: x,
+          y: y,
+          c: color,
+          // The dropped piece nature has no importance:
+          p: V.KNIGHT
+        })
+      ],
+      vanish: [],
+      start: { x: -1, y: -1 }
+    });
+    this.play(tMove);
+    const moveOk = !this.underCheck(color);
+    this.undo(tMove);
+    return moveOk;
   }
 
   setOtherVariables(fen) {
@@ -299,10 +318,13 @@ export class TeleportRules extends ChessRules {
         moves2.forEach(m2 => {
           this.play(m2);
           const score = this.getCurrentScore();
-          const mvEval =
-            ["1-0", "0-1"].includes(score)
-              ? (score == "1-0" ? 1 : -1) * maxeval
-              : (score == "1/2" ? 0 : initEval);
+          let mvEval = 0;
+          if (["1-0", "0-1"].includes(score))
+            mvEval = (score == "1-0" ? 1 : -1) * maxeval;
+          else if (score == "*")
+            // Add small fluctuations to avoid dropping pieces always on the
+            // first square available.
+            mvEval = initEval + 0.05 - Math.random() / 10;
           if (
             (color == 'w' && mvEval > m.eval) ||
             (color == 'b' && mvEval < m.eval)