]> git.sesse.net Git - remoteglot/commitdiff
Slightly less weird FEN generation.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Dec 2022 11:10:38 +0000 (12:10 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Dec 2022 11:10:38 +0000 (12:10 +0100)
www/js/chessboard-0.3.0.js

index dc0e427b7fae69679c032147280fcdff7876fa17..26c021704f5b45fc5b85b4f376f0ba981b67403a 100644 (file)
@@ -149,6 +149,7 @@ function objToFen(obj) {
   }
 
   var fen = '';
+  let num_empty = 0;
 
   var currentRow = 8;
   for (var i = 0; i < 8; i++) {
@@ -157,32 +158,30 @@ function objToFen(obj) {
 
       // piece exists
       if (obj.hasOwnProperty(square) === true) {
+        if (num_empty > 0) {
+          fen += num_empty;
+          num_empty = 0;
+        }
         fen += pieceCodeToFen(obj[square]);
       }
 
       // empty space
       else {
-        fen += '1';
+        ++num_empty;
       }
     }
 
     if (i !== 7) {
+      if (num_empty > 0) {
+        fen += num_empty;
+        num_empty = 0;
+      }
       fen += '/';
     }
 
     currentRow--;
   }
 
-  // squeeze the numbers together
-  // haha, I love this solution...
-  fen = fen.replace(/11111111/g, '8');
-  fen = fen.replace(/1111111/g, '7');
-  fen = fen.replace(/111111/g, '6');
-  fen = fen.replace(/11111/g, '5');
-  fen = fen.replace(/1111/g, '4');
-  fen = fen.replace(/111/g, '3');
-  fen = fen.replace(/11/g, '2');
-
   return fen;
 }