Portable Game Notation


Portable Game Notation is a standard plain text format for recording chess games, which can be read by humans and is also supported by most chess software.

History

PGN was devised around 1993, by Steven J. Edwards, and was first popularized and specified via the Usenet newsgroup rec.games.chess.

Usage

PGN is structured "for easy reading and writing by human users and for easy parsing and generation by computer programs." The chess moves themselves are given in algebraic chess notation. The usual filename extension is .pgn.
There are two formats in the PGN specification, the "import" format and the "export" format. The import format describes data that may have been prepared by hand, and is intentionally lax; a program that can read PGN data should be able to handle the somewhat lax import format. The export format is rather strict and describes data prepared under program control, similar to a pretty printed source program reformatted by a compiler. The export format representations generated by different programs on the same computer should be exactly equivalent, byte for byte.
PGN code begins with a set of "tag pairs", followed by the "movetext".

Tag pairs

Tag pairs begin with an initial left bracket . A quote inside a tag value is represented by the backslash immediately followed by a quote. A backslash inside a tag value is represented by two adjacent backslashes. There are no special control codes involving escape characters, or carriage returns and linefeeds to separate the fields, and superfluous embedded spaces are usually skipped when parsing.
PGN data for archival storage is required to provide seven bracketed fields, referred to as "tags" and together known as the STR. In export format, the STR tag pairs must appear before any other tag pairs that may appear, and in this order:
  1. Event: the name of the tournament or match event.
  2. Site: the location of the event. This is in City, Region COUNTRY format, where COUNTRY is the three-letter International Olympic Committee code for the country. An example is New York City, NY USA.
  3. Date: the starting date of the game, in YYYY.MM.DD form. ?? is used for unknown values.
  4. Round: the playing round ordinal of the game within the event.
  5. White: the player of the white pieces, in Lastname, Firstname format.
  6. Black: the player of the black pieces, same format as White.
  7. Result: the result of the game. It is recorded as White score, dash, then Black score, or *.
The standard allows for supplementation in the form of other optional tag pairs. The more common tag pairs include:
The movetext describes the actual moves of the game. This includes move number indicators and movetext in Standard Algebraic Notation.
For most moves the SAN consists of the letter abbreviation for the piece, an x if there is a capture, and the two-character algebraic name of the final square the piece moved to. The letter abbreviations are K, Q, R, B, and N. The pawn is given an empty abbreviation in SAN movetext, but in other contexts the abbreviation P is used. The algebraic name of any square is as per usual algebraic chess notation; from white's perspective, the leftmost square closest to white is a1, the rightmost square closest to the white is h1, and the rightmost square closest to black side is h8.
In a few cases a more detailed representation is needed to resolve ambiguity; if so, the piece's file letter, numerical rank, or the exact square is inserted after the moving piece's name. Thus, Nge2 specifies that the knight originally on the g-file moves to e2.
SAN kingside castling is indicated by the sequence O-O; queenside castling is indicated by the sequence O-O-O. Pawn promotions are notated by appending = to the destination square, followed by the piece the pawn is promoted to. For example: e8=Q. If the move is a checking move,
+ is also appended; if the move is a checkmating move, # is appended instead. For example: e8=Q#.
An annotator who wishes to suggest alternative moves to those actually played in the game may insert variations enclosed in parentheses. They may also comment on the game by inserting Numeric Annotation Glyphs into the movetext. Each NAG reflects a subjective impression of the move preceding the NAG or of the resultant position.
If the game result is anything other than *, the result is repeated at the end of the movetext.

Comments

Comments are inserted by either a ;. Comments do not nest.

Example

Here is the PGN format of the 29th game of the 1992 match played in Yugoslavia between Bobby Fischer and Boris Spassky:

1. e4 e5 2. Nf3 Nc6 3. Bb5 a6
4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7
11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5
Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6
23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5
hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5
35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6
Nf2 42. g4 Bd3 43. Re6 1/2-1/2

Handling chess variants

Many chess variants can be recorded using PGN, provided the names of the pieces can be limited to one character, usually a letter and not a number. They are typically noted with a tag named "Variant" giving the name of the rules. The term "Variation" must be avoided, as that refers to the name of an opening variation. Note that traditional chess programs can only handle, at most, a few variants. Forsyth-Edwards Notation is used to record the starting position for variants which have initial positions other than the orthodox chess initial position.

Chess programs which can open, edit and save PGN files

Programs for Microsoft Windows
Programs for Linux
Programs for the Android Operating System
Programs for Mac OS
Programs for iOS