Fix MarseillRules
[vchess.git] / client / src / variants / Dark.js
index 76e9462..bc14cfa 100644 (file)
@@ -1,4 +1,8 @@
-class DarkRules extends ChessRules
+import { ChessRules } from "@/base_rules";
+import { ArrayFun} from "@/utils/array";
+import { randInt } from "@/utils/alea";
+
+export const VariantRules = class DarkRules extends ChessRules
 {
        // Standard rules, in the shadow
        setOtherVariables(fen)
@@ -6,8 +10,8 @@ class DarkRules extends ChessRules
                super.setOtherVariables(fen);
                const [sizeX,sizeY] = [V.size.x,V.size.y];
                this.enlightened = {
-                       "w": doubleArray(sizeX,sizeY),
-                       "b": doubleArray(sizeX,sizeY)
+                       "w": ArrayFun.init(sizeX,sizeY),
+                       "b": ArrayFun.init(sizeX,sizeY)
                };
                // Setup enlightened: squares reachable by each side
                // (TODO: one side would be enough ?)
@@ -100,11 +104,11 @@ class DarkRules extends ChessRules
                super.updateVariables(move);
                if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
                {
-                       // We took opponent king !
+                       // We took opponent king ! (because if castle vanish[1] is a rook)
                        this.kingPos[this.turn] = [-1,-1];
                }
 
-               // Update moves for both colors:
+               // Update lights for both colors:
                this.updateEnlightened();
        }
 
@@ -126,14 +130,19 @@ class DarkRules extends ChessRules
                        }
                }
 
-               // Update moves for both colors:
+               // Update lights for both colors:
                this.updateEnlightened();
        }
 
-       checkGameEnd()
-       {
-               // No valid move: our king disappeared
-               return this.turn == "w" ? "0-1" : "1-0";
+  getCurrentScore()
+  {
+               const color = this.turn;
+               const kp = this.kingPos[color];
+               if (kp[0] < 0) //king disappeared
+                       return (color == "w" ? "0-1" : "1-0");
+    if (this.atLeastOneMove()) // game not over
+      return "*";
+    return "1/2"; //no moves but kings still there (seems impossible)
        }
 
        static get THRESHOLD_MATE()
@@ -148,7 +157,6 @@ class DarkRules extends ChessRules
                const color = this.turn;
                const oppCol = V.GetOppCol(color);
                const pawnShift = (color == "w" ? -1 : 1);
-               const kp = this.kingPos[color];
 
                // Do not cheat: the current enlightment is all we can see
                const myLight = JSON.parse(JSON.stringify(this.enlightened[color]));
@@ -156,6 +164,7 @@ class DarkRules extends ChessRules
                // Can a slider on (i,j) apparently take my king?
                // NOTE: inaccurate because assume yes if some squares are shadowed
                const sliderTake = ([i,j], piece) => {
+                 const kp = this.kingPos[color];
                        let step = undefined;
                        if (piece == V.BISHOP)
                        {
@@ -180,10 +189,10 @@ class DarkRules extends ChessRules
                        // Check for obstacles
                        let obstacle = false;
                        for (
-                               let x=kp[0]+step[0], y=kp[1]+step[1];
-                               x != i && y != j;
-                               x += step[0], y+= step[1])
-                       {
+        let x=kp[0]+step[0], y=kp[1]+step[1];
+                         x != i && y != j;
+                               x += step[0], y += step[1])
+      {
                                if (myLight[x][y] && this.board[x][y] != V.EMPTY)
                                {
                                        obstacle = true;
@@ -197,6 +206,7 @@ class DarkRules extends ChessRules
 
                // Do I see something which can take my king ?
                const kingThreats = () => {
+                 const kp = this.kingPos[color];
                        for (let i=0; i<V.size.x; i++)
                        {
                                for (let j=0; j<V.size.y; j++)
@@ -250,7 +260,8 @@ class DarkRules extends ChessRules
                                move.eval = -maxeval;
                        }
                        this.undo(move);
-                       if (!!move.eval)
+
+      if (!!move.eval)
                                continue;
 
                        move.eval = 0; //a priori...
@@ -282,6 +293,6 @@ class DarkRules extends ChessRules
                let candidates = [0];
                for (let j=1; j<moves.length && moves[j].eval == moves[0].eval; j++)
                        candidates.push(j);
-               return moves[sample(candidates)];
+               return moves[candidates[randInt(candidates.length)]];
        }
 }