X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fbase_rules.js;h=63b535dab609c5a8ff879b0b2e4066eaee7088af;hb=15c1295af871a5f416b0e5b43127512c8095497a;hp=a10a43ca270f405ff5dde22e0c7ba04170f577c6;hpb=331fc58c932d6d7055b202d0c6dc0d77212a89f8;p=vchess.git diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index a10a43ca..63b535da 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -54,7 +54,6 @@ class ChessRules constructor(fen, moves) { this.moves = moves; - this.hashStates = {}; //for repetitions detection // Use fen string to initialize variables, flags and board this.board = VariantRules.GetBoard(fen); this.setFlags(fen); @@ -728,16 +727,10 @@ class ChessRules this.kingPos[c] = [move.start.x, move.start.y]; } - // Store a hash of the position + flags + turn after a move is played - // (for repetitions detection) - addHashState() + // Hash of position+flags+turn after a move is played (to detect repetitions) + getHashState() { - const strToHash = this.getFen() + " " + this.turn; - const hash = hex_md5(strToHash); - if (!this.hashStates[hash]) - this.hashStates[hash] = 1; - else - this.hashStates[hash]++; + return hex_md5(this.getFen() + " " + this.turn); } play(move, ingame) @@ -756,7 +749,7 @@ class ChessRules VariantRules.PlayOnBoard(this.board, move); if (!!ingame) - this.addHashState(); + move.hash = this.getHashState(); } undo(move) @@ -779,6 +772,20 @@ class ChessRules // Check for 3 repetitions (position + flags + turn) checkRepetition() { + 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); }); }