rename getOppCol into static GetOppCol + start thinking about problems page
[vchess.git] / public / javascripts / variants / Magnetic.js
index 225489e..8adb1ba 100644 (file)
@@ -1,14 +1,6 @@
 class MagneticRules extends ChessRules
 {
-       static get HasEnpassant { return false; }
-
-       setOtherVariables(fen)
-       {
-               // No en-passant:
-               const parsedFen = V.ParseFen(fen);
-               this.setFlags(fenParsed.flags);
-               this.scanKingsRooks(fen);
-       }
+       static get HasEnpassant() { return false; }
 
        getPotentialMovesFrom([x,y])
        {
@@ -24,67 +16,6 @@ class MagneticRules extends ChessRules
                return moves;
        }
 
-       getPotentialPawnMoves([x,y])
-       {
-               const color = this.turn;
-               let moves = [];
-               const [sizeX,sizeY] = [V.size.x,V.size.y];
-               const shift = (color == "w" ? -1 : 1);
-               const firstRank = (color == 'w' ? sizeX-1 : 0);
-               const startRank = (color == "w" ? sizeX-2 : 1);
-               const lastRank = (color == "w" ? 0 : sizeX-1);
-
-               if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank)
-               {
-                       // Normal moves
-                       if (this.board[x+shift][y] == V.EMPTY)
-                       {
-                               moves.push(this.getBasicMove([x,y], [x+shift,y]));
-                               // Next condition because variants with pawns on 1st rank allow them to jump
-                               if ([startRank,firstRank].includes(x) && this.board[x+2*shift][y] == V.EMPTY)
-                               {
-                                       // Two squares jump
-                                       moves.push(this.getBasicMove([x,y], [x+2*shift,y]));
-                               }
-                       }
-                       // Captures
-                       if (y>0 && this.board[x+shift][y-1] != V.EMPTY
-                               && this.canTake([x,y], [x+shift,y-1]))
-                       {
-                               moves.push(this.getBasicMove([x,y], [x+shift,y-1]));
-                       }
-                       if (y<sizeY-1 && this.board[x+shift][y+1] != V.EMPTY
-                               && this.canTake([x,y], [x+shift,y+1]))
-                       {
-                               moves.push(this.getBasicMove([x,y], [x+shift,y+1]));
-                       }
-               }
-
-               if (x+shift == lastRank)
-               {
-                       // Promotion
-                       const pawnColor = this.getColor(x,y); //can be different for checkered
-                       let promotionPieces = [V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
-                       promotionPieces.forEach(p => {
-                               // Normal move
-                               if (this.board[x+shift][y] == V.EMPTY)
-                                       moves.push(this.getBasicMove([x,y], [x+shift,y], {c:pawnColor,p:p}));
-                               // Captures
-                               if (y>0 && this.board[x+shift][y-1] != V.EMPTY
-                                       && this.canTake([x,y], [x+shift,y-1]))
-                               {
-                                       moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:pawnColor,p:p}));
-                               }
-                               if (y<sizeY-1 && this.board[x+shift][y+1] != V.EMPTY
-                                       && this.canTake([x,y], [x+shift,y+1]))
-                               {
-                                       moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:pawnColor,p:p}));
-                               }
-                       });
-               }
-               return moves; //no en-passant
-       }
-
        // Complete a move with magnetic actions
        // TODO: job is done multiple times for (normal) promotions.
        applyMagneticLaws(move)
@@ -208,41 +139,31 @@ class MagneticRules extends ChessRules
                return true; //TODO: is it right?
        }
 
-       underCheck(move)
+       underCheck(color)
        {
                return false; //there is no check
        }
 
        getCheckSquares(move)
        {
-               const c = this.getOppCol(this.turn); //opponent
-               const saveKingPos = this.kingPos[c]; //king might be taken
-               this.play(move);
-               // The only way to be "under check" is to have lost the king (thus game over)
-               let res = this.kingPos[c][0] < 0
-                       ? [JSON.parse(JSON.stringify(saveKingPos))]
-                       : [];
-               this.undo(move);
-               return res;
+               return [];
        }
 
        updateVariables(move)
        {
                super.updateVariables(move);
-               const c = this.getColor(move.start.x,move.start.y);
-               if (this.board[move.end.x][move.end.y] != V.EMPTY
-                       && c != this.getColor(move.end.x,move.end.y)
-                       && this.getPiece(move.end.x,move.end.y) == V.KING)
+               const c = move.vanish[0].c;
+               if (move.vanish.length >= 2 && move.vanish[1].p == V.KING)
                {
                        // We took opponent king !
-                       const oppCol = this.getOppCol(c);
+                       const oppCol = V.GetOppCol(c);
                        this.kingPos[oppCol] = [-1,-1];
                        this.castleFlags[oppCol] = [false,false];
                }
                // Did we magnetically move our (init) rooks or opponents' ones ?
                const firstRank = (c == "w" ? 7 : 0);
                const oppFirstRank = 7 - firstRank;
-               const oppCol = this.getOppCol(c);
+               const oppCol = V.GetOppCol(c);
                move.vanish.forEach(psq => {
                        if (psq.x == firstRank && this.INIT_COL_ROOK[c].includes(psq.y))
                                this.castleFlags[c][psq.y==this.INIT_COL_ROOK[c][0] ? 0 : 1] = false;
@@ -254,8 +175,8 @@ class MagneticRules 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[oppCol][0] < 0)
                {
                        // Last move took opponent's king
@@ -281,3 +202,5 @@ class MagneticRules extends ChessRules
                return 500; //checkmates evals may be slightly below 1000
        }
 }
+
+const VariantRules = MagneticRules;