X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FDoublemove1.js;h=95d94d7a8021386c2a27e0c2633819d44700535b;hp=f51dd7ac411c77d21c92841f2d31c37b869703e0;hb=c3ff3a0c807d97c0311a06491318fe02440266db;hpb=964eda04ad6415b4ec95387ea08b63a3d0f0f9cc diff --git a/client/src/variants/Doublemove1.js b/client/src/variants/Doublemove1.js index f51dd7ac..95d94d7a 100644 --- a/client/src/variants/Doublemove1.js +++ b/client/src/variants/Doublemove1.js @@ -1,5 +1,4 @@ import { ChessRules } from "@/base_rules"; -import { randInt } from "@/utils/alea"; export class Doublemove1Rules extends ChessRules { static IsGoodEnpassant(enpassant) { @@ -77,27 +76,61 @@ export class Doublemove1Rules extends ChessRules { return moves; } + atLeastOneMove() { + const color = this.turn; + for (let i = 0; i < V.size.x; i++) { + for (let j = 0; j < V.size.y; j++) { + if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { + const moves = this.getPotentialMovesFrom([i, j]); + if (moves.length > 0) { + for (let k = 0; k < moves.length; k++) { + const m = moves[k]; + // NOTE: not using play() here (=> infinite loop) + V.PlayOnBoard(this.board, m); + if (m.vanish[0].p == V.KING) + this.kingPos[color] = [m.appear[0].x, m.appear[0].y]; + const res = !this.underCheck(color); + V.UndoOnBoard(this.board, m); + if (m.vanish[0].p == V.KING) + this.kingPos[color] = [m.start.x, m.start.y]; + if (res) return true; + } + } + } + } + } + return false; + } + play(move) { move.flags = JSON.stringify(this.aggregateFlags()); move.turn = [this.turn, this.subTurn]; V.PlayOnBoard(this.board, move); const epSq = this.getEpSquare(move); + const oppCol = V.GetOppCol(this.turn); if (this.movesCount == 0) { // First move in game this.turn = "b"; this.epSquares.push([epSq]); this.movesCount = 1; } - // Does this move give check on subturn 1? If yes, skip subturn 2 - else if (this.subTurn == 1 && this.underCheck(V.GetOppCol(this.turn))) { - this.turn = V.GetOppCol(this.turn); + // Does this move give check on subturn 1 or reach stalemate? + // If yes, skip subturn 2 + else if ( + this.subTurn == 1 && + ( + this.underCheck(V.GetOppCol(this.turn)) || + !this.atLeastOneMove() + ) + ) { + this.turn = oppCol; this.epSquares.push([epSq]); move.checkOnSubturn1 = true; this.movesCount++; } else { if (this.subTurn == 2) { - this.turn = V.GetOppCol(this.turn); + this.turn = oppCol; let lastEpsq = this.epSquares[this.epSquares.length - 1]; lastEpsq.push(epSq); } @@ -115,9 +148,8 @@ export class Doublemove1Rules 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) { - this.kingPos[c][0] = move.appear[0].x; - this.kingPos[c][1] = move.appear[0].y; + 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; } @@ -130,7 +162,7 @@ export class Doublemove1Rules 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) ) { @@ -206,43 +238,39 @@ export class Doublemove1Rules extends ChessRules { }; const moves11 = this.getAllValidMoves(); - 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]); if (this.turn != color) { // We gave check with last move: search the best opponent move - doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() }); + const evalM = getBestMoveEval() + 0.05 - Math.random() / 10; + if ( + (color == 'w' && evalM > bestEval) || + (color == 'b' && evalM < bestEval) + ) { + doubleMove = moves11[i]; + bestEval = evalM; + } } else { let moves12 = this.getAllValidMoves(); for (let j = 0; j < moves12.length; j++) { this.play(moves12[j]); - doubleMoves.push({ - moves: [moves11[i], moves12[j]], - eval: getBestMoveEval() + 0.05 - Math.random() / 10 - }); + 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]); } - - // TODO: array + sort + candidates logic not required when adding small - // fluctuations to the eval function (could also be generalized). - 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); - } - const selected = doubleMoves[randInt(candidates.length)].moves; - if (selected.length == 1) return selected[0]; - return selected; + return doubleMove; } };