blib/lib/Game/Go/Terminal.pm

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