Initial commit of GNU Go v3.8.
[sgk-go] / interface / gtp_examples / sgf2tst
CommitLineData
7eeb782e
AT
1#! /usr/bin/perl -w
2
3# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
4# This program is distributed with GNU Go, a Go program. #
5# #
6# Write gnugo@gnu.org or see http://www.gnu.org/software/gnugo/ #
7# for more information. #
8# #
9# Copyright 1999, 2000, 2001 by the Free Software Foundation. #
10# #
11# This program is free software; you can redistribute it and/or #
12# modify it under the terms of the GNU General Public License #
13# as published by the Free Software Foundation - version 3, #
14# or (at your option) any later version. #
15# #
16# This program is distributed in the hope that it will be #
17# useful, but WITHOUT ANY WARRANTY; without even the implied #
18# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR #
19# PURPOSE. See the GNU General Public License in file COPYING #
20# for more details. #
21# #
22# You should have received a copy of the GNU General Public #
23# License along with this program; if not, write to the Free #
24# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, #
25# Boston, MA 02111, USA. #
26# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
27
28
29
30# This is a script which converts a game record annotated with test cases to
31# the *.tst format.
32#
33# The script currently only processes SGF files without variations, but that's
34# quite useful enough when annotating games.
35#
36# All GTP commands are put into the SGF comment field. The comment field is
37# interpretted in order:
38#
39# 1) Lines starting with "#" are copied into the tst file.
40# 2) owl_attack, owl_defend, attack, defend, and eval_eye commands can
41# be put in such as:
42# owl_attack A1
43# 1 G2
44# 3) Otherwise, a single line is interpreted as the correct answer, with
45# the appropriate "gg_genmove" directive added automatically.
46#
47# See regression/trevora.tst for examples. The sgf files for this test
48# are in regression/games/trevor/auto/a??.sgf
49
50use strict;
51use warnings;
52
53local $/;
54undef $/;
55
56my $autoprobnum = 100;
57my $increment = 10;
58
59while (<>) {
60 my $content = $_;
61 if ($content !~ /C\[/) {
62 print STDERR "Warning : $ARGV : No comments.\n";
63 next;
64 }
65
66 print "\n\n# $ARGV problems:\n\n";
67
68 $content =~ s/^\(;//;
69 $content .= ';';
70
71 my $DEBUG = 0;
72
73 my $i=0;
74 my $done=0;
75 while ($content =~ /(.*?);/sg && ($done=1)) { # for each node.
76 $i++;
77 my $node = $1;
78 print "CONTENT:'$content':CONTENT\n\n" if $DEBUG;
79 print "NODE:'$node':NODE\n\n" if $DEBUG;
80 next if $node !~ /C\[(.*)\]/s ;
81 my $comments = "";
82 my $command = "";
83 my $comment = $1;
84 my ($othercolor) = $node =~ /(W|B)\[/ or die "No W or B move here: $ARGV($i): $node";
85 while ($comment =~ /^(.*)$/mg) { # i.e. for each line of comment
86 my $line = $1;
87 $line =~ s/\s*$//;
88 $line =~ s/^\s*//;
89 if ($line =~ /^#/) {
90 $comments .= "$line\n";
91 next;
92 }
93 $command .= "loadsgf $ARGV $i\n";
94 my $probnum = $autoprobnum;
95 if ($line =~ /^([0-9]*)\s*((?:owl_attack|attack|owl_defend|defend|eval_eye).*)$/) {
96 if ($1 eq "") {
97 $probnum = $autoprobnum;
98 } else {
99 $probnum = $1;
100 $autoprobnum -= $increment;
101 }
102
103 $command .= "$probnum $2\n"; #ah, this line is a specific gtp command.
104 $comment =~ /^(.*)$/mg; #read next line for answer.
105 $line = $1;
106 $line =~ s/\s*$//;
107 $line =~ s/^\s*//;
108 } else {
109 $command .= "$probnum gg_genmove " . ($othercolor eq 'B' ? 'white' : 'black') . "\n";
110 }
111 $autoprobnum += $increment;
112 $command .= "#? [$line]*\n";
113 print $command if $DEBUG;
114 }
115 print "$comments$command\n\n";
116 }
117
118}
119