Subject: SAN Kit: FEN (Forsyth-Edwards Notation)

Revised: 1993.09.24

The current version of the SAN Kit handles Standard Algebraic Notation for
indicating chess moves.  However, the kit also has a set of routines for
dealing with position notation.  Having a standard position notation is
particularly important for chess programmers as it allows them to share
position databases.  For example, there exist standard position notation
databases with many of the classical benchmark tests for chessplaying programs,
and by using a common position notation format many hours of tedious data entry
can be saved.

This version of the SAN Kit has a set of routines to both parse and generate
FEN (Forsyth-Edwards Notation).  FEN is based on a 19th century standard for
position recording (known as Forsyth) that has been slightly extended for use
with chess software.  FEN specifies the piece placement, the active color, the
castling availability, the en passant target square, the halfmove clock, and
the fullmove number.  These can all fit on a single text line in an easily read
format.

The length of a FEN specification varies somewhat according to the position.
In some cases, the specification could be eighty or more characters in length
and so may not fit conveniently on some displays.  However, these positions
aren't too common.

A FEN specification has six fields.  Each field is composed only of nonblank
printing ASCII characters.  Adjacent fields are separated by a single ASCII
space character.

The first field represents the placement of the pieces on the board.  The board
contents are specified starting with the eighth rank and ending with the first
rank.  For each rank, the squares are specified from file a to file h.  White
pieces are identified by uppercase SAN piece letters ("PNBRQK") and black
pieces are identified by lowercase SAN piece letters ("pnbrqk").  Empty squares
are represented by the digits one through eight; the digit used represents the
count of contiguous empty squares.  A solidus character "/" is used to separate
data of adjacent ranks.

The second field represents the active color.  A lower case "w" is used if
White is to move; a lower case "b" is used if Black is the active player.

The third field represents castling availability.  This indicates potential
future castling that may not be possible at the moment due to blocking pieces
or enemy attacks.  If there is no castling availability for either side, the
single character symbol "-" is used.  Otherwise, a combination of from one to
four characters are present.  If White has kingside castling availability, the
uppercase letter "K" appears.  If White has queenside castling avialability,
the uppercase letter "Q" appears.  If Black has kingside castling availability,
the lowercase letter "k" appears.  If Black has queenside castling
availability, then the lowercase letter "q" appears.  Those letters which
appear will be ordered first uppercase before lowercase and second kingside
before queenside.  There is no whitespace between the letters.  (An earlier
version of FEN used the symbol "nil" instead of "-" to indicate a condition of
no castling availability.)

The fourth field is the en passant target square.  If there is no en passant
target square then the single character symbol "-" appears.  If there is an en
passant target square then is is represented by a lowercase file character
immediately followed by by a rank digit.  Obviously, the rank digit will be "3"
following a white pawn double advance (Black is the active color) or else be
the digit "6" after a black pawn double advance (White being the active color).
(An earlier version of FEN used the symbol "nil" instead of "-" to indicate a
condition of no en passant target square.)

The fifth field is a nonnegative integer representing the halfmove clock.  This
number is the count of halfmoves (or ply) since the last pawn advance or
capturing move.  This value is used for the fifty move draw rule.

The sixth and last field is a positive integer that gives the fullmove number.
This will have the value "1" for the first move of a game for both White and
Black.  It increments by one immediately after each move by Black.

Note that the position described does not have to be legal.  It is the
responsibility of the scanning program to determine legality.  The SAN Kit
supplies the appropriate status routines to do this checking.

Examples:

Here's the FEN for the starting position:

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

And after the move 1. e4:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1

And then after 1. ... c5:

rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2

And then after 2. Nf3:

rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2

For two kings on their home squares and a white pawn on e2 (WTM) with thirty
eight full moves played with five halfmoves since the last pawn move or
capture:

4k3/8/8/8/8/8/4P3/4K3 w - - 5 39

More examples can be seen in the BK.ci and the WAC.ci benchmark files that
are located in the SAN Kit documentation directory.

The SAN Kit command processor has two commands that use FEN: "dvfe" (display
value: Forsyth-Edwards) and "svde" (set value: Forsyth-Edwards).  The dvfe
command displays the FEN for the current position.  The svfe command takes a
FEN and sets the current position accordingly.

FEN: EOF
