blib/lib/Game/Fanorona/Terminal.pm

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