blib/lib/Game/Fanorona/Terminal.pm

filter: Statementstmt 95.4% 913 / 957 Branchbran 83.2% 428 / 514 Conditioncond 67.4% 114 / 169 MC/DCmcdc 47.1% 49 / 104 Subroutinesub 98.9% 91 / 92 Podpod 93.3% 14 / 15 totaltotal 86.9% 1609 / 1851 CC 344 SCAR 25.2
CC
344
Cov
89%
pick
44.7
BUILD
23.0
_pick_ended
20.3
CRAP 12.4
« blib/lib/Game/Fanorona/Notation.pm ↑ Summary blib/lib/Game/Fanorona/Rules.pm »
1 package Game::Fanorona::Terminal;
2  
3 17 ▶use 5.014;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 17 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 17 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 17 ▶use Game::Fanorona;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 17 ▶use Game::Fanorona::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 17 ▶use Game::Fanorona::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 17 ▶use Game::Fanorona::Notation qw(parse_position move_points);
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 17 our $VERSION = '0.01';
16  
17 17 our (@COMMANDS, @KEYS, %SEQUENCE, %CONTROL, %DIRECTION, %GROUND, %INK, %GLYPH, %FRAME, %ENDING, @RULES, @LEVEL);
18  
19 17 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 @COMMANDS = (
21 [ 'd2-e3A' => 'play a move: every point the stone stands on, A or W after a step that takes' ],
22 [ 'moves' => 'the first step of every legal move, and what each would take' ],
23 [ 'hint' => 'what the program would play here' ],
24 [ 'board' => 'draw the board again' ],
25 [ 'level N' => 'how hard the program plays, 1 to 5' ],
26 [ 'side WHO' => 'white, black, both or neither: which side is yours' ],
27 [ 'new' => 'start another game; new telo or new dimy for a smaller board' ],
28 [ 'save FILE' => 'write the game to a file' ],
29 [ 'load FILE' => 'read a game back from a file' ],
30 [ 'draw' => 'offer a draw; accept and decline answer one' ],
31 [ 'resign' => 'give the game up' ],
32 [ 'rules' => 'the rules of the game' ],
33 [ 'help' => 'this list' ],
34 [ 'quit' => 'stop playing' ],
35 );
36  
37 17 @KEYS = (
38 [ 'arrows' => 'walk the cursor over the stones that can move; then over the points' ],
39 [ '' => 'the stone picked up may step to, diagonals too: up, then right' ],
40 [ 'enter' => 'pick the stone up; step to the point the cursor is on; and with' ],
41 [ '' => 'the cursor on no point, play the move as it stands' ],
42 [ 'y u b n' => 'step at once: up-left, up-right, down-left, down-right' ],
43 [ '7 9 1 3' => 'the same four on a numeric pad, with 8 4 6 2 for up, left, right, down' ],
44 [ 'space' => 'whatever enter would do' ],
45 [ 'tab' => 'the next stone that can move, in the order they are drawn' ],
46 [ 'a w' => 'when a step can take two ways: the row ahead, or the row behind' ],
47 [ 'backspace' => 'take the cursor off a point; then take back one step' ],
48 [ 'escape' => 'put the stone down where it started' ],
49 [ 'h' => 'show what the program would play' ],
50 [ 'r' => 'the rules' ],
51 [ ':' => 'type a command' ],
52 [ 'q' => 'stop playing' ],
53 );
54  
55 17 %SEQUENCE = (
56 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
57 'H' => 'home', 'F' => 'end',
58 '1~' => 'home', '4~' => 'end', '7~' => 'home', '8~' => 'end',
59 'Z' => 'back_tab',
60 );
61  
62 17 %CONTROL = (
63 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
64 "\x7f" => 'backspace', "\x08" => 'backspace',
65 "\x03" => 'interrupt', "\x04" => 'eof',
66 );
67  
68 17 %DIRECTION = (
69 up => [ 0, 1 ], down => [ 0, -1 ], left => [ -1, 0 ], right => [ 1, 0 ],
70 y => [ -1, 1 ], u => [ 1, 1 ], b => [ -1, -1 ], n => [ 1, -1 ],
71 8 => [ 0, 1 ], 2 => [ 0, -1 ], 4 => [ -1, 0 ], 6 => [ 1, 0 ],
72 7 => [ -1, 1 ], 9 => [ 1, 1 ], 1 => [ -1, -1 ], 3 => [ 1, -1 ],
73 );
74  
75 17 %GROUND = (
76 board => '48;5;137',
77 cursor => '48;5;33',
78 moving => '48;5;28',
79 reach => '48;5;37',
80 taken => '48;5;160',
81 trail => '48;5;130',
82 approach => '48;5;160',
83 withdrawal => '48;5;91',
84 );
85  
86 17 %INK = (
87 white => '1;38;5;231',
88 black => '1;38;5;16',
89 line => '38;5;94',
90 cursor => '1;38;5;231',
91 moving => '1;38;5;231',
92 reach => '1;38;5;16',
93 taken => '1;38;5;231',
94 trail => '1;38;5;231',
95 approach => '1;38;5;231',
96 withdrawal => '1;38;5;231',
97 title => '1;38;5;220',
98 good => '1;38;5;34',
99 bad => '1;38;5;160',
100 quiet => '38;5;245',
101 key => '1;38;5;33',
102 warn => '1;38;5;208',
103 );
104  
105 17 %GLYPH = (
106 wide => { white => "\x{25CB}", black => "\x{25CF}", empty => '+', taken => "\x{2715}" },
107 ascii => { white => 'O', black => 'X', empty => '+', taken => '*' },
108 );
109  
110 17 %FRAME = (
111 cursor => [ '[', ']' ],
112 moving => [ '<', '>' ],
113 reach => [ '(', ')' ],
114 taken => [ '{', '}' ],
115 trail => [ ':', ':' ],
116 approach => [ '{', '}' ],
117 withdrawal => [ '{', '}' ],
118 );
119  
120 17 %ENDING = (
121 stones => '%2$s has no stone left. %1$s wins.',
122 no_moves => '%2$s has no move left, and so has lost. %1$s wins.',
123 resign => '%2$s resigns. %1$s wins.',
124 repetition => 'The same position has come round once too often. The game is drawn.',
125 quiet => 'Nothing has been taken for %3$d moves. The game is drawn.',
126 ply_cap => 'The game has run its full length with no end. It is drawn.',
127 agreement => 'A draw, by agreement.',
128 );
129  
130 17 @LEVEL = (
131 'takes the most it can and looks no further',
132 'looks a little way ahead',
133 'looks ahead, and misses things',
134 'hard to beat',
135 'the strongest here',
136 );
137  
138 17 @RULES = (
139 'Fanorona is the game of Madagascar. The points are joined by lines, and a',
140 'stone steps one point along a line. Every point has the lines up, down and',
141 'sideways; only the points where a diagonal is drawn have those as well.',
142 '',
143 'TAKING BY APPROACH. Step a stone up to an enemy stone, so that the enemy is',
144 'on the next point of the same line, and it is taken, with every enemy stone',
145 'standing in an unbroken row behind it.',
146 '',
147 'TAKING BY WITHDRAWAL. Step a stone straight away from an enemy stone it stood',
148 'beside, along the line that joins them, and the same row is taken.',
149 '',
150 'A step that could take both ways takes one way only, and the mover says which.',
151 '',
152 'If any stone can take, the move must take. Which stone, and which way, is the',
153 'mover\'s choice. After taking, the same stone may take again, and again: each',
154 'further step must take, must not run in the direction of the step before it,',
155 'and must not come back to a point the stone has stood on in this move. The',
156 'mover may stop after any step that has taken.',
157 '',
158 'A step that takes nothing is allowed only when nothing can be taken.',
159 '',
160 'White moves first. A side with no stone left has lost, and so has a side that',
161 'cannot move. The game is drawn when the same position comes round for the',
162 'third time, when %d moves running take nothing, or when both sides agree.',
163 );
164 }
165  
166 17 has game => (is => 'rw');
167  
168 17 has [qw/in out/] => (is => 'rw');
169  
170 17 has [qw/interactive colour unicode picking/] => (is => 'rw');
171  
172 17 has human => (is => 'rw', default => 'white');
173  
174 17 has level => (is => 'rw', default => 3);
175  
176 17 has variant => (is => 'rw');
177  
178 17 has keysource => (is => 'rw');
179  
180 17 has width => (is => 'rw');
181  
182 17 has pause => (is => 'rw');
183  
184 17 has menu => (is => 'rw');
185  
186 17 has raw => (is => 'rw', default => 0);
187  
188 96 ▶has notice => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
189  
190 17 has focus => (is => 'rw');
191  
192 17 has chosen => (is => 'rw');
193  
194 96 ▶has prefix => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
195  
196 17 has last => (is => 'rw');
197  
198 96 ▶has _pending => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
199  
200 17 has _ask => (is => 'rw');
201  
202 17 has _target => (is => 'rw');
203  
204 17 has _complaint => (is => 'rw');
205  
206 17 has _choice => (is => 'rw');
207  
208 17 has _cramped => (is => 'rw', default => 0);
209  
210 17 has _redraw => (is => 'rw', default => 1);
211  
212 17 my %WORD = (w => 'white', b => 'black');
213  
214 1275 ▶my $other = sub { $_[0] eq 'white' ? 'black' : 'white' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'white' ? : 643 632 1275
215  
216 647 ▶my $coordinates = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 647 my ($name) = @_;
218 647 return (ord(substr $name, 0, 1) - 97, substr($name, 1) - 1);
219 17 };
220  
221 1163 ▶my $stones_of = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
222 1163 my ($position) = @_;
223 1163 ▶ my $parsed = parse_position($position) or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = parse_position($position) or return {} 1163 0 1163
224 1163 my $stones = $parsed->{stones};
225 1163 return { map { $_ => $WORD{ $stones->{$_} } } keys %{$stones} };
226 17 };
227  
228 293 ▶my $drawn = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
229 293 my (@names) = @_;
230 293 my %place = map { $_ => [ $coordinates->($_) ] } @names;
231 293 ▶ return sort { $place{$b}[1] <=> $place{$a}[1] || $place{$a}[0] <=> $place{$b}[0] } @names;
Condition66%2/3
$place{$b}[1] <=> $place{$a}[1] || $place{$a}[0] <=> $place{$b}[0]
l!l&&r!l&&!r
1811940
MC/DC0%0/2
$place{$b}[1] <=> $place{$a}[1] || $place{$a}[0] <=> $place{$b}[0]
$place{$b}[1] <=> $place{$a}[1] $place{$a}[0] <=> $place{$b}[0]
Truth table66%2/3
A || B
A= $place{$b}[1] <=> $place{$a}[1]B= $place{$a}[0] <=> $place{$b}[0]
ABresult
1X1
011
000
232 17 };
233  
234 150 ▶my $plain = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
235 150 my ($text) = @_;
236 150 $text =~ s/\e\[[0-9;?]*[A-Za-z]//g;
237 150 return $text;
238 17 };
239  
240 138 ▶my $count = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
241 138 my ($n) = @_;
242 138 ▶ return $n == 1 ? '1 stone' : "$n stones";
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 105 33 138
243 17 };
244  
245 96 ▶sub BUILD {
Subroutine BUILD: called CC=21 SCAR=31.4
Pod undocumented
246 96 my ($self) = @_;
247 96 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 60 36 96
248 96 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 19 77 96
249 96 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 9 87 96
-t $self->in ? : 0 9 9
250 96 ▶ $self->colour($self->interactive && !$ENV{NO_COLOR} ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 14 82 96
$self->interactive && !$ENV{'NO_COLOR'} ? : 0 14 14
Condition33%1/3
$self->interactive and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$self->interactive and not $ENV{"NO_COLOR"}
$self->interactive not $ENV{"NO_COLOR"}
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not $ENV{"NO_COLOR"}
ABresult
0X0
100
111
251 96 ▶ $self->unicode($self->colour ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 14 82 96
$self->colour ? : 0 14 14
252 96 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 12 84 96
$self->interactive ? : 0 12 12
253 96 ▶ $self->picking(0) unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 59 37 96
254 96 ▶ $self->pause($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pause;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pause 23 73 96
$self->interactive && !$self->keysource ? : 0 23 23
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
2300
MC/DC0%0/2
$self->interactive and not $self->keysource
$self->interactive not $self->keysource
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not $self->keysource
ABresult
0X0
100
111
255 96 ▶ $self->menu(0) unless defined $self->menu;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->menu 94 2 96
256  
257 96 ▶ Carp::croak("Game::Fanorona::Terminal: side is white, black, both or neither, not '" . $self->human . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->human =~ /\A(?:white|black|both|neither)\z/u 2 94 96
258 unless $self->human =~ /\A(?:white|black|both|neither)\z/;
259 94 ▶ Carp::croak('Game::Fanorona::Terminal: level is 1 to ' . scalar(@LEVEL))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->level =~ /\A[1-5]\z/u 2 92 94
260 unless $self->level =~ /\A[1-5]\z/;
261  
262 92 ▶ $self->game($self->_fresh_game) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 92 0 92
263  
264 90 ▶ binmode $self->out, ':encoding(UTF-8)' if $self->unicode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->unicode 1 89 90
265 90 my $previous = select $self->out;
266 90 $| = 1;
267 90 select $previous;
268 90 return;
269 }
270  
271 96 ▶sub _fresh_game {
Subroutine _fresh_game: called CC=2 SCAR=6.9
272 96 my ($self) = @_;
273 96 ▶ return Game::Fanorona->new(defined $self->variant ? (variant => $self->variant) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $self->variant ? : 41 55 96
274 }
275  
276 30 ▶sub budget { (Game::Fanorona::Bot->levels)[ $_[0]->level - 1 ] }
Subroutine budget: called CC=1 SCAR=0.0
Pod documented
277  
278 78 ▶sub start {
Subroutine start: called CC=8 SCAR=22.2
Pod documented
279 78 my ($self) = @_;
280 78 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 78 78
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
41370
MC/DC0%0/2
$self->picking and not $self->_enter_raw
$self->picking not $self->_enter_raw
Truth table66%2/3
A and not(B)
A= $self->pickingB= not $self->_enter_raw
ABresult
0X0
100
111
281  
282 78 my $interrupt = $SIG{INT};
283 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
284 0 $self->_leave_raw;
285 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
286 0 kill 'INT', $$;
287 78 };
288  
289 78 ▶ $self->_say($self->_paint('Fanorona.', 'title') . ' Type help for the commands.') unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 41 37 78
290 78 my $played = eval {
291 78 ▶ $self->_turns if !$self->menu || !$self->raw || $self->_ask_how_to_play;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->menu && $self->raw or $self->_ask_how_to_play 77 1 78
Condition83%5/6
not $self->menu && $self->raw or $self->_ask_how_to_play
l!l&&r!l&&!r
7611
$self->menu && $self->raw
!ll&&!rl&&r
7602
MC/DC66%2/3
not $self->menu && $self->raw or $self->_ask_how_to_play
$self->menu $self->raw $self->_ask_how_to_play
Truth table75%3/4
not(A && B) or C
A= $self->menuB= $self->rawC= $self->_ask_how_to_play
ABCresult
0XX1
10X1
1111
1100
292 78 1;
293 };
294 78 my $error = $@;
295 78 $self->_leave_raw;
296 78 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 78 78
297 78 return 0;
298 }
299  
300 77 ▶sub _turns {
Subroutine _turns: called CC=13 SCAR=25.7
301 77 my ($self) = @_;
302 77 while (1) {
303 203 ▶ my $picking = $self->picking && $self->raw;
Condition66%2/3
$self->picking && $self->raw
!ll&&!rl&&r
152051
MC/DC50%1/2
$self->picking && $self->raw
$self->picking $self->raw
Truth table66%2/3
A && B
A= $self->pickingB= $self->raw
ABresult
0X0
100
111
304 203 my $game = $self->game;
305  
306 203 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 19 184 203
307 19 ▶ my $line = $picking ? $self->_pick_ended : $self->_read_ended;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 2 17 19
308 19 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 13 6 19
309 6 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 2 4 6
310 4 next;
311 }
312 184 ▶ if ($self->_bot_plays($game->turn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_bot_plays($game->turn)) 29 155 184
313 29 $self->_bot_move;
314 29 next;
315 }
316  
317 155 ▶ $self->render if $self->_redraw && !$picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_redraw and not $picking 81 74 155
Condition100%3/3
$self->_redraw and not $picking
!ll&&!rl&&r
274781
MC/DC100%2/2
$self->_redraw and not $picking
$self->_redraw not $picking
Truth table100%3/3
A and not(B)
A= $self->_redrawB= not $picking
ABresult
0X0
100
111
318 155 ▶ my $line = $picking ? $self->pick : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 47 108 155
319 155 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 26 129 155
320 26 ▶ $self->_say('Bye.') unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 24 2 26
321 26 last;
322 }
323 129 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 36 93 129
324 }
325 77 return $self;
326 }
327  
328 17 ▶sub _read_ended {
Subroutine _read_ended: called CC=2 SCAR=6.9
329 17 my ($self) = @_;
330 17 ▶ $self->render if $self->_redraw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_redraw 11 6 17
331 17 return $self->_read_line('new or quit> ');
332 }
333  
334 2 ▶sub _pick_ended {
Subroutine _pick_ended: called CC=10 SCAR=30.1
335 2 my ($self) = @_;
336 2 $self->_put_down;
337 2 while (1) {
338 2 $self->show('ended');
339 2 my $key = $self->_read_key;
340 2 ▶ return undef 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 2 2
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
002
not defined $key or $key eq "eof"
l!l&&r!l&&!r
002
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
341 2 ▶ return 'new' if $key eq 'n' || $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" or $key eq "enter" 1 1 2
Condition66%2/3
$key eq "n" or $key eq "enter"
l!l&&r!l&&!r
101
MC/DC50%1/2
$key eq "n" or $key eq "enter"
$key eq "n" $key eq "enter"
Truth table66%2/3
A or B
A= $key eq "n"B= $key eq "enter"
ABresult
1X1
011
000
342 1 ▶ return 'quit' if $key eq 'q' || $key eq 'escape';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "escape" 1 0 1
Condition33%1/3
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
100
MC/DC0%0/2
$key eq "q" or $key eq "escape"
$key eq "q" $key eq "escape"
Truth table33%1/3
A or B
A= $key eq "q"B= $key eq "escape"
ABresult
1X1
011
000
343 0 ▶ return 'rules' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 0 0
344 0 ▶ return $self->_read_line(': ') if $key eq ':';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq ":" 0 0 0
345 }
346 }
347  
348 184 ▶sub _bot_plays {
Subroutine _bot_plays: called CC=4 SCAR=13.9
349 184 my ($self, $side) = @_;
350 184 ▶ return 0 if $self->human eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "both" 143 41 184
351 41 ▶ return 1 if $self->human eq 'neither';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "neither" 24 17 41
352 17 ▶ return $side eq $self->human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->human ? : 12 5 17
353 }
354  
355 29 ▶sub _bot_move {
Subroutine _bot_move: called CC=6 SCAR=18.1
356 29 my ($self) = @_;
357 29 my $game = $self->game;
358 29 my $side = $game->turn;
359 29 ▶ $self->show('thinking') if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 2 27 29
360 29 my $before = $stones_of->($game->position);
361 29 my $thought = Game::Fanorona::Bot->play($game, level => $self->budget);
362 29 ▶ return undef unless $thought;
Branch: 1 branch
BranchTrueFalseTotal
unless $thought 0 29 29
363 29 $self->_played($side, $before);
364 29 ▶ if ($self->human eq 'neither') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->human eq "neither") 24 5 29
365 24 ▶ $self->render unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 24 0 24
366 24 ▶ select undef, undef, undef, 0.4 if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 24 24
367 }
368 29 return $thought->{move};
369 }
370  
371 85 ▶sub _played {
Subroutine _played: called CC=4 SCAR=13.9
372 85 my ($self, $side, $before) = @_;
373 85 my $game = $self->game;
374 85 my $move = ($game->moves)[-1];
375 85 my $after = $stones_of->($game->position);
376 85 ▶ my @taken = $drawn->(grep { !$after->{$_} && $before->{$_} ne $side } keys %{$before});
Condition100%3/3
!$after->{$_} && $before->{$_} ne $side
!ll&&!rl&&r
10938576
MC/DC100%2/2
!$after->{$_} && $before->{$_} ne $side
!$after->{$_} $before->{$_} ne $side
Truth table100%3/3
not(A) && B
A= !$after->{$_}B= $before->{$_} ne $side
ABresult
0X0
100
111
377 85 my $variant = $game->variant;
378 85 my @points = move_points($move, cols => $variant->cols, rows => $variant->rows);
379 85 $self->last({ side => $side, move => $move, points => \@points, taken => \@taken });
380  
381 85 my $line = ucfirst($side) . ' played ' . $self->_paint($move, 'key');
382 85 ▶ $line .= ', taking ' . $count->(scalar @taken) if @taken;
Branch: 1 branch
BranchTrueFalseTotal
if @taken 48 37 85
383 85 $line .= '.';
384 85 ▶ if ($self->raw) { $self->notice([$line]) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) { } 12 73 85
385 73 else { $self->_say($line) }
386 85 $self->_redraw(1);
387 85 return $self;
388 }
389  
390 96 ▶sub _keys_available {
Subroutine _keys_available: called CC=5 SCAR=21.7
391 96 my ($self) = @_;
392 96 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 37 59 96
393 59 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 59 0 59
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0590
MC/DC0%0/2
defined $self->in and -t $self->in
defined $self->in -t $self->in
Truth table33%1/3
A and B
A= defined $self->inB= -t $self->in
ABresult
0X0
100
111
394 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
395 }
396  
397 37 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=5 SCAR=25.6
398 37 my ($self) = @_;
399 37 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 37 37
400 37 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 37 0 37
401 37 $self->raw(1);
402 37 return $self;
403 }
404 0 ▶ return undef unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 0 0 0
405 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
406 0 $self->raw(1);
407 0 my $out = $self->out;
408 0 print {$out} "\e[?25l";
409 0 return $self;
410 }
411  
412 78 ▶sub _leave_raw {
Subroutine _leave_raw: called CC=3 SCAR=13.1
413 78 my ($self) = @_;
414 78 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 41 37 78
415 37 ▶ unless ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->keysource) 0 37 37
416 0 eval { Term::ReadKey::ReadMode(0, $self->in) };
417 0 my $out = $self->out;
418 0 print {$out} "\e[?25h";
419 }
420 37 $self->raw(0);
421 37 return $self;
422 }
423  
424 296 ▶sub _read_char {
Subroutine _read_char: called CC=6 SCAR=25.4
425 296 my ($self, $wait) = @_;
426 296 my $pending = $self->_pending;
427 296 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 4 292 296
428 292 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 292 0 292
429 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
430 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
431 0 select undef, undef, undef, 0.05;
432 0 return Term::ReadKey::ReadKey(-1, $self->in);
433 }
434  
435 214 ▶sub _read_key {
Subroutine _read_key: called CC=4 SCAR=13.9
436 214 my ($self) = @_;
437 214 my $char = $self->_read_char(1);
438 214 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 212 214
439 212 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Fanorona::Terminal::CONTROL{$char} 61 151 212
440 151 ▶ return $self->_read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 43 108 151
441 108 return $char;
442 }
443  
444 43 ▶sub _read_sequence {
Subroutine _read_sequence: called CC=8 SCAR=21.5
445 43 my ($self) = @_;
446 43 my $opener = $self->_read_char(0);
447 43 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 43 43
448 43 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 4 39 43
Condition66%2/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
3904
MC/DC50%1/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table66%2/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
449 4 unshift @{ $self->_pending }, $opener;
450 4 return 'escape';
451 }
452 39 my $tail = '';
453 39 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 39 0 39
454 39 my $char = $self->_read_char(0);
455 39 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 39 39
456 39 $tail .= $char;
457 39 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/u 39 0 39
458 }
459 39 ▶ return $SEQUENCE{$tail} || 'unknown';
Condition50%1/2
$Game::Fanorona::Terminal::SEQUENCE{$tail} || 'unknown'
l!l
390
MC/DC0%0/1
$Game::Fanorona::Terminal::SEQUENCE{$tail} || 'unknown'
$Game::Fanorona::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Fanorona::Terminal::SEQUENCE{$tail}
Aresult
11
00
460 }
461  
462 40 ▶sub steer {
Subroutine steer: called CC=9 SCAR=22.0
Pod documented
463 40 my ($self, $from, $key, $among) = @_;
464 40 ▶ my $way = $DIRECTION{$key} or return $from;
Branch: 1 branch
BranchTrueFalseTotal
my $way = $Game::Fanorona::Terminal::DIRECTION{$key} or return $from 39 1 40
465 39 my ($ff, $fr) = $coordinates->($from);
466 39 my ($best, $cost);
467 39 for my $name (@{$among}) {
468 100 ▶ next if $name eq $from;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $from 17 83 100
469 83 my ($f, $r) = $coordinates->($name);
470 83 my ($x, $y) = ($f - $ff, $r - $fr);
471 83 my $along = $x * $way->[0] + $y * $way->[1];
472 83 my $across = abs($x * $way->[1] - $y * $way->[0]);
473 83 ▶ next if $along <= 0 || $across > $along;
Branch: 1 branch
BranchTrueFalseTotal
if $along <= 0 or $across > $along 34 49 83
Condition100%3/3
$along <= 0 or $across > $along
l!l&&r!l&&!r
29549
MC/DC100%2/2
$along <= 0 or $across > $along
$along <= 0 $across > $along
Truth table100%3/3
A or B
A= $along <= 0B= $across > $along
ABresult
1X1
011
000
474 49 my $this = $along + 2 * $across;
475 49 ▶ ($best, $cost) = ($name, $this) if !defined $cost || $this < $cost;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cost or $this < $cost 35 14 49
Condition100%3/3
not defined $cost or $this < $cost
l!l&&r!l&&!r
33214
MC/DC100%2/2
not defined $cost or $this < $cost
not defined $cost $this < $cost
Truth table100%3/3
not(A) or B
A= not defined $costB= $this < $cost
ABresult
1X1
011
000
476 }
477 39 ▶ return defined $best ? $best : $from;
Branch: 1 branch
BranchTrueFalseTotal
defined $best ? : 33 6 39
478 }
479  
480 187 ▶sub lit {
Subroutine lit: called CC=1 SCAR=0.0
Pod documented
481 187 my ($self) = @_;
482 187 return [ $drawn->($self->game->starts) ];
483 }
484  
485 378 ▶sub so_far {
Subroutine so_far: called CC=3 SCAR=11.0
Pod documented
486 378 my ($self) = @_;
487 378 my $prefix = $self->prefix;
488 378 ▶ return '' unless defined $self->chosen && @{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->chosen and @{$prefix;} 149 229 378
Condition100%3/3
defined $self->chosen and @{$prefix;}
!ll&&!rl&&r
7772229
MC/DC100%2/2
defined $self->chosen and @{$prefix;}
defined $self->chosen @{$prefix;}
Truth table100%3/3
A and B
A= defined $self->chosenB= @{$prefix;}
ABresult
0X0
100
111
489 229 return $self->chosen . join '', map { substr $_->{string}, 2 } @{$prefix};
490 }
491  
492 184 ▶sub _standing {
Subroutine _standing: called CC=2 SCAR=6.9
493 184 my ($self) = @_;
494 184 my $prefix = $self->prefix;
495 184 ▶ return @{$prefix} ? $prefix->[-1]{to} : $self->chosen;
Branch: 1 branch
BranchTrueFalseTotal
@{$prefix;} ? : 77 107 184
496 }
497  
498 267 ▶sub _offers {
Subroutine _offers: called CC=2 SCAR=7.0
499 267 my ($self) = @_;
500 267 my $found = $self->game->continuations($self->so_far);
501 267 ▶ return { complete => 0, stop => 0, steps => [] } unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 267 267
502 267 return $found;
503 }
504  
505 12 ▶sub playable {
Subroutine playable: called CC=4 SCAR=14.0
Pod documented
506 12 my ($self) = @_;
507 12 ▶ return 0 unless @{ $self->prefix };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->prefix;} 2 10 12
508 10 my $offers = $self->_offers;
509 10 ▶ return $offers->{complete} || $offers->{stop} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$offers->{'complete'} || $offers->{'stop'} ? : 10 0 10
Condition66%2/3
$offers->{"complete"} or $offers->{"stop"}
l!l&&r!l&&!r
910
MC/DC0%0/2
$offers->{"complete"} or $offers->{"stop"}
$offers->{"complete"} $offers->{"stop"}
Truth table66%2/3
A or B
A= $offers->{"complete"}B= $offers->{"stop"}
ABresult
1X1
011
000
510 }
511  
512 103 ▶sub _put_down {
Subroutine _put_down: called CC=1 SCAR=0.0
513 103 my ($self) = @_;
514 103 $self->chosen(undef);
515 103 $self->prefix([]);
516 103 $self->_ask(undef);
517 103 $self->_target(undef);
518 103 $self->_complaint(undef);
519 103 return $self;
520 }
521  
522 24 ▶sub _beyond {
Subroutine _beyond: called CC=4 SCAR=15.9
523 24 my ($self, $key) = @_;
524 24 my $way = $DIRECTION{$key};
525 24 my ($file, $rank) = $coordinates->($self->_standing);
526 24 ($file, $rank) = ($file + $way->[0], $rank + $way->[1]);
527 24 ▶ return ($file < 0 || $file > 25 || $rank < 0) ? '' : chr(97 + $file) . ($rank + 1);
Branch: 1 branch
BranchTrueFalseTotal
$file < 0 || $file > 25 || $rank < 0 ? : 0 24 24
Condition33%2/6
$file < 0 or $file > 25 or $rank < 0
l!l&&r!l&&!r
0024
$file < 0 or $file > 25
l!l&&r!l&&!r
0024
MC/DC0%0/3
$file < 0 or $file > 25 or $rank < 0
$file < 0 $file > 25 $rank < 0
Truth table25%1/4
A or B or C
A= $file < 0B= $file > 25C= $rank < 0
ABCresult
1XX1
01X1
0011
0000
528 }
529  
530 23 ▶sub _advance {
Subroutine _advance: called CC=1 SCAR=0.0
531 23 my ($self, $key) = @_;
532 23 return $self->_step_to($self->_beyond($key));
533 }
534  
535 19 ▶sub _aim {
Subroutine _aim: called CC=4 SCAR=13.9
536 19 my ($self, $key) = @_;
537 19 my $standing = $self->_standing;
538 19 my %seen;
539 19 my @lit = $drawn->(grep { !$seen{$_}++ } map { $_->{to} } grep { $_->{from} eq $standing } @{ $self->_offers->{steps} });
540 19 ▶ my $from = defined $self->_target ? $self->_target : $standing;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_target ? : 6 13 19
541 19 my $to = $self->steer($from, $key, \@lit);
542 19 ▶ if ($to eq $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq $from) 2 17 19
543 2 ▶ return 1 if defined $self->_target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_target 1 1 2
544 1 return $self->_step_to($self->_beyond($key));
545 }
546 17 $self->_target($to);
547 17 return 1;
548 }
549  
550 33 ▶sub _step_to {
Subroutine _step_to: called CC=12 SCAR=24.9
551 33 my ($self, $target) = @_;
552 33 my $standing = $self->_standing;
553 33 $self->_target(undef);
554 33 ▶ my @there = grep { $_->{from} eq $standing && $_->{to} eq $target } @{ $self->_offers->{steps} };
Condition100%3/3
$_->{'from'} eq $standing && $_->{'to'} eq $target
!ll&&!rl&&r
133244
MC/DC100%2/2
$_->{'from'} eq $standing && $_->{'to'} eq $target
$_->{'from'} eq $standing $_->{'to'} eq $target
Truth table100%3/3
A && B
A= $_->{'from'} eq $standingB= $_->{'to'} eq $target
ABresult
0X0
100
111
555 33 ▶ if (!@there) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@there) 6 27 33
556 6 my $game = $self->game;
557 6 my $said = 'that point is not on the board';
558 6 ▶ if ($self->_offers->{complete}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_offers->{'complete'}) { } 1 5 6
elsif (length $target) { } 5 0 5
559 1 $said = 'the move is complete, and enter plays it';
560 }
561 elsif (length $target) {
562 5 ▶ my $tried = (length $self->so_far ? $self->so_far : $standing) . "-$target";
Branch: 1 branch
BranchTrueFalseTotal
length $self->so_far ? : 4 1 5
563 5 $game->continuations($tried);
564 5 my $error = $game->error;
565 5 ▶ if ($error && $error->code eq 'paika_in_chain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $error->code eq "paika_in_chain") 4 1 5
Condition66%2/3
$error and $error->code eq "paika_in_chain"
!ll&&!rl&&r
014
MC/DC50%1/2
$error and $error->code eq "paika_in_chain"
$error $error->code eq "paika_in_chain"
Truth table66%2/3
A and B
A= $errorB= $error->code eq "paika_in_chain"
ABresult
0X0
100
111
566 4 $game->continuations($tried . 'A');
567 4 my $closer = $game->error;
568 4 ▶ $error = $closer if $closer && $closer->code ne 'takes_nothing';
Branch: 1 branch
BranchTrueFalseTotal
if $closer and $closer->code ne "takes_nothing" 3 1 4
Condition66%2/3
$closer and $closer->code ne "takes_nothing"
!ll&&!rl&&r
013
MC/DC50%1/2
$closer and $closer->code ne "takes_nothing"
$closer $closer->code ne "takes_nothing"
Truth table66%2/3
A and B
A= $closerB= $closer->code ne "takes_nothing"
ABresult
0X0
100
111
569 }
570 5 ▶ $said = $error->message if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 5 0 5
571 }
572 6 $self->_complaint(ucfirst($said) . '.');
573 6 return 0;
574 }
575 27 ▶ if (@there > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@there > 1) 17 10 27
576 17 $self->_ask([ @there ]);
577 17 return 0;
578 }
579 10 $self->prefix([ @{ $self->prefix }, $there[0] ]);
580 10 return 1;
581 }
582  
583 20 ▶sub _answer {
Subroutine _answer: called CC=6 SCAR=17.9
584 20 my ($self, $key) = @_;
585 20 my $ask = $self->_ask;
586 20 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 1 19 20
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
1019
MC/DC50%1/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table66%2/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
587 1 $self->_ask(undef);
588 1 return 0;
589 }
590 19 ▶ my $how = $key eq 'a' ? 'approach' : $key eq 'w' ? 'withdrawal' : return 0;
Branch: 2 branches
BranchTrueFalseTotal
$key eq 'a' ? : 13 6 19
$key eq 'w' ? : 1 5 6
591 14 my ($step) = grep { $_->{how} eq $how } @{$ask};
592 14 ▶ return 0 unless $step;
Branch: 1 branch
BranchTrueFalseTotal
unless $step 0 14 14
593 14 $self->prefix([ @{ $self->prefix }, $step ]);
594 14 $self->_ask(undef);
595 14 return 1;
596 }
597  
598 47 ▶sub pick {
Subroutine pick: called CC=44 SCAR=38.0
Pod documented
599 47 my ($self) = @_;
600 47 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 47 47
601 47 $self->_put_down;
602  
603 47 while (1) {
604 185 my $lit = $self->lit;
605 185 ▶ return undef unless @{$lit};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$lit;} 0 185 185
606 185 ▶ $self->focus($lit->[0]) unless defined $self->focus && grep { $_ eq $self->focus } @{$lit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->focus and grep {$_ eq $self->focus;} @{$lit;} 46 139 185
Condition66%2/3
defined $self->focus and grep {$_ eq $self->focus;} @{$lit;}
!ll&&!rl&&r
460139
MC/DC50%1/2
defined $self->focus and grep {$_ eq $self->focus;} @{$lit;}
defined $self->focus grep {$_ eq $self->focus;} @{$lit;}
Truth table66%2/3
A and B
A= defined $self->focusB= grep {$_ eq $self->focus;} @{$lit;}
ABresult
0X0
100
111
607 185 ▶ my $mode = $self->_ask ? 'ask' : defined $self->chosen ? 'where' : 'which';
Branch: 2 branches
BranchTrueFalseTotal
$self->_ask ? : 22 163 185
defined $self->chosen ? : 86 77 163
608  
609 185 $self->show($mode);
610 185 my $key = $self->_read_key;
611 185 ▶ return undef 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" 2 183 185
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
20183
not defined $key or $key eq "eof"
l!l&&r!l&&!r
20183
MC/DC33%1/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table50%2/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
612 183 $self->_complaint(undef);
613 183 ▶ if ($self->_cramped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cramped) 5 178 183
614 5 ▶ return 'quit' if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 1 4 5
615 4 next;
616 }
617  
618 178 ▶ if ($mode eq 'ask') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "ask") 20 158 178
619 20 $self->_answer($key);
620 20 next;
621 }
622 158 ▶ if ($DIRECTION{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Game::Fanorona::Terminal::DIRECTION{$key}) 53 105 158
623 53 ▶ if ($mode eq 'which') { $self->focus($self->steer($self->focus, $key, $lit)) }
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'which') { } 11 42 53
elsif ($key =~ /\A(?:up|down|left|right)\z/u) { } 19 23 42
624 19 elsif ($key =~ /\A(?:up|down|left|right)\z/) { $self->_aim($key) }
625 23 else { $self->_advance($key) }
626 53 next;
627 }
628 105 ▶ if ($mode eq 'which' && ($key eq 'tab' || $key eq 'back_tab')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which" and $key eq "tab" || $key eq "back_tab") 6 99 105
Condition100%6/6
$mode eq "which" and $key eq "tab" || $key eq "back_tab"
!ll&&!rl&&r
44556
$key eq "tab" or $key eq "back_tab"
l!l&&r!l&&!r
5155
MC/DC100%3/3
$mode eq "which" and $key eq "tab" || $key eq "back_tab"
$mode eq "which" $key eq "tab" $key eq "back_tab"
Truth table100%4/4
A and B or C
A= $mode eq "which"B= $key eq "tab"C= $key eq "back_tab"
ABCresult
0XX0
1000
11X1
1011
629 6 my ($at) = grep { $lit->[$_] eq $self->focus } 0 .. $#{$lit};
630 6 ▶ $self->focus($lit->[ ($at + ($key eq 'tab' ? 1 : -1)) % @{$lit} ]);
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'tab' ? : 5 1 6
631 6 next;
632 }
633 99 ▶ if ($mode eq 'which' && $key eq 'home') { $self->focus($lit->[0]); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which" and $key eq "home") 1 98 99
Condition100%3/3
$mode eq "which" and $key eq "home"
!ll&&!rl&&r
44541
MC/DC100%2/2
$mode eq "which" and $key eq "home"
$mode eq "which" $key eq "home"
Truth table100%3/3
A and B
A= $mode eq "which"B= $key eq "home"
ABresult
0X0
100
111
634 98 ▶ if ($mode eq 'which' && $key eq 'end') { $self->focus($lit->[-1]); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which" and $key eq "end") 1 97 98
Condition100%3/3
$mode eq "which" and $key eq "end"
!ll&&!rl&&r
44531
MC/DC100%2/2
$mode eq "which" and $key eq "end"
$mode eq "which" $key eq "end"
Truth table100%3/3
A and B
A= $mode eq "which"B= $key eq "end"
ABresult
0X0
100
111
635  
636 97 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 47 50 97
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
41650
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
637 47 ▶ if ($mode eq 'which') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which") 26 21 47
638 26 $self->chosen($self->focus);
639 26 $self->prefix([]);
640 26 next;
641 }
642 21 ▶ if (defined $self->_target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_target) 9 12 21
643 9 $self->_step_to($self->_target);
644 9 next;
645 }
646 12 ▶ next unless $self->playable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->playable 2 10 12
647 10 my $game = $self->game;
648 10 my $side = $game->turn;
649 10 my $before = $stones_of->($game->position);
650 10 ▶ unless ($game->play($self->so_far)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->play($self->so_far)) 0 10 10
651 0 $self->_complaint(ucfirst($game->error->message) . '.');
652 0 next;
653 }
654 10 $self->_put_down;
655 10 $self->focus(undef);
656 10 $self->_played($side, $before);
657 10 return '';
658 }
659 50 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 7 43 50
660 7 ▶ if (defined $self->_target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_target) 1 6 7
661 1 $self->_target(undef);
662 1 next;
663 }
664 6 my @steps = @{ $self->prefix };
665 6 ▶ if (@steps) {
Branch: 2 branches
BranchTrueFalseTotal
if (@steps) { } 4 2 6
elsif ($mode eq 'where') { } 2 0 2
666 4 pop @steps;
667 4 $self->prefix(\@steps);
668 }
669 elsif ($mode eq 'where') {
670 2 $self->focus($self->chosen);
671 2 $self->_put_down;
672 }
673 6 next;
674 }
675 43 ▶ if ($key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape") 3 40 43
676 3 ▶ $self->focus($self->chosen) if defined $self->chosen;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->chosen 3 0 3
677 3 $self->_put_down;
678 3 next;
679 }
680 40 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 3 37 40
681 3 my $thought = Game::Fanorona::Bot->hint($self->game);
682 3 ▶ if ($thought && @{ $thought->{steps} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thought and @{$thought->{"steps"};}) 3 0 3
Condition33%1/3
$thought and @{$thought->{"steps"};}
!ll&&!rl&&r
003
MC/DC0%0/2
$thought and @{$thought->{"steps"};}
$thought @{$thought->{"steps"};}
Truth table33%1/3
A and B
A= $thoughtB= @{$thought->{"steps"};}
ABresult
0X0
100
111
683 3 $self->chosen($thought->{steps}[0]{from});
684 3 $self->prefix([ @{ $thought->{steps} } ]);
685 3 $self->_complaint('The program would play ' . $self->_paint($thought->{move}, 'key')
686 . '. Enter plays it; escape puts the stone back.');
687 }
688 3 next;
689 }
690 37 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 1 36 37
691 1 $self->show('help');
692 1 $self->_read_key;
693 1 next;
694 }
695 36 ▶ if ($key eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "r") 1 35 36
696 1 $self->show('rules');
697 1 $self->_read_key;
698 1 next;
699 }
700 35 ▶ if ($key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":") 2 33 35
701 2 $self->_put_down;
702 2 return $self->_read_line(': ');
703 }
704 33 ▶ if ($key eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 32 1 33
705 32 $self->_put_down;
706 32 return 'quit';
707 }
708 1 ▶ next if $key eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "unknown" 0 1 1
709 1 $self->_complaint('That key does nothing here. Press ? for the ones that do.');
710 }
711 }
712  
713 315 ▶sub marks {
Subroutine marks: called CC=19 SCAR=29.4
Pod documented
714 315 my ($self, $mode) = @_;
715 315 my $game = $self->game;
716 315 my $stones = $stones_of->($game->position);
717 315 my (%mark, %sign);
718  
719 315 ▶ unless ($mode eq 'where' || $mode eq 'ask') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode eq "where" or $mode eq "ask") 207 108 315
Condition100%3/3
$mode eq "where" or $mode eq "ask"
l!l&&r!l&&!r
8622207
MC/DC100%2/2
$mode eq "where" or $mode eq "ask"
$mode eq "where" $mode eq "ask"
Truth table100%3/3
A or B
A= $mode eq "where"B= $mode eq "ask"
ABresult
1X1
011
000
720 207 ▶ if (my $last = $self->last) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->last) 86 121 207
721 86 $mark{$_} = 'trail' for @{ $last->{points} }, @{ $last->{taken} };
722 86 $sign{$_} = 'taken' for @{ $last->{taken} };
723 }
724 207 ▶ return (\%mark, $stones, \%sign) unless $mode eq 'which' && defined $self->focus;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "which" and defined $self->focus 130 77 207
Condition66%2/3
$mode eq "which" and defined $self->focus
!ll&&!rl&&r
130077
MC/DC50%1/2
$mode eq "which" and defined $self->focus
$mode eq "which" defined $self->focus
Truth table66%2/3
A and B
A= $mode eq "which"B= defined $self->focus
ABresult
0X0
100
111
725 77 my $focus = $self->focus;
726 77 for my $step (grep { $_->{from} eq $focus } @{ $self->_offers->{steps} }) {
727 166 $mark{ $step->{to} } = 'reach';
728 166 delete $sign{ $step->{to} };
729 }
730 77 $mark{$focus} = 'cursor';
731 77 return (\%mark, $stones, \%sign);
732 }
733  
734 108 my $prefix = $self->prefix;
735 108 ▶ if (@{$prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$prefix;}) 46 62 108
736 46 my $after = $game->preview($self->so_far);
737 46 ▶ $stones = $stones_of->($after) if defined $after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after 46 0 46
738 46 for my $step (@{$prefix}) {
739 53 $mark{ $step->{from} } = 'trail';
740 53 for my $name (@{ $step->{takes} }) {
741 57 $mark{$name} = 'taken';
742 57 $sign{$name} = 'taken';
743 }
744 }
745 }
746 108 my $standing = $self->_standing;
747 108 ▶ if ($mode eq 'ask') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'ask') { } 22 86 108
748 22 my $ask = $self->_ask;
749 22 $mark{ $ask->[0]{to} } = 'reach';
750 22 delete $sign{ $ask->[0]{to} };
751 22 for my $step (@{$ask}) {
752 44 ▶ my $how = $step->{how} eq 'approach' ? 'approach' : 'withdrawal';
Branch: 1 branch
BranchTrueFalseTotal
$step->{'how'} eq 'approach' ? : 22 22 44
753 44 for my $name (@{ $step->{takes} }) {
754 44 $mark{$name} = $how;
755 44 $sign{$name} = substr $how, 0, 1;
756 }
757 }
758 }
759 else {
760 86 for my $step (grep { $_->{from} eq $standing } @{ $self->_offers->{steps} }) {
761 161 $mark{ $step->{to} } = 'reach';
762 161 delete $sign{ $step->{to} };
763 }
764 86 ▶ $mark{ $self->_target } = 'cursor' if defined $self->_target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_target 18 68 86
765 }
766 108 $mark{$standing} = 'moving';
767 108 delete $sign{$standing};
768 108 return (\%mark, $stones, \%sign);
769 }
770  
771 1318 ▶sub _paint {
Subroutine _paint: called CC=2 SCAR=6.9
772 1318 my ($self, $text, $ink) = @_;
773 1318 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 1311 7 1318
774 7 return "\e[$INK{$ink}m$text\e[0m";
775 }
776  
777 52641 ▶sub _paint_on {
Subroutine _paint_on: called CC=2 SCAR=6.9
778 52641 my ($self, $text, $ink, $ground) = @_;
779 52641 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 52283 358 52641
780 358 return "\e[$GROUND{$ground};$INK{$ink}m$text\e[0m";
781 }
782  
783 14155 ▶sub _glyph {
Subroutine _glyph: called CC=2 SCAR=6.9
784 14155 my ($self, $what) = @_;
785 14155 ▶ return $GLYPH{ $self->unicode ? 'wide' : 'ascii' }{$what};
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 47 14108 14155
786 }
787  
788 13239 ▶sub _cell {
Subroutine _cell: called CC=10 SCAR=23.0
789 13239 my ($self, $stone, $mark, $sign, $first, $final) = @_;
790 13239 ▶ my $glyph = $self->_glyph(defined $sign ? $sign : $stone ? $stone : 'empty');
Branch: 2 branches
BranchTrueFalseTotal
defined $sign ? : 172 13067 13239
$stone ? : 6216 6851 13067
791 13239 ▶ $glyph = $sign unless defined $glyph;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glyph 44 13195 13239
792 13239 ▶ if (!$mark) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mark) 12387 852 13239
793 12387 ▶ return $self->_paint_on($first ? ' ' : '-', 'line', 'board')
Branch: 3 branches
BranchTrueFalseTotal
$first ? : 1466 10921 12387
$stone ? : 5945 6442 12387
$final ? : 1449 10938 12387
794 . $self->_paint_on($glyph, $stone ? $stone : 'line', 'board')
795 . $self->_paint_on($final ? ' ' : '-', 'line', 'board');
796 }
797 852 my ($open, $close) = @{ $FRAME{$mark} };
798 852 ▶ return $self->_paint_on($open, $mark, $mark)
Branch: 1 branch
BranchTrueFalseTotal
defined $sign || !$stone ? : 581 271 852
Condition100%3/3
defined $sign or not $stone
l!l&&r!l&&!r
172409271
MC/DC100%2/2
defined $sign or not $stone
defined $sign not $stone
Truth table100%3/3
A or not(B)
A= defined $signB= not $stone
ABresult
1X1
011
000
799 . $self->_paint_on($glyph, (defined $sign || !$stone) ? $mark : $stone, $mark)
800 . $self->_paint_on($close, $mark, $mark);
801 }
802  
803 630 ▶sub _baseline {
Subroutine _baseline: called CC=1 SCAR=0.0
804 630 my ($self, $side) = @_;
805 630 my $start = $stones_of->($self->game->variant->start);
806 630 return scalar grep { $_ eq $side } values %{$start};
807 }
808  
809 315 ▶sub _panel {
Subroutine _panel: called CC=8 SCAR=20.8
810 315 my ($self, $stones) = @_;
811 315 my $game = $self->game;
812 315 my %left = (white => 0, black => 0);
813 315 $left{$_}++ for values %{$stones};
814 315 my $rows = $game->variant->rows;
815 315 my %panel;
816 315 for my $side (qw(black white)) {
817 630 my $taken = $self->_baseline($other->($side)) - $left{ $other->($side) };
818 630 ▶ $taken = 0 if $taken < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $taken < 0 0 630 630
819 $panel{ $side eq 'black' ? 0 : 2 * ($rows - 1) } = sprintf '%s %s %2d left taken %d',
820 630 ▶ ucfirst $side, $self->_paint($self->_glyph($side), 'key'), $left{$side}, $taken;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 315 315 630
821 }
822 315 my ($quiet, $limit) = ($game->quiet, $game->quiet_limit);
823 315 ▶ $panel{ $rows - 1 } = $self->_paint("Nothing taken for $quiet of $limit", 'warn')
Branch: 1 branch
BranchTrueFalseTotal
if $limit > 0 and $quiet * 2 > $limit and not $game->over 3 312 315
Condition100%6/6
$limit > 0 and $quiet * 2 > $limit and not $game->over
!ll&&!rl&&r
31113
$limit > 0 and $quiet * 2 > $limit
!ll&&!rl&&r
73044
MC/DC100%3/3
$limit > 0 and $quiet * 2 > $limit and not $game->over
$limit > 0 $quiet * 2 > $limit not $game->over
Truth table100%4/4
A and B and not(C)
A= $limit > 0B= $quiet * 2 > $limitC= not $game->over
ABCresult
0XX0
10X0
1100
1111
824 if $limit > 0 && $quiet * 2 > $limit && !$game->over;
825 315 return \%panel;
826 }
827  
828 315 ▶sub board_lines {
Subroutine board_lines: called CC=12 SCAR=24.8
Pod documented
829 315 my ($self, $mode) = @_;
830 315 ▶ my ($mark, $stones, $sign) = $self->marks(defined $mode ? $mode : 'plain');
Branch: 1 branch
BranchTrueFalseTotal
defined $mode ? : 308 7 315
831 315 my $variant = $self->game->variant;
832 315 my ($cols, $rows) = ($variant->cols, $variant->rows);
833 315 my @lines;
834 315 for my $rank (reverse 0 .. $rows - 1) {
835 1523 my $line = '';
836 1523 for my $file (0 .. $cols - 1) {
837 13239 my $name = chr(97 + $file) . ($rank + 1);
838 13239 $line .= $self->_cell($stones->{$name}, $mark->{$name}, $sign->{$name}, $file == 0, $file == $cols - 1);
839 13239 ▶ $line .= $self->_paint_on('-', 'line', 'board') if $file < $cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $file < $cols - 1 11716 1523 13239
840 }
841 1523 push @lines, ($rank + 1) . ' ' . $line;
842 1523 ▶ next unless $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless $rank 315 1208 1523
843 1208 my $link = '';
844 1208 for my $file (0 .. $cols - 1) {
845 10560 $link .= ' | ';
846 10560 ▶ $link .= (($file + $rank - 1) % 2 ? '\\' : '/') if $file < $cols - 1;
Branch: 2 branches
BranchTrueFalseTotal
if $file < $cols - 1 9352 1208 10560
($file + $rank - 1) % 2 ? : 4676 4676 9352
847 }
848 1208 push @lines, ' ' . $self->_paint_on($link, 'line', 'board');
849 }
850 315 push @lines, ' ' . join ' ', map { ' ' . chr(97 + $_) . ' ' } 0 .. $cols - 1;
851  
852 315 my $panel = $self->_panel($stones);
853 315 $lines[$_] .= ' ' . $panel->{$_} for keys %{$panel};
854 315 ▶ unless ($self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->colour) 313 2 315
855 313 s{\s+\z}{} for @lines;
856 }
857 315 return \@lines;
858 }
859  
860 308 ▶sub _columns {
Subroutine _columns: called CC=4 SCAR=15.9
861 308 my ($self) = @_;
862 308 ▶ return $self->width if defined $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->width 15 293 308
863 293 ▶ return undef unless $self->interactive && !$self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->keysource 293 0 293
Condition66%2/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
1091840
MC/DC0%0/2
$self->interactive and not $self->keysource
$self->interactive not $self->keysource
Truth table66%2/3
A and not(B)
A= $self->interactiveB= not $self->keysource
ABresult
0X0
100
111
864 0 my ($wide) = eval { Term::ReadKey::GetTerminalSize($self->out) };
865 0 return $wide;
866 }
867  
868 308 ▶sub _too_narrow {
Subroutine _too_narrow: called CC=4 SCAR=13.9
869 308 my ($self, $lines) = @_;
870 308 $self->_cramped(0);
871 308 my $have = $self->_columns;
872 308 ▶ return undef unless defined $have && $have > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have and $have > 0 293 15 308
Condition66%2/3
defined $have and $have > 0
!ll&&!rl&&r
293015
MC/DC50%1/2
defined $have and $have > 0
defined $have $have > 0
Truth table66%2/3
A and B
A= defined $haveB= $have > 0
ABresult
0X0
100
111
873 15 my ($need) = sort { $b <=> $a } map { length $plain->($_) } @{$lines};
874 15 ▶ return undef if $have >= $need;
Branch: 1 branch
BranchTrueFalseTotal
if $have >= $need 7 8 15
875 8 $self->_cramped(1);
876 8 return "This window is $have columns wide and the board needs $need. Widen it and press a key, or q to stop.";
877 }
878  
879 302 ▶sub _status_lines {
Subroutine _status_lines: called CC=5 SCAR=16.1
880 302 my ($self) = @_;
881 302 my $game = $self->game;
882 302 ▶ if (my $result = $game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $game->result) 16 286 302
883 16 my $winner = $result->winner;
884 16 ▶ my @with = (
Branch: 2 branches
BranchTrueFalseTotal
defined $winner ? : 10 6 16
defined $winner ? : 10 6 16
885 ucfirst(defined $winner ? $winner : ''), ucfirst(defined $winner ? $other->($winner) : ''),
886 $game->quiet_limit,
887 );
888 16 (my $line = $ENDING{ $result->how }) =~ s/%([123])\$[sd]/$with[ $1 - 1 ]/g;
889 16 return [ $self->_paint($line, 'good') ];
890 }
891 286 my $side = $game->turn;
892 286 my @lines = (sprintf 'Move %d. %s to play, %s.', $game->ply + 1, ucfirst $side, $self->_glyph($side));
893 286 my $offer = $game->draw_offered_by;
894 286 ▶ push @lines, $self->_paint(ucfirst($offer) . ' has offered a draw: accept or decline.', 'warn') if defined $offer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $offer 0 286 286
895 286 return \@lines;
896 }
897  
898 108 ▶sub _preview_lines {
Subroutine _preview_lines: called CC=5 SCAR=16.1
899 108 my ($self) = @_;
900 108 my $prefix = $self->prefix;
901 108 ▶ my @aimed = defined $self->_target ? ('Enter steps to ' . $self->_paint($self->_target, 'key') . '.') : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_target ? : 18 90 108
902 108 ▶ return [ 'Choose where the stone at ' . $self->chosen . ' goes.', @aimed ] unless @{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$prefix;} 62 46 108
903 46 my $taken = 0;
904 46 $taken += @{ $_->{takes} } for @{$prefix};
905 46 my $line = 'So far ' . $self->_paint($self->so_far, 'key');
906 46 ▶ $line .= ', taking ' . $count->($taken) if $taken;
Branch: 1 branch
BranchTrueFalseTotal
if $taken 46 0 46
907 46 return [ "$line.", @aimed ];
908 }
909  
910 196 ▶sub _legend {
Subroutine _legend: called CC=10 SCAR=23.0
911 196 my ($self, $mode) = @_;
912 196 my $text;
913 196 ▶ if ($mode eq 'which') {
Branch: 6 branches
BranchTrueFalseTotal
if ($mode eq 'which') { } 72 124 196
elsif ($mode eq 'where') { } 86 38 124
elsif ($mode eq 'ask') { } 22 16 38
elsif ($mode eq 'ended') { } 2 14 16
elsif ($mode eq 'thinking') { } 2 12 14
elsif ($mode eq 'menu') { } 8 4 12
914 72 $text = 'arrows choose a stone enter pick it up h hint ? help : command q quit';
915 }
916 elsif ($mode eq 'where') {
917 $text = defined $self->_target
918 ? 'enter step there arrows choose another point backspace not there esc start again'
919 68 ▶ : @{ $self->prefix }
Branch: 1 branch
BranchTrueFalseTotal
$self->_offers->{'complete'} ? : 17 19 36
920 86 ▶ ? ($self->_offers->{complete} ? '' : 'arrows choose the next step ')
Branch: 2 branches
BranchTrueFalseTotal
defined $self->_target ? : 18 68 86
@{$self->prefix;} ? : 36 32 68
921 . 'enter play it backspace one step back esc start again'
922 : 'arrows choose where it goes, enter steps there y u b n step at once esc put it down';
923 }
924 elsif ($mode eq 'ask') {
925 22 my $ask = $self->_ask;
926 22 my %takes = map { $_->{how} => scalar @{ $_->{takes} } } @{$ask};
927 $text = 'a take ' . $count->($takes{approach}) . ' ahead w take ' . $count->($takes{withdrawal})
928 22 . ' behind esc neither';
929 }
930 2 elsif ($mode eq 'ended') { $text = 'n new game r rules : command q quit' }
931 2 elsif ($mode eq 'thinking') { $text = 'The program is thinking.' }
932 8 elsif ($mode eq 'menu') { $text = 'up and down choose a line left and right change it enter play q quit' }
933 4 else { $text = 'Any key goes back.' }
934 196 return $self->_paint($text, 'quiet');
935 }
936  
937 2 ▶sub _help_lines {
Subroutine _help_lines: called CC=3 SCAR=11.0
938 2 my ($self) = @_;
939 2 my @lines = ('The keys:', '');
940 2 push @lines, ' ' . $self->_paint(sprintf('%-11s', $_->[0]), 'key') . ' ' . $_->[1] for @KEYS;
941 2 push @lines, '', 'The commands, typed after a colon or at the prompt:', '';
942 2 push @lines, ' ' . $self->_paint(sprintf('%-11s', $_->[0]), 'key') . ' ' . $_->[1] for @COMMANDS;
943 2 return \@lines;
944 }
945  
946 3 ▶sub _rules_lines {
Subroutine _rules_lines: called CC=2 SCAR=6.9
947 3 my ($self) = @_;
948 3 my $limit = $self->game->quiet_limit;
949 3 ▶ return [ map { /%d/ ? sprintf($_, $limit) : $_ } @RULES ];
Branch: 1 branch
BranchTrueFalseTotal
/%d/u ? : 3 69 72
950 }
951  
952 8 ▶sub _menu_lines {
Subroutine _menu_lines: called CC=5 SCAR=16.1
953 8 my ($self) = @_;
954 8 my $choice = $self->_choice;
955 8 my @lines = ('How shall we play?', '');
956 8 for my $n (0 .. $#{ $choice->{rows} }) {
957 24 my ($name, $values) = @{ $choice->{rows}[$n] };
958 24 my $value = $values->[ $choice->{at}{$name} ];
959 24 my $shown = sprintf '%-8s < %s >', $name, $value;
960 24 ▶ $shown .= ' ' . $LEVEL[ $value - 1 ] if $name eq 'level';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "level" 8 16 24
961 24 ▶ push @lines, ($n == $choice->{row} ? '> ' : ' ') . ($n == $choice->{row} ? $self->_paint($shown, 'key') : $shown);
Branch: 2 branches
BranchTrueFalseTotal
$n == $choice->{'row'} ? : 8 16 24
$n == $choice->{'row'} ? : 8 16 24
962 }
963 8 return \@lines;
964 }
965  
966 2 ▶sub _ask_how_to_play {
Subroutine _ask_how_to_play: called CC=13 SCAR=25.9
967 2 my ($self) = @_;
968 2 my @names = Game::Fanorona::Variant->names;
969 2 my @rows = (
970 [ side => [qw(white black both neither)] ],
971 [ level => [ 1 .. scalar @LEVEL ] ],
972 [ variant => \@names ],
973 );
974 2 my %now = (side => $self->human, level => $self->level, variant => $self->game->variant->name);
975 2 my %at;
976 2 for my $row (@rows) {
977 6 my ($name, $values) = @{$row};
978 6 ($at{$name}) = grep { $values->[$_] eq $now{$name} } 0 .. $#{$values};
979 6 ▶ $at{$name} = 0 unless defined $at{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at{$name} 0 6 6
980 }
981 2 $self->_choice({ rows => \@rows, at => \%at, row => 0 });
982 2 while (1) {
983 8 $self->show('menu');
984 8 my $key = $self->_read_key;
985 8 ▶ return 0 if !defined $key || $key =~ /\A(?:eof|interrupt|q|escape)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key =~ /\A(?:eof|interrupt|q|escape)\z/u 1 7 8
Condition66%2/3
not defined $key or $key =~ /\A(?:eof|interrupt|q|escape)\z/u
l!l&&r!l&&!r
017
MC/DC50%1/2
not defined $key or $key =~ /\A(?:eof|interrupt|q|escape)\z/u
not defined $key $key =~ /\A(?:eof|interrupt|q|escape)\z/u
Truth table66%2/3
not(A) or B
A= not defined $keyB= $key =~ /\A(?:eof|interrupt|q|escape)\z/u
ABresult
1X1
011
000
986 7 my $choice = $self->_choice;
987 7 my ($name, $values) = @{ $rows[ $choice->{row} ] };
988 7 ▶ if ($key eq 'up') { $choice->{row} = ($choice->{row} - 1) % @rows }
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'up') { } 0 7 7
elsif ($key eq 'down' or $key eq 'tab') { } 2 5 7
elsif ($key eq 'left') { } 1 4 5
elsif ($key eq 'right' or $key eq ' ') { } 3 1 4
elsif ($key eq 'enter') { } 1 0 1
989 2 ▶ elsif ($key eq 'down' || $key eq 'tab') { $choice->{row} = ($choice->{row} + 1) % @rows }
Condition66%2/3
$key eq "down" or $key eq "tab"
l!l&&r!l&&!r
205
MC/DC50%1/2
$key eq "down" or $key eq "tab"
$key eq "down" $key eq "tab"
Truth table66%2/3
A or B
A= $key eq "down"B= $key eq "tab"
ABresult
1X1
011
000
990 1 elsif ($key eq 'left') { $at{$name} = ($at{$name} - 1) % @{$values} }
991 3 ▶ elsif ($key eq 'right' || $key eq ' ') { $at{$name} = ($at{$name} + 1) % @{$values} }
Condition66%2/3
$key eq "right" or $key eq " "
l!l&&r!l&&!r
301
MC/DC50%1/2
$key eq "right" or $key eq " "
$key eq "right" $key eq " "
Truth table66%2/3
A or B
A= $key eq "right"B= $key eq " "
ABresult
1X1
011
000
992 1 elsif ($key eq 'enter') { last }
993 }
994 1 $self->human($rows[0][1][ $at{side} ]);
995 1 $self->level($rows[1][1][ $at{level} ]);
996 1 my $variant = $rows[2][1][ $at{variant} ];
997 1 ▶ if ($variant ne $now{variant}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant ne $now{"variant"}) 1 0 1
998 1 $self->variant($variant);
999 1 $self->game($self->_fresh_game);
1000 }
1001 1 $self->_choice(undef);
1002 1 return 1;
1003 }
1004  
1005 202 ▶sub screen {
Subroutine screen: called CC=10 SCAR=23.0
Pod documented
1006 202 my ($self, $mode) = @_;
1007 202 ▶ $mode = 'plain' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 202 202
1008 202 my @lines = ($self->_paint('Fanorona', 'title'), '');
1009 202 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 1 201 202
1010 1 push @lines, @{ $self->_help_lines }, '', $self->_legend('help');
1011 1 return \@lines;
1012 }
1013 201 ▶ if ($mode eq 'rules') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "rules") 1 200 201
1014 1 push @lines, @{ $self->_rules_lines }, '', $self->_legend('rules');
1015 1 return \@lines;
1016 }
1017 200 ▶ if ($mode eq 'menu') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "menu") 8 192 200
1018 8 push @lines, @{ $self->_menu_lines }, '', $self->_legend('menu');
1019 8 return \@lines;
1020 }
1021 192 my $board = $self->board_lines($mode);
1022 192 ▶ if (defined(my $narrow = $self->_too_narrow($board))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $narrow = $self->_too_narrow($board))) 6 186 192
1023 6 push @lines, $narrow;
1024 6 return \@lines;
1025 }
1026 186 my @said = @{ $self->notice };
1027 186 ▶ push @said, $self->_paint($self->_complaint, 'warn') if defined $self->_complaint;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_complaint 10 176 186
1028 186 ▶ push @lines, @said, '' if @said;
Branch: 1 branch
BranchTrueFalseTotal
if @said 26 160 186
1029 186 push @lines, @{$board}, '';
1030 186 push @lines, @{ $self->_status_lines };
1031 186 ▶ push @lines, @{ $self->_preview_lines } if $mode eq 'where' || $mode eq 'ask';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "where" or $mode eq "ask" 108 78 186
Condition100%3/3
$mode eq "where" or $mode eq "ask"
l!l&&r!l&&!r
862278
MC/DC100%2/2
$mode eq "where" or $mode eq "ask"
$mode eq "where" $mode eq "ask"
Truth table100%3/3
A or B
A= $mode eq "where"B= $mode eq "ask"
ABresult
1X1
011
000
1032 186 push @lines, '', $self->_legend($mode);
1033 186 return \@lines;
1034 }
1035  
1036 199 ▶sub show {
Subroutine show: called CC=3 SCAR=11.5
Pod documented
1037 199 my ($self, $mode) = @_;
1038 199 my $out = $self->out;
1039 199 my $lines = $self->screen($mode);
1040 199 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 199 0 199
1041 199 print {$out} "\e[H", join('', map { "$_\e[K\n" } @{$lines}), "\e[J";
1042 }
1043 else {
1044 0 print {$out} "$_\n" for @{$lines};
1045 }
1046 199 $self->_redraw(0);
1047 199 return $self;
1048 }
1049  
1050 116 ▶sub render {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
1051 116 my ($self) = @_;
1052 116 my $board = $self->board_lines('plain');
1053 116 my $narrow = $self->_too_narrow($board);
1054 116 $self->_cramped(0);
1055 116 $self->_say($_) for @{ $self->notice };
1056 116 ▶ $self->_say($_) for defined $narrow ? ($narrow) : (@{$board}, ''), @{ $self->_status_lines };
Branch: 1 branch
BranchTrueFalseTotal
defined $narrow ? : 2 114 116
1057 116 $self->_redraw(0);
1058 116 return $self;
1059 }
1060  
1061 1542 ▶sub _say {
Subroutine _say: called CC=2 SCAR=7.0
1062 1542 my ($self, $line) = @_;
1063 1542 my $out = $self->out;
1064 1542 ▶ print {$out} (defined $line ? $line : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 1542 0 1542
1065 1542 return $self;
1066 }
1067  
1068 33 ▶sub _remark {
Subroutine _remark: called CC=4 SCAR=13.9
1069 33 my ($self, @lines) = @_;
1070 33 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 2 31 33
1071 2 my @all = (@{ $self->notice }, @lines);
1072 2 ▶ splice @all, 0, @all - 8 if @all > 8;
Branch: 1 branch
BranchTrueFalseTotal
if @all > 8 0 2 2
1073 2 $self->notice(\@all);
1074 2 return $self;
1075 }
1076 31 $self->_say($_) for @lines;
1077 31 return $self;
1078 }
1079  
1080 127 ▶sub _read_line {
Subroutine _read_line: called CC=7 SCAR=19.5
1081 127 my ($self, $prompt) = @_;
1082 127 my $out = $self->out;
1083 127 my $game = $self->game;
1084 127 ▶ print {$out} defined $prompt ? $prompt : ($game->turn || 'game over') . '> ';
Branch: 1 branch
BranchTrueFalseTotal
defined $prompt ? : 19 108 127
Condition50%1/2
$game->turn || 'game over'
l!l
1080
MC/DC0%0/1
$game->turn || 'game over'
$game->turn
Truth table50%1/2
A
A= $game->turn
Aresult
11
00
1085 127 ▶ return $self->_keyed_line if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 2 125 127
1086 125 my $line = readline $self->in;
1087 125 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 37 88 125
1088 37 ▶ print {$out} "\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 35 2 37
1089 37 return undef;
1090 }
1091 88 chomp $line;
1092 88 $line =~ s/\r\z//;
1093 88 ▶ print {$out} "$line\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 82 6 88
1094 88 return $line;
1095 }
1096  
1097 2 ▶sub _keyed_line {
Subroutine _keyed_line: called CC=10 SCAR=24.9
1098 2 my ($self) = @_;
1099 2 my $out = $self->out;
1100 2 my $line = '';
1101 2 while (1) {
1102 17 my $key = $self->_read_key;
1103 17 ▶ return undef 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 17 17
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0017
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0017
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
1104 17 ▶ last if $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" 2 15 17
1105 15 ▶ if ($key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape") 0 15 15
1106 0 $line = '';
1107 0 last;
1108 }
1109 15 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 1 14 15
1110 1 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 1 1
1111 1 chop $line;
1112 1 print {$out} "\b \b";
1113 1 next;
1114 }
1115 14 ▶ next if length $key > 1 || $key lt ' ';
Branch: 1 branch
BranchTrueFalseTotal
if length $key > 1 or $key lt " " 0 14 14
Condition33%1/3
length $key > 1 or $key lt " "
l!l&&r!l&&!r
0014
MC/DC0%0/2
length $key > 1 or $key lt " "
length $key > 1 $key lt " "
Truth table33%1/3
A or B
A= length $key > 1B= $key lt " "
ABresult
1X1
011
000
1116 14 $line .= $key;
1117 14 print {$out} $key;
1118 }
1119 2 print {$out} "\n";
1120 2 return $line;
1121 }
1122  
1123 17 my %DO;
1124  
1125 137 ▶sub command {
Subroutine command: called CC=10 SCAR=23.1
Pod documented
1126 137 my ($self, $line) = @_;
1127 137 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 137 137
1128 137 $line =~ s/\A\s+//;
1129 137 $line =~ s/\s+\z//;
1130 137 ▶ return 0 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 12 125 137
1131 125 my ($word, $rest) = split /\s+/, $line, 2;
1132 125 $word = lc $word;
1133 125 ▶ return 1 if $word eq 'quit' || $word eq 'exit' || $word eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "quit" or $word eq "exit" or $word eq "q" 38 87 125
Condition66%4/6
$word eq "quit" or $word eq "exit" or $word eq "q"
l!l&&r!l&&!r
38087
$word eq "quit" or $word eq "exit"
l!l&&r!l&&!r
38087
MC/DC33%1/3
$word eq "quit" or $word eq "exit" or $word eq "q"
$word eq "quit" $word eq "exit" $word eq "q"
Truth table50%2/4
A or B or C
A= $word eq "quit"B= $word eq "exit"C= $word eq "q"
ABCresult
1XX1
01X1
0011
0000
1134 87 ▶ $self->_redraw(1) if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 3 84 87
1135 87 ▶ if (my $do = $DO{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $do = $DO{$word}) 38 49 87
1136 38 my $was = $self->game->over;
1137 38 ▶ $self->$do(defined $rest ? $rest : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $rest ? : 13 25 38
1138 38 ▶ $self->_redraw(1) if $self->game->over != $was;
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->over != $was 6 32 38
1139 38 return 0;
1140 }
1141 49 $self->_play_text($line);
1142 49 return 0;
1143 }
1144  
1145 49 ▶sub _play_text {
Subroutine _play_text: called CC=3 SCAR=11.0
1146 49 my ($self, $text) = @_;
1147 49 my $game = $self->game;
1148 49 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 1 48 49
1149 1 $self->_remark('The game is over. new starts another.');
1150 1 return 0;
1151 }
1152 48 my $side = $game->turn;
1153 48 my $before = $stones_of->($game->position);
1154 48 ▶ unless ($game->play($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->play($text)) 2 46 48
1155 2 $self->_remark("$text: " . $game->error->message . '. help lists the commands.');
1156 2 return 0;
1157 }
1158 46 $self->_played($side, $before);
1159 46 return 1;
1160 }
1161  
1162 7 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod documented
1163 7 my ($self, $file) = @_;
1164 7 ▶ unless (defined $file && length $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and length $file) 1 6 7
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
016
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
1165 1 $self->_remark('load takes the name of a file.');
1166 1 return 0;
1167 }
1168 6 my $fh;
1169 6 ▶ unless (open $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 2 4 6
1170 2 $self->_remark("Cannot read $file: $!.");
1171 2 return 0;
1172 }
1173 4 my $text = do { local $/; <$fh> };
1174 4 close $fh;
1175 4 ▶ my ($game, $why, $line) = Game::Fanorona->from_text(defined $text ? $text : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 4 0 4
1176 4 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 2 2 4
1177 2 ▶ $self->_remark("$file is not a game of fanorona: $why, line " . (defined $line ? $line : '?') . '.');
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 2 0 2
1178 2 return 0;
1179 }
1180 2 $self->_begin($game);
1181 2 my @moves = $game->moves;
1182 2 $self->_remark("Loaded $file: " . scalar(@moves) . ' moves.');
1183 2 return 1;
1184 }
1185  
1186 9 ▶sub _mine {
Subroutine _mine: called CC=4 SCAR=13.9
1187 9 my ($self) = @_;
1188 9 my $game = $self->game;
1189 9 ▶ return undef if $self->human eq 'neither' || $game->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "neither" or $game->over 2 7 9
Condition66%2/3
$self->human eq "neither" or $game->over
l!l&&r!l&&!r
207
MC/DC50%1/2
$self->human eq "neither" or $game->over
$self->human eq "neither" $game->over
Truth table66%2/3
A or B
A= $self->human eq "neither"B= $game->over
ABresult
1X1
011
000
1190 7 ▶ return $self->human eq 'both' ? $game->turn : $self->human;
Branch: 1 branch
BranchTrueFalseTotal
$self->human eq 'both' ? : 5 2 7
1191 }
1192  
1193 5 ▶sub _begin {
Subroutine _begin: called CC=1 SCAR=0.0
1194 5 my ($self, $game) = @_;
1195 5 $self->game($game);
1196 5 $self->last(undef);
1197 5 $self->notice([]);
1198 5 $self->focus(undef);
1199 5 $self->_put_down;
1200 5 $self->_redraw(1);
1201 5 return $self;
1202 }
1203  
1204 %DO = (
1205 1 ▶ help => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
1206 1 my ($self) = @_;
1207 1 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 1 1
1208 0 $self->show('help');
1209 0 $self->_read_key;
1210 0 return;
1211 }
1212 1 $self->_say($_) for @{ $self->_help_lines };
1213 1 return;
1214 },
1215 2 ▶ rules => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
1216 2 my ($self) = @_;
1217 2 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 2 2
1218 0 $self->show('rules');
1219 0 $self->_read_key;
1220 0 return;
1221 }
1222 2 $self->_say($_) for @{ $self->_rules_lines };
1223 2 return;
1224 },
1225 2 ▶ board => sub { $_[0]->_redraw(1); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1226 2 ▶ moves => sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
1227 2 my ($self) = @_;
1228 2 my $game = $self->game;
1229 2 my @steps = $game->steps;
1230 2 ▶ return $self->_remark('The game is over.') unless @steps;
Branch: 1 branch
BranchTrueFalseTotal
unless @steps 0 2 2
1231 2 ▶ return $self->_remark('The stones that can move are the ones the cursor walks.') if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 2 2
1232 2 my %from;
1233 2 push @{ $from{ $_->{from} } }, $_ for @steps;
1234 2 for my $name ($drawn->(keys %from)) {
1235 $self->_say(" $name " . join ' ', map {
1236 10 ▶ $_->{string} . (@{ $_->{takes} } ? ' takes ' . scalar @{ $_->{takes} } : '')
Branch: 1 branch
BranchTrueFalseTotal
@{$_->{'takes'};} ? : 10 0 10
1237 9 } @{ $from{$name} });
1238 }
1239 $self->_say('A stone that has taken may take again: type the whole move, as d2-e3A-e4W.')
1240 2 ▶ if grep { @{ $_->{takes} } } @steps;
Branch: 1 branch
BranchTrueFalseTotal
if grep {@{$_->{"takes"};};} @steps 2 0 2
1241 2 return;
1242 },
1243 2 ▶ hint => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1244 2 my ($self) = @_;
1245 2 my $thought = Game::Fanorona::Bot->hint($self->game);
1246 2 ▶ return $self->_remark('The game is over.') unless $thought;
Branch: 1 branch
BranchTrueFalseTotal
unless $thought 0 2 2
1247 2 return $self->_remark('The program would play ' . $self->_paint($thought->{move}, 'key') . '.');
1248 },
1249 5 ▶ level => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1250 5 my ($self, $rest) = @_;
1251 5 ▶ return $self->_remark('A level is a number from 1 to ' . scalar(@LEVEL) . '.') unless $rest =~ /\A[1-5]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\A[1-5]\z/u 2 3 5
1252 3 $self->level($rest);
1253 3 return $self->_remark("Level $rest: the program " . $LEVEL[ $rest - 1 ] . '.');
1254 },
1255 2 ▶ side => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
1256 2 my ($self, $rest) = @_;
1257 2 $rest = lc $rest;
1258 2 ▶ return $self->_remark('A side is white, black, both or neither.') unless $rest =~ /\A(?:white|black|both|neither)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\A(?:white|black|both|neither)\z/u 1 1 2
1259 1 $self->human($rest);
1260 1 ▶ return $self->_remark($rest eq 'both' ? 'Both sides are played here.'
Branch: 2 branches
BranchTrueFalseTotal
$rest eq 'both' ? : 0 1 1
$rest eq 'neither' ? : 0 1 1
1261 : $rest eq 'neither' ? 'The program plays both sides.' : 'You play ' . $rest . '.');
1262 },
1263 4 ▶ new => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1264 4 my ($self, $rest) = @_;
1265 4 ▶ if (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 2 2 4
1266 2 ▶ return $self->_remark("$rest is not a board. The boards are " . join(', ', Game::Fanorona::Variant->names) . '.')
Branch: 1 branch
BranchTrueFalseTotal
unless "Game::Fanorona::Variant"->from_string($rest) 1 1 2
1267 unless Game::Fanorona::Variant->from_string($rest);
1268 1 $self->variant($rest);
1269 }
1270 3 $self->_begin($self->_fresh_game);
1271 3 return $self->_remark('A new game.');
1272 },
1273 3 ▶ save => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
1274 3 my ($self, $rest) = @_;
1275 3 ▶ return $self->_remark('save takes the name of a file.') unless length $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest 1 2 3
1276 2 my $text = $self->game->as_text;
1277 2 ▶ open my $fh, '>', $rest or return $self->_remark("Cannot write $rest: $!.");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $rest or return $self->_remark("Cannot write ${rest}: $!.") 2 0 2
1278 2 ▶ my $saved = (print {$fh} $text) && close $fh;
Condition33%1/3
print({$fh;} $text) && close $fh
!ll&&!rl&&r
002
MC/DC0%0/2
print({$fh;} $text) && close $fh
print({$fh;} $text) close $fh
Truth table33%1/3
A && B
A= print({$fh;} $text)B= close $fh
ABresult
0X0
100
111
1279 2 ▶ return $self->_remark($saved ? "Saved to $rest." : "Cannot write $rest: $!.");
Branch: 1 branch
BranchTrueFalseTotal
$saved ? : 2 0 2
1280 },
1281 4 ▶ load => sub { $_[0]->load($_[1]); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1282 5 ▶ draw => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
1283 5 my ($self) = @_;
1284 5 my $game = $self->game;
1285 5 my $side = $self->_mine;
1286 5 ▶ return $self->_remark('There is no game being played here to draw.') unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 1 4 5
1287 4 ▶ unless ($game->offer_draw($side)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->offer_draw($side)) 0 4 4
1288 0 return $self->_remark(ucfirst($game->error->message) . '.');
1289 }
1290 4 ▶ if ($self->human eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->human eq "both") 2 2 4
1291 2 return $self->_remark(ucfirst($side) . ' offers a draw. accept or decline answers it.');
1292 }
1293 2 my $reply = Game::Fanorona::Bot->answer_draw($game);
1294 2 ▶ if (defined $reply && $reply eq 'accept') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reply and $reply eq "accept") 1 1 2
Condition66%2/3
defined $reply and $reply eq "accept"
!ll&&!rl&&r
011
MC/DC50%1/2
defined $reply and $reply eq "accept"
defined $reply $reply eq "accept"
Truth table66%2/3
A and B
A= defined $replyB= $reply eq "accept"
ABresult
0X0
100
111
1295 1 $game->accept_draw($other->($side));
1296 1 return $self->_remark('The program accepts.');
1297 }
1298 1 $game->decline_draw($other->($side));
1299 1 return $self->_remark('The program declines the draw.');
1300 },
1301 1 ▶ accept => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1302 1 my ($self) = @_;
1303 1 my $game = $self->game;
1304 1 my $offer = $game->draw_offered_by;
1305 1 ▶ return $self->_remark('No draw has been offered.') unless defined $offer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offer 0 1 1
1306 1 $game->accept_draw($other->($offer));
1307 1 return;
1308 },
1309 1 ▶ decline => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1310 1 my ($self) = @_;
1311 1 my $game = $self->game;
1312 1 my $offer = $game->draw_offered_by;
1313 1 ▶ return $self->_remark('No draw has been offered.') unless defined $offer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offer 0 1 1
1314 1 $game->decline_draw($other->($offer));
1315 1 return $self->_remark(ucfirst($other->($offer)) . ' declines the draw.');
1316 },
1317 4 ▶ resign => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1318 4 my ($self) = @_;
1319 4 my $side = $self->_mine;
1320 4 ▶ return $self->_remark('There is no game being played here to resign.') unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 1 3 4
1321 3 $self->game->resign($side);
1322 3 return;
1323 },
1324 17 );
1325  
1326 17 1;
1327  
1328 __END__
« blib/lib/Game/Fanorona/Notation.pm ↑ Summary blib/lib/Game/Fanorona/Rules.pm »