The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Games::Go::SGF - Parse and dissect Standard Go Format files

SYNOPSIS

  use Games::Go::SGF;
  my $sgf = new Games::Go::SGF($sgfdata);
  print "Game played on ".$sgf->date."\n";
  print $sgf->white. " (W) vs. ".$sgf->black." (B)\n";
  print "Board size: ".$sgf->size.". Komi: ".$sgf->komi."\n";

  while ($move = $sgf->move($move_no++)) {
    print "$move_no: ".$move->move,"\n";
  }

DESCRIPTION

This is a very simple SGF file parser, of currently limited functionality. It can read and step through SGF files, follow variations, and so on. It's good enough for getting simple statistics about games of Go, and building up Games::Go::Board objects representing games stored as SGF.

$sgf->move returns either a normal Games::Go::SGF::Node or a Games::Go::SGF::Variation object. The variation object has the additional methods mainline() to get the main line of the game, variation($n) to get the first node in the n'th variation, and variations to retrieve an array of variations. $variation->move will, by default, follow the mainline.

The parser will report 'bad sgf' with an explanation if:

there are certain duplicated property identifiers (tags) within a file (eg SZ), there are certain duplicated tags within a node (eg B), there is a certain mixture of tags within a node eg ( (B or W) and (AB or AW or AE) )

The parser will also quietly re-organise tags within a node if it is badly formed. eg CR[aa]CR[ab] becomes CR[aa]:[ab]

Some property value validation checks are made, some of which are Go specific. For example B[ab] is OK, but B[ab:ac] will not parse.

3. Added AUTOLOAD functions to SGF::Node and SGF, so property values can be found from the sgf tag. eg. $sgf->RU.

METHODS

General use

The value of any property can be obtained using the sgf property identifier. For example, to get the value of 'RU', use

    my $rules = $sgf->RU

In addition, the following aliases are available:

    $sgf->date  # equivalent to $sgf->DT
    $sgf->time  # equivalent to $sgf->DT
    $sgf->white # equivalent to $sgf->PW
    $sgf->black # equivalent to $sgf->PB
    $sgf->size  # equivalent to $sgf->SZ
    $sgf->komi  # equivalent to $sgf->KM

Properties found in the root of the sgf file (all those listed above for example) are available to be read regardless of the current node, other properties are node specific ($sgf->B for example)

tags

The tags method returns an array containing the properties that were found in the current node.

    print $sgf->tags

colour

The colour method returns 'B', 'W', or 'None', depending on whether the tag B, or W, or neither of them was found in the current node.

    print $sgf->colour

TODO

The ability to write as well as read SGF files. Could be faster if validation was rewritten somehow. Make variations easier to navigate.

AUTHOR

Simon Cozens simon@cpan.org Daniel Gilder

SEE ALSO

Games::Go::Board, http://www.red-bean.com/sgf/