Early draft of Wildebeest + Grand
[vchess.git] / public / javascripts / variants / Atomic.js
index bf860df..b86e782 100644 (file)
@@ -1,8 +1,8 @@
 class AtomicRules extends ChessRules
 {
-       getPotentialMovesFrom([x,y], c, lastMove)
+       getPotentialMovesFrom([x,y])
        {
-               let moves = super.getPotentialMovesFrom([x,y], c, lastMove);
+               let moves = super.getPotentialMovesFrom([x,y]);
 
                // Handle explosions
                moves.forEach(m => {
@@ -28,31 +28,32 @@ class AtomicRules extends ChessRules
                return moves;
        }
 
-       getPotentialKingMoves(x, y, c)
+       getPotentialKingMoves([x,y])
        {
+               const V = VariantRules;
                // King cannot capture:
                let moves = [];
-               let [sizeX,sizeY] = VariantRules.size;
-               const steps = VariantRules.steps[VariantRules.QUEEN];
+               let [sizeX,sizeY] = V.size;
+               const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
                for (let step of steps)
                {
                        var i = x + step[0];
                        var j = y + step[1];
                        if (i>=0 && i<sizeX && j>=0 && j<sizeY && this.board[i][j] == VariantRules.EMPTY)
-                               moves.push(this.getBasicMove(x, y, i, j));
+                               moves.push(this.getBasicMove([x,y], [i,j]));
                }
-               return moves.concat(this.getCastleMoves(x,y,c));
+               return moves.concat(this.getCastleMoves([x,y]));
        }
 
-       isAttacked(sq, color)
+       isAttacked(sq, colors)
        {
-               if (this.getPiece(sq[0],sq[1]) == VariantRules.KING && this.isAttackedByKing(sq, color))
+               if (this.getPiece(sq[0],sq[1]) == VariantRules.KING && this.isAttackedByKing(sq, colors))
                        return false; //king cannot take...
-               return (this.isAttackedByPawn(sq, color)
-                       || this.isAttackedByRook(sq, color)
-                       || this.isAttackedByKnight(sq, color)
-                       || this.isAttackedByBishop(sq, color)
-                       || this.isAttackedByQueen(sq, color));
+               return (this.isAttackedByPawn(sq, colors)
+                       || this.isAttackedByRook(sq, colors)
+                       || this.isAttackedByKnight(sq, colors)
+                       || this.isAttackedByBishop(sq, colors)
+                       || this.isAttackedByQueen(sq, colors));
        }
 
        updateVariables(move)
@@ -72,7 +73,7 @@ class AtomicRules extends ChessRules
                                && Math.abs(this.kingPos[c][1]-move.end.y) <= 1)
                        {
                                this.kingPos[c] = [-1,-1];
-                               this.flags[c] = [false,false];
+                               this.castleFlags[c] = [false,false];
                        }
 
                        // Did we explode opponent king ?
@@ -80,7 +81,7 @@ class AtomicRules extends ChessRules
                                && Math.abs(this.kingPos[oppCol][1]-move.end.y) <= 1)
                        {
                                this.kingPos[oppCol] = [-1,-1];
-                               this.flags[oppCol] = [false,false];
+                               this.castleFlags[oppCol] = [false,false];
                        }
                        else
                        {
@@ -88,18 +89,17 @@ class AtomicRules extends ChessRules
                                if (Math.abs(move.end.x-oppFirstRank) <= 1)
                                {
                                        if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][0]) <= 1)
-                                               this.flags[oppCol][0] = false;
+                                               this.castleFlags[oppCol][0] = false;
                                        if (Math.abs(move.end.y-this.INIT_COL_ROOK[oppCol][1]) <= 1)
-                                               this.flags[oppCol][1] = false;
+                                               this.castleFlags[oppCol][1] = false;
                                }
                        }
                }
        }
 
-       undo(move)
+       unupdateVariables(move)
        {
-               super.undo(move);
-
+               super.unupdateVariables(move);
                const c = this.getColor(move.start.x,move.start.y);
                const oppCol = this.getOppCol(c);
                if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
@@ -113,8 +113,9 @@ class AtomicRules extends ChessRules
                }
        }
 
-       underCheck(move, c)
+       underCheck(move)
        {
+               const c = this.turn;
                const oppCol = this.getOppCol(c);
                this.play(move);
                let res = undefined;
@@ -131,21 +132,24 @@ class AtomicRules extends ChessRules
                return res;
        }
 
-       getCheckSquares(move, c)
+       getCheckSquares(move)
        {
-               const saveKingPos = this.kingPos[c]; //king might explode
+               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 = [ this.kingPos[c] ]
+                       res = [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
                this.undo(move);
                return res;
        }
 
-       checkGameEnd(color)
+       checkGameEnd()
        {
+               const color = this.turn;
                const kp = this.kingPos[color];
                if (kp[0] < 0) //king disappeared
                        return color == "w" ? "0-1" : "1-0";