2 # This is a naive Perl script that will convert SGF files to GTP
3 # format so that you can feed them to Pachi, insert genmove at
4 # the right places etc. Might not work on obscure SGF files,
5 # and of course there must be no variations.
7 # When called with a filename argument, it will create the output
8 # file with .gtp extension instead of .sgf.
13 open STDIN
, "$ARGV[0]" or die "$ARGV[0]: $!";
14 my $ofile = $ARGV[0]; $ofile =~ s/sgf$//i; $ofile .= 'gtp';
15 open STDOUT
, ">$ofile" or die "$ofile: $!";
18 local $/ = undef; my $sgf = <>;
19 my $size = ($sgf =~ /SZ\[(\d+)\]/)[0];
21 $sgf =~ s/\bC\[.*?\]//gs; # no comments
22 #$sgf =~ s/\).*//gs; # cut at end of principal branch
24 print "boardsize " . $size;
26 if ($sgf =~ s/\bKM\[([\d.]+)\]//gs) {
29 if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) {
30 print "fixed_handicap $1";
33 my $abcd = "abcdefghijklmnopqrstuvwxyz";
35 my @m = split /;/, $sgf;
37 /^([BW])\[\]/ and print "play $1 pass";
38 /^([BW])\[(\w\w)\]/ or next;
39 my ($color, $coord) = ($1, $2);
40 my ($x, $y) = split //, $coord;
42 $y = $size - index($abcd, $y);
43 print "play $color $x$y";