There's quite a lot of work that's been done in the world of chess. For example, there is a standardized file format, .pgn
, that describes a chess game, including the list of moves. Additionally, there's another file format .fen
, that describes a board position. The challenge today is to convert a list of chess moves (in algebraic notation) to a board position.
Definitions
- Position
[a-h][1-8]
: Given by appending the row (file) and the column (rank). Defines one of the 64 possible positions on the board. - Piece
[KQRBNP]?
: Represents the King, Queen, Rook, Bishop, KNight, Pawn. In algebraic notation,P
is not used. When used on a board, letters are uppercase if they are White, otherwise lowercase. - Move
[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](=[KQRBN])?(+*?)?|O-O(-O)
: A piece followed by the position it is moving to.- If the piece is ambiguous, then the row, or the column, or both are given.
- If the piece is capturing a piece, then
x
is placed between the piece and the position. - If the move is a castling, then
O-O
is given for king-side, otherwiseO-O-O
. - If a pawn is being promoted, the move is appended with
=
followed by the piece it is being promoted to. - If a move puts the king in check, it is appended with a
+
. - If a move puts the king in checkmate, its appended with
#
. - The color of the piece is determined by the turn number (White and black alternate turns, starting with black.)
- Board
(([1-8]|[KQRBNPkqrbnp])*\/){8}
: Rows are given by listing the pieces in row order. If there are empty squares, each run of empty square is given using the length of the run. Rows are separated using/
A board's initial position is rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
, which represents:
rnbqkbnr
pppppppp
PPPPPPPP
RNBQKBNR
You need to take a list of Moves and return a Board. You can assume that your input is valid.
Examples
-> rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
e4 -> rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR
e4,c5 -> rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
e4,d5,exd5,e5 -> rnbqkbnr/ppp2ppp/8/3Pp3/8/8/PPPP1PPP/RNBQKBNR
e4,d5,exd5,e5,dxe6 -> rnbqkbnr/ppp2ppp/4P3/8/8/8/PPPP1PPP/RNBQKBNR
e4,d5,exd5,e5,dxe6,Bc5,Nf3,Nf6,d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