X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMagnetic.js;h=8480d7994a39312b8d9ecbf8a0ae33013a396b1e;hb=375ecdd1387e729f85ed114e82253469e4849869;hp=2d576e1b5eaad331fe668293d9d72cf9cbdafaae;hpb=7f0711a8515a199efe66ab903a2e710d77b1ce76;p=vchess.git diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js index 2d576e1b..8480d799 100644 --- a/public/javascripts/variants/Magnetic.js +++ b/public/javascripts/variants/Magnetic.js @@ -1,30 +1,45 @@ class MagneticRules extends ChessRules { - getEpSquare(move) + static get HasEnpassant() { return false; } + + getPotentialMovesFrom([x,y]) { - return undefined; //no en-passant + let standardMoves = super.getPotentialMovesFrom([x,y]); + let moves = []; + standardMoves.forEach(m => { + let newMove_s = this.applyMagneticLaws(m); + if (newMove_s.length == 1) + moves.push(newMove_s[0]); + else //promotion + moves = moves.concat(newMove_s); + }); + return moves; } // Complete a move with magnetic actions - applyMagneticLaws([x,y], move) + // TODO: job is done multiple times for (normal) promotions. + applyMagneticLaws(move) { + 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 + const [x,y] = [move.appear[aIdx].x, move.appear[aIdx].y]; + const color = this.turn; + const lastRank = (color=="w" ? 0 : 7); const standardMove = JSON.parse(JSON.stringify(move)); this.play(standardMove); - const color = this.getColor(x,y); - const [sizeX,sizeY] = VariantRules.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=0 && j=2 || Math.abs(j-y)>=2) - && this.getPiece(i,j) != VariantRules.KING) + if ((Math.abs(i-x)>=2 || Math.abs(j-y)>=2) && this.getPiece(i,j) != V.KING) { move.vanish.push( new PiPo({ @@ -47,13 +62,13 @@ class MagneticRules extends ChessRules else { // Repulsion - if (this.getPiece(i,j) != VariantRules.KING) + if (this.getPiece(i,j) != V.KING) { // 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=0 && jj= 0 && x+shift < sizeX && x+shift != lastRank) + let moves = []; + // Scan move for pawn (max 1) on 8th rank + for (let i=1; i { + let tmp = m.appear[0]; + m.appear[0] = m.appear[i]; + m.appear[i] = tmp; + }); + break; } - // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y-1])); - if (y { - // Normal move - if (this.board[x+shift][y] == V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); - // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) - moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p})); - if (y { + 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) @@ -306,22 +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 + } } + +const VariantRules = MagneticRules;