X-Git-Url: https://git.auder.net/img/rock_paper_scissors_lizard_spock.gif?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=4b0b470521bac7b394d8234386bae0bd3100f31c;hb=baba60703f661aab20f2327098c3a0af572f0704;hp=e405cbaa4e595da56210f09a7e0920c27c406be1;hpb=b6487fb9c41705187cf97215fc9e8f86a59057c7;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index e405cbaa..4b0b4705 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -221,7 +221,7 @@ class ChessRules // On which squares is color under check ? (for interface) getCheckSquares(color) { - return this.isAttacked(this.kingPos[color], [this.getOppCol(color)]) + return this.isAttacked(this.kingPos[color], [V.GetOppCol(color)]) ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate! : []; } @@ -280,7 +280,7 @@ class ChessRules return pieces["b"].join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" + pieces["w"].join("").toUpperCase() + - " w 1111 -"; //add turn + flags + enpassant + " w 0 1111 -"; //add turn + flags + enpassant } // "Parse" FEN: just return untransformed string data @@ -498,11 +498,17 @@ class ChessRules } // Get opponent color - getOppCol(color) + static GetOppCol(color) { return (color=="w" ? "b" : "w"); } + // Get next color (for compatibility with 3 and 4 players games) + static GetNextCol(color) + { + return V.GetOppCol(color); + } + // Pieces codes (for a clearer code) static get PAWN() { return 'p'; } static get ROOK() { return 'r'; } @@ -728,7 +734,7 @@ class ChessRules return []; //x isn't first rank, or king has moved (shortcut) // Castling ? - const oppCol = this.getOppCol(c); + const oppCol = V.GetOppCol(c); let moves = []; let i = 0; const finalSquares = [ [2,3], [V.size.y-2,V.size.y-3] ]; //king, then rook @@ -818,7 +824,7 @@ class ChessRules getAllValidMoves() { const color = this.turn; - const oppCol = this.getOppCol(color); + const oppCol = V.GetOppCol(color); let potentialMoves = []; for (let i=0; i= V.THRESHOLD_MATE); - if (!finish && !this.atLeastOneMove()) + if (!finish) { - // Test mate (for other variants) - const score = this.checkGameEnd(); - if (score != "1/2") + const score = this.getCurrentScore(); + if (["1-0","0-1"].includes(score)) finish = true; } this.undo(moves1[i]); @@ -1160,8 +1151,9 @@ class ChessRules // Initial self evaluation is very low: "I'm checkmated" moves1[i].eval = (color=="w" ? -1 : 1) * maxeval; this.play(moves1[i]); + const score1 = this.getCurrentScore(); let eval2 = undefined; - if (this.atLeastOneMove()) + if (score1 == "*") { // Initial enemy evaluation is very low too, for him eval2 = (color=="w" ? 1 : -1) * maxeval; @@ -1170,15 +1162,10 @@ class ChessRules for (let j=0; j eval2)) { @@ -1188,10 +1175,7 @@ class ChessRules } } else - { - const score = this.checkGameEnd(); - eval2 = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval); - } + eval2 = (score1=="1/2" ? 0 : (score1=="1-0" ? 1 : -1) * maxeval); if ((color=="w" && eval2 > moves1[i].eval) || (color=="b" && eval2 < moves1[i].eval)) { @@ -1239,17 +1223,9 @@ class ChessRules { const maxeval = V.INFINITY; const color = this.turn; - if (!this.atLeastOneMove()) - { - switch (this.checkGameEnd()) - { - case "1/2": - return 0; - default: - const score = this.checkGameEnd(); - return (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval); - } - } + const score = this.getCurrentScore(); + if (score != "*") + return (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval); if (depth == 0) return this.evalPosition(); const moves = this.getAllValidMoves("computer");