sgf2gtp.pl: Add tagline and usage
[pachi.git] / tools / sgf2gtp.pl
blobcb921d78a5e6a2ea9ba1a9e88378ae7c6da01e4b
1 #!/usr/bin/perl -l
3 # sgf2gtp - Convert SGF game record to GTP command stream
5 # Usage: sgf2gtp [FILENAME]
7 # This is a naive Perl script that will convert SGF files to GTP
8 # format so that you can feed them to Pachi, insert genmove at
9 # the right places etc. Might not work on obscure SGF files,
10 # and of course there must be no variations.
12 # When called with a filename argument, it will create the output
13 # file with .gtp extension instead of .sgf.
15 use warnings;
17 if ($ARGV[0]) {
18 open STDIN, "$ARGV[0]" or die "$ARGV[0]: $!";
19 my $ofile = $ARGV[0]; $ofile =~ s/sgf$//i; $ofile .= 'gtp';
20 open STDOUT, ">$ofile" or die "$ofile: $!";
23 local $/ = undef; my $sgf = <>;
24 my $size = ($sgf =~ /SZ\[(\d+)\]/)[0];
25 $size ||= 19;
26 $sgf =~ s/\bC\[.*?\]//gs; # no comments
27 #$sgf =~ s/\).*//gs; # cut at end of principal branch
29 print "boardsize " . $size;
30 print "clear_board";
31 if ($sgf =~ s/\bKM\[([\d.]+)\]//gs) {
32 print "komi $1";
34 if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) {
35 print "fixed_handicap $1";
38 my $abcd = "abcdefghijklmnopqrstuvwxyz";
40 my @m = split /;/, $sgf;
41 foreach (@m) {
42 /^([BW])\[\]/ and print "play $1 pass";
43 /^([BW])\[(\w\w)\]/ or next;
44 my ($color, $coord) = ($1, $2);
45 my ($x, $y) = split //, $coord;
46 ($x ge 'i') and $x++;
47 $y = $size - index($abcd, $y);
48 print "play $color $x$y";