My chess Program.js is not running smoothly;
I need to extract the FEN from the board list that looks like this
board1 = [
"R", "N", "B", "K", "Q", "B", "N", "Q",
"P", "P", "P", "P", "P", "P", "P", "P",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
"p", "p", "p", "p", "p", "p", "p", "p",
"r", "n", "b", "q", "k", "b", "n", "r"
]
I have looked at various examples, but they all use nested lists like this
board2 = [
[],
[]
]
and it doesn't work with my current system
The algorithm I created works only for the initial case shown in "board1" list
For a different scenario, it generates something like this (for board 3)
rbqkbr/pp2ppp2223p33P3222/PP2PPP/RBQKBR/
board3 = ['R', ' ', 'B', 'K', 'Q', 'B', ' ', 'R',
'P', 'P', 'P', ' ', ' ', 'P', 'P', 'P',
' ', ' ', 'N', ' ', ' ', 'N', ' ', ' ',
' ', ' ', ' ', 'P', 'P', ' ', ' ', ' ',
' ', ' ', ' ', 'p', 'p', ' ', ' ', ' ',
' ', ' ', 'n', ' ', ' ', 'n', ' ', ' ',
'p', 'p', 'p', ' ', ' ', 'p', 'p', 'p',
'r', ' ', 'b', 'k', 'q', 'b', ' ', 'r'
]
I am looking for an algorithm that can generate the correct FEN for any board setup, such as the one mentioned above
r1bqkb1r/ppp2ppp/2n2n2/3pp3/3PP3/2N2N2/PPP2PPP/R1BKQB1R/
This is the algorithm I currently have
function getFEN2(board) {
let result;
let counter = 0;
let save = [];
for (a in board) {
index = parseInt(a)
v = board[index];
if (v === " ") {
counter += 1;
if (counter > 1) {
save[save.length - 1] = counter.toString();
} else if (cnt < 1) {
save.push(counter.toString());
}
} else if (v !== " ") {
save.push(v);
counter = 0;
}
if ((index + 1) % 8 === 0) {
save.push("/");
counter = 0;
}
}
result = save.join("");
return result;
// Thank you for your help