blib/lib/Game/Go/Terminal.pm

filter: Statementstmt 80.5% 444 / 551 Branchbran 66.7% 223 / 334 Conditioncond 67.7% 61 / 90 MC/DCmcdc 49.0% 27 / 55 Subroutinesub 87.0% 47 / 54 Podpod 94.4% 17 / 18 totaltotal 74.3% 819 / 1102 CC 210 SCAR 32.1
CC
210
Cov
75%
run
240.0
pick_point
35.5
_human_marking
19.5
CRAP 24.7
« blib/lib/Game/Go/GTP.pm ↑ Summary blib/lib/Game/Go.pm »
1 package Game::Go::Terminal;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Game::Go;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Game::Go::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Game::Go::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Game::Go::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Game::Go::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.03';
17  
18 2 our %STONE = (
19 wide => {
20 black => "\x{25cf}", white => "\x{25cb}",
21 black_last => "\x{25c9}", white_last => "\x{25ce}",
22 took => "\x{00d7}",
23 star => "\x{254b}",
24 cross => "\x{253c}",
25 tl => "\x{250c}", tr => "\x{2510}", bl => "\x{2514}", br => "\x{2518}",
26 t => "\x{252c}", b => "\x{2534}", l => "\x{251c}", r => "\x{2524}",
27 bar => "\x{2500}",
28 },
29 ascii => {
30 black => 'X', white => 'O',
31 black_last => 'x', white_last => 'o',
32 took => 'x',
33 star => '*',
34 cross => '+',
35 tl => '+', tr => '+', bl => '+', br => '+',
36 t => '+', b => '+', l => '+', r => '+',
37 bar => '-',
38 },
39 );
40  
41 2 my $B = Game::Go::Rules::BLACK;
42 2 my $W = Game::Go::Rules::WHITE;
43  
44 2 has in => (is => 'ro');
45 2 has out => (is => 'ro');
46  
47 2 has size => (is => 'ro', isa => Int, default => 9);
48 2 has level => (is => 'ro', isa => Int, default => 2);
49 2 has colour => (is => 'ro', default => 'b');
50 2 has komi => (is => 'ro');
51 2 has handicap => (is => 'ro', isa => Int, default => 0);
52 2 has seed => (is => 'ro', default => 'terminal');
53  
54 2 has ascii => (is => 'ro', default => 0);
55 2 has quiet => (is => 'ro', default => 0);
56 2 has ansi => (is => 'rw');
57  
58 2 has game => (is => 'rw');
59 2 has bot => (is => 'rw');
60  
61 2 has raw => (is => 'rw', default => 0);
62  
63 2 has keysource => (is => 'rw');
64  
65 45 ▶has pending => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
66  
67 2 has _picking => (is => 'rw', init_arg => 'picking', private => 1);
68  
69 2 our %SEQUENCE = (
70 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
71 'H' => 'home', 'F' => 'end',
72 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
73 '7~' => 'home', '8~' => 'end',
74 );
75  
76 2 our %CONTROL = (
77 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
78 "\x7f" => 'backspace', "\x08" => 'backspace',
79 "\x03" => 'interrupt', "\x04" => 'eof',
80 );
81  
82 2 has _human => (is => 'rw', private => 1);
83 2 has _shown => (is => 'rw', private => 1, default => 0);
84  
85 45 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=27.1
Pod undocumented
86 45 my ($self) = @_;
87  
88 45 my $size = $self->size;
89 die "Game::Go::Terminal: size must be 9, 13 or 19, not '$size'\n"
90 45 ▶ unless grep { $_ == $size } Game::Go->sizes;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $size;} "Game::Go"->sizes 2 43 45
91  
92 43 my $level = $self->level;
93 43 ▶ die "Game::Go::Terminal: level must be 1 to 5, not '$level'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $level >= 1 and $level <= 5 2 41 43
Condition100%3/3
$level >= 1 and $level <= 5
!ll&&!rl&&r
1141
MC/DC100%2/2
$level >= 1 and $level <= 5
$level >= 1 $level <= 5
Truth table100%3/3
A and B
A= $level >= 1B= $level <= 5
ABresult
0X0
100
111
94 unless $level >= 1 && $level <= 5;
95  
96 41 my $colour = lc $self->colour;
97 41 ▶ $colour = 'b' if $colour eq 'black' || $colour eq 'dark';
Branch: 1 branch
BranchTrueFalseTotal
if $colour eq "black" or $colour eq "dark" 3 38 41
Condition100%3/3
$colour eq "black" or $colour eq "dark"
l!l&&r!l&&!r
2138
MC/DC100%2/2
$colour eq "black" or $colour eq "dark"
$colour eq "black" $colour eq "dark"
Truth table100%3/3
A or B
A= $colour eq "black"B= $colour eq "dark"
ABresult
1X1
011
000
98 41 ▶ $colour = 'w' if $colour eq 'white' || $colour eq 'light';
Branch: 1 branch
BranchTrueFalseTotal
if $colour eq "white" or $colour eq "light" 2 39 41
Condition100%3/3
$colour eq "white" or $colour eq "light"
l!l&&r!l&&!r
1139
MC/DC100%2/2
$colour eq "white" or $colour eq "light"
$colour eq "white" $colour eq "light"
Truth table100%3/3
A or B
A= $colour eq "white"B= $colour eq "light"
ABresult
1X1
011
000
99 41 my $who = Game::Go::Rules::from_letter($colour);
100 41 ▶ die "Game::Go::Terminal: colour must be black or white, not '" . $self->colour . "'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $who 1 40 41
101 unless $who;
102 40 $self->_human($who);
103  
104 40 my $most = Game::Go::Rules::max_handicap($size);
105 40 ▶ die "Game::Go::Terminal: handicap must be 0 to $most on a ${size}x$size board\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->handicap < 0 or $self->handicap > $most 1 39 40
Condition66%2/3
$self->handicap < 0 or $self->handicap > $most
l!l&&r!l&&!r
0139
MC/DC50%1/2
$self->handicap < 0 or $self->handicap > $most
$self->handicap < 0 $self->handicap > $most
Truth table66%2/3
A or B
A= $self->handicap < 0B= $self->handicap > $most
ABresult
1X1
011
000
106 if $self->handicap < 0 || $self->handicap > $most;
107  
108 39 ▶ if (defined $self->komi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->komi) 1 38 39
109 1 ▶ die "Game::Go::Terminal: komi must be a multiple of 0.5\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->komi =~ /\A-?[0-9]+(?:\.[05])?\z/ 1 0 1
110 unless $self->komi =~ /\A-?[0-9]+(?:\.[05])?\z/;
111 }
112  
113 38 ▶ $self->game(Game::Go->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $self->komi ? : 0 38 38
114 size => $size,
115 handicap => $self->handicap,
116 (defined $self->komi ? (komi => $self->komi) : ()),
117 seed => $self->seed,
118 ));
119 38 $self->bot(Game::Go::Bot->new(level => $level, seed => $self->seed));
120  
121 38 ▶ $self->ansi($self->_want_ansi) unless defined $self->ansi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ansi 11 27 38
122 38 $self->_encode_out;
123 38 return;
124 }
125  
126 38 ▶sub _encode_out {
Subroutine _encode_out: called CC=5 SCAR=16.1
127 38 my ($self) = @_;
128 38 ▶ return if $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii 27 11 38
129  
130 11 ▶ my $out = $self->out or return;
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or return 2 9 11
131 2 my @layers = eval { PerlIO::get_layers($out) };
132 2 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
133 2 ▶ return if grep { /\A(?:utf8|encoding)/ } @layers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A(?:utf8|encoding)/;} @layers 1 1 2
134  
135 1 binmode $out, ':encoding(UTF-8)';
136 1 return;
137 }
138  
139 11 ▶sub _want_ansi {
Subroutine _want_ansi: called CC=4 SCAR=13.9
140 11 my ($self) = @_;
141 11 ▶ return 0 if exists $ENV{NO_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"NO_COLOR"} 1 10 11
142 10 my $out = $self->out;
143 10 ▶ return 0 unless $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $out 7 3 10
144 3 ▶ return -t $out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $out ? : 0 3 3
145 }
146  
147 1012 ▶sub _say {
Subroutine _say: called CC=4 SCAR=14.2
148 1012 my ($self, @lines) = @_;
149 1012 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 1012 1012
150 1012 ▶ my $out = $self->out or return;
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or return 1012 0 1012
151 1012 print {$out} "$_\n" for @lines;
152 1012 return;
153 }
154  
155 15866 ▶sub _ink {
Subroutine _ink: called CC=2 SCAR=6.9
156 15866 my ($self, $code, $text) = @_;
157 15866 ▶ return $text unless $self->ansi;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ansi 15646 220 15866
158 220 return "\e[${code}m$text\e[0m";
159 }
160  
161 13933 ▶sub glyph {
Subroutine glyph: called CC=12 SCAR=24.9
Pod documented
162 13933 my ($self, $colour, %o) = @_;
163  
164 13933 ▶ my $set = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 13932 1 13933
165 13933 my $stone = $STONE{$set};
166  
167 13933 ▶ if ($colour == $B || $colour == $W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour == $B or $colour == $W) 3478 10455 13933
Condition100%3/3
$colour == $B or $colour == $W
l!l&&r!l&&!r
43343510455
MC/DC100%2/2
$colour == $B or $colour == $W
$colour == $B $colour == $W
Truth table100%3/3
A or B
A= $colour == $BB= $colour == $W
ABresult
1X1
011
000
168 my $face = $o{last}
169 ? ($colour == $B ? $stone->{black_last} : $stone->{white_last})
170 3478 ▶ : ($colour == $B ? $stone->{black} : $stone->{white});
Branch: 3 branches
BranchTrueFalseTotal
$o{'last'} ? : 134 3344 3478
$colour == $B ? : 1 133 134
$colour == $B ? : 42 3302 3344
171 3478 ▶ my $ink = $o{last} ? '1;31' : $colour == $B ? '1;30' : '1;37';
Branch: 2 branches
BranchTrueFalseTotal
$o{'last'} ? : 134 3344 3478
$colour == $B ? : 42 3302 3344
172 3478 return $self->_ink($ink, $face);
173 }
174  
175 return $o{took} ? $self->_ink('1;31', $stone->{took})
176 : $o{star} ? $stone->{star}
177 10455 ▶ : $stone->{$o{line} || 'cross'};
Branch: 2 branches
BranchTrueFalseTotal
$o{'took'} ? : 0 10455 10455
$o{'star'} ? : 612 9843 10455
Condition50%1/2
$o{'line'} || 'cross'
l!l
98430
MC/DC0%0/1
$o{'line'} || 'cross'
$o{'line'}
Truth table50%1/2
A
A= $o{'line'}
Aresult
11
00
178 }
179  
180 172 ▶sub board_text {
Subroutine board_text: called CC=18 SCAR=29.2
Pod documented
181 172 my ($self, %o) = @_;
182 172 my $game = $self->game;
183 172 my $size = $game->size;
184 172 my $board = $game->board;
185  
186 172 my @letters = map { Game::Go::Notation::col_letter($_) } 0 .. $size - 1;
187 172 my $width = length $size;
188 172 $o{stars} = { map { $_ => 1 } @{ $game->star_points } };
189  
190 172 ▶ my $set = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 172 0 172
191 172 my $bar = $STONE{$set}{bar};
192 172 ▶ my %took = map { $_ => 1 } @{ $o{took} || [] };
Condition50%1/2
$o{'took'} || []
l!l
1720
MC/DC0%0/1
$o{'took'} || []
$o{'took'}
Truth table50%1/2
A
A= $o{'took'}
Aresult
11
00
193  
194 172 my @lines;
195 172 push @lines, sprintf('%*s %s', $width, '', join ' ', @letters);
196  
197 172 for my $row (0 .. $size - 1) {
198 1548 my $number = $size - $row;
199 1548 my @cells;
200 1548 for my $col (0 .. $size - 1) {
201 13932 my $at = $board->at($col, $row);
202 13932 my $pt = $game->point($col, $row);
203  
204 13932 ▶ my $on = defined $o{cursor} && $o{cursor} == $pt;
Condition100%3/3
defined $o{'cursor'} && $o{'cursor'} == $pt
!ll&&!rl&&r
12150176022
MC/DC100%2/2
defined $o{'cursor'} && $o{'cursor'} == $pt
defined $o{'cursor'} $o{'cursor'} == $pt
Truth table100%3/3
A && B
A= defined $o{'cursor'}B= $o{'cursor'} == $pt
ABresult
0X0
100
111
205  
206 my $cell = $on && $at == Game::Go::Rules::EMPTY
207 ? $self->_ink('7', $self->glyph($o{to_play} || $B))
208 : $on
209 ? $self->_ink('7', $self->glyph($at))
210 : $self->glyph($at,
211 star => $o{stars}{$pt},
212 took => ($at == Game::Go::Rules::EMPTY && $took{$pt}),
213 13932 ▶ last => (defined $o{last} && $o{last} == $pt),
Branch: 2 branches
BranchTrueFalseTotal
$on && $at == 0 ? : 22 13910 13932
$on ? : 0 13910 13910
Condition66%8/12
$on and $at == 0
!ll&&!rl&&r
13910022
$o{'to_play'} || $B
l!l&&r!l&&!r
2200
$at == 0 && $took{$pt}
!ll&&!rl&&r
3455104550
defined $o{'last'} && $o{'last'} == $pt
!ll&&!rl&&r
305610720134
MC/DC50%1/2
$on and $at == 0
$on $at == 0
MC/DC0%0/2
$o{'to_play'} || $B
$o{'to_play'} $B
MC/DC0%0/2
$at == 0 && $took{$pt}
$at == 0 $took{$pt}
MC/DC100%2/2
defined $o{'last'} && $o{'last'} == $pt
defined $o{'last'} $o{'last'} == $pt
Truth table66%2/3
A and B
A= $onB= $at == 0
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $o{'to_play'}B= $B
ABresult
1X1
011
000
Truth table66%2/3
A && B
A= $at == 0B= $took{$pt}
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= defined $o{'last'}B= $o{'last'} == $pt
ABresult
0X0
100
111
214 line => $self->_line_at($col, $row, $size));
215  
216 13932 push @cells, { text => $cell, cursor => $on };
217 }
218  
219 1548 my $line = '';
220 1548 for my $i (0 .. $#cells) {
221 $line .= $cells[$i]{cursor} ? '['
222 13932 ▶ : $cells[ $i - 1 ]{cursor} ? ']'
Branch: 3 branches
BranchTrueFalseTotal
if $i 12384 1548 13932
$cells[$i]{'cursor'} ? : 20 12364 12384
$cells[$i - 1]{'cursor'} ? : 21 12343 12364
223 : $self->_ink('2', $bar)
224 if $i;
225 13932 $line .= $cells[$i]{text};
226 }
227  
228 push @lines, sprintf('%*d%s%s%s%d', $width, $number,
229 ($cells[0]{cursor} ? '[' : ' '), $line,
230 1548 ▶ ($cells[-1]{cursor} ? ']' : ' '), $number);
Branch: 2 branches
BranchTrueFalseTotal
$cells[0]{'cursor'} ? : 2 1546 1548
$cells[-1]{'cursor'} ? : 1 1547 1548
231 }
232  
233 172 push @lines, sprintf('%*s %s', $width, '', join ' ', @letters);
234 172 return @lines;
235 }
236  
237 13910 ▶sub _line_at {
Subroutine _line_at: called CC=13 SCAR=25.6
238 13910 my ($self, $col, $row, $size) = @_;
239 13910 my $top = $row == 0;
240 13910 my $bottom = $row == $size - 1;
241 13910 my $left = $col == 0;
242 13910 my $right = $col == $size - 1;
243  
244 13910 ▶ return 'tl' if $top && $left;
Branch: 1 branch
BranchTrueFalseTotal
if $top and $left 172 13738 13910
Condition100%3/3
$top and $left
!ll&&!rl&&r
123621376172
MC/DC100%2/2
$top and $left
$top $left
Truth table100%3/3
A and B
A= $topB= $left
ABresult
0X0
100
111
245 13738 ▶ return 'tr' if $top && $right;
Branch: 1 branch
BranchTrueFalseTotal
if $top and $right 172 13566 13738
Condition100%3/3
$top and $right
!ll&&!rl&&r
123621204172
MC/DC100%2/2
$top and $right
$top $right
Truth table100%3/3
A and B
A= $topB= $right
ABresult
0X0
100
111
246 13566 ▶ return 'bl' if $bottom && $left;
Branch: 1 branch
BranchTrueFalseTotal
if $bottom and $left 172 13394 13566
Condition100%3/3
$bottom and $left
!ll&&!rl&&r
120181376172
MC/DC100%2/2
$bottom and $left
$bottom $left
Truth table100%3/3
A and B
A= $bottomB= $left
ABresult
0X0
100
111
247 13394 ▶ return 'br' if $bottom && $right;
Branch: 1 branch
BranchTrueFalseTotal
if $bottom and $right 172 13222 13394
Condition100%3/3
$bottom and $right
!ll&&!rl&&r
120181204172
MC/DC100%2/2
$bottom and $right
$bottom $right
Truth table100%3/3
A and B
A= $bottomB= $right
ABresult
0X0
100
111
248 13222 ▶ return 't' if $top;
Branch: 1 branch
BranchTrueFalseTotal
if $top 1204 12018 13222
249 12018 ▶ return 'b' if $bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $bottom 1204 10814 12018
250 10814 ▶ return 'l' if $left;
Branch: 1 branch
BranchTrueFalseTotal
if $left 1202 9612 10814
251 9612 ▶ return 'r' if $right;
Branch: 1 branch
BranchTrueFalseTotal
if $right 1203 8409 9612
252 8409 return 'cross';
253 }
254  
255 172 ▶sub _show {
Subroutine _show: called CC=1 SCAR=0.0
256 172 my ($self, %o) = @_;
257 172 my $game = $self->game;
258 172 $self->_say('', $self->board_text(%o));
259  
260 172 my $p = $game->prisoners;
261 $self->_say(sprintf(' captures: black %d, white %d komi %s',
262 172 $p->{$B}, $p->{$W}, $game->komi));
263 172 return;
264 }
265  
266  
267 13 ▶sub start {
Subroutine start: called CC=7 SCAR=19.6
Pod documented
268 13 my ($self) = @_;
269 13 my $game = $self->game;
270  
271 13 $self->_intro;
272  
273 13 my $guard = 0;
274 13 ▶ while ($game->status eq 'active' && $guard++ < 4 * $game->size * $game->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "active" and $guard++ < 4 * $game->size * $game->size) 261 2 263
Condition66%2/3
$game->status eq "active" and $guard++ < 4 * $game->size * $game->size
!ll&&!rl&&r
20261
MC/DC50%1/2
$game->status eq "active" and $guard++ < 4 * $game->size * $game->size
$game->status eq "active" $guard++ < 4 * $game->size * $game->size
Truth table66%2/3
A and B
A= $game->status eq "active"B= $guard++ < 4 * $game->size * $game->size
ABresult
0X0
100
111
275 261 my ($who) = $game->waiting_on;
276 261 ▶ last unless defined $who;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $who 0 261 261
277  
278 261 ▶ my $ok = $who == $self->_human ? $self->_human_turn($who)
Branch: 1 branch
BranchTrueFalseTotal
$who == $self->_human ? : 136 125 261
279 : $self->_bot_turn($who);
280 261 ▶ last unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 11 250 261
281 }
282  
283 13 $self->_finish;
284 13 ▶ return $game->outcome || $game->result;
Condition66%2/3
$game->outcome || $game->result
l!l&&r!l&&!r
2110
MC/DC0%0/2
$game->outcome || $game->result
$game->outcome $game->result
Truth table66%2/3
A || B
A= $game->outcomeB= $game->result
ABresult
1X1
011
000
285 }
286  
287 13 ▶sub _intro {
Subroutine _intro: called CC=2 SCAR=7.0
288 13 my ($self) = @_;
289 13 my $game = $self->game;
290 13 ▶ $self->_say(
Branch: 1 branch
BranchTrueFalseTotal
$game->handicap ? : 0 13 13
291 sprintf('Go, %dx%d, komi %s%s.', $game->size, $game->size, $game->komi,
292 $game->handicap ? sprintf(', handicap %d', $game->handicap) : ''),
293 sprintf('You are %s; the bot is level %d.',
294 Game::Go::Rules::colour_name($self->_human), $self->bot->level),
295 'Enter a point such as D4, or: pass, resign, board, libs D4, help.',
296 );
297 13 return;
298 }
299  
300 478 ▶sub _prompt {
Subroutine _prompt: called CC=5 SCAR=16.3
301 478 my ($self, $text) = @_;
302 478 my $out = $self->out;
303 478 ▶ print {$out} $text unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 478 0 478
304 478 ▶ my $in = $self->in or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $in = $self->in or return undef 478 0 478
305 478 my $line = <$in>;
306 478 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 477 478
307  
308 477 ▶ print {$out} "\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 477 0 477
309  
310 477 chomp $line;
311 477 return $line;
312 }
313  
314 133 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
315 133 my ($self) = @_;
316 133 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 133 133
317 133 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 133 0 133
318 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
319 }
320  
321 135 ▶sub picking {
Subroutine picking: called CC=6 SCAR=18.1
Pod documented
322 135 my ($self, @set) = @_;
323 135 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 1 134 135
$set[0] ? : 0 1 1
324 135 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 135 135
325 135 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 4 131 135
$self->_picking ? : 1 3 4
326 131 return $self->keys_available;
327 }
328  
329 10 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=20.4
Pod documented
330 10 my ($self) = @_;
331 10 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 10 10
332 10 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 9 1 10
333 1 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 1 0 1
334 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
335 0 $self->raw(1);
336 0 return $self;
337 }
338  
339 0 ▶sub leave_raw {
Subroutine leave_raw: not called CC=3 SCAR=24.8
Pod documented
340 0 my ($self) = @_;
341 0 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 0 0
342 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
343 0 $self->raw(0);
344 0 return $self;
345 }
346  
347 83 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
348 83 my ($self, $wait) = @_;
349 83 my $pending = $self->pending;
350 83 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 81 83
351 81 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 81 0 81
352 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
353 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
354 0 select undef, undef, undef, 0.05;
355 0 return Term::ReadKey::ReadKey(-1, $self->in);
356 }
357  
358 41 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
359 41 my ($self) = @_;
360 41 my $char = $self->read_char(1);
361 41 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 40 41
362 40 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Go::Terminal::CONTROL{$char} 6 34 40
363 34 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 21 13 34
364 13 return $char;
365 }
366  
367 21 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
368 21 my ($self) = @_;
369 21 my $opener = $self->read_char(0);
370 21 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 21 21
371 21 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 19 21
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
1812
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
372 2 unshift @{ $self->pending }, $opener;
373 2 return 'escape';
374 }
375 19 my $tail = '';
376 19 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 21 0 21
377 21 my $char = $self->read_char(0);
378 21 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 21 21
379 21 $tail .= $char;
380 21 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 19 2 21
381 }
382 19 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Go::Terminal::SEQUENCE{$tail} || 'escape'
l!l
190
MC/DC0%0/1
$Game::Go::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Go::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Go::Terminal::SEQUENCE{$tail}
Aresult
11
00
383 }
384  
385 27 ▶sub preview {
Subroutine preview: called CC=4 SCAR=13.9
Pod documented
386 27 my ($self, $who, $pt) = @_;
387  
388 27 my $game = $self->game;
389 27 my ($col, $row) = $game->col_row($pt);
390 27 my $at = $game->board->at($col, $row);
391  
392 27 ▶ return { refused => 'there is a stone there' }
Branch: 1 branch
BranchTrueFalseTotal
if $at != 0 1 26 27
393 if $at != Game::Go::Rules::EMPTY;
394  
395 26 my $trial = $game->clone;
396 26 my $move = $trial->play($who, $pt);
397 26 ▶ return { refused => $move->message } if ref $move eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $move eq "Game::Go::Error" 1 25 26
398  
399 return {
400 move => $move,
401 25 ▶ caps => scalar @{ $move->caps || [] },
Condition50%1/2
$move->caps || []
l!l
250
MC/DC0%0/1
$move->caps || []
$move->caps
Truth table50%1/2
A
A= $move->caps
Aresult
11
00
402 libs => $trial->board->libs($col, $row),
403 size => $trial->board->chain_size($col, $row),
404 };
405 }
406  
407 27 ▶sub point_line {
Subroutine point_line: called CC=5 SCAR=16.1
Pod documented
408 27 my ($self, $who, $pt) = @_;
409  
410 27 my $game = $self->game;
411 27 my $name = Game::Go::Notation::to_human($game->size, $game->col_row($pt));
412 27 my $peek = $self->preview($who, $pt);
413  
414 27 ▶ return "$name: " . $peek->{refused} if $peek->{refused};
Branch: 1 branch
BranchTrueFalseTotal
if $peek->{"refused"} 2 25 27
415  
416 25 my $line = "$name: ";
417 25 ▶ $line .= sprintf 'takes %d, ', $peek->{caps} if $peek->{caps};
Branch: 1 branch
BranchTrueFalseTotal
if $peek->{"caps"} 1 24 25
418 $line .= $peek->{size} == 1
419 ? sprintf('a lone stone with %d liberties', $peek->{libs})
420 : sprintf('joins a group of %d with %d liberties',
421 25 ▶ $peek->{size}, $peek->{libs});
Branch: 1 branch
BranchTrueFalseTotal
$peek->{'size'} == 1 ? : 24 1 25
422 25 ▶ $line .= $self->_ink('1;31', ' (atari)') if $peek->{libs} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $peek->{"libs"} == 1 0 25 25
423  
424 25 return $line;
425 }
426  
427 22 ▶sub interactive {
Subroutine interactive: called CC=3 SCAR=11.4
Pod documented
428 22 my ($self) = @_;
429 22 ▶ my $out = $self->out or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or return 0 22 0 22
430 22 ▶ return (eval { -t $out }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $out } } ? : 0 22 22
431 }
432  
433 22 ▶sub clear {
Subroutine clear: called CC=2 SCAR=10.9
Pod documented
434 22 my ($self) = @_;
435 22 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 22 0 22
436 0 my $out = $self->out;
437 0 print {$out} "\e[2J\e[3J\e[H";
438 0 return $self;
439 }
440  
441 9 ▶sub pick_point {
Subroutine pick_point: called CC=21 SCAR=35.7
Pod documented
442 9 my ($self, $who) = @_;
443  
444 9 my $game = $self->game;
445 9 my $size = $game->size;
446 9 my $at = $self->_last_point;
447 9 ▶ $at = $game->point(int($size / 2), int($size / 2)) unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 9 0 9
448  
449 9 my ($col, $row) = $game->col_row($at);
450 9 my @notice;
451  
452 9 while (1) {
453 22 my $pt = $game->point($col, $row);
454 22 $self->clear;
455 22 $self->_show($self->_since, cursor => $pt, to_play => $who);
456 22 $self->_say(' ' . $self->point_line($who, $pt));
457 22 $self->_say(' ' . $self->_ink('2', $_)) for @notice;
458 22 @notice = ();
459 22 $self->_say(' ' . $self->_ink('2',
460 'arrows to move, enter to play, p pass, ? for the keys'));
461  
462 22 my $key = $self->read_key;
463 22 ▶ return 0 if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 0 22 22
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0022
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0022
MC/DC0%0/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
464  
465 22 ▶ if ($key eq 'left') { $col = ($col - 1) % $size; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "left") 8 14 22
466 14 ▶ if ($key eq 'right') { $col = ($col + 1) % $size; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "right") 4 10 14
467 10 ▶ if ($key eq 'up') { $row = ($row - 1) % $size; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "up") 0 10 10
468 10 ▶ if ($key eq 'down') { $row = ($row + 1) % $size; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "down") 0 10 10
469 10 ▶ if ($key eq 'home') { $col = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home") 0 10 10
470 10 ▶ if ($key eq 'end') { $col = $size - 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end") 0 10 10
471  
472 10 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 1 9 10
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
109
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
473 1 my $move = $game->play($who, $pt);
474 1 ▶ if (ref $move eq 'Game::Go::Error') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "Game::Go::Error") 0 1 1
475 0 @notice = ($move->message);
476 0 next;
477 }
478 1 $self->_say(sprintf(' you play %s%s.',
479 Game::Go::Notation::to_human($size, $col, $row),
480 $self->_took($move)));
481 1 return 1;
482 }
483  
484 9 ▶ if ($key eq 'p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "p") 1 8 9
485 1 my $out = $game->pass($who);
486 1 ▶ return $self->_refused($out) if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 1 1
487 1 $self->_say(' you pass.');
488 1 return 1;
489 }
490  
491 8 ▶ if ($key eq 'r') { $game->resign($who); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "r") 0 8 8
492 8 ▶ if ($key eq 'q') { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 7 1 8
493  
494 1 ▶ if ($key eq 't') { $self->picking(0); return $self->_typed_turn($who) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t") 0 1 1
495  
496 1 ▶ if ($key eq '?') { @notice = $self->pick_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 1 1
497  
498 1 @notice = ('that key does nothing here. ? for the ones that do');
499 }
500 }
501  
502 0 ▶sub pick_help {
Subroutine pick_help: not called CC=1 SCAR=6.9
Pod documented
503 return (
504 0 'arrows move the cursor, enter plays the point it is on.',
505 'the line under the board says what that move would do.',
506 'p passes, r resigns, t goes back to typing, q stops.',
507 );
508 }
509  
510 136 ▶sub _human_turn {
Subroutine _human_turn: called CC=4 SCAR=13.9
511 136 my ($self, $who) = @_;
512 136 my $game = $self->game;
513  
514 136 ▶ return $self->_human_marking($who) if $game->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $game->phase eq "marking" 3 133 136
515  
516 133 ▶ if ($self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking) 1 132 133
517 1 ▶ return $self->pick_point($who) if $self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enter_raw 0 1 1
518 1 $self->picking(0);
519 }
520  
521 133 return $self->_typed_turn($who);
522 }
523  
524 133 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=11 SCAR=24.0
525 133 my ($self, $who) = @_;
526 133 my $game = $self->game;
527  
528 133 $self->_show($self->_since);
529  
530 133 while (1) {
531 139 my $line = $self->_prompt('your move> ');
532 139 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 138 139
533 138 $line =~ s/\A\s+|\s+\z//g;
534 138 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 138 138
535  
536 138 ▶ if ($line =~ /\Ahelp\z/i) { $self->_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Ahelp\z/i) 1 137 138
537 137 ▶ if ($line =~ /\Aboard\z/i) { $self->_show($self->_since); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Aboard\z/i) 1 136 137
538 136 ▶ if ($line =~ /\Alibs\s+(\S+)\z/i) { $self->_libs($1); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Alibs\s+(\S+)\z/i) 2 134 136
539 134 ▶ if ($line =~ /\A(?:resign|quit)\z/i) { $game->resign($who); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A(?:resign|quit)\z/i) 10 124 134
540  
541 124 ▶ if ($line =~ /\Apass\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Apass\z/i) 114 10 124
542 114 my $out = $game->pass($who);
543 114 ▶ return $self->_refused($out) if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 114 114
544 114 $self->_say(' you pass.');
545 114 return 1;
546 }
547  
548 10 my ($col, $row) = Game::Go::Notation::from_human($game->size, $line);
549 10 ▶ unless (defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col) 1 9 10
550 1 $self->_say(" '$line' is not a point on this board. Try D4, or help.");
551 1 next;
552 }
553  
554 9 my $move = $game->play($who, $game->point($col, $row));
555 9 ▶ if (ref $move eq 'Game::Go::Error') { $self->_say(' ' . $move->message); next }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "Game::Go::Error") 1 8 9
556  
557 8 $self->_say(sprintf(' you play %s%s.', uc $line, $self->_took($move)));
558 8 return 1;
559 }
560 }
561  
562 125 ▶sub _bot_turn {
Subroutine _bot_turn: called CC=10 SCAR=24.6
563 125 my ($self, $who) = @_;
564 125 my $game = $self->game;
565  
566 125 my $move = $self->bot->choose($game, $who);
567 125 ▶ unless ($move) { $self->_say(' the bot has nothing to play.'); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 0 125 125
568  
569 125 my $name = Game::Go::Rules::colour_name($who);
570  
571 125 ▶ if ($move->kind eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->kind eq "pass") 2 123 125
572 2 $game->pass($who);
573 2 $self->_say(" $name passes.");
574 2 return 1;
575 }
576 123 ▶ if ($move->kind eq 'play') {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->kind eq "play") 121 2 123
577 121 my $out = $game->play($who, $move->point);
578 121 ▶ return $self->_refused($out) if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 121 121
579 121 my ($col, $row) = $game->col_row($move->point);
580 121 $self->_say(sprintf(' %s plays %s%s.', $name,
581 Game::Go::Notation::to_human($game->size, $col, $row), $self->_took($out)));
582 121 return 1;
583 }
584  
585 2 ▶ my $answer =
Branch: 4 branches
BranchTrueFalseTotal
$move->kind eq 'mark' ? : 0 2 2
$move->kind eq 'done' ? : 0 2 2
$move->kind eq 'accept' ? : 2 0 2
$move->kind eq 'dispute' ? : 0 0 0
586 $move->kind eq 'mark' ? $game->mark($who, $move->point)
587 : $move->kind eq 'done' ? $game->done($who)
588 : $move->kind eq 'accept' ? $game->accept($who)
589 : $move->kind eq 'dispute' ? $game->dispute($who)
590 : undef;
591 2 ▶ return $self->_refused($answer) if ref $answer eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $answer eq "Game::Go::Error" 0 2 2
592  
593 2 $self->_say(" $name: " . $self->_marking_words($move->kind));
594 2 return 1;
595 }
596  
597 130 ▶sub _took {
Subroutine _took: called CC=3 SCAR=17.3
598 130 my ($self, $move) = @_;
599 130 ▶ return '' unless ref $move eq 'Game::Go::Move' && $move->captured;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "Game::Go::Move" and $move->captured 130 0 130
Condition33%1/3
ref $move eq "Game::Go::Move" and $move->captured
!ll&&!rl&&r
01300
MC/DC0%0/2
ref $move eq "Game::Go::Move" and $move->captured
ref $move eq "Game::Go::Move" $move->captured
Truth table33%1/3
A and B
A= ref $move eq "Game::Go::Move"B= $move->captured
ABresult
0X0
100
111
600 0 my $game = $self->game;
601 my @names = map {
602 0 Game::Go::Notation::to_human($game->size, $game->col_row($_))
603 0 } @{ $move->caps };
604 0 return sprintf(', taking %s', join ' ', @names);
605 }
606  
607 181 ▶sub _last_point {
Subroutine _last_point: called CC=3 SCAR=11.0
608 181 my ($self) = @_;
609 181 for my $e (reverse @{ $self->game->log }) {
610 223 ▶ return $e->{payload}{pt} if $e->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"kind"} eq "play" 134 89 223
611 }
612 47 return undef;
613 }
614  
615 172 ▶sub _last_took {
Subroutine _last_took: called CC=4 SCAR=13.9
616 172 my ($self) = @_;
617 172 for my $e (reverse @{ $self->game->log }) {
618 214 ▶ return $e->{payload}{caps} || [] if $e->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"kind"} eq "play" 134 80 214
Condition50%1/2
$e->{'payload'}{'caps'} || []
l!l
1340
MC/DC0%0/1
$e->{'payload'}{'caps'} || []
$e->{'payload'}{'caps'}
Truth table50%1/2
A
A= $e->{'payload'}{'caps'}
Aresult
11
00
619 }
620 38 return [];
621 }
622  
623 172 ▶sub _since {
Subroutine _since: called CC=1 SCAR=0.0
624 172 my ($self) = @_;
625 172 return (last => $self->_last_point, took => $self->_last_took);
626 }
627  
628 0 ▶sub _refused {
Subroutine _refused: not called CC=2 SCAR=17.9
629 0 my ($self, $out) = @_;
630 0 ▶ $self->_say(' ' . $out->message) if ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 0 0
631 0 return 0;
632 }
633  
634 2 ▶sub _libs {
Subroutine _libs: called CC=3 SCAR=11.1
635 2 my ($self, $name) = @_;
636 2 my $game = $self->game;
637 2 my ($col, $row) = Game::Go::Notation::from_human($game->size, $name);
638 2 ▶ unless (defined $col) { $self->_say(" '$name' is not a point."); return }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col) 0 2 2
639  
640 2 my $board = $game->board;
641 2 my $at = $board->at($col, $row);
642 2 ▶ if ($at == Game::Go::Rules::EMPTY) { $self->_say(' ' . uc($name) . ' is empty.'); return }
Branch: 1 branch
BranchTrueFalseTotal
if ($at == 0) 1 1 2
643  
644 1 $self->_say(sprintf(' %s is a %s group of %d, with %d liberties.',
645 uc $name, Game::Go::Rules::colour_name($at),
646 $board->chain_size($col, $row), $board->libs($col, $row)));
647 1 return;
648 }
649  
650 1 ▶sub _help {
Subroutine _help: called CC=1 SCAR=0.0
651 1 my ($self) = @_;
652 1 $self->_say(
653 '',
654 ' D4 play there. Columns are A to T with I left out,',
655 ' and rows are numbered from the bottom.',
656 ' pass pass. Two passes in a row stop play.',
657 ' board draw the board again.',
658 ' libs D4 how many liberties that group has.',
659 ' resign give up.',
660 '',
661 );
662 1 return;
663 }
664  
665 2 ▶sub _marking_words {
Subroutine _marking_words: called CC=6 SCAR=22.8
666 2 my ($self, $kind) = @_;
667 2 ▶ return 'marks a group as dead' if $kind eq 'mark';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "mark" 0 2 2
668 2 ▶ return 'unmarks a group' if $kind eq 'unmark';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "unmark" 0 2 2
669 2 ▶ return 'offers that as the count' if $kind eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "done" 0 2 2
670 2 ▶ return 'agrees, and the game is over' if $kind eq 'accept';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "accept" 2 0 2
671 0 ▶ return 'disagrees, so play resumes' if $kind eq 'dispute';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "dispute" 0 0 0
672 0 return $kind;
673 }
674  
675 3 ▶sub _human_marking {
Subroutine _human_marking: called CC=13 SCAR=29.7
676 3 my ($self, $who) = @_;
677 3 my $game = $self->game;
678 3 my $m = $game->marking;
679  
680 3 ▶ unless ($self->_shown) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_shown) 3 0 3
681 3 $self->_say(
682 '',
683 ' Both players passed, so play has STOPPED. The game is not over yet:',
684 ' before it can be counted you have to agree which stones are dead.',
685 );
686 3 $self->_shown(1);
687 }
688  
689 3 $self->_show($self->_since);
690  
691 3 ▶ if (!$m->proposed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->proposed) 2 1 3
692 2 $self->_say(
693 sprintf(' It is your count to offer. Marked dead so far: %s.',
694 $self->_dead_words),
695 ' Type a point to mark that group dead, or "done" to offer the count.',
696 );
697 2 while (1) {
698 338 my $line = $self->_prompt('mark> ');
699 338 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 338 338
700 338 $line =~ s/\A\s+|\s+\z//g;
701 338 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 338 338
702 338 ▶ if ($line =~ /\Adone\z/i) { $game->done($who); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Adone\z/i) 2 336 338
703 336 ▶ if ($line =~ /\Aboard\z/i) { $self->_show($self->_since); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Aboard\z/i) 0 336 336
704  
705 336 my ($col, $row) = Game::Go::Notation::from_human($game->size, $line);
706 336 ▶ unless (defined $col) { $self->_say(" '$line' is not a point."); next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $col) 336 0 336
707 0 my $out = $game->mark($who, $game->point($col, $row));
708 0 ▶ if (ref $out eq 'Game::Go::Error') { $self->_say(' ' . $out->message); next }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out eq "Game::Go::Error") 0 0 0
709 0 $self->_say(sprintf(' %s: %s', uc $line, $self->_marking_words($out->kind)));
710 0 $self->_say(' Marked dead: ' . $self->_dead_words);
711 }
712 }
713  
714 $self->_say(
715 1 sprintf(' The bot offers this count. Marked dead: %s.', $self->_dead_words),
716 ' Type "accept" to agree and finish, or "dispute" to put it back on the',
717 ' board and play it out. Disputing costs you the move: your opponent',
718 ' plays first.',
719 );
720 1 while (1) {
721 1 my $line = $self->_prompt('accept or dispute> ');
722 1 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1 1
723 1 $line =~ s/\A\s+|\s+\z//g;
724 1 ▶ if ($line =~ /\Aaccept\z/i) { $game->accept($who); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Aaccept\z/i) 0 1 1
725 1 ▶ if ($line =~ /\Adispute\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\Adispute\z/i) 1 0 1
726 1 my $out = $game->dispute($who);
727 1 ▶ if (ref $out eq 'Game::Go::Error') { $self->_say(' ' . $out->message); next }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out eq "Game::Go::Error") 0 1 1
728 1 $self->_shown(0);
729 1 $self->_say(' Back to the board, and your opponent has the move.');
730 1 return 1;
731 }
732 0 $self->_say(' "accept" or "dispute".');
733 }
734 }
735  
736 3 ▶sub _dead_words {
Subroutine _dead_words: called CC=3 SCAR=12.0
737 3 my ($self) = @_;
738 3 my $game = $self->game;
739 3 ▶ my $m = $game->marking or return 'nothing';
Branch: 1 branch
BranchTrueFalseTotal
my $m = $game->marking or return "nothing" 3 0 3
740 3 my @points = @{ $m->dead_points };
741 3 ▶ return 'nothing' unless @points;
Branch: 1 branch
BranchTrueFalseTotal
unless @points 3 0 3
742 return join ' ', map {
743 0 Game::Go::Notation::to_human($game->size, $game->col_row($_))
744 } @points;
745 }
746  
747  
748 13 ▶sub _finish {
Subroutine _finish: called CC=10 SCAR=23.9
749 13 my ($self) = @_;
750 13 my $game = $self->game;
751 13 $self->_show($self->_since);
752  
753 13 my $r = $game->outcome;
754 13 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 11 2 13
755 11 ▶ $self->_say('', ' ' . ($game->result || 'unfinished') . '.');
Condition100%2/2
$game->result || 'unfinished'
l!l
101
MC/DC100%1/1
$game->result || 'unfinished'
$game->result
Truth table100%2/2
A
A= $game->result
Aresult
11
00
756 11 return;
757 }
758  
759 2 my $t = $r->territory;
760 2 my $p = $r->prisoners;
761 $self->_say(
762 '',
763 sprintf(' black %3d territory -%3d prisoners%s = %s',
764 $t->{$B} // 0, $p->{$W} // 0, ' ' x 12, $r->scores->{$B}),
765 sprintf(' white %3d territory -%3d prisoners + %s komi = %s',
766 2 ▶ $t->{$W} // 0, $p->{$B} // 0, $r->komi, $r->scores->{$W}),
Condition50%4/8
$t->{$B} // 0
l!l
20
$p->{$W} // 0
l!l
20
$t->{$W} // 0
l!l
20
$p->{$B} // 0
l!l
20
MC/DC0%0/1
$t->{$B} // 0
$t->{$B}
MC/DC0%0/1
$p->{$W} // 0
$p->{$W}
MC/DC0%0/1
$t->{$W} // 0
$t->{$W}
MC/DC0%0/1
$p->{$B} // 0
$p->{$B}
Truth table50%1/2
A
A= $t->{$B}
Aresult
11
00
Truth table50%1/2
A
A= $p->{$W}
Aresult
11
00
Truth table50%1/2
A
A= $t->{$W}
Aresult
11
00
Truth table50%1/2
A
A= $p->{$B}
Aresult
11
00
767 '',
768 ' ' . $r->stringify . '.',
769 );
770  
771 2 ▶ $self->_say(' The count was not agreed, so it was scored by area.')
Branch: 1 branch
BranchTrueFalseTotal
if ($r->scored_by || "") eq "area" 0 2 2
Condition50%1/2
$r->scored_by || ''
l!l
20
MC/DC0%0/1
$r->scored_by || ''
$r->scored_by
Truth table50%1/2
A
A= $r->scored_by
Aresult
11
00
772 if ($r->scored_by || '') eq 'area';
773  
774 $self->_say(' A negative score means the game was long past resigning.')
775 2 ▶ if grep { $_ < 0 } values %{ $r->scores };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ < 0;} values %{$r->scores;} 0 2 2
776  
777 2 return;
778 }
779  
780 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
781 0 return <<'USAGE';
782 go - play Go against the bot
783  
784 go [options]
785  
786 --size N 9, 13 or 19 (default 9)
787 --level N 1 to 5 (default 2)
788 --colour C black or white (default black)
789 --komi N a multiple of 0.5 (default 6.5, or 0.5 with a handicap)
790 --handicap N 2 to 9 on 19x19, 2 to 5 on the smaller boards
791 --seed S the seed the bot plays from
792 --ascii X and O rather than stones
793 --no-pick type the points out instead of pointing at them
794 --help
795  
796 Columns are A to T with I left out, and rows are numbered from the bottom, so
797 the bottom left point is A1. Two passes STOP play; the game is not over until
798 both players have agreed which stones are dead.
799  
800 On a terminal the arrow keys walk a cursor over the board and the point under
801 it is bracketed, showing the stone you would play there. The line under the
802 board says what that move would do: what it takes, how many liberties it
803 would have, or why it cannot be played. Enter plays it, p passes, r resigns,
804 t goes back to typing and ? lists the keys. Needs Term::ReadKey; without it,
805 or off a terminal, the points are typed.
806  
807 The last move is a ringed stone and a point a capture has just emptied is an
808 x, so both are visible with --ascii and with colour off.
809  
810 Exit status is 0 for a win, 1 for a loss, 2 for a bad option.
811 USAGE
812 }
813  
814 0 ▶sub run {
Subroutine run: not called CC=15 SCAR=54.8
Pod documented
815 0 my ($class, @argv) = @_;
816 0 my %opt = (
817 size => 9,
818 level => 2,
819 colour => 'black',
820 handicap => 0,
821 seed => 'terminal',
822 );
823  
824 0 my $ok = eval {
825 0 ▶ local $SIG{__WARN__} = sub { print STDERR "go: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
826 0 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
827 'size=i', 'level=i', 'colour|color=s', 'komi=s',
828 'handicap=i', 'seed=s', 'ascii', 'pick!', 'help|h',
829 );
830 };
831 0 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
832 0 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 0 0 0
833 0 ▶ if (@argv) { print STDERR "go: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
834  
835 0 my $terminal = eval {
836 $class->new(
837 in => \*STDIN,
838 out => \*STDOUT,
839 size => $opt{size},
840 level => $opt{level},
841 colour => $opt{colour},
842 (defined $opt{komi} ? (komi => $opt{komi}) : ()),
843 handicap => $opt{handicap},
844 seed => $opt{seed},
845 ascii => ($opt{ascii} ? 1 : 0),
846 0 ▶ (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 4 branches
BranchTrueFalseTotal
defined $opt{'komi'} ? : 0 0 0
$opt{'ascii'} ? : 0 0 0
defined $opt{'pick'} ? : 0 0 0
$opt{'pick'} ? : 0 0 0
847 );
848 };
849 0 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 0 0 0
850 0 ▶ my $why = $@ || "could not start\n";
Condition0%0/2
$@ || "could not start\n"
l!l
00
MC/DC0%0/1
$@ || "could not start\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
851 0 $why =~ s/\AGame::Go::Terminal: //;
852 0 print STDERR "go: $why";
853 0 return 2;
854 }
855  
856 0 local @SIG{qw(INT TERM HUP)};
857 0 for my $sig (qw(INT TERM HUP)) {
858 0 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
859 }
860  
861 0 my $result = eval { $terminal->start };
862 0 my $died = $@;
863 0 $terminal->leave_raw;
864 0 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 0 0
865  
866 0 my $game = $terminal->game;
867 0 ▶ return 0 unless defined $game->winner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game->winner 0 0 0
868  
869 0 ▶ my $human = lc($opt{colour}) =~ /\Aw/ ? Game::Go::WHITE : Game::Go::BLACK;
Branch: 1 branch
BranchTrueFalseTotal
lc($opt{'colour'}) =~ /\Aw/ ? : 0 0 0
870 0 ▶ return $game->winner == $human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$game->winner == $human ? : 0 0 0
871 }
872  
873  
874 2 1;
875  
876 __END__
« blib/lib/Game/Go/GTP.pm ↑ Summary blib/lib/Game/Go.pm »