rename getOppCol into static GetOppCol + start thinking about problems page
[vchess.git] / public / javascripts / variants / Atomic.js
index e2f8ea3..255444f 100644 (file)
@@ -58,7 +58,7 @@ class AtomicRules extends ChessRules
        updateVariables(move)
        {
                super.updateVariables(move);
-               const color = this.getColor(move.start.x,move.start.y);
+               const color = move.vanish[0].c;
                if (move.appear.length == 0) //capture
                {
                        const firstRank = {"w": 7, "b": 0};
@@ -89,8 +89,8 @@ class AtomicRules extends ChessRules
        unupdateVariables(move)
        {
                super.unupdateVariables(move);
-               const c = this.getColor(move.start.x,move.start.y);
-               const oppCol = this.getOppCol(c);
+               const c = move.vanish[0].c;
+               const oppCol = V.GetOppCol(c);
                if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
                {
                        // There is a chance that last move blowed some king away..
@@ -102,37 +102,30 @@ class AtomicRules extends ChessRules
                }
        }
 
-       underCheck(move)
+       underCheck(color)
        {
-               const c = this.turn;
-               const oppCol = this.getOppCol(c);
-               this.play(move);
+               const oppCol = V.GetOppCol(color);
                let res = undefined;
                // If our king disappeared, move is not valid
-               if (this.kingPos[c][0] < 0)
+               if (this.kingPos[color][0] < 0)
                        res = true;
                // If opponent king disappeared, move is valid
                else if (this.kingPos[oppCol][0] < 0)
                        res = false;
                // Otherwise, if we remain under check, move is not valid
                else
-                       res = this.isAttacked(this.kingPos[c], [oppCol]);
-               this.undo(move);
+                       res = this.isAttacked(this.kingPos[color], [oppCol]);
                return res;
        }
 
-       getCheckSquares(move)
+       getCheckSquares(color)
        {
-               const c = this.getOppCol(this.turn);
-               // King might explode:
-               const saveKingPos = JSON.parse(JSON.stringify(this.kingPos[c]));
-               this.play(move);
                let res = [ ];
-               if (this.kingPos[c][0] < 0)
-                       res = [saveKingPos];
-               else if (this.isAttacked(this.kingPos[c], [this.getOppCol(c)]))
-                       res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
-               this.undo(move);
+               if (this.kingPos[color][0] >= 0 //king might have exploded
+                       && this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]))
+               {
+                       res = [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
+               }
                return res;
        }
 
@@ -142,8 +135,10 @@ class AtomicRules extends ChessRules
                const kp = this.kingPos[color];
                if (kp[0] < 0) //king disappeared
                        return color == "w" ? "0-1" : "1-0";
-               if (!this.isAttacked(kp, [this.getOppCol(color)]))
+               if (!this.isAttacked(kp, [V.GetOppCol(color)]))
                        return "1/2";
                return color == "w" ? "0-1" : "1-0"; //checkmate
        }
 }
+
+const VariantRules = AtomicRules;