From a672cdf742583ecb42116f3b5e12c18dd3b37cf5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 27 Dec 2022 18:15:54 +0100 Subject: [PATCH] Faster and smaller update_imbalance(). --- www/js/remoteglot.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 27ded16..be32c44 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -1827,23 +1827,19 @@ let update_historic_analysis = function() { * @param {string} fen */ let update_imbalance = function(fen) { - let hiddenboard = new Chess(fen); let imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0}; - for (let row = 0; row < 8; ++row) { - for (let col = 0; col < 8; ++col) { - let col_text = String.fromCharCode('a1'.charCodeAt(0) + col); - let row_text = String.fromCharCode('a1'.charCodeAt(1) + row); - let square = col_text + row_text; - let contents = hiddenboard.get(square); - if (contents !== null) { - if (contents.color === 'w') { - ++imbalance[contents.type]; - } else { - --imbalance[contents.type]; - } - } + for (const c of fen) { + if (c === ' ') { + // End of board + break; + } + if (c != c.toUpperCase()) { + --imbalance[c]; + } else if (c != c.toLowerCase()) { + ++imbalance[c.toLowerCase()]; } } + let white_imbalance = document.getElementById('whiteimbalance'); let black_imbalance = document.getElementById('blackimbalance'); white_imbalance.textContent = ''; -- 2.39.2