From: Benjamin Auder Date: Fri, 27 Mar 2020 03:21:46 +0000 (+0100) Subject: Fix computer games for Apocalypse variant X-Git-Url: https://git.auder.net/assets/img/%24%7BgetWhatsApp%28link%29%7D?a=commitdiff_plain;h=9edfb7146fdc4dd08914b2a117d2852e705353aa;p=vchess.git Fix computer games for Apocalypse variant --- diff --git a/client/src/variants/Apocalypse.js b/client/src/variants/Apocalypse.js index 1eb482ec..ef10b3e1 100644 --- a/client/src/variants/Apocalypse.js +++ b/client/src/variants/Apocalypse.js @@ -69,12 +69,12 @@ export class ApocalypseRules extends ChessRules { // 4) Check whiteMove if ( ( - fenParsed.turn == "w" && + fenParsed.turn == "b" && // NOTE: do not check really JSON stringified move... (!fenParsed.whiteMove || fenParsed.whiteMove == "-") ) || - (fenParsed.turn == "b" && fenParsed.whiteMove != "-") + (fenParsed.turn == "w" && fenParsed.whiteMove != "-") ) { return false; } @@ -462,31 +462,36 @@ export class ApocalypseRules extends ChessRules { // TODO: this situation should not happen return null; - if (Math.random() < 0.5) - // Return a random move - return moves[randInt(moves.length)]; - // Rank moves at depth 1: + let validMoves = []; + let illegalMoves = []; moves.forEach(m => { - // Warning: m.vanish[0] might refer to an empty square! Or self - const skipPlayUndo = ( - m.vanish.length == 2 && - ( - m.vanish[1].c == m.vanish[0].c || - this.board[m.vanish[1].x][m.vanish[1].y] == V.EMPTY - ) - ); - if (!skipPlayUndo) V.PlayOnBoard(this.board, m); - m.eval = this.evalPosition(); - if (!skipPlayUndo) V.UndoOnBoard(this.board, m); + // Warning: m might be illegal! + if (!m.illegal) { + V.PlayOnBoard(this.board, m); + m.eval = this.evalPosition(); + V.UndoOnBoard(this.board, m); + validMoves.push(m); + } else illegalMoves.push(m); }); - moves.sort((a, b) => { + + const illegalRatio = illegalMoves.length / moves.length; + if (Math.random() < illegalRatio) + // Return a random illegal move + return illegalMoves[randInt(illegalMoves.length)]; + + validMoves.sort((a, b) => { return (color == "w" ? 1 : -1) * (b.eval - a.eval); }); let candidates = [0]; - for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++) + for ( + let i = 1; + i < validMoves.length && validMoves[i].eval == moves[0].eval; + i++ + ) { candidates.push(i); - return moves[candidates[randInt(candidates.length)]]; + } + return validMoves[candidates[randInt(candidates.length)]]; } getNotation(move) { diff --git a/client/src/variants/Synchrone.js b/client/src/variants/Synchrone.js index 55d49dae..eb82bb55 100644 --- a/client/src/variants/Synchrone.js +++ b/client/src/variants/Synchrone.js @@ -16,12 +16,12 @@ export class SynchroneRules extends ChessRules { // 5) Check whiteMove if ( ( - fenParsed.turn == "w" && + fenParsed.turn == "b" && // NOTE: do not check really JSON stringified move... (!fenParsed.whiteMove || fenParsed.whiteMove == "-") ) || - (fenParsed.turn == "b" && fenParsed.whiteMove != "-") + (fenParsed.turn == "w" && fenParsed.whiteMove != "-") ) { return false; }