Prepare some more variants (unfinished)
[vchess.git] / public / javascripts / variants / Magnetic.js
index e126f1c..8480d79 100644 (file)
@@ -1,9 +1,6 @@
 class MagneticRules extends ChessRules
 {
-       getEpSquare(move)
-       {
-               return undefined; //no en-passant
-       }
+       static get HasEnpassant() { return false; }
 
        getPotentialMovesFrom([x,y])
        {
@@ -23,7 +20,6 @@ class MagneticRules extends ChessRules
        // TODO: job is done multiple times for (normal) promotions.
        applyMagneticLaws(move)
        {
-               const V = VariantRules;
                if (move.appear[0].p == V.KING && move.appear.length==1)
                        return [move]; //kings are not charged
                const aIdx = (move.appear[0].p != V.KING ? 0 : 1); //if castling, rook is charged
@@ -32,11 +28,10 @@ class MagneticRules extends ChessRules
                const lastRank = (color=="w" ? 0 : 7);
                const standardMove = JSON.parse(JSON.stringify(move));
                this.play(standardMove);
-               const [sizeX,sizeY] = V.size;
                for (let step of [[-1,0],[1,0],[0,-1],[0,1]])
                {
                        let [i,j] = [x+step[0],y+step[1]];
-                       while (i>=0 && i<sizeX && j>=0 && j<sizeY)
+                       while (V.OnBoard(i,j))
                        {
                                if (this.board[i][j] != V.EMPTY)
                                {
@@ -71,7 +66,7 @@ class MagneticRules extends ChessRules
                                                {
                                                        // Push it until we meet an obstacle or edge of the board
                                                        let [ii,jj] = [i+step[0],j+step[1]];
-                                                       while (ii>=0 && ii<sizeX && jj>=0 && jj<sizeY)
+                                                       while (V.OnBoard(ii,jj))
                                                        {
                                                                if (this.board[ii][jj] != V.EMPTY)
                                                                        break;
@@ -112,7 +107,8 @@ class MagneticRules extends ChessRules
                // Scan move for pawn (max 1) on 8th rank
                for (let i=1; i<move.appear.length; i++)
                {
-                       if (move.appear[i].p==V.PAWN && move.appear[i].c==color && move.appear[i].x==lastRank)
+                       if (move.appear[i].p==V.PAWN && move.appear[i].c==color
+                               && move.appear[i].x==lastRank)
                        {
                                move.appear[i].p = V.ROOK;
                                moves.push(move);
@@ -136,10 +132,11 @@ class MagneticRules extends ChessRules
                return moves;
        }
 
-       // TODO: verify this assertion
        atLeastOneMove()
        {
-               return true; //always at least one possible move
+               if (this.kingPos[this.turn][0] < 0)
+                       return false;
+               return true; //TODO: is it right?
        }
 
        underCheck(move)
@@ -154,8 +151,8 @@ class MagneticRules extends ChessRules
                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)) ]
-                       : [ ];
+                       ? [JSON.parse(JSON.stringify(saveKingPos))]
+                       : [];
                this.undo(move);
                return res;
        }
@@ -164,15 +161,25 @@ class MagneticRules extends ChessRules
        {
                super.updateVariables(move);
                const c = this.getColor(move.start.x,move.start.y);
-               if (c != this.getColor(move.end.x,move.end.y)
-                       && this.board[move.end.x][move.end.y] != VariantRules.EMPTY
-                       && this.getPiece(move.end.x,move.end.y) == VariantRules.KING)
+               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)
                {
                        // We took opponent king !
                        const oppCol = this.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);
+               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;
+                       else if (psq.x == oppFirstRank && this.INIT_COL_ROOK[oppCol].includes(psq.y))
+                               this.castleFlags[oppCol][psq.y==this.INIT_COL_ROOK[oppCol][0] ? 0 : 1] = false;
+               });
        }
 
        unupdateVariables(move)
@@ -194,41 +201,16 @@ class MagneticRules extends ChessRules
                }
        }
 
-       checkGameOver()
-       {
-               if (this.checkRepetition())
-                       return "1/2";
-
-               const color = this.turn;
-               // TODO: do we need "atLeastOneMove()"?
-               if (this.atLeastOneMove() && this.kingPos[color][0] >= 0)
-                       return "*";
-
-               return this.checkGameEnd();
-       }
-
        checkGameEnd()
        {
                // No valid move: our king disappeared
                return this.turn == "w" ? "0-1" : "1-0";
        }
 
-       static get THRESHOLD_MATE() {
-               return 500; //checkmates evals may be slightly below 1000
-       }
-
-       getComputerMove()
+       static get THRESHOLD_MATE()
        {
-               let moves1 = this.getAllValidMoves();
-               // Can I mate in 1 ?
-               for (let i of _.shuffle(_.range(moves1.length)))
-               {
-                       this.play(moves1[i]);
-                       const finish = (Math.abs(this.evalPosition()) >= VariantRules.THRESHOLD_MATE);
-                       this.undo(moves1[i]);
-                       if (finish)
-                               return moves1[i];
-               }
-               return super.getComputerMove(moves1);
+               return 500; //checkmates evals may be slightly below 1000
        }
 }
+
+const VariantRules = MagneticRules;