From 797f795afdb7f34b8434ee66e4bbaaea2ec56a4d Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 7 Nov 2011 19:18:55 +0200 Subject: [PATCH] sgf2gtp.pl -g: Add support for automatic genmove output --- tools/sgf2gtp.pl | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tools/sgf2gtp.pl b/tools/sgf2gtp.pl index 89e8c9b..c70995a 100755 --- a/tools/sgf2gtp.pl +++ b/tools/sgf2gtp.pl @@ -2,7 +2,7 @@ # # sgf2gtp - Convert SGF game record to GTP command stream # -# Usage: sgf2gtp [-n MOVENUM] [FILENAME] +# Usage: sgf2gtp [-g] [-n MOVENUM] [FILENAME] # # This is a naive Perl script that will convert SGF files to GTP # format so that you can feed them to Pachi, insert genmove at @@ -12,10 +12,17 @@ # When called with a filename argument, it will create the output # file with .gtp extension instead of .sgf. # +# -g: Automatically append genmove command for the other color. # -n MOVENUM: Output at most first MOVENUM moves. use warnings; +my $genmove; +if ($ARGV[0] and $ARGV[0] eq '-g') { + shift @ARGV; + $genmove = 1; +} + my $maxmoves; if ($ARGV[0] and $ARGV[0] eq '-n') { shift @ARGV; @@ -45,12 +52,14 @@ if ($sgf =~ s/\bHA\[(\d+)\]//gs and $1 > 0) { my $abcd = "abcdefghijklmnopqrstuvwxyz"; my $movenum = 0; +my $last_color = 'w'; my @m = split /;/, $sgf; foreach (@m) { $maxmoves and $movenum >= $maxmoves and last; if (/^([BW])\[\]/) { + $last_color = $1; $movenum++; print "play $1 pass"; next; @@ -58,11 +67,17 @@ foreach (@m) { unless (/^([BW])\[(\w\w)\]/) { next; } - $movenum++; my ($color, $coord) = ($1, $2); my ($x, $y) = split //, $coord; ($x ge 'i') and $x++; $y = $size - index($abcd, $y); + + $last_color = $color; + $movenum++; print "play $color $x$y"; } + +if ($genmove) { + print "genmove ".(uc $last_color eq 'W' ? 'B' : 'W'); +} -- 2.11.4.GIT