X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FDoublemove2.js;h=287f94919bd1cc73bef6aaaa6f36453f2631ea1b;hb=HEAD;hp=080d13b62f20ded75233c4ff6814c0e276b39d87;hpb=bc0b9205e41c5db0552e4ccf060b945342e36ed0;p=vchess.git diff --git a/client/src/variants/Doublemove2.js b/client/src/variants/Doublemove2.js index 080d13b6..287f9491 100644 --- a/client/src/variants/Doublemove2.js +++ b/client/src/variants/Doublemove2.js @@ -2,6 +2,7 @@ import { ChessRules } from "@/base_rules"; import { randInt } from "@/utils/alea"; export class Doublemove2Rules extends ChessRules { + static IsGoodEnpassant(enpassant) { const squares = enpassant.split(","); if (squares.length > 2) return false; @@ -79,7 +80,7 @@ export class Doublemove2Rules extends ChessRules { return moves; } - isAttacked(sq, color) { + isAttacked() { // Goal is king capture => no checks return false; } @@ -110,7 +111,13 @@ export class Doublemove2Rules extends ChessRules { else { this.epSquares.push([epSq]); this.movesCount++; - if (this.movesCount == 1) this.turn = "b"; + if ( + this.movesCount == 1 || + // King is captured at subTurn 1? + (move.vanish.length == 2 && move.vanish[1].p == V.KING) + ) { + this.turn = "b"; + } } if (this.movesCount > 1) this.subTurn = 3 - this.subTurn; this.postPlay(move); @@ -121,15 +128,17 @@ export class Doublemove2Rules extends ChessRules { const piece = move.vanish[0].p; const firstRank = c == "w" ? V.size.x - 1 : 0; - if (piece == V.KING && move.appear.length > 0) { + if (piece == V.KING) { this.kingPos[c] = [move.appear[0].x, move.appear[0].y]; this.castleFlags[c] = [V.size.y, V.size.y]; return; } const oppCol = V.GetOppCol(c); - if (move.vanish.length == 2 && move.vanish[1].p == V.KING) + if (move.vanish.length == 2 && move.vanish[1].p == V.KING) { // Opponent's king is captured, game over this.kingPos[oppCol] = [-1, -1]; + move.captureKing = true; //for undo + } const oppFirstRank = V.size.x - 1 - firstRank; if ( move.start.x == firstRank && //our rook moves? @@ -138,7 +147,7 @@ export class Doublemove2Rules extends ChessRules { const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1); this.castleFlags[c][flagIdx] = V.size.y; } - else if ( + if ( move.end.x == oppFirstRank && //we took opponent rook? this.castleFlags[oppCol].includes(move.end.y) ) { @@ -150,7 +159,7 @@ export class Doublemove2Rules extends ChessRules { undo(move) { this.disaggregateFlags(JSON.parse(move.flags)); V.UndoOnBoard(this.board, move); - if (this.subTurn == 2 || this.movesCount == 1) { + if (this.subTurn == 2 || this.movesCount == 1 || !!move.captureKing) { this.epSquares.pop(); this.movesCount--; if (this.movesCount == 0) this.turn = "w"; @@ -202,34 +211,29 @@ export class Doublemove2Rules extends ChessRules { if (this.movesCount == 0) // First white move at random: return moves11[randInt(moves11.length)]; - let doubleMoves = []; + let doubleMove = null; + let bestEval = Number.POSITIVE_INFINITY * (color == 'w' ? -1 : 1); // Rank moves using a min-max at depth 2 for (let i = 0; i < moves11.length; i++) { this.play(moves11[i]); const moves12 = this.getAllValidMoves(); for (let j = 0; j < moves12.length; j++) { this.play(moves12[j]); - doubleMoves.push({ - moves: [moves11[i], moves12[j]], - // Small fluctuations to uniformize play a little - eval: getBestMoveEval() + 0.05 - Math.random() / 10 - }); + // Small fluctuations to uniformize play a little + const evalM = getBestMoveEval() + 0.05 - Math.random() / 10 + if ( + (color == 'w' && evalM > bestEval) || + (color == 'b' && evalM < bestEval) + ) { + doubleMove = [moves11[i], moves12[j]]; + bestEval = evalM; + } this.undo(moves12[j]); } this.undo(moves11[i]); } - - doubleMoves.sort((a, b) => { - return (color == "w" ? 1 : -1) * (b.eval - a.eval); - }); - let candidates = [0]; //indices of candidates moves - for ( - let i = 1; - i < doubleMoves.length && doubleMoves[i].eval == doubleMoves[0].eval; - i++ - ) { - candidates.push(i); - } - return doubleMoves[randInt(candidates.length)].moves; + // TODO: not always the best move played (why ???) + return doubleMove; } + };