time_left(): give precedence to gtp peer for byoyomi
[pachi.git] / tools / sgf2gtp.pl
blob133fc7e1934f9a05c55fab7e5d63ec17b4b9916e
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 $sgf =~ s/\bC\[.*?\]//gs; # no comments
21 #$sgf =~ s/\).*//gs; # cut at end of principal branch
23 print "boardsize " . $size;
24 print "clear_board";
25 print "komi " . ($sgf =~ /KM\[([\d.]+)\]/)[0];
26 if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) {
27 print "fixed_handicap $1";
30 my $abcd = "abcdefghijklmnopqrstuvwxyz";
32 my @m = split /;/, $sgf;
33 foreach (@m) {
34 /^([BW])\[\]/ and print "play $1 pass";
35 /^([BW])\[(\w\w)\]/ or next;
36 my ($color, $coord) = ($1, $2);
37 my ($x, $y) = split //, $coord;
38 ($x ge 'i') and $x++;
39 $y = $size - index($abcd, $y);
40 print "play $color $x$y";