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 go 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.

General use

The value of any property can be obtained using its sgf tag. 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)

METHODS

tags

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

    print $sgf->tags;

colour (aka color)

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;
    print $sgf->color; # another way to spell 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 (version 0.01)

Simon Cozens simon@cpan.org

MODIFICATIONS (version 0.02)

Daniel Gilder

SEE ALSO

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