-var update_imbalance = function(fen) {
- var hiddenboard = new Chess(fen);
- var imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0};
- for (var row = 0; row < 8; ++row) {
- for (var col = 0; col < 8; ++col) {
- var col_text = String.fromCharCode('a1'.charCodeAt(0) + col);
- var row_text = String.fromCharCode('a1'.charCodeAt(1) + row);
- var square = col_text + row_text;
- var contents = hiddenboard.get(square);
- if (contents !== null) {
- if (contents.color === 'w') {
- ++imbalance[contents.type];
- } else {
- --imbalance[contents.type];
- }
- }
+function update_imbalance(fen) {
+ let imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0};
+ 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 = '';
+ black_imbalance.textContent = '';
+ for (let piece in imbalance) {
+ for (let i = 0; i < imbalance[piece]; ++i) {
+ let i1 = document.createElement('img');
+ i1.src = svg_pieces['w' + piece.toUpperCase()];
+ i1.setAttribute('alt', piece.toUpperCase());
+ i1.classList.add('imbalance-piece');
+ white_imbalance.appendChild(i1);
+
+ let i2 = document.createElement('img');
+ i2.src = svg_pieces['b' + piece.toUpperCase()];
+ i2.setAttribute('alt', piece.toUpperCase());
+ i2.classList.add('imbalance-inverted-piece');
+ white_imbalance.appendChild(i2);
+ }
+ for (let i = 0; i < -imbalance[piece]; ++i) {
+ let i1 = document.createElement('img');
+ i1.src = svg_pieces['b' + piece.toUpperCase()];
+ i1.setAttribute('alt', piece.toUpperCase());
+ i1.classList.add('imbalance-piece');
+ black_imbalance.appendChild(i1);
+
+ let i2 = document.createElement('img');
+ i2.src = svg_pieces['w' + piece.toUpperCase()];
+ i2.setAttribute('alt', piece.toUpperCase());
+ i2.classList.add('imbalance-inverted-piece');
+ black_imbalance.appendChild(i2);