board_to_fen: Convert a chessboard state to a FEN string

View source: R/board_to_fen.R

board_to_fenR Documentation

Convert a chessboard state to a FEN string

Description

The purpose of board_to_fen() is simply to reverse the operation of fen_to_board(). As such, board_to_fen() accepts as input a list of game data (board) in the format produced by fen_to_board().

Usage

board_to_fen(board)

Arguments

board

A list containing an 8x8 matrix of the board, whose trun it is, castling rights, en passant target square, halfmove clock, and fullmove number.

Details

Theboard argument is a named list containing the same data as a FEN string, but in a format that is more accessible in R:

⁠$board⁠ An 8x8 character matrix representing the chessboard, with lowercase letters for the black pieces and uppercase for white. ⁠$to_move⁠: Whose turn it is. Either 'w' or 'b'. ⁠$castling_rights⁠: Which castling privileges remain. Can be any combination of the characters 'K', 'Q', 'k', 'q', in order from uppercase to lowercase and then king before queen to show queenside and kingside castling rights for white or black, or '-' if no castling rights remain. ⁠$ep_target⁠: If the previous move was a two-square pawn advance, ⁠$ep_target⁠ will give the name of the square where an enemy pawn capturing en passant would land, even if no enemy pawn can currently capture en passant. Otherwise, the value is '-'. ⁠$halfmove_clock⁠: An integer for keeping track of the number of half moves since the last pawn move or capture. Used to detect 50 move rule draws. ⁠$fullmove_number⁠: An integer showing the current move number. Increases by one after every black move.

See section 16.1 of the PGN specification for detailed information about the FEN format. Also see the documentation for fen_to_board() for more details about the board format.

Value

A FEN string representing the board.

See Also

  • fen_to_board() To convert a FEN string to a board matrix.

  • print_board() Prints the board with rows in the correct order.

Examples

board <- fen_to_board()
board_to_fen(board)

dryguy/rbitr documentation built on Oct. 15, 2024, 6:18 a.m.