blib/lib/Game/Go/GTP.pm

filter: Statementstmt 98.8% 164 / 166 Branchbran 83.3% 85 / 102 Conditioncond 54.5% 18 / 33 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 15 / 15 Podpod 66.6% 2 / 3 totaltotal 85.2% 289 / 339 CC 68 SCAR 36.2
CC
68
Cov
89%
_run
57.0
handle
10.1
_colour
6.2
CRAP 37.5
« blib/lib/Game/Go/SGF.pm ↑ Summary blib/lib/Game/Go.pm »
1 package Game::Go::GTP;
2  
3 2 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 2 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 2 ▶use Game::Go;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 2 ▶use Game::Go::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 ▶use Game::Go::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 ▶use Game::Go::Notation;
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.02';
15  
16 2 my $B = Game::Go::Rules::BLACK;
17 2 my $W = Game::Go::Rules::WHITE;
18  
19 2 has game => (is => 'rw');
20 2 has bot => (is => 'rw');
21 2 has size => (is => 'rw', isa => Int, default => 19);
22 2 has komi => (is => 'rw', default => 6.5);
23 2 has level => (is => 'ro', isa => Int, default => 2);
24 2 has seed => (is => 'ro', default => 'gtp');
25  
26 2 has quit => (is => 'rw', default => 0);
27  
28 2 our @COMMANDS;
29 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 @COMMANDS = qw(
31 protocol_version name version known_command list_commands quit
32 boardsize clear_board komi play genmove undo showboard
33 final_score final_status_list
34 );
35 }
36  
37 13 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
38 13 my ($self) = @_;
39 13 $self->_reset;
40 13 return;
41 }
42  
43 24 ▶sub _reset {
Subroutine _reset: called CC=1 SCAR=0.0
44 24 my ($self) = @_;
45 24 $self->game(Game::Go->new(
46 size => $self->size,
47 komi => $self->komi,
48 seed => $self->seed,
49 ko => 'simple',
50 ));
51 24 $self->bot(Game::Go::Bot->new(level => $self->level, seed => $self->seed));
52 24 return;
53 }
54  
55 72 ▶sub handle {
Subroutine handle: called CC=10 SCAR=23.2
Pod documented
56 72 my ($self, $line) = @_;
57 72 ▶ return '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 72 72
58  
59 72 $line =~ s/#.*\z//;
60 72 $line =~ s/\s+\z//;
61 72 $line =~ s/\A\s+//;
62 72 ▶ return '' unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 3 69 72
63  
64 69 my ($id, @words) = ();
65 69 my @parts = split /\s+/, $line;
66 69 ▶ $id = shift @parts if @parts && $parts[0] =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if @parts and $parts[0] =~ /\A[0-9]+\z/ 2 67 69
Condition66%2/3
@parts and $parts[0] =~ /\A[0-9]+\z/
!ll&&!rl&&r
0672
MC/DC50%1/2
@parts and $parts[0] =~ /\A[0-9]+\z/
@parts $parts[0] =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= @partsB= $parts[0] =~ /\A[0-9]+\z/
ABresult
0X0
100
111
67 69 ▶ my $cmd = lc(shift(@parts) // '');
Condition50%1/2
shift @parts // ""
l!l
690
MC/DC0%0/1
shift @parts // ""
shift @parts
Truth table50%1/2
A
A= shift @parts
Aresult
11
00
68 69 @words = @parts;
69  
70 69 ▶ my $tag = defined $id ? $id : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 2 67 69
71  
72 69 ▶ unless (grep { $_ eq $cmd } @COMMANDS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $cmd;} @Game::Go::GTP::COMMANDS) 3 66 69
73 3 return "?$tag unknown command\n\n";
74 }
75  
76 66 my ($ok, $body) = $self->_run($cmd, \@words);
77 66 ▶ $body = '' unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 66 66
78 66 ▶ return ($ok ? '=' : '?') . "$tag $body\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 55 11 66
79 }
80  
81 66 ▶sub _run {
Subroutine _run: called CC=51 SCAR=40.4
82 66 my ($self, $cmd, $words) = @_;
83 66 my $game = $self->game;
84  
85 66 ▶ return (1, '2') if $cmd eq 'protocol_version';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "protocol_version" 5 61 66
86 61 ▶ return (1, 'Game::Go') if $cmd eq 'name';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "name" 1 60 61
87 60 ▶ return (1, $Game::Go::VERSION) if $cmd eq 'version';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "version" 1 59 60
88 59 ▶ return (1, join "\n", @COMMANDS) if $cmd eq 'list_commands';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "list_commands" 1 58 59
89 58 ▶ return (1, (grep { $_ eq lc($words->[0] // '') } @COMMANDS) ? 'true' : 'false')
Branch: 2 branches
BranchTrueFalseTotal
if $cmd eq "known_command" 11 47 58
grep({$_ eq lc($words->[0] // '');} @Game::Go::GTP::COMMANDS) ? : 10 1 11
Condition50%1/2
$words->[0] // ""
l!l
1650
MC/DC0%0/1
$words->[0] // ""
$words->[0]
Truth table50%1/2
A
A= $words->[0]
Aresult
11
00
90 if $cmd eq 'known_command';
91  
92 47 ▶ if ($cmd eq 'quit') { $self->quit(1); return (1, '') }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "quit") 2 45 47
93  
94 45 ▶ if ($cmd eq 'boardsize') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "boardsize") 11 34 45
95 11 ▶ my $n = $words->[0] // '';
Condition50%1/2
$words->[0] // ""
l!l
110
MC/DC0%0/1
$words->[0] // ""
$words->[0]
Truth table50%1/2
A
A= $words->[0]
Aresult
11
00
96 return (0, 'unacceptable size')
97 11 ▶ unless $n =~ /\A[0-9]+\z/ && grep { $_ == $n } Game::Go->sizes;
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /\A[0-9]+\z/ and grep {$_ == $n;} "Game::Go"->sizes 2 9 11
Condition100%3/3
$n =~ /\A[0-9]+\z/ and grep {$_ == $n;} "Game::Go"->sizes
!ll&&!rl&&r
119
MC/DC100%2/2
$n =~ /\A[0-9]+\z/ and grep {$_ == $n;} "Game::Go"->sizes
$n =~ /\A[0-9]+\z/ grep {$_ == $n;} "Game::Go"->sizes
Truth table100%3/3
A and B
A= $n =~ /\A[0-9]+\z/B= grep {$_ == $n;} "Game::Go"->sizes
ABresult
0X0
100
111
98 9 $self->size($n);
99 9 $self->_reset;
100 9 return (1, '');
101 }
102  
103 34 ▶ if ($cmd eq 'clear_board') { $self->_reset; return (1, '') }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "clear_board") 1 33 34
104  
105 33 ▶ if ($cmd eq 'komi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "komi") 2 31 33
106 2 ▶ my $k = $words->[0] // '';
Condition50%1/2
$words->[0] // ""
l!l
20
MC/DC0%0/1
$words->[0] // ""
$words->[0]
Truth table50%1/2
A
A= $words->[0]
Aresult
11
00
107 2 ▶ return (0, 'syntax error') unless $k =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ 1 1 2
108 1 $self->komi($k + 0);
109 1 $self->_reset;
110 1 return (1, '');
111 }
112  
113 31 ▶ if ($cmd eq 'play') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "play") 20 11 31
114 20 my $colour = $self->_colour($words->[0]);
115 20 ▶ return (0, 'syntax error') unless $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour 1 19 20
116 19 ▶ my $where = uc($words->[1] // '');
Condition50%1/2
$words->[1] // ""
l!l
190
MC/DC0%0/1
$words->[1] // ""
$words->[1]
Truth table50%1/2
A
A= $words->[1]
Aresult
11
00
117  
118 19 ▶ if ($where eq 'PASS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq "PASS") 3 16 19
119 3 my $out = $game->pass($colour);
120 3 ▶ return (0, 'illegal move') if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 3 3
121 3 return (1, '');
122 }
123  
124 16 my ($col, $row) = Game::Go::Notation::from_human($game->size, $where);
125 16 ▶ return (0, 'invalid coordinate') unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 3 13 16
126  
127 13 ▶ $game->turn($colour) if $game->turn != $colour && $game->phase eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
if $game->turn != $colour and $game->phase eq "play" 0 13 13
Condition33%1/3
$game->turn != $colour and $game->phase eq "play"
!ll&&!rl&&r
1300
MC/DC0%0/2
$game->turn != $colour and $game->phase eq "play"
$game->turn != $colour $game->phase eq "play"
Truth table33%1/3
A and B
A= $game->turn != $colourB= $game->phase eq "play"
ABresult
0X0
100
111
128  
129 13 my $out = $game->play($colour, $game->point($col, $row));
130 13 ▶ return (0, 'illegal move') if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 2 11 13
131 11 return (1, '');
132 }
133  
134 11 ▶ if ($cmd eq 'genmove') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "genmove") 2 9 11
135 2 my $colour = $self->_colour($words->[0]);
136 2 ▶ return (0, 'syntax error') unless $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour 0 2 2
137 2 ▶ return (1, 'resign') unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 0 2 2
138  
139 2 ▶ $game->turn($colour) if $game->phase eq 'play' && $game->turn != $colour;
Branch: 1 branch
BranchTrueFalseTotal
if $game->phase eq "play" and $game->turn != $colour 0 2 2
Condition33%1/3
$game->phase eq "play" and $game->turn != $colour
!ll&&!rl&&r
020
MC/DC0%0/2
$game->phase eq "play" and $game->turn != $colour
$game->phase eq "play" $game->turn != $colour
Truth table33%1/3
A and B
A= $game->phase eq "play"B= $game->turn != $colour
ABresult
0X0
100
111
140  
141 2 my $move = $self->bot->choose($game, $colour);
142 2 ▶ return (1, 'pass') unless $move && $move->kind eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless $move and $move->kind eq "play" 0 2 2
Condition33%1/3
$move and $move->kind eq "play"
!ll&&!rl&&r
002
MC/DC0%0/2
$move and $move->kind eq "play"
$move $move->kind eq "play"
Truth table33%1/3
A and B
A= $moveB= $move->kind eq "play"
ABresult
0X0
100
111
143  
144 2 my ($col, $row) = $game->col_row($move->point);
145 2 my $where = Game::Go::Notation::to_human($game->size, $col, $row);
146 2 $game->play($colour, $move->point);
147 2 return (1, $where);
148 }
149  
150 9 ▶ if ($cmd eq 'undo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "undo") 3 6 9
151 3 my $events = $game->events;
152 3 my $cut = -1;
153 3 for my $i (reverse 0 .. $#$events) {
154 3 ▶ next if $events->[$i]{actor} eq 'sys';
Branch: 1 branch
BranchTrueFalseTotal
if $events->[$i]{"actor"} eq "sys" 1 2 3
155 2 $cut = $i;
156 2 last;
157 }
158 3 ▶ return (0, 'cannot undo') if $cut < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cut < 0 1 2 3
159  
160 2 my @keep = @$events[0 .. $cut - 1];
161 2 my $fresh = Game::Go->new(
162 size => $game->size, komi => $game->komi,
163 seed => $self->seed, ko => 'simple',
164 );
165 2 ▶ eval { $fresh->replay(\@keep); 1 } or return (0, 'cannot undo');
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $fresh->replay(\@keep); 1 } } or return 0, "cannot undo" 2 0 2
166 2 $self->game($fresh);
167 2 return (1, '');
168 }
169  
170 6 ▶ if ($cmd eq 'showboard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "showboard") 2 4 6
171 2 return (1, "\n" . $game->board->to_text);
172 }
173  
174 4 ▶ if ($cmd eq 'final_score') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "final_score") 1 3 4
175 1 my $raw = $game->raw_score;
176 1 my ($b, $w) = ($raw->{score_b} / 10, $raw->{score_w} / 10);
177 1 ▶ return (1, '0') if $b == $w;
Branch: 1 branch
BranchTrueFalseTotal
if $b == $w 0 1 1
178 1 ▶ return (1, $b > $w ? sprintf('B+%s', $b - $w) : sprintf('W+%s', $w - $b));
Branch: 1 branch
BranchTrueFalseTotal
$b > $w ? : 1 0 1
179 }
180  
181 3 ▶ if ($cmd eq 'final_status_list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "final_status_list") 3 0 3
182 3 ▶ my $what = lc($words->[0] // 'dead');
Condition50%1/2
$words->[0] // "dead"
l!l
30
MC/DC0%0/1
$words->[0] // "dead"
$words->[0]
Truth table50%1/2
A
A= $words->[0]
Aresult
11
00
183 3 ▶ return (0, 'syntax error') unless $what =~ /\A(?:dead|alive)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $what =~ /\A(?:dead|alive)\z/ 1 2 3
184  
185 2 my $dead = $game->dead_guess_from(playouts => 60, seed => 1);
186 2 my %dead = map { $_ => 1 } @$dead;
187  
188 2 my @out;
189 2 for my $row (0 .. $game->size - 1) {
190 18 for my $col (0 .. $game->size - 1) {
191 162 my $pt = $game->point($col, $row);
192 162 my $at = $game->board->at($col, $row);
193 162 ▶ next unless Game::Go::Rules::is_colour($at);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($at) 158 4 162
194 4 ▶ my $is_dead = $dead{$pt} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dead{$pt} ? : 0 4 4
195 4 ▶ next unless $what eq ($is_dead ? 'dead' : 'alive');
Branch: 2 branches
BranchTrueFalseTotal
unless $what eq ($is_dead ? "dead" : "alive") 2 2 4
$is_dead ? : 0 4 4
196 2 push @out, Game::Go::Notation::to_human($game->size, $col, $row);
197 }
198 }
199 2 return (1, join ' ', @out);
200 }
201  
202 0 return (0, 'unknown command');
203 }
204  
205 22 ▶sub _colour {
Subroutine _colour: called CC=6 SCAR=18.2
206 22 my ($self, $word) = @_;
207 22 ▶ return undef unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 0 22 22
208 22 my $c = lc $word;
209 22 ▶ return $B if $c eq 'b' || $c eq 'black';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "b" or $c eq "black" 14 8 22
Condition66%2/3
$c eq "b" or $c eq "black"
l!l&&r!l&&!r
1408
MC/DC50%1/2
$c eq "b" or $c eq "black"
$c eq "b" $c eq "black"
Truth table66%2/3
A or B
A= $c eq "b"B= $c eq "black"
ABresult
1X1
011
000
210 8 ▶ return $W if $c eq 'w' || $c eq 'white';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "w" or $c eq "white" 7 1 8
Condition66%2/3
$c eq "w" or $c eq "white"
l!l&&r!l&&!r
701
MC/DC50%1/2
$c eq "w" or $c eq "white"
$c eq "w" $c eq "white"
Truth table66%2/3
A or B
A= $c eq "w"B= $c eq "white"
ABresult
1X1
011
000
211 1 return undef;
212 }
213  
214 1 ▶sub run {
Subroutine run: called CC=4 SCAR=14.0
Pod documented
215 1 my ($self, $in, $out) = @_;
216 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 4 0 4
217 4 my $response = $self->handle($line);
218 4 ▶ next unless length $response;
Branch: 1 branch
BranchTrueFalseTotal
unless length $response 0 4 4
219 4 print {$out} $response;
220 4 ▶ last if $self->quit;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quit 1 3 4
221 }
222 1 return 0;
223 }
224  
225 2 1;
226  
227 __END__
« blib/lib/Game/Go/SGF.pm ↑ Summary blib/lib/Game/Go.pm »