File: sgf2tst

package info (click to toggle)
gnugo 3.8-13
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,656 kB
  • sloc: ansic: 56,447; perl: 3,771; lisp: 2,804; sh: 722; python: 682; makefile: 653; awk: 113; sed: 22
file content (119 lines) | stat: -rw-r--r-- 4,171 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /usr/bin/perl -w

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 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 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  #
# for more details.                                             #
#                                                               #
# 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.                                        #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #



# This is a script which converts a game record annotated with test cases to
# the *.tst format.
# 
# The script currently only processes SGF files without variations, but that's
# quite useful enough when annotating games.
#
# All GTP commands are put into the SGF comment field.  The comment field is
# interpretted in order:
# 
#  1) Lines starting with "#" are copied into the tst file.
#  2) owl_attack, owl_defend, attack, defend, and eval_eye commands can 
#     be put in such as:
#         owl_attack A1
#         1 G2
#  3) Otherwise, a single line is interpreted as the correct answer, with
#     the appropriate "gg_genmove" directive added automatically.
# 
# See regression/trevora.tst for examples. The sgf files for this test
# are in regression/games/trevor/auto/a??.sgf

use strict;
use warnings;

local $/;
undef $/;

my $autoprobnum = 100;
my $increment = 10;

while (<>) {
  my $content = $_;
  if ($content !~ /C\[/) {
    print STDERR "Warning : $ARGV : No comments.\n";
    next;
  }

  print "\n\n# $ARGV problems:\n\n";
  
  $content =~ s/^\(;//;
  $content .= ';';
  
  my $DEBUG = 0;  
  
  my $i=0;
  my $done=0;
  while ($content =~ /(.*?);/sg  && ($done=1)) {  # for each node.
    $i++;
    my $node = $1;
    print "CONTENT:'$content':CONTENT\n\n" if $DEBUG;
    print "NODE:'$node':NODE\n\n" if $DEBUG;
    next if $node !~ /C\[(.*)\]/s ;
    my $comments = "";
    my $command = "";
    my $comment = $1;
    my ($othercolor) = $node =~ /(W|B)\[/       or die "No W or B move here: $ARGV($i): $node";
    while ($comment =~ /^(.*)$/mg) { # i.e. for each line of comment
      my $line = $1;
      $line =~ s/\s*$//;
      $line =~ s/^\s*//;
      if ($line =~ /^#/) {
        $comments .= "$line\n";
        next;
      }
      $command .= "loadsgf $ARGV $i\n";
      my $probnum = $autoprobnum;
      if ($line =~ /^([0-9]*)\s*((?:owl_attack|attack|owl_defend|defend|eval_eye).*)$/) {
        if ($1 eq "") {
          $probnum = $autoprobnum;
        } else {
          $probnum = $1;
          $autoprobnum -= $increment;
        }

        $command .= "$probnum $2\n";  #ah, this line is a specific gtp command.
        $comment =~ /^(.*)$/mg;          #read next line for answer.
        $line = $1;
        $line =~ s/\s*$//;
        $line =~ s/^\s*//;
      } else {
        $command .= "$probnum gg_genmove " . ($othercolor eq 'B' ? 'white' : 'black') . "\n";
      }
      $autoprobnum += $increment;
      $command .= "#? [$line]*\n";
      print $command if $DEBUG;
    }
    print "$comments$command\n\n";
  }
  
}