-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoQLib.pm
154 lines (127 loc) · 3.7 KB
/
GoQLib.pm
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# DGSLib: A Library to access dragongoserver.net
# Copyright (C) 2006-2010 Yves Rutschle
#
# 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; either
# version 2 of the License, 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 for more
# details.
#
# The full text for the General Public License is here:
# http://www.gnu.org/licenses/gpl.html
package GoQLib;
use strict;
use Exporter;
use Carp qw/cluck/;
use vars qw( @EXPORT @ISA );
@ISA = qw(Exporter);
@EXPORT = qw(
convert_coord_std_to_letters
convert_coord_letters_to_std
get_marked_dead_from_gnugo
make_accessors
is_coord_list_identical
ldiff
);
# A19 -> aa
sub convert_coord_std_to_letters {
my ($board_size, $c) = @_;
$c =~ /(\w)(\d+)/;
my ($column, $line) = ($1, $2);
$column =~ tr/A-HJ-T/a-s/;
$line = ('a'..'s')[$board_size-$line];
return "$column$line";
}
# aa -> A19
sub convert_coord_letters_to_std {
my ($board_size, @c) = @_;
my @out;
cluck "convert_coord_letters_to_std: undefined argument"
unless defined $c[0];
foreach my $c (@c) {
$c =~ /(.)(.)/;
my ($column, $line) = ($1, $2);
$column =~ tr/a-s/A-HJ-T/;
$line = ($board_size - (ord($line)-ord('a')));
push @out, "$column$line";
}
return wantarray ? @out : shift @out;
}
# This returns the list of stones marked dead by GnuGO
sub get_marked_dead_from_gnugo {
my (@sgf) = @_;
my @dead_stones;
foreach my $line (@sgf) {
while ($line =~ s/\[(\w\w):X\]//) {
push @dead_stones, $1;
}
}
return @dead_stones;
}
#############################################################################
# Generic functions that aren't even Go related
sub cdr {
my @r = @{$_[0]};
shift @r;
return \@r;
}
# Given two refs to sorted stone lists, compare if they're identical
sub is_coord_list_identical {
my ($r1, $r2) = @_;
(not defined $r1->[0] and not defined $r2->[0]) or
($r1->[0] eq $r2->[0]) and
is_coord_list_identical(
(cdr $r1),
(cdr $r2)); # and I don't even *know* lisp
}
# List of elements that aren't in both lists
sub ldiff {
my ($r1, $r2) = @_;
my @out;
foreach my $e (@$r1) {
push @out, $e unless grep {$e eq $_ } @$r2;
}
foreach my $e (@$r2) {
push @out, $e unless grep {$e eq $_ } @$r1;
}
return @out;
}
# Create accessors in a package, using a prefix in the hash string, and
# optional tracing of value assigns.
# E.g.:
#
# my @l = qw( login passwd );
# make_accessors(
# package => "MyClass",
# prefix => "cls_",
# trace_assigns => 1,
# accessors => \@l,
# );
# Default is no prefix, no trace. package and accessors are mandatory.
sub make_accessors {
my (%opts) = @_;
my $trace = $opts{trace_assigns} || 0;
my $prefix = $opts{prefix} || "";
my $package = $opts{package};
my @list = @{$opts{accessors}};
my $subs;
foreach my $data ( @list ) {
$subs .= qq{
package $package;
sub $data {
warn "$prefix$data = \$_[1]\\n" if $trace and defined \$_[1] ;
\$_[0]->{$prefix$data} = defined \$_[1] ?
\$_[1] :
\$_[0]->{$prefix$data};
}
}
}
eval $subs;
}
1;