# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This program is distributed with GNU Go, a Go program. #
# Write gnugo@gnu.org or see http://www.gnu.org/software/gnugo/ #
# for more information. #
# Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 #
# 2008 and 2009 by the Free Software Foundation. #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation - version 3, #
# or (at your option) any later version. #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the implied #
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR #
# PURPOSE. See the GNU General Public License in file COPYING #
# You should have received a copy of the GNU General Public #
# License along with this program; if not, write to the Free #
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, #
# Boston, MA 02111, USA. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Here is a small perlscript twogtp. Its purpose is to run
# two programs against each other. Both must support the Go
# Text Protocol. For example GNU Go 2.7.241 or higher works.
# It is easier to implement this program in gtp than gmp.
# The script is almost trivial. It also works with cygwin on
# twogtp --white '<path to program 1> --mode gtp <options>' \
# --black '<path to program 2> --mode gtp <options>' \
# Possible twogtp options:
# --verbose 1 (to list moves) or --verbose 2 (to draw board)
# --size <board size> (default 19)
# --games <number of games to play> (-1 to play forever)
#following added globally to allow "use strict" :
#end of "use strict" repairs
twogtp --white \'<path to program 1> --mode gtp [program options]\' \\
--black \'<path to program 2> --mode gtp [program options]\' \\
--verbose 1 (to list moves) or --verbose 2 (to draw board)
--size <board size> (default 19)
--games <number of games to play> (-1 to play forever)
"verbose|v=i" => \
$verbose,
"handicap|h=i" => \
$handicap,
"size|boardsize|s=i" => \
$size,
"sgffile|o=s" => \
$sgffilename,
$white = '../gnugo.exe --mode gtp --quiet';
warn "Defaulting white to: $white";
$black = '../gnugo.exe --mode gtp --quiet';
warn "Defaulting black to: $black";
die $helpstring unless defined $white and defined $black;
my $black_in = new FileHandle
; # stdin of black player
my $black_out = new FileHandle
; # stdout of black player
my $white_in = new FileHandle
; # stdin of white player
my $white_out = new FileHandle
; # stdout of white player
my $b_gtp_ver; # gtp version of black player
my $w_gtp_ver; # gtp version of white player
$pidb = open2
($black_out, $black_in, $black);
print "black pid: $pidb\n" if $verbose;
$pidw = open2
($white_out, $white_in, $white);
print "white pid: $pidw\n" if $verbose;
$sgffile = rename_sgffile
($games, $sgffilename) if defined $sgffilename;
if ((defined $sgffilename) && !open(SGFFILEHANDLE
, ">$sgffile")) {
printf("can't open $sgffile\n");
print $black_in "protocol_version\n";
$b_gtp_ver = eat_gtp_ver
($black_out);
print $black_in "boardsize $size\n";
eat_no_response
($black_out);
print $black_in "clear_board\n";
eat_no_response
($black_out);
print $black_in "komi $komi\n";
eat_no_response
($black_out);
print $white_in "protocol_version\n";
$w_gtp_ver = eat_gtp_ver
($white_out);
print $white_in "boardsize $size\n";
eat_no_response
($white_out);
print $white_in "clear_board\n";
eat_no_response
($white_out);
print $white_in "komi $komi\n";
eat_no_response
($white_out);
print SGFFILEHANDLE
"(;GM[1]FF[4]RU[Japanese]SZ[$size]HA[$handicap]KM[$komi]"
print $black_in "fixed_handicap $handicap\n";
$handicap_stones = eat_handicap
($black_out);
if (defined $sgffilename) {
print SGFFILEHANDLE
$handicap_stones;
print $white_in "fixed_handicap $handicap\n";
$handicap_stones = eat_handicap
($white_out);
print $black_in "genmove_black\n";
print $black_in "genmove black\n";
$move = eat_move
($black_out);
$sgfmove = standard_to_sgf
($move);
print SGFFILEHANDLE
";B[$sgfmove]\n" if defined $sgffilename;
print "Black plays $move\n" if $verbose;
print $white_in "black $move\n";
print $white_in "play black $move\n";
eat_no_response
($white_out);
print $white_in "showboard\n";
eat_showboard
($white_out);
eat_no_response
($white_out);
print $white_in "genmove_white\n";
print $white_in "genmove white\n";
$move = eat_move
($white_out);
$sgfmove = standard_to_sgf
($move);
print SGFFILEHANDLE
";W[$sgfmove]\n" if defined $sgffilename;
print "White plays $move\n" if $verbose;
print $black_in "white $move\n";
print $black_in "play white $move\n";
eat_no_response
($black_out);
print $black_in "showboard\n";
eat_showboard
($black_out);
eat_no_response
($black_out);
print $white_in "final_score\n";
$resultw = eat_score
($white_out);
print "Result according to W: $resultw\n";
print $black_in "final_score\n";
$resultb = eat_score
($black_out);
print "Result according to B: $resultb\n";
print $white_in "quit\n";
print $black_in "quit\n";
if (defined $sgffilename) {
print "sgf file: $sgffile\n";
$game_list{$sgffile} = $resultw . "|" . $resultb
print "games remaining: $games\n";
if (defined $sgffilename) {
my $index_out = new FileHandle
;
open ($index_out, "> " . index_name
($sgffilename));
"<HTML><HEAD><TITLE>game results</TITLE></HEAD>
<BODY><H3>Game Results</H3>
<H4>White: ".html_encode
($white)."</H4>
<H4>Black: ".html_encode
($black)."</H4>
foreach (sort by_result
keys(%game_list)) {
print $index_out "<TR><TD><A href=\"$_\">$_</A></TD>" .
"<TD>".html_encode
(game_result
($_))."</TD></TR>\n";
print $index_out "</TABLE></BODY></HTML>\n";
#i.e.: B+13.5 (upper bound: -13.5, lower: -13.5)|B+13.5 (upper bound: -13.5, lower: -13.5)
#Make sure that all 4 values are the same. I've not seen them different yet.
#If they are ever different, need to improve the HTML output (now just -999) -
# an explanation of the score mismatch problem would be appropriate.
$_ =~ /^.*upper bound..([0-9+.\-]*)..lower..\1.\|.*upper bound..\1..lower..\1./;
game_result
($a) <=> game_result
($b) || $a cmp $b;
chop($line = <$h>) or die "No response!";
$line =~ s/(\s|\n)*$//smg;
if (!defined($line = <$h>)) {
$line =~ s/(\s|\n)*$//smg;
my ($equals, $move) = split(' ', $line, 2);
defined($move) or confess
"no move found: line was: '$line'";
# ignore empty lines, die if process is gone
chop($line = <$h>) or die "No response!";
@vertices = split(" ", $line);
foreach $vertex (@vertices) {
$vertex = standard_to_sgf
($vertex);
$sgf_handicap = "$sgf_handicap\[$vertex\]";
# ignore empty lines, die if process is gone
chop($line = <$h>) or die "No response!";
my ($equals, $result) = split(' ', $line, 2);
chop($line = <$h>) or die "No response!";
my ($equals, $result) = split(' ', $line, 2);
chop($line = <$h>) or die "No response!";
my ($equals, $result) = split(' ', $line, 2);
while (!($line =~ /^\s*$/)) {
for (@_) { confess
"Yikes!" if !defined($_); }
for (@_) { tr/A-Z/a-z/ };
return "tt" if $_ eq "pass";
$second = chr($size+1-$2+96);
my $nogames = int shift(@_);
# Annoying to loose _001 on game #1 in multi-game set.
# Could record as an additional parameter.
# return "$_.sgf" if ($nogames == 1);
return sprintf("$_" . "_%03d.sgf", $nogames);
return $_ . "_index.html";