The Forsyth-Edwards notation (FEN) is a standard notation with which a certain game situation of a chess game can be described. FEN contains all information that is needed to continue a chess game from a certain game situation. The system was developed by David Forsyth — journalist with a Scottish paper — who already popularized it in the 19th century. Steven J. Edwards later expanded the FEN so that it could also be used with computers.
In the first part of FEN (we leave out a few parts of the game situation, for example who's turn it is) the placement of the pieces on the chess board is described from the white player's point of view. The rows are described from top to bottom, and the filling of the fields in each row is described from left to right. According to the Standard Algebraic notation1 (SAN) every chess piece is described with one letter: K = king, Q = queen, R = rook, B = bishop, N = knight, P = pawn. White pieces are indicated with uppercase letters (KQRBNP) and black pieces with a lowercase letter (kqrbnp). Empty fields are indicated with numbers 1 to 8 that indicated the number of consecutive empty fields. A slash (/) is used to separate the rows.
Write a function fen2grid which can be used to convert the positioning of a chess board in FEN to a string representation of the format of a $$8 \times 8$$ grid. In this last description, the rows are separated by newlines (\n) and every empty field is indicated with one certain ASCII character. The pieces are indicated with the same letters as with FEN. To this function, a string with the FEN description should be given as a first argument. As a second, optional, argument, one can also give the ASCII character that is used to indicate the empty fields with (standard, an asterisk (*) is used for this). The function must print the string with the grid representation as a result.
Write a function grid2fen that does the exact opposite of fen2grid, and thus converts a description in the format of a grid to a description in FEN. Give the function a string with grid description as a first argument. As a second, optional argument, a character indicating the way empty fields are indicated in the grid description. The asterisk (*) is again used as a standard value. The function must print the string with FEN description.
>>> print(fen2grid('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR'))
rnbqkbnr
pppppppp
********
********
********
********
PPPPPPPP
RNBQKBNR
>>> print(fen2grid('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR', '.'))
rnbqkbnr
pppppppp
........
........
....P...
........
PPPP.PPP
RNBQKBNR
>>> print(fen2grid('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR', '+'))
rnbqkbnr
pp+ppppp
++++++++
++p+++++
++++P+++
++++++++
PPPP+PPP
RNBQKBNR
>>> grid = '''rnbqkbnr
... pppppppp
... ********
... ********
... ********
... ********
... PPPPPPPP
... RNBQKBNR'''
>>> print(grid2fen(grid))
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
>>> print(grid2fen(fen2grid('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR')))
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR
>>> print(grid2fen(fen2grid('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR', '.'), '.'))
rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
>>> print(grid2fen(fen2grid('rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R', '+'), '+'))
rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R