Skip to content

Latest commit

 

History

History
148 lines (102 loc) · 3.72 KB

pgn.rst

File metadata and controls

148 lines (102 loc) · 3.72 KB

PGN parsing and writing

Parsing

chess.pgn.read_game

Writing

If you want to export your game with all headers, comments and variations, you can do it like this:

>>> import chess >>> import chess.pgn >>> >>> game = chess.pgn.Game() >>> game.headers["Event"] = "Example" >>> node = game.add_variation(chess.Move.from_uci("e2e4")) >>> node = node.add_variation(chess.Move.from_uci("e7e5")) >>> node.comment = "Comment" >>> >>> print(game) [Event "Example"] [Site "?"] [Date "????.??.??"] [Round "?"] [White "?"] [Black "?"] [Result ""] <BLANKLINE> 1. e4 e5 { Comment }

Remember that games in files should be separated with extra blank lines.

>>> print(game, file=open("/dev/null", "w"), end="nn")

Use the ~chess.pgn.StringExporter() or ~chess.pgn.FileExporter() visitors if you need more control.

Game model

Games are represented as a tree of moves. Each ~chess.pgn.GameNode can have extra information, such as comments. The root node of a game (~chess.pgn.Game extends the ~chess.pgn.GameNode) also holds general information, such as game headers.

chess.pgn.Game

chess.pgn.GameNode

Visitors

Visitors are an advanced concept for game tree traversal.

chess.pgn.BaseVisitor

The following visitors are readily available.

chess.pgn.GameBuilder

chess.pgn.HeadersBuilder

chess.pgn.BoardBuilder

chess.pgn.SkipVisitor

chess.pgn.StringExporter

chess.pgn.FileExporter

NAGs

Numeric anotation glyphs describe moves and positions using standardized codes that are understood by many chess programs. During PGN parsing, annotations like !, ?, !!, etc., are also converted to NAGs.

chess.pgn.NAG_GOOD_MOVE

chess.pgn.NAG_MISTAKE

chess.pgn.NAG_BRILLIANT_MOVE

chess.pgn.NAG_BLUNDER

chess.pgn.NAG_SPECULATIVE_MOVE

chess.pgn.NAG_DUBIOUS_MOVE

Skimming

These functions allow for quickly skimming games without fully parsing them.

chess.pgn.read_headers

chess.pgn.skip_game