X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=63b535dab609c5a8ff879b0b2e4066eaee7088af;hb=15c1295af871a5f416b0e5b43127512c8095497a;hp=5020094ce6582c326473acbc74f1e1b9b1901348;hpb=a3c86ec9b60326a8ae3d8f237493fb09627aed95;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 5020094c..63b535da 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -727,6 +727,12 @@ class ChessRules this.kingPos[c] = [move.start.x, move.start.y]; } + // Hash of position+flags+turn after a move is played (to detect repetitions) + getHashState() + { + return hex_md5(this.getFen() + " " + this.turn); + } + play(move, ingame) { // DEBUG: @@ -741,6 +747,9 @@ class ChessRules this.moves.push(move); this.epSquares.push( this.getEpSquare(move) ); VariantRules.PlayOnBoard(this.board, move); + + if (!!ingame) + move.hash = this.getHashState(); } undo(move) @@ -760,22 +769,24 @@ class ChessRules ////////////// // END OF GAME - // Basic check for 3 repetitions (in the last moves only) - // TODO: extend to usual 3-repetition recognition (storing FEN with move?) + // Check for 3 repetitions (position + flags + turn) checkRepetition() { - if (this.moves.length >= 8) + if (!this.hashStates) + this.hashStates = {}; + const startIndex = + Object.values(this.hashStates).reduce((a,b) => { return a+b; }, 0) + // Update this.hashStates with last move (or all moves if continuation) + // NOTE: redundant storage, but faster and moderate size + for (let i=startIndex; i { return (elt >= 3); }); } // Is game over ? And if yes, what is the score ?