blib/lib/Game/Go/Terminal.pm

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