Forsyth-Edwards Notation (FEN)
Chess software needs ways to describe both individual board positions and complete games. Two standards are used for this purpose:
The two formats serve different purposes: A FEN string is a snapshot of a position, while a PGN movetext represents a sequence of positions and moves, optionally including a complete game tree through variations.
FEN is useful for position editors, chess engines, puzzles, opening databases, and links that need to encode a position in a compact form. PGN is intended for exchanging complete games and associated information between chess programs, databases, websites, and humans.
Both formats are text based and designed to be easy to parse. FEN uses a fixed six-field structure, whereas PGN combines tag pairs containing metadata with movetext written using Standard Algebraic Notation (SAN).
Forsyth-Edwards Notation, usually abbreviated FEN, originated from Forsyth Notation, a system developed in the 19th century by Scottish journalist David Forsyth for recording chess positions. Forsyth's original notation focused on piece placement.
FEN extended this idea to encode enough state information to describe a position relevant to normal chess play. It became necessary for computer chess because a position can be transmitted or stored as a single line of ASCII text.
FEN is standardized as a six-field record. The fields are separated by single spaces:
<Piece placement> <Side to move> <Castling availability> <En passant target square> <Halfmove clock> <Fullmove number>
A FEN contains:
For example:
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1
This FEN represents:
The board portion is:
rnbqkbnr
pppppppp
8
8
4P3
8
PPPP1PPP
RNBQKBNR
Each rank is separated by /, starting with rank 8 and ending with rank 1.
The first FEN field describes the board from rank 8 to rank 1, with each rank separated by /.
Within each rank, squares are described from file a to file h.
Piece letters are:
| Symbol | Piece | Color |
|---|---|---|
| K | King | White |
| Q | Queen | White |
| R | Rook | White |
| B | Bishop | White |
| N | Knight | White |
| P | Pawn | White |
| k | King | Black |
| q | Queen | Black |
| r | Rook | Black |
| b | Bishop | Black |
| n | Knight | Black |
| p | Pawn | Black |
Uppercase letters represent White pieces; lowercase letters represent Black pieces. Empty consecutive squares are represented by a digit from 1 through 8. A rank must describe eight squares. Digits represent runs of empty squares rather than individual square names.
The second field contains one character:
w
for White to move, or:
b
for Black to move.
The third field records the castling rights that remain available. The possible symbols are:
K
White can castle kingside.
Q
White can castle queenside.
k
Black can castle kingside.
q
Black can castle queenside.
The symbols may occur in any combination in the conventional order:
KQkq
If no castling rights remain, the field is:
-
The fourth field specifies the en passant target square. If the immediately preceding move was a two-square pawn advance, the square passed over by the pawn is recorded.
For example, after:
1.e4
the en passant target square is:
e3
Thus a corresponding FEN can contain:
... b KQkq e3 0 1
If there is no en passant target square, the field is:
-
The target square identifies the square onto which an en passant capture would be made. It does not indicate that an en passant capture is necessarily legal or that an opposing pawn can actually capture.
The fifth field is the halfmove clock, used for the fifty-move rule. It counts the number of halfmoves since the last pawn move, or capture.
... - 0 1
indicates that no halfmove has elapsed since a pawn move or capture. After a non-pawn, non-capturing move, the counter increases by one. After a pawn move or capture, it resets to zero. Because a chess move consists of two halfmoves, the value counts individual player moves rather than complete move numbers.
The sixth field is the fullmove number.
It starts at:
1
and increments after Black makes a move.
For example, immediately before White's first move:
... w ... 0 1
After White plays 1.e4, the side to move is Black, while the fullmove number remains 1:
... b ... 0 1
After Black makes the first move, the fullmove number becomes 2.
Portable Game Notation, or PGN, was developed in the early 1990s by Steven Edwards and others in the computer-chess community to provide a standardized representation of chess games.
The goal was provide a complete interchange format for chess games. A PGN can contain game metadata, the main line of play, comments, recursive variations, annotations, termination information, and other commonly required data.
PGN is designed as a human-readable text format while remaining sufficiently structured for software to parse.
A PGN consists primarily of two logical sections:
Tag section
Movetext section
A complete PGN commonly looks like:
[Event "Casual game"]
[Site "London ENG"]
[Date "1912.10.29"]
[EventDate "?"]
[Round "?"]
[Result "1-0"]
[White "Edward Lasker"]
[Black "George Alan Thomas"]
[ECO "A40"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "35"]
1.d4 {Notes by Stockfish 8 v270317 (minimum 120s/ply)} e6
2.Nf3 f5 3.Nc3 Nf6 {3...d5 was played in S Vukovic vs
Matulovic, 1959 (0-1)} 4.Bg5 {4.e3 was played in Tinsley vs E
Schallopp, 1890 (1-0)} Be7 {4...Bb4 was played in D Hoelken vs
Tartakower, 1905 (0-1)} 5.Bxf6 Bxf6 6.e4 fxe4 7.Nxe4 b6 8.Ne5
{8.Bd3 was played in B Grachev vs S Cherednichenko, 2014
(1-0)} O-O 9.Bd3 {? 9.g3 Bb7 10.Qe2 Nc6 11.Nxc6 Bxc6 12.O-O-O
b5 13.Bg2 Be7 = +0.23 (33 ply)} Bb7 {? 9...Bxe5 10.dxe5 Nc6
11.g3 Nxe5 12.f4 Nxd3+ 13.Qxd3 Rf5 =/+ -0.84 (37 ply)} 10.Qh5
{= +0.34 (32 ply)} Qe7 {? 10...Bxe5 11.Nd2 g6 12.Qxe5 Nc6
13.Qg3 Nb4 14.Bxg6 hxg6 = 0.00 (40 ply)} 11.Qxh7+ $3 {+-
mate-in-7} Kxh7 12.Nxf6+ Kh6 13.Neg4+ Kg5 14.h4+ ; mate-in-4
Kf4 15.g3+ Kf3 16.Be2+ Kg2 17.Rh2+ Kg1 18.Kd2# 1-0
PGN metadata is represented using tag pairs:
[TagName "TagValue"]
For example:
[Event "Casual game"]
[Site "London ENG"]
[Date "1912.10.29"]
[White "Edward Lasker"]
[Black "George Alan Thomas"]
[Result "1-0"]
The tag name identifies the field, while the quoted value contains its value. The PGN specification defines seven required tags known as the Seven Tag Roster:
Event
Site
Date
Round
White
Black
Result
They should occur in this order when present.
The name or description of the competition or event:
[Event "Casual game"]
The location:
[Site "London ENG"]
The game date in:
YYYY.MM.DD
format. Unknown components may be represented with ?:
[Date "1912.??.??"]
The round identifier:
[Round "3"]
PGN also permits structured round identifiers for events where a simple integer is insufficient.
The White player's name:
[White "Edward Lasker"]
The Black player's name:
[Black "George Alan Thomas"]
The game result:
1-0
0-1
1/2-1/2
*
The asterisk means that the game result is unknown, ongoing, or otherwise not specified.
PGN implementations commonly use additional tags such as:
[EventDate "?"]
[WhiteElo "?"]
[BlackElo "?"]
[ECO "A40"]
[Annotator "Stockfish"]
[PlyCount "35"]
[TimeControl "600+5"]
[Opening "Queen's Pawn Game"]
[Variation "Chigorin Variation"]
[SetUp "1"]
[FEN "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"]
The exact set of additional tags depends on the application and PGN ecosystem.
PGN can describe a game beginning from a non-standard starting position.
This is done with:
[SetUp "1"]
[FEN "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"]
SetUp "1" indicates that the initial position is specified by the FEN tag.
The movetext then begins from that position rather than the standard initial position.
This makes FEN and PGN complementary: FEN specifies the initial state, while PGN describes the subsequent game tree.
The movetext contains the actual game score.
A simple sequence is:
1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 Nf6
The moves use Standard Algebraic Notation (SAN).
Typical SAN components include:
K King
Q Queen
R Rook
B Bishop
N Knight
x capture
+ check
# checkmate
O-O kingside castling
O-O-O queenside castling
=Q promotion
Pawn moves omit a piece letter:
e4
A pawn capture includes its originating file:
exd5
A piece capture contains x:
Nxf6
When two pieces of the same type can legally move to the same square, SAN includes enough information to identify the moving piece.
For example:
Nbd2
means the knight from the b-file moves to d2.
Similarly:
N1d2
identifies the knight on rank 1.
If necessary, both file and rank can be included.
Check is indicated with:
+
Example:
Qh5+
Checkmate is conventionally indicated with:
#
Example:
Qh7#
Castling is:
O-O
for kingside castling and:
O-O-O
for queenside castling.
Promotion is written by appending the promoted piece:
e8=Q
A promotion capture can be:
fxg8=Q+
PGN supports two types of comments.
A comment can be enclosed in { and }:
1.e4 {A common opening move} e5
Comments can span multiple lines:
3.Nc3 Nf6 {Black develops the kingside knight.
The position remains balanced.}
This is the most common comment form in PGN.
A comment can also begin with ; and continue to the end of the line:
1.e4 e5 ; Classical King's Pawn opening
2.Nf3 Nc6
The semicolon comment terminates at the newline.
PGN can represent alternative moves using parentheses.
For example:
1.e4 e5
2.Nf3 Nc6
3.Bb5 (3.Bc4 Nf6 4.d3) a6
The parenthesized sequence is an alternative variation.
Variations can themselves contain variations:
1.e4
(1.d4
(1.c4 e5)
d5)
e5
This means that PGN movetext is represented as a tree.
A parenthesized variation normally represents an alternative continuation from the position immediately before the move that starts the variation.
For example:
9.Bd3
(9.g3 Bb7 10.Qe2 Nc6 11.Nxc6 Bxc6)
Bb7
The main line is:
9.Bd3 Bb7
The variation is:
9.g3 Bb7 10.Qe2 Nc6 11.Nxc6 Bxc6
PGN supports Numeric Annotation Glyphs, abbreviated NAGs, which are represented by $ followed by an integer:
$1
For example:
1.e4 $1 e5
NAGs are used for move annotations such as:
$1 good move
$2 mistake
$3 brilliant move
$4 blunder
$5 interesting move
$6 dubious move
NAGs can be used together with human-readable comments:
10.Qh5 $1 {A strong attacking move}
PGN data frequently combines comments and NAGs with engine or human annotations.
Examples include:
10.Qh5 {= +0.34 (32 ply)}
or:
11.Qxh7+ {+- mate-in-7}
White's move is normally prefixed by a move number followed by a period:
1.e4
2.Nf3
3.Bb5
Black's move normally follows the White move:
1.e4 e5
A Black move can also be explicitly introduced with three periods:
1...e5
A PGN game ends with a termination marker corresponding to the Result tag:
1-0
0-1
1/2-1/2
*
PGN is generally tolerant of whitespace between tokens.
For example, these are semantically equivalent:
1.e4 e5 2.Nf3 Nc6
and:
1.e4
e5
2.Nf3
Nc6
An incomplete or ongoing game can use:
*
For example:
[Result "*"]
1.e4 e5 2.Nf3 Nc6 *