fen_to_board | R Documentation |
Forsyth-Edwards Notation (FEN) is a compact way to represent the complete state of a chess game as a single line of ASCII text. In addition to showing the position of the pieces on the board, it stores information about whose turn it is, castling rights, en passant availability, the 50 move rule, and the move number. A FEN string contains six fields separated by spaces.
fen_to_board(fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
fen |
A valid FEN string. Defaults to starting position if omitted. |
The function fen_to_board()
makes the information in a FEN string more
accessible for use in R. The board representation in converted into an 8x8
matrix and is stored in a named list along with the other fields. The names
are $board, $to_move, $castling_rights, $ep_target, $halfmove_clock, and
$fullmove_number. See
section 16.1 of the PGN specification
for detailed information about each field.
In FEN, the pieces are represented as letters, with lowercase for black and uppercase for white.
p, P = pawn n, N = knight b, B = bishop r, R = rook q, Q = queen k, K = king
Empty squares in FEN are represented by digits indicating a horizontal group of that many empty squares in a row, with rows separated by the / character. For example, the FEN string of the starting position is:
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
Valid FEN strings may be supplied to the fen_to_board
function using the
fen
argument. Currently, the function does not check the validity of the
FEN string, so it is up to the user to ensure that only valid FEN strings are
used. If no argument is supplied, the starting position is the default.
The FEN piece notation is preserved for matrix representation, but empty
squares are represented as empty strings rather than digits. Files a-h have
matrix row indices 1-8, and ranks 1-8 have matrix column indices 1-8.
Squares can be accessed by their coordinates as board[rank, file]
(i.e.,
square b7 is at board[7, 2]
). They may also be accessed by column names
(i.e., board[7, 'b']
).
A named list containing data from the FEN string, including an 8x8 matrix of the board.
When printing a matrix in R, row 1 appears at the top. This is the
opposite convention from algebraic notation when the chessboard is viewed
with white at the bottom, where row 1 is the bottom row. Therefore
print(fen_to_board()$board)
shows a board that has been flipped vertically,
as if viewed from below. Rotating the board does not fix the appearance,
since the columns will still appear to be in reverse order. To print the
board in the conventional manner, use the function print_board()
.
board_to_fen()
To convert a board matrix to a FEN string.
print_board()
Prints the board with rows in the correct order.
board <- fen_to_board()$board
# Show the piece on e1
board[1, 5]
# Show the entire board
print_board(board)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.