+ // Check if FEN describe a position
+ static IsGoodFen(fen)
+ {
+ const fenParts = fen.split(" ");
+ if (fenParts.length== 0 || fenParts.length > 3)
+ return false;
+ // 1) Check position
+ const position = fenParts[0];
+ const rows = position.split("/");
+ if (rows.length != V.size.x)
+ return false;
+ for (let row of rows)
+ {
+ let sumElts = 0;
+ for (let i=0; i<row.length; i++)
+ {
+ if (V.PIECES.includes(row[i].toLowerCase()))
+ sumElts++;
+ else
+ {
+ const num = parseInt(row[i]);
+ if (isNaN(num))
+ return false;
+ sumElts += num;
+ }
+ }
+ if (sumElts != V.size.y)
+ return false;
+ }
+ // 2) Check flags (if present)
+ if (fenParts.length >= 2)
+ {
+ if (!V.IsGoodFlags(fenParts[1]))
+ return false;
+ }
+ // 3) Check turn (if present)
+ if (fenParts.length == 3)
+ {
+ if (!["w","b"].includes(fenParts[2]))
+ return false;
+ }
+ return true;
+ }
+
+ // For FEN checking
+ static IsGoodFlags(flags)
+ {
+ return !!flags.match(/^[01]{4,4}$/);
+ }
+