Search code examples
pythonchess

How to convert FEN ID onto a Chess Board?


So I was wondering on how I can convert a FEN ID onto a chess board.

Code:

import io

def board_to_fen(board):
    # Use StringIO to build string more efficiently than concatenating
    with io.StringIO() as s:
        for row in board:
            empty = 0
            for cell in row:
                c = cell[0]
                if c in ('w', 'b'):
                    if empty > 0:
                        s.write(str(empty))
                        empty = 0
                    s.write(cell[1].upper() if c == 'w' else cell[1].lower())
                else:
                    empty += 1
            if empty > 0:
                s.write(str(empty))
            s.write('/')
        # Move one position back to overwrite last '/'
        s.seek(s.tell() - 1)
        # If you do not have the additional information choose what to put
        s.write(' w KQkq - 0 1')
        return s.getvalue()


board = [
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
        ['--', '--', '--', '--', '--', '--', '--', '--'],
    ]
print(board_to_fen(board))

board_to_fen(board)

So we have the board above where '--' represents a square on the board, a blank square. So you can use notation like this: 'w' is for white and 'b' is for black. For the pieces:

R: Rook N: Knight B: Bishop Q: Queen K: King p: pawn

As you imagine you place something like: 'wK' in a '--' spot for white King and the rest of the pieces.

As you can tell I've already made the FEN ID for the chess board, but I want to know how I can use a FEN string outside to generate a board on the blank one above.

I appreciate any help I receive. If you need anymore information let me know.


Solution

  • It's basically the opposite of what you did.

    fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
    
    def fen_to_board(fen):
        board = []
        for row in fen.split('/'):
            brow = []
            for c in row:
                if c == ' ':
                    break
                elif c in '12345678':
                    brow.extend( ['--'] * int(c) )
                elif c == 'p':
                    brow.append( 'bp' )
                elif c == 'P':
                    brow.append( 'wp' )
                elif c > 'Z':
                    brow.append( 'b'+c.upper() )
                else:
                    brow.append( 'w'+c )
    
            board.append( brow )
        return board
    
    from pprint import pprint
    pprint( fen_to_board(fen) )
    
    [timr@Tims-Pro:~/src]$ python fen.py
    [['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
     ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
     ['--', '--', '--', '--', '--', '--', '--', '--'],
     ['--', '--', '--', '--', '--', '--', '--', '--'],
     ['--', '--', '--', '--', '--', '--', '--', '--'],
     ['--', '--', '--', '--', '--', '--', '--', '--'],
     ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
     ['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']]
    [timr@Tims-Pro:~/src]$