Read a position (FEN) (Chess)

Chess programs describe a position with FEN: ranks 8 to 1 separated by /, pieces as letters (white upper case KQRBNP, black lower case), digits for empty squares, then w or b for the side to move (more fields may follow). Print the board from rank 8 down: 8 r n b q k b n r … 1 R N B Q K B N R (. for empty), then a b c d e f g h and white to move or black to move. Print invalid fen if a rank does not add up to 8 squares, a character is unknown, there are not 8 ranks, the side is not w or b, or there is not exactly one king of each colour.

Input: One FEN line.

Output: The board and the side to move.

Difficulty: Medium. Write your answer in the browser editor, run the examples, then submit it against hidden tests. Python and JavaScript are free; other languages are part of licode Club.

Hint

  • Walk each rank left to right: a digit skips that many squares.

More Chess practice