Home Up Mail an Joachim Pimiskern Impressum

TSgfParser

TSgfParser is a freeware Delphi / Object Pascal component for parsing SGF (Smart Game Format) files. SGF is the de-facto file format standard for go games. The component comes with a graphical browser TSgfTreeView.

Download sgf_06_Dez_2003.zip (24.442 bytes)

Installation

Add sgfparse.pas, sgftview.pas and sgftoken.pas to a new or an existing package. Compile the demo project project1.dpr and start it.

Using the components

Add the form main.pas of the demo application to your go program, eventually under another name. Replace calls of public procedures of TFormDemoBoard (demoboard.pas) by board manipulationg methods of your own Go program.

Links

The SGF format
Grammar that was used for TSgfParser
Meaning of the SGF properties

Philosophy

Every programmer has his own ideas on how to represent a go position, whether it be an two-dimensional integer array, a byte-array, or a list of pairs of bits. Therefore an application independent SGF parser component should not place stones onto an internal board. TSgfParser does not interprete SGF property names or SGF property values. Instead, whenever a node is visited by browsing through the game tree, an event is triggered that informs the application about each pair property name / property value of that node. The application is responsible for manipulating it's internal board, setting or removing stones, marking, numbering, and so on.

Demo application

The demo program is not intended to be used as a game viewer, although it operates so in a restricted way. When playing with the demo program, you will notice, that captured stones are not removed (By the way, the SGF specification provides an AE (add empty) property to remove stones, but normally every go program is able to recognize captured stones by itself. So should your's).

Implementation details

The parser constructs a syntax tree from a SGF file. This tree consists of two types of components, TSgfData (a kind of variant object), and TSgfMultiPurposeList, which is actually a list of TSgfData.

Some special Properties of TSgfTreeView

property SgfParser : TSgfParser Another required non-visual component that does the parsing of a SGF file.
property OnExecProperty: TSgfPropertyEvent Inform the application about the properties of a node. Situations when this event is triggered are given in the Purpose parameter:
TNodeInformationPurpose = (nipInit,nipVariant,nipNodePath,nipLastNode);
nipInit When a SGF file node is added to the SgfTreeView.
nipVariant When a variant node is added. Use this information to change the TreeNode.Text property.
nipFirstNode The first node of a path from the root to an activated node.
nipNodePath A node that lies on the path from the root to a just activated node.
nipLastNode The last node of a path from the root to an activated node, just the activated node.
nipName An event for setting the name of a node. The user should check the properties of the node, especially whether the node is a black or white setting of a move, an then increment TSgfTreeView.Number. TSgfTreeView takes care of restoring this number after returning from a variant.

How to get meta information about the game

Normally meta information like GM, SZ, PW, PB is stored in the first node of a game. Whenever a node in the SgfTreeView is clicked, all pairs property name / property value from the root of the tree downto the clicked node are told to the application via the event OnExecProperty. So all you have to do is to catch the appropriate informations when they pass by.

Saving games - TSgfParser.SaveToFile vs TSgfTreeView.SaveToFile

TSgfParser.SaveToFile is meant to make it possible to build up game trees without the presence of a graphical representation, here, without a TSgfTreeView. You may prefer this way if you're using the TSgfParser component to create a command line application.

TSgfTreeView.AddGameTree(Text) Creates a new game tree and sets its caption to 'text'. TSgfTreeView.AddProperty(PropertyName,PropertyValue) Adds a property and its value to a node. TSgfTreeView.Items.Insert(Node,Text) Insert new node above the selected node.

Home Up Mail an Joachim Pimiskern Impressum