X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FMagnetic.js;h=17efeaa923918e94506cc4768b468e8a8a8381a9;hb=643479f8d7c3622b57fc49c4f10d9950793ebf4f;hp=e126f1cee175f9aafe0691cb70a34802a8a175d6;hpb=9e42b4dd8a70b9593cc44ab181b4df32032ca250;p=vchess.git diff --git a/public/javascripts/variants/Magnetic.js b/public/javascripts/variants/Magnetic.js index e126f1ce..17efeaa9 100644 --- a/public/javascripts/variants/Magnetic.js +++ b/public/javascripts/variants/Magnetic.js @@ -1,8 +1,13 @@ class MagneticRules extends ChessRules { - getEpSquare(move) + static get HasEnpassant { return false; } + + setOtherVariables(fen) { - return undefined; //no en-passant + // No en-passant: + const parsedFen = V.ParseFen(fen); + this.setFlags(fenParsed.flags); + this.scanKingsRooks(fen); } getPotentialMovesFrom([x,y]) @@ -19,11 +24,71 @@ 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 { + // 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=0 && i=0 && j=0 && ii=0 && jj { + 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 +270,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;