2

I've seen a post explaining how to do this but in python, but it was done in a python library, so I tried converting it to javascript without a library and this is how it turned out.

function board_to_fen(board)
{
    let result = "";
    for(let y = 0; y < board.length; y++)
    {
        let empty = 0;
        for(let x = 0; x < y.length; x++)
        {
            let c = y[x][0];
            if(c == 'w' || c == 'b') {
                if(empty > 0)
                {
                    result += empty.toString();
                    empty = 0;
                }
                if(c == 'w')
                {
                    result += y[x][1].toUpperCase();
                } else {
                    result += y[x][1].toLowerCase();
                }                
            } else {
                empty += 1;
            }
        }
        if(empty == 0)
        {
            result += empty.toString();
        }
        result += '/';        
    }
    result += ' w KQkq - 0 1';
    return result;
}

let board = [
    ['bk', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
    ['em', 'bn', 'em', 'wr', 'em', 'wp', 'em', 'em'],
    ['br', 'em', 'bp', 'em', 'em', 'bn', 'wn', 'em'],
    ['em', 'em', 'bp', 'bp', 'bp', 'em', 'wp', 'bp'],
    ['bp', 'bp', 'em', 'bp', 'wn', 'em', 'wp', 'em'],
    ['em', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
    ['em', 'em', 'em', 'wk', 'em', 'em', 'em', 'em'],
    ['em', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
];

console.log(board_to_fen(board));

The correct fen for that Chess Board Diagram is "k7/1n1R1P2/r1p2nN1/2ppp1Pp/pp1pN1P1/8/3K4/8 w KQkq - 0" but in the console it prints out "0/0/0/0/0/0/0/0/ w KQkq - 0 1".

1 Answer 1

1

Just required some minor fixes when referencing the board. Occurrences of y[x] essentially needed to be changed to board[y][x], as y is an index into the board array.

function board_to_fen(board)
{
    let result = "";
    for(let y = 0; y < board.length; y++)
    {
        let empty = 0;
        for(let x = 0; x < board[y].length; x++)
        {
            let c = board[y][x][0];  // Fixed
            if(c == 'w' || c == 'b') {
                if(empty > 0)
                {
                    result += empty.toString();
                    empty = 0;
                }
                if(c == 'w')
                {
                    result += board[y][x][1].toUpperCase();  // Fixed
                } else {
                    result += board[y][x][1].toLowerCase();  // Fixed
                }                
            } else {
                empty += 1;
            }
        }
        if(empty > 0)   // Fixed
        {
            result += empty.toString();
        }
        if(y < board.length - 1)  // Added to eliminate last '/'
        {
          result += '/';
        }
    }
    result += ' w KQkq - 0 1';
    return result;
}

let board = [
    ['bk', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
    ['em', 'bn', 'em', 'wr', 'em', 'wp', 'em', 'em'],
    ['br', 'em', 'bp', 'em', 'em', 'bn', 'wn', 'em'],
    ['em', 'em', 'bp', 'bp', 'bp', 'em', 'wp', 'bp'],
    ['bp', 'bp', 'em', 'bp', 'wn', 'em', 'wp', 'em'],
    ['em', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
    ['em', 'em', 'em', 'wk', 'em', 'em', 'em', 'em'],
    ['em', 'em', 'em', 'em', 'em', 'em', 'em', 'em'],
];

console.log(board_to_fen(board));
Sign up to request clarification or add additional context in comments.

1 Comment

For those landing here, see also stackoverflow.com/questions/68976802/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.