Remove some redundant code [Checkered/Magnetic still buggish]
[vchess.git] / public / javascripts / variants / Atomic.js
index b7c7504..2b7b1ac 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,7 +28,7 @@ class AtomicRules extends ChessRules
                return moves;
        }
 
-       getPotentialKingMoves(x, y, c)
+       getPotentialKingMoves([x,y])
        {
                // King cannot capture:
                let moves = [];
@@ -39,20 +39,20 @@ class AtomicRules extends ChessRules
                        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 +72,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 +80,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 +88,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 +112,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,8 +131,24 @@ class AtomicRules extends ChessRules
                return res;
        }
 
-       checkGameEnd(color)
+       getCheckSquares(move)
+       {
+               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);
+               return res;
+       }
+
+       checkGameEnd()
        {
+               const color = this.turn;
                const kp = this.kingPos[color];
                if (kp[0] < 0) //king disappeared
                        return color == "w" ? "0-1" : "1-0";