README: Add a link to my thesis
[pachi/t.git] / tools / sgf2gtp.pl
blobe23f497c2da9363dd2f2093e5227959c4bacec2e
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 = ($sgf =~ /SZ\[(\d+)\]/)[0];
20 $size ||= 19;
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 if ($sgf =~ s/\bKM\[([\d.]+)\]//gs) {
27 print "komi $1";
29 if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) {
30 print "fixed_handicap $1";
33 my $abcd = "abcdefghijklmnopqrstuvwxyz";
35 my @m = split /;/, $sgf;
36 foreach (@m) {
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;
41 ($x ge 'i') and $x++;
42 $y = $size - index($abcd, $y);
43 print "play $color $x$y";