Added some investigations of another game. Move 61 seems to yield rather random...
[pachi/pachi-r6144.git] / tools / sgf2gtp.pl
blob0a0e1fbc65e48c9b398ff67852d4cc681bb096b8
1 #!/usr/bin/perl -l
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.
10 use warnings;
12 if ($ARGV[0]) {
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 = 19;
20 if ($sgf =~ /SZ\[(\d+)\]/) { $size = $1; }
21 $sgf =~ s/\bC\[.*?\]//gs; # no comments
22 #$sgf =~ s/\).*//gs; # cut at end of principal branch
24 print "boardsize " . $size;
25 print "clear_board";
26 print "komi " . ($sgf =~ /KM\[([\d.]+)\]/)[0];
27 if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) {
28 print "fixed_handicap $1";
31 my $abcd = "abcdefghijklmnopqrstuvwxyz";
33 my @m = split /;/, $sgf;
34 foreach (@m) {
35 /^([BW])\[\]/ and print "play $1 pass";
36 /^([BW])\[(\w\w)\]/ or next;
37 my ($color, $coord) = ($1, $2);
38 my ($x, $y) = split //, $coord;
39 ($x ge 'i') and $x++;
40 $y = $size - index($abcd, $y);
41 print "play $color $x$y";