blib/lib/Game/Sternhalma/Terminal.pm

filter: Statementstmt 88.3% 825 / 934 Branchbran 74.6% 363 / 486 Conditioncond 55.3% 98 / 177 MC/DCmcdc 37.2% 41 / 110 Subroutinesub 98.0% 102 / 104 Podpod 96.5% 28 / 29 totaltotal 79.1% 1457 / 1840 CC 332 SCAR 32.4
CC
332
Cov
81%
_ask_how_to_play
210.0
_menu_lines
110.0
pick
69.1
CRAP 25.6
↑ Summary blib/lib/Game/Sternhalma/Notation.pm »
1 package Game::Sternhalma::Terminal;
2  
3 23 ▶use 5.014;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 23 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 23 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 23 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 23 ▶use Game::Terminal::Kit::History;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 23 ▶use Game::Terminal::Kit::Keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 23 ▶use Game::Terminal::Kit::Line;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 23 ▶use Game::Terminal::Kit::Log;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 23 ▶use Game::Terminal::Kit::Run;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 23 ▶use Game::Terminal::Kit::Screen;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 23 ▶use Game::Terminal::Kit::Steer;
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 23 ▶use Game::Sternhalma;
Subroutine __ANON__: called CC=1 SCAR=0.0
19 23 ▶use Game::Sternhalma::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
20 23 ▶use Game::Sternhalma::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
21 23 ▶use Game::Sternhalma::Layout ();
Subroutine __ANON__: called CC=1 SCAR=0.0
22 23 ▶use Game::Sternhalma::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 23 our $VERSION = '0.01';
25  
26 23 our (@COMMANDS, @KEYS, @COLOUR, %NAME, %LETTER, %PIECE, %INK, %GROUND, %GLYPH, %FRAME, %ENDING, @RULES, @LEVEL);
27  
28 23 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 23 @COMMANDS = (
30 [ 'e5-f5' => 'play a move: the hole a piece leaves and the hole it ends in' ],
31 [ 'c1:e5:g5' => 'or every hole of a chain of hops, joined by colons' ],
32 [ 'pass' => 'pass, when you have no move' ],
33 [ 'moves' => 'every move you may make' ],
34 [ 'hint' => 'what the program would play here' ],
35 [ 'board' => 'draw the board again' ],
36 [ 'level N' => 'how hard the program plays, 1 to 4' ],
37 [ 'new [N]' => 'start another game, for N seats if given: 2, 3, 4 or 6' ],
38 [ 'save FILE' => 'write the game to a file' ],
39 [ 'load FILE' => 'read a game back from a file' ],
40 [ 'undo' => 'take back your last move, and the replies to it' ],
41 [ 'redo' => 'play again what undo took back' ],
42 [ 'resign' => 'give the game up for the seat to move' ],
43 [ 'rules' => 'the rules of the game' ],
44 [ 'help' => 'this list' ],
45 [ 'quit' => 'stop playing' ],
46 );
47  
48 23 @KEYS = (
49 [ 'arrows' => 'walk the cursor over the pieces that can move; then over the' ],
50 [ '' => 'holes the piece picked up can reach, with its way there shown' ],
51 [ 'tab' => 'the next of them, in the order they are drawn' ],
52 [ 'enter' => 'pick the piece up; then move it to the hole the cursor is on' ],
53 [ 'space' => 'whatever enter would do' ],
54 [ 'escape' => 'put the piece back' ],
55 [ 'h' => 'show what the program would play' ],
56 [ 'u' => 'take back your last move, and the replies to it' ],
57 [ 'r' => 'play again what u took back' ],
58 [ 'R' => 'the rules' ],
59 [ ':' => 'type a command' ],
60 [ 'q' => 'stop playing' ],
61 );
62  
63 # A seat's colour is its starting point's: clockwise from the top, so two
64 # seats are red and blue, three red, green and purple.
65 23 @COLOUR = qw(red yellow green blue purple orange);
66 23 %NAME = map { $_ => ucfirst } @COLOUR;
67 23 %LETTER = (red => 'R', yellow => 'Y', green => 'G', blue => 'B', purple => 'P', orange => 'O');
68  
69 # a piece is its seat's letter, written on its seat's colour
70 23 %PIECE = (
71 red => '48;5;160;1;38;5;231',
72 yellow => '48;5;226;1;38;5;16',
73 green => '48;5;34;1;38;5;16',
74 blue => '48;5;27;1;38;5;231',
75 purple => '48;5;129;1;38;5;231',
76 orange => '48;5;208;1;38;5;16',
77 );
78  
79 23 %GROUND = (
80 board => '48;5;235',
81 cursor => '48;5;231',
82 held => '48;5;201',
83 reach => '48;5;51',
84 path => '48;5;220',
85 last => '48;5;166',
86 );
87  
88 23 %INK = (
89 hole => '38;5;245',
90 red => '38;5;203',
91 yellow => '38;5;227',
92 green => '38;5;77',
93 blue => '38;5;75',
94 purple => '38;5;177',
95 orange => '38;5;215',
96 label => '38;5;109',
97 cursor => '1;38;5;16',
98 held => '1;38;5;16',
99 reach => '1;38;5;16',
100 path => '1;38;5;16',
101 last => '1;38;5;16',
102 title => '1;38;5;220',
103 good => '1;38;5;34',
104 quiet => '38;5;245',
105 key => '1;38;5;51',
106 warn => '1;38;5;208',
107 );
108  
109 23 %GLYPH = (
110 wide => { hole => "\x{00B7}", home => "\x{2022}" },
111 ascii => { hole => '.', home => '.' },
112 );
113  
114 23 %FRAME = (
115 cursor => [ '[', ']' ],
116 held => [ '<', '>' ],
117 reach => [ '(', ')' ],
118 path => [ '{', '}' ],
119 last => [ ':', ':' ],
120 );
121  
122 23 %ENDING = (
123 home => 'Every seat but one is home.',
124 cap => 'The game has reached its cap of %d moves.',
125 deadlock => 'Nobody can move.',
126 resign => 'Nobody is left to race.',
127 );
128  
129 23 @LEVEL = (
130 'moves any piece forward, at random',
131 'takes the move that looks best, and looks no further',
132 'looks ahead',
133 'the strongest here',
134 );
135  
136 23 @RULES = (
137 'Sternhalma is played on a star of 121 holes. Each seat starts with its',
138 'pieces in one point of the star, and races them into the point opposite.',
139 '',
140 'A STEP moves a piece to a neighbouring empty hole, in any of six directions.',
141 '',
142 'A HOP jumps a piece over one neighbouring piece, anyone\'s, into the empty',
143 'hole straight beyond it. A piece may hop again from where it lands, and',
144 'again, as long as it likes, and stop when it likes. Nothing is taken.',
145 '',
146 'A move is a step or a chain of hops, never both. Here a move is chosen by',
147 'its two ends; the way the piece goes is shown before it goes.',
148 '',
149 'A seat is home when every hole of its home is full and at least one piece',
150 'there is its own, so a piece left in your home cannot keep you out. The',
151 'first seat home wins, and the rest race on for the places after.',
152 '',
153 'A seat with no move passes. When every seat still racing passes in a row,',
154 'or the game reaches %d moves, the seats still out are placed by how far',
155 'their pieces are from home.',
156 );
157 }
158  
159 23 has game => (is => 'rw');
160  
161 23 has [qw/in out/] => (is => 'rw');
162  
163 23 has [qw/interactive colour unicode picking/] => (is => 'rw');
164  
165 23 has seats => (is => 'rw', default => 2);
166  
167 23 has pieces => (is => 'rw', default => 10);
168  
169 23 has bots => (is => 'rw');
170  
171 23 has level => (is => 'rw', default => 3);
172  
173 23 has seed => (is => 'rw', default => 1);
174  
175 23 has keysource => (is => 'rw');
176  
177 23 has quiet => (is => 'rw', default => 0);
178  
179 23 has width => (is => 'rw');
180  
181 23 has pause => (is => 'rw');
182  
183 23 has menu => (is => 'rw');
184  
185 23 has [qw/_keyboard _screen _log _frame/] => (is => 'rw');
186  
187 23 has focus => (is => 'rw');
188  
189 23 has chosen => (is => 'rw');
190  
191 23 has target => (is => 'rw');
192  
193 23 has last => (is => 'rw');
194  
195 23 has history => (is => 'rw');
196  
197 23 has _travel => (is => 'rw');
198  
199 23 has _complaint => (is => 'rw');
200  
201 23 has _choice => (is => 'rw');
202  
203 23 has _cramped => (is => 'rw', default => 0);
204  
205 23 has _redraw => (is => 'rw', default => 1);
206  
207 23 my $E = 'Game::Sternhalma::Engine';
208  
209 # WHERE A HOLE IS DRAWN. Row r is the hole's row, 0 at the top; c runs from
210 # -12 to 12 across the widest row, two to a hole, so a row offset by half a
211 # hole is offset by one. A hole is three columns, its mark's brackets either
212 # side of it, and holes are four columns apart, so no mark ever touches
213 # another's or changes a row's width.
214 23 my (@ROW, @C, @BY_ROW);
215 23 for my $h (0 .. Game::Sternhalma::Notation::HOLES - 1) {
216 2783 my ($x, $y) = $E->xy($h);
217 2783 $ROW[$h] = $y + 8;
218 2783 $C[$h] = 2 * $x + $y;
219 2783 push @{ $BY_ROW[ $ROW[$h] ] }, $h;
220 }
221 23 my $MARGIN = 2;
222 23 my $BOARD_WIDTH = $MARGIN + 2 * 24 + 3;
223  
224 13014 ▶sub column { $MARGIN + 2 * ($C[ $_[1] ] + 12) + 1 }
Subroutine column: called CC=1 SCAR=0.0
Pod documented
225  
226 2345 ▶my $plain = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
227 2345 my ($text) = @_;
228 2345 $text =~ s/\e\[[0-9;?]*[A-Za-z]//g;
229 2345 return $text;
230 23 };
231  
232 79 ▶my $name = sub { Game::Sternhalma::Notation::name_of($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
233  
234 270 ▶my $drawn = sub { sort { $a <=> $b } @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
235  
236 23 my @PLACE = qw(0th 1st 2nd 3rd 4th 5th 6th);
237  
238 73 ▶sub BUILD {
Subroutine BUILD: called CC=29 SCAR=35.0
Pod undocumented
239 73 my ($self) = @_;
240 73 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 47 26 73
241 73 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 23 50 73
242 73 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 11 62 73
-t $self->in ? : 0 11 11
243 73 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 21 52 73
$self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? : 0 21 21
Condition33%1/3
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
2100
MC/DC0%0/2
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
$self->interactive not Game::Terminal::Kit::Screen::no_colour()
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not Game::Terminal::Kit::Screen::no_colour()
ABresult
0X0
100
111
244 73 ▶ $self->unicode($self->colour ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 21 52 73
$self->colour ? : 0 21 21
245 73 ▶ $self->_keyboard(Game::Terminal::Kit::Keys->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->keysource ? : 13 60 73
246 in => $self->in,
247 ($self->keysource ? (keysource => $self->keysource) : ()),
248 ));
249 73 ▶ $self->_screen(Game::Terminal::Kit::Screen->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->unicode ? : 1 72 73
defined $self->width ? : 3 70 73
250 out => $self->out,
251 interactive => $self->interactive,
252 colour => $self->colour,
253 ascii => $self->unicode ? 0 : 1,
254 (defined $self->width ? (columns => $self->width) : ()),
255 ));
256 73 $self->_log(Game::Terminal::Kit::Log->new(keep => 8));
257 73 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 20 53 73
$self->interactive ? : 0 20 20
258 73 ▶ $self->picking(0) unless $self->_keyboard->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->keys_available 60 13 73
259 73 ▶ $self->pause($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pause;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pause 32 41 73
$self->interactive && !$self->keysource ? : 0 32 32
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
3200
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
260 73 ▶ $self->menu(0) unless defined $self->menu;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->menu 73 0 73
261  
262 73 ▶ Carp::croak('Game::Sternhalma::Terminal: level is 1 to ' . scalar(@LEVEL))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->level and $self->level =~ /\A[1-4]\z/u 2 71 73
Condition66%2/3
defined $self->level and $self->level =~ /\A[1-4]\z/u
!ll&&!rl&&r
0271
MC/DC50%1/2
defined $self->level and $self->level =~ /\A[1-4]\z/u
defined $self->level $self->level =~ /\A[1-4]\z/u
Truth table66%2/3
A and B
A= defined $self->levelB= $self->level =~ /\A[1-4]\z/u
ABresult
0X0
100
111
263 unless defined $self->level && $self->level =~ /\A[1-4]\z/;
264 71 ▶ Carp::croak('Game::Sternhalma::Terminal: a seed is a whole number')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed and $self->seed =~ /\A[0-9]{1,15}\z/u 0 71 71
Condition33%1/3
defined $self->seed and $self->seed =~ /\A[0-9]{1,15}\z/u
!ll&&!rl&&r
0071
MC/DC0%0/2
defined $self->seed and $self->seed =~ /\A[0-9]{1,15}\z/u
defined $self->seed $self->seed =~ /\A[0-9]{1,15}\z/u
Truth table33%1/3
A and B
A= defined $self->seedB= $self->seed =~ /\A[0-9]{1,15}\z/u
ABresult
0X0
100
111
265 unless defined $self->seed && $self->seed =~ /\A[0-9]{1,15}\z/;
266 71 ▶ if ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game) { } 13 58 71
267 13 $self->seats($self->game->seats);
268 13 $self->pieces($self->game->pieces);
269 }
270 else {
271 58 my $why = Game::Sternhalma::Layout::why_not($self->seats, $self->pieces);
272 58 ▶ Carp::croak("Game::Sternhalma::Terminal: $why") if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 5 53 58
273 }
274 66 ▶ $self->bots([ 1 .. $self->seats - 1 ]) unless $self->bots;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bots 9 57 66
275 66 $self->_check_bots($self->bots);
276 64 ▶ $self->game($self->_fresh_game) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 51 13 64
277  
278 $self->history(Game::Terminal::Kit::History->new(
279 220 ▶ save => sub { $_[0]->log },
Subroutine __ANON__: called CC=1 SCAR=0.0
280 5 ▶ load => sub { Game::Sternhalma->replay($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
281 64 ▶ )) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 64 0 64
282 64 $self->history->start($self->game);
283  
284 64 $self->_screen->utf8->flush_on;
285 64 return;
286 }
287  
288 66 ▶sub _check_bots {
Subroutine _check_bots: called CC=7 SCAR=20.2
289 66 my ($self, $bots) = @_;
290 66 ▶ Carp::croak('Game::Sternhalma::Terminal: bots is a list of seats') unless ref $bots eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bots eq "ARRAY" 0 66 66
291 66 for my $seat (@{$bots}) {
292 33 ▶ Carp::croak('Game::Sternhalma::Terminal: there is no seat ' . ($seat // 'undef') . ' in a game of ' . $self->seats)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat =~ /\A[0-9]+\z/u and $seat < $self->seats 2 31 33
Condition50%4/8
defined $seat and $seat =~ /\A[0-9]+\z/u and $seat < $self->seats
!ll&&!rl&&r
0231
defined $seat and $seat =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0033
$seat // "undef"
l!l
20
MC/DC33%1/3
defined $seat and $seat =~ /\A[0-9]+\z/u and $seat < $self->seats
defined $seat $seat =~ /\A[0-9]+\z/u $seat < $self->seats
MC/DC0%0/1
$seat // "undef"
$seat
Truth table50%2/4
A and B and C
A= defined $seatB= $seat =~ /\A[0-9]+\z/uC= $seat < $self->seats
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $seat
Aresult
11
00
293 unless defined $seat && $seat =~ /\A[0-9]+\z/ && $seat < $self->seats;
294 }
295 64 my %seen;
296 64 $self->bots([ grep { !$seen{$_}++ } sort { $a <=> $b } @{$bots} ]);
297 64 return $self;
298 }
299  
300 54 ▶sub _fresh_game {
Subroutine _fresh_game: called CC=1 SCAR=0.0
301 54 my ($self) = @_;
302 54 return Game::Sternhalma->new(seats => $self->seats, pieces => $self->pieces);
303 }
304  
305 360 ▶sub rung { $_[0]->level - 1 }
Subroutine rung: called CC=1 SCAR=0.0
Pod documented
306  
307 21432 ▶sub colour_of {
Subroutine colour_of: called CC=2 SCAR=7.1
Pod documented
308 21432 my ($self, $seat) = @_;
309 21432 my $point = (Game::Sternhalma::Layout::points($self->game->seats))[$seat];
310 21432 ▶ return defined $point ? $COLOUR[$point] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $point ? : 21432 0 21432
311 }
312  
313 483 ▶sub seat_name { $NAME{ $_[0]->colour_of($_[1]) } }
Subroutine seat_name: called CC=1 SCAR=0.0
Pod documented
314  
315 3611 ▶sub is_bot {
Subroutine is_bot: called CC=1 SCAR=0.0
Pod documented
316 3611 my ($self, $seat) = @_;
317 3611 return scalar grep { $_ == $seat } @{ $self->bots };
318 }
319  
320 378 ▶sub people {
Subroutine people: called CC=1 SCAR=0.0
Pod documented
321 378 my ($self) = @_;
322 378 return grep { !$self->is_bot($_) } 0 .. $self->game->seats - 1;
323 }
324  
325 44 ▶sub start {
Subroutine start: called CC=4 SCAR=15.1
Pod documented
326 44 my ($self) = @_;
327 44 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 44 44
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
31130
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
328 44 $self->history->start($self->game);
329  
330 44 ▶ $self->_say($self->_paint('Sternhalma.', 'title') . ' Type help for the commands.') unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 31 13 44
331 Game::Terminal::Kit::Run::guard(
332 44 ▶ sub { $self->_turns if !$self->menu || !$self->raw || $self->_ask_how_to_play },
Subroutine __ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
if not $self->menu && $self->raw or $self->_ask_how_to_play 44 0 44
Condition33%2/6
not $self->menu && $self->raw or $self->_ask_how_to_play
l!l&&r!l&&!r
4400
$self->menu && $self->raw
!ll&&!rl&&r
4400
MC/DC0%0/3
not $self->menu && $self->raw or $self->_ask_how_to_play
$self->menu $self->raw $self->_ask_how_to_play
Truth table25%1/4
not(A && B) or C
A= $self->menuB= $self->rawC= $self->_ask_how_to_play
ABCresult
0XX1
10X1
1111
1100
333 44 $self->_keyboard, $self->_screen,
334 );
335 44 return 0;
336 }
337  
338 44 ▶sub _turns {
Subroutine _turns: called CC=14 SCAR=26.4
339 44 my ($self) = @_;
340 44 while (1) {
341 465 ▶ my $picking = $self->picking && $self->raw;
Condition66%2/3
$self->picking && $self->raw
!ll&&!rl&&r
435030
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
342 465 my $game = $self->game;
343  
344 465 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 13 452 465
345 13 ▶ $self->person_turn if $self->people;
Branch: 1 branch
BranchTrueFalseTotal
if $self->people 9 4 13
346 13 ▶ my $line = $picking ? $self->_pick_ended : $self->_read_ended;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 2 11 13
347 13 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 8 5 13
348 5 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 2 3 5
349 3 next;
350 }
351 452 ▶ if ($self->is_bot($game->to_move)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bot($game->to_move)) 357 95 452
352 357 $self->_bot_move;
353 357 next;
354 }
355  
356 95 $self->person_turn;
357 95 ▶ $self->render if $self->_redraw && !$picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_redraw and not $picking 48 47 95
Condition100%3/3
$self->_redraw and not $picking
!ll&&!rl&&r
222548
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
358 95 ▶ my $line = $picking ? $self->pick : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 25 70 95
359 95 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 19 76 95
360 19 ▶ $self->_say('Bye.') unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 19 0 19
361 19 last;
362 }
363 76 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 15 61 76
364 }
365 44 return $self;
366 }
367  
368 104 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
369 104 my ($self) = @_;
370 104 $self->history->turn($self->game);
371 104 return $self;
372 }
373  
374 11 ▶sub _read_ended {
Subroutine _read_ended: called CC=2 SCAR=6.9
375 11 my ($self) = @_;
376 11 ▶ $self->render if $self->_redraw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_redraw 6 5 11
377 11 return $self->_read_line('new or quit> ');
378 }
379  
380 2 ▶sub _pick_ended {
Subroutine _pick_ended: called CC=12 SCAR=33.1
381 2 my ($self) = @_;
382 2 $self->_put_down;
383 2 while (1) {
384 2 $self->show('ended');
385 2 my $key = $self->_read_key;
386 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
387 2 ▶ return 'new' if $key eq 'n' || $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" or $key eq "enter" 0 2 2
Condition33%1/3
$key eq "n" or $key eq "enter"
l!l&&r!l&&!r
002
MC/DC0%0/2
$key eq "n" or $key eq "enter"
$key eq "n" $key eq "enter"
Truth table33%1/3
A or B
A= $key eq "n"B= $key eq "enter"
ABresult
1X1
011
000
388 2 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 1 1 2
389 1 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 1 1
390 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
391 0 ▶ return 'rules' if $key eq 'R';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "R" 0 0 0
392 0 ▶ return $self->_read_line(': ') if $key eq ':';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq ":" 0 0 0
393 }
394 }
395  
396 357 ▶sub _bot_move {
Subroutine _bot_move: called CC=8 SCAR=21.2
397 357 my ($self) = @_;
398 357 my $game = $self->game;
399 357 my $seat = $game->to_move;
400 357 ▶ $self->show('thinking') if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 3 354 357
401 357 ▶ my $move = Game::Sternhalma::Bot->choose($game, rung => $self->rung, seed => $self->seed) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $move = "Game::Sternhalma::Bot"->choose($game, "rung", $self->rung, "seed", $self->seed) or return undef 357 0 357
402 357 ▶ my $path = $move->{kind} eq 'pass' ? [] : [ $game->board->path($move->{from}, $move->{to}) ];
Branch: 1 branch
BranchTrueFalseTotal
$move->{'kind'} eq 'pass' ? : 0 357 357
403 357 $self->_go($path, $seat);
404 357 my $flag = $game->play($move);
405 357 ▶ return undef if defined $flag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flag 0 357 357
406 357 $self->_played($seat);
407 357 ▶ if (!$self->people) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->people) 352 5 357
408 352 ▶ $self->render unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 352 0 352
409 352 ▶ select undef, undef, undef, 0.3 if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 352 352
410 }
411 357 return ($game->moves)[-1];
412 }
413  
414 # THE WAY THERE, LIT HOLE BY HOLE, before the move is made: only on a screen
415 # that is redrawn, and only when there is time to watch it.
416 365 ▶sub _go {
Subroutine _go: called CC=5 SCAR=25.2
417 365 my ($self, $path, $seat) = @_;
418 365 ▶ return $self unless @{$path} > 1 && $self->pause && $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$path;} > 1 and $self->pause and $self->raw 365 0 365
Condition33%2/6
@{$path;} > 1 and $self->pause and $self->raw
!ll&&!rl&&r
36500
@{$path;} > 1 and $self->pause
!ll&&!rl&&r
03650
MC/DC0%0/3
@{$path;} > 1 and $self->pause and $self->raw
@{$path;} > 1 $self->pause $self->raw
Truth table25%1/4
A and B and C
A= @{$path;} > 1B= $self->pauseC= $self->raw
ABCresult
0XX0
10X0
1100
1111
419 0 for my $i (1 .. $#{$path}) {
420 0 $self->_travel({ seat => $seat, path => $path, at => $i });
421 0 $self->show('travel');
422 0 select undef, undef, undef, 0.12;
423 }
424 0 $self->_travel(undef);
425 0 return $self;
426 }
427  
428 388 ▶sub _played {
Subroutine _played: called CC=5 SCAR=16.1
429 388 my ($self, $seat) = @_;
430 388 my $game = $self->game;
431 388 my $move = ($game->moves)[-1];
432 388 $self->last({ seat => $seat, move => $move, path => _path_of($move) });
433 388 my $who = $self->seat_name($seat);
434 388 ▶ my $line = $move eq 'pass' ? "$who has no move, and passes."
Branch: 2 branches
BranchTrueFalseTotal
$move eq 'pass' ? : 0 388 388
$move =~ /\Aresign/u ? : 2 386 388
435 : $move =~ /\Aresign/ ? "$who resigns."
436 : "$who played " . $self->_paint($move, 'key') . '.';
437 388 ▶ if ($self->raw) { $self->_log->forget->record($line) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) { } 11 377 388
438 377 else { $self->_say($line) }
439 388 ▶ if (grep { $_ == $seat } $game->finished) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $seat;} $game->finished) 8 380 388
440 8 my $place = $PLACE[ ($game->places)[$seat] ];
441 8 $self->_remark($self->_paint("$who is home, $place.", 'good'));
442 }
443 388 $self->_redraw(1);
444 388 return $self;
445 }
446  
447 390 ▶sub _path_of {
Subroutine _path_of: called CC=3 SCAR=11.0
448 390 my ($move) = @_;
449 390 my $m = Game::Sternhalma::Notation::parse_move($move);
450 390 ▶ return [] unless $m && $m->{holes};
Branch: 1 branch
BranchTrueFalseTotal
unless $m and $m->{"holes"} 2 388 390
Condition66%2/3
$m and $m->{"holes"}
!ll&&!rl&&r
02388
MC/DC50%1/2
$m and $m->{"holes"}
$m $m->{"holes"}
Truth table66%2/3
A and B
A= $mB= $m->{"holes"}
ABresult
0X0
100
111
451 388 return [ @{ $m->{holes} } ];
452 }
453  
454 1411 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
455 1411 my ($self) = @_;
456 1411 return $self->_keyboard->raw;
457 }
458  
459 497 ▶sub notice {
Subroutine notice: called CC=1 SCAR=0.0
Pod documented
460 497 my ($self) = @_;
461 497 return [ $self->_log->lines ];
462 }
463  
464 13 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=4 SCAR=14.6
465 13 my ($self) = @_;
466 13 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 13 13
467 13 ▶ return undef unless $self->_keyboard->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->enter_raw 0 13 13
468 13 ▶ $self->_screen->hide_cursor unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 13 13
469 13 return $self;
470 }
471  
472 88 ▶sub _read_key {
Subroutine _read_key: called CC=1 SCAR=0.0
473 88 my ($self) = @_;
474 88 return $self->_keyboard->read_key;
475 }
476  
477 # ---- choosing a move with the keys ---------------------------------------------------------
478  
479 87 ▶sub lit {
Subroutine lit: called CC=2 SCAR=6.9
Pod documented
480 87 my ($self) = @_;
481 87 my %from;
482 87 $from{ $_->{from} } = 1 for grep { $_->{kind} eq 'move' } $self->game->legal;
483 87 return [ $drawn->(keys %from) ];
484 }
485  
486 181 ▶sub reach {
Subroutine reach: called CC=4 SCAR=14.0
Pod documented
487 181 my ($self, $from) = @_;
488 181 ▶ $from = $self->chosen unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 48 133 181
489 181 ▶ return [] unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 181 181
490 181 ▶ return [ $drawn->(map { $_->{to} } grep { $_->{kind} eq 'move' && $_->{from} == $from } $self->game->legal) ];
Condition66%2/3
$_->{'kind'} eq 'move' && $_->{'from'} == $from
!ll&&!rl&&r
02706505
MC/DC50%1/2
$_->{'kind'} eq 'move' && $_->{'from'} == $from
$_->{'kind'} eq 'move' $_->{'from'} == $from
Truth table66%2/3
A && B
A= $_->{'kind'} eq 'move'B= $_->{'from'} == $from
ABresult
0X0
100
111
491 }
492  
493 28 ▶sub way {
Subroutine way: called CC=4 SCAR=14.0
Pod documented
494 28 my ($self, $from, $to) = @_;
495 28 ▶ my ($move) = grep { $_->{kind} eq 'move' && $_->{from} == $from && $_->{to} == $to } $self->game->legal;
Condition83%5/6
$_->{'kind'} eq 'move' && $_->{'from'} == $from && $_->{'to'} == $to
!ll&&!rl&&r
4206428
$_->{"kind"} eq "move" and $_->{"from"} == $from
!ll&&!rl&&r
042092
MC/DC66%2/3
$_->{'kind'} eq 'move' && $_->{'from'} == $from && $_->{'to'} == $to
$_->{"kind"} eq "move" $_->{"from"} == $from $_->{'to'} == $to
Truth table75%3/4
A and B && C
A= $_->{"kind"} eq "move"B= $_->{"from"} == $fromC= $_->{'to'} == $to
ABCresult
0XX0
10X0
1100
1111
496 28 ▶ return $move ? $move->{path} : [];
Branch: 1 branch
BranchTrueFalseTotal
$move ? : 28 0 28
497 }
498  
499 15 ▶sub steer {
Subroutine steer: called CC=3 SCAR=11.0
Pod documented
500 15 my ($self, $from, $key, $among) = @_;
501 15 ▶ my @way = Game::Terminal::Kit::Steer::way($key) or return $from;
Branch: 1 branch
BranchTrueFalseTotal
my(@way) = Game::Terminal::Kit::Steer::way($key) or return $from 14 1 15
502 14 my @holes = ($from, grep { $_ != $from } @{$among});
503 14 my @points = map { [ $C[$_], 1.75 * $ROW[$_] ] } @holes;
504 14 my $to = Game::Terminal::Kit::Steer::nearest(\@points, 0, @way, cone => 1);
505 14 ▶ return defined $to ? $holes[$to] : $from;
Branch: 1 branch
BranchTrueFalseTotal
defined $to ? : 11 3 14
506 }
507  
508 66 ▶sub _put_down {
Subroutine _put_down: called CC=1 SCAR=0.0
509 66 my ($self) = @_;
510 66 $self->chosen(undef);
511 66 $self->target(undef);
512 66 $self->_complaint(undef);
513 66 return $self;
514 }
515  
516 35 ▶sub _ring {
Subroutine _ring: called CC=3 SCAR=11.0
517 35 my ($self, $list, $at, $key) = @_;
518 35 my ($i) = grep { $list->[$_] == $at } 0 .. $#{$list};
519 35 ▶ $i = 0 unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 35 35
520 35 ▶ return $list->[ ($i + ($key eq 'tab' ? 1 : -1)) % @{$list} ];
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'tab' ? : 34 1 35
521 }
522  
523 25 ▶sub pick {
Subroutine pick: called CC=47 SCAR=42.4
Pod documented
524 25 my ($self) = @_;
525 25 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 25 25
526 25 $self->_put_down;
527  
528 25 while (1) {
529 84 my $game = $self->game;
530 84 my $lit = $self->lit;
531 84 ▶ my $mode = defined $self->chosen ? 'where' : @{$lit} ? 'which' : 'pass';
Branch: 2 branches
BranchTrueFalseTotal
defined $self->chosen ? : 19 65 84
@{$lit;} ? : 65 0 65
532 84 ▶ if ($mode eq 'which') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which") 65 19 84
533 65 ▶ $self->focus($lit->[0]) unless defined $self->focus && grep { $_ == $self->focus } @{$lit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->focus and grep {$_ == $self->focus;} @{$lit;} 21 44 65
Condition100%3/3
defined $self->focus and grep {$_ == $self->focus;} @{$lit;}
!ll&&!rl&&r
18344
MC/DC100%2/2
defined $self->focus and grep {$_ == $self->focus;} @{$lit;}
defined $self->focus grep {$_ == $self->focus;} @{$lit;}
Truth table100%3/3
A and B
A= defined $self->focusB= grep {$_ == $self->focus;} @{$lit;}
ABresult
0X0
100
111
534 }
535 84 ▶ if ($mode eq 'where') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "where") 19 65 84
536 19 my $reach = $self->reach;
537 19 ▶ $self->target($reach->[0]) unless defined $self->target && grep { $_ == $self->target } @{$reach};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->target and grep {$_ == $self->target;} @{$reach;} 9 10 19
Condition66%2/3
defined $self->target and grep {$_ == $self->target;} @{$reach;}
!ll&&!rl&&r
9010
MC/DC50%1/2
defined $self->target and grep {$_ == $self->target;} @{$reach;}
defined $self->target grep {$_ == $self->target;} @{$reach;}
Truth table66%2/3
A and B
A= defined $self->targetB= grep {$_ == $self->target;} @{$reach;}
ABresult
0X0
100
111
538 }
539  
540 84 $self->show($mode);
541 84 my $key = $self->_read_key;
542 84 ▶ 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 84 84
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0084
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0084
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
543 84 $self->_complaint(undef);
544 84 ▶ if ($self->_cramped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cramped) 0 84 84
545 0 ▶ return 'quit' if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 0 0 0
546 0 next;
547 }
548  
549 84 ▶ if (my @way = Game::Terminal::Kit::Steer::way($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@way) = Game::Terminal::Kit::Steer::way($key)) 8 76 84
550 8 ▶ if ($mode eq 'which') { $self->focus($self->steer($self->focus, $key, $lit)) }
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'which') { } 8 0 8
elsif ($mode eq 'where') { } 0 0 0
551 0 elsif ($mode eq 'where') { $self->target($self->steer($self->target, $key, $self->reach)) }
552 8 next;
553 }
554 76 ▶ if ($key eq 'tab' || $key eq 'back_tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tab" or $key eq "back_tab") 35 41 76
Condition100%3/3
$key eq "tab" or $key eq "back_tab"
l!l&&r!l&&!r
34141
MC/DC100%2/2
$key eq "tab" or $key eq "back_tab"
$key eq "tab" $key eq "back_tab"
Truth table100%3/3
A or B
A= $key eq "tab"B= $key eq "back_tab"
ABresult
1X1
011
000
555 35 ▶ if ($mode eq 'which') { $self->focus($self->_ring($lit, $self->focus, $key)) }
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'which') { } 26 9 35
elsif ($mode eq 'where') { } 9 0 9
556 9 elsif ($mode eq 'where') { $self->target($self->_ring($self->reach, $self->target, $key)) }
557 35 next;
558 }
559 41 ▶ if ($key eq 'home' || $key eq 'end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home" or $key eq "end") 2 39 41
Condition100%3/3
$key eq "home" or $key eq "end"
l!l&&r!l&&!r
1139
MC/DC100%2/2
$key eq "home" or $key eq "end"
$key eq "home" $key eq "end"
Truth table100%3/3
A or B
A= $key eq "home"B= $key eq "end"
ABresult
1X1
011
000
560 2 ▶ my $list = $mode eq 'which' ? $lit : $mode eq 'where' ? $self->reach : next;
Branch: 2 branches
BranchTrueFalseTotal
$mode eq 'which' ? : 2 0 2
$mode eq 'where' ? : 0 0 0
561 2 ▶ my $to = $key eq 'home' ? $list->[0] : $list->[-1];
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'home' ? : 1 1 2
562 0 ▶ if ($mode eq 'which') { $self->focus($to) } else { $self->target($to) }
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'which') { } 2 0 2
563 2 next;
564 }
565 39 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 17 22 39
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
17022
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
566 17 ▶ if ($mode eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "pass") 0 17 17
567 0 my $seat = $game->to_move;
568 0 $game->play('pass');
569 0 $self->_played($seat);
570 0 return '';
571 }
572 17 ▶ if ($mode eq 'which') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which") 9 8 17
573 9 $self->chosen($self->focus);
574 9 $self->target(undef);
575 9 next;
576 }
577 8 my ($from, $to, $seat) = ($self->chosen, $self->target, $game->to_move);
578 8 my $path = $self->way($from, $to);
579 8 $self->_go($path, $seat);
580 8 ▶ if (defined(my $flag = $game->play({ from => $from, to => $to }))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $flag = $game->play({"from", $from, "to", $to}))) 0 8 8
581 0 $self->_complaint(ucfirst(Game::Sternhalma->reason($flag)) . '.');
582 0 next;
583 }
584 8 $self->_put_down;
585 8 $self->focus($to);
586 8 $self->_played($seat);
587 8 return '';
588 }
589 22 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 1 21 22
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
1021
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
590 1 ▶ $self->focus($self->chosen) if defined $self->chosen;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->chosen 1 0 1
591 1 $self->_put_down;
592 1 next;
593 }
594 21 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 1 20 21
595 1 my $move = Game::Sternhalma::Bot->choose($game, rung => (Game::Sternhalma::Bot->rungs)[-1], seed => $self->seed);
596 1 ▶ next unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 1 1
597 1 ▶ if ($move->{kind} eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->{"kind"} eq "pass") 0 1 1
598 0 $self->_complaint('The program would pass. Enter passes.');
599 0 next;
600 }
601 1 $self->chosen($move->{from});
602 1 $self->target($move->{to});
603 1 $self->_complaint('The program would play ' . $self->_paint($game->board->move_text($move->{from}, $move->{to}), 'key')
604 . '. Enter plays it; escape puts the piece back.');
605 1 next;
606 }
607 20 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 1 19 20
608 1 $self->show('help');
609 1 $self->_read_key;
610 1 next;
611 }
612 19 ▶ if ($key eq 'u' || $key eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "u" or $key eq "r") 5 14 19
Condition100%3/3
$key eq "u" or $key eq "r"
l!l&&r!l&&!r
4114
MC/DC100%2/2
$key eq "u" or $key eq "r"
$key eq "u" $key eq "r"
Truth table100%3/3
A or B
A= $key eq "u"B= $key eq "r"
ABresult
1X1
011
000
613 5 $self->_put_down;
614 5 ▶ return $key eq 'u' ? 'undo' : 'redo';
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'u' ? : 4 1 5
615 }
616 14 ▶ if ($key eq 'R') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "R") 1 13 14
617 1 $self->show('rules');
618 1 $self->_read_key;
619 1 next;
620 }
621 13 ▶ if ($key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":") 0 13 13
622 0 $self->_put_down;
623 0 return $self->_read_line(': ');
624 }
625 13 ▶ if ($key eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 12 1 13
626 12 $self->_put_down;
627 12 return 'quit';
628 }
629 1 ▶ next if $key eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "unknown" 0 1 1
630 1 $self->_complaint('That key does nothing here. Press ? for the ones that do.');
631 }
632 }
633  
634 # ---- drawing --------------------------------------------------------------------------------
635  
636 509 ▶sub marks {
Subroutine marks: called CC=16 SCAR=29.1
Pod documented
637 509 my ($self, $mode) = @_;
638 509 my $game = $self->game;
639 509 my %piece;
640 509 for my $h (0 .. Game::Sternhalma::Notation::HOLES - 1) {
641 61589 my $seat = $game->at($h);
642 61589 ▶ $piece{$h} = $seat if defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seat 16990 44599 61589
643 }
644 509 my %mark;
645 509 ▶ if (my $travel = $self->_travel) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $travel = $self->_travel) 0 509 509
646 0 my @path = @{ $travel->{path} };
647 0 delete $piece{ $path[0] };
648 0 $mark{ $path[$_] } = 'path' for 0 .. $travel->{at} - 1;
649 0 $piece{ $path[ $travel->{at} ] } = $travel->{seat};
650 0 $mark{ $path[ $travel->{at} ] } = 'held';
651 0 return (\%mark, \%piece);
652 }
653 509 ▶ if ($mode ne 'where' && (my $last = $self->last)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "where" and my $last = $self->last) 389 120 509
Condition100%3/3
$mode ne "where" and my $last = $self->last
!ll&&!rl&&r
20100389
MC/DC100%2/2
$mode ne "where" and my $last = $self->last
$mode ne "where" my $last = $self->last
Truth table100%3/3
A and B
A= $mode ne "where"B= my $last = $self->last
ABresult
0X0
100
111
654 389 $mark{$_} = 'last' for @{ $last->{path} };
655 }
656 509 ▶ if ($mode eq 'which' && defined $self->focus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which" and defined $self->focus) 66 443 509
Condition66%2/3
$mode eq "which" and defined $self->focus
!ll&&!rl&&r
443066
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
657 66 $mark{$_} = 'reach' for @{ $self->reach($self->focus) };
658 66 $mark{ $self->focus } = 'cursor';
659 }
660 509 ▶ if ($mode eq 'where' && defined $self->chosen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "where" and defined $self->chosen) 20 489 509
Condition66%2/3
$mode eq "where" and defined $self->chosen
!ll&&!rl&&r
489020
MC/DC50%1/2
$mode eq "where" and defined $self->chosen
$mode eq "where" defined $self->chosen
Truth table66%2/3
A and B
A= $mode eq "where"B= defined $self->chosen
ABresult
0X0
100
111
661 20 $mark{$_} = 'reach' for @{ $self->reach };
662 20 ▶ if (defined(my $target = $self->target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $target = $self->target)) 20 0 20
663 20 my @path = @{ $self->way($self->chosen, $target) };
664 20 $mark{ $path[$_] } = 'path' for 1 .. $#path - 1;
665 20 $mark{$target} = 'cursor';
666 }
667 20 $mark{ $self->chosen } = 'held';
668 }
669 509 return (\%mark, \%piece);
670 }
671  
672 9887 ▶sub _paint {
Subroutine _paint: called CC=1 SCAR=0.0
673 9887 my ($self, $text, $ink) = @_;
674 9887 return $self->_screen->paint($text, $INK{$ink});
675 }
676  
677 239888 ▶sub _paint_with {
Subroutine _paint_with: called CC=1 SCAR=0.0
678 239888 my ($self, $text, $sgr) = @_;
679 239888 return $self->_screen->paint($text, $sgr);
680 }
681  
682 44599 ▶sub _glyph {
Subroutine _glyph: called CC=2 SCAR=6.9
683 44599 my ($self, $what) = @_;
684 44599 ▶ return $GLYPH{ $self->unicode ? 'wide' : 'ascii' }{$what};
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 101 44498 44599
685 }
686  
687 # whose home a hole is in, for the colour of an empty hole
688 509 ▶sub _home_colours {
Subroutine _home_colours: called CC=2 SCAR=6.9
689 509 my ($self) = @_;
690 509 my $seats = $self->game->seats;
691 509 my %by_point;
692 509 for my $seat (0 .. $seats - 1) {
693 1699 $by_point{ Game::Sternhalma::Layout::home_of($seats, $seat) } = $self->colour_of($seat);
694 }
695 509 return \%by_point;
696 }
697  
698 61589 ▶sub _cell {
Subroutine _cell: called CC=6 SCAR=17.9
699 61589 my ($self, $h, $seat, $mark, $homes) = @_;
700 61589 ▶ my ($open, $close) = $mark ? @{ $FRAME{$mark} } : (' ', ' ');
Branch: 1 branch
BranchTrueFalseTotal
$mark ? : 1212 60377 61589
701 61589 my $middle;
702 61589 ▶ if (defined $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seat) { } 16990 44599 61589
703 16990 my $colour = $self->colour_of($seat);
704 16990 $middle = $self->_paint_with($LETTER{$colour}, $PIECE{$colour});
705 }
706 else {
707 44599 my $home = $homes->{ $E->point_of($h) };
708 44599 ▶ $middle = $self->_paint_with($self->_glyph($home ? 'home' : 'hole'), "$GROUND{board};$INK{ $home // 'hole' }");
Branch: 1 branch
BranchTrueFalseTotal
$home ? : 6798 37801 44599
Condition100%2/2
$home // "hole"
l!l
679837801
MC/DC100%1/1
$home // "hole"
$home
Truth table100%2/2
A
A= $home
Aresult
11
00
709 }
710 61589 ▶ return $self->_paint_with(' ', $GROUND{board}) . $middle . $self->_paint_with(' ', $GROUND{board}) unless $mark;
Branch: 1 branch
BranchTrueFalseTotal
unless $mark 60377 1212 61589
711 1212 return $self->_paint_with($open, "$GROUND{$mark};$INK{$mark}") . $middle . $self->_paint_with($close, "$GROUND{$mark};$INK{$mark}");
712 }
713  
714 509 ▶sub _panel {
Subroutine _panel: called CC=7 SCAR=19.5
715 509 my ($self) = @_;
716 509 my $game = $self->game;
717 509 my @places = $game->places;
718 509 my @lines = ($self->_paint(' seat who left', 'quiet'));
719 509 for my $seat (0 .. $game->seats - 1) {
720 1699 my $colour = $self->colour_of($seat);
721 1699 ▶ my $turn = !$game->over && $game->to_move == $seat ? '>' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
!$game->over && $game->to_move == $seat ? : 498 1201 1699
Condition100%3/3
not $game->over and $game->to_move == $seat
!ll&&!rl&&r
251176498
MC/DC100%2/2
not $game->over and $game->to_move == $seat
not $game->over $game->to_move == $seat
Truth table100%3/3
not(A) and B
A= not $game->overB= $game->to_move == $seat
ABresult
0X0
100
111
722 1699 ▶ my $who = $self->is_bot($seat) ? 'level ' . $self->level : 'you';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_bot($seat) ? : 1408 291 1699
723 my $where = !$places[$seat] ? $game->distance_left($seat)
724 1699 ▶ : (grep { $_ == $seat } $game->finished) ? "home $PLACE[ $places[$seat] ]"
Branch: 2 branches
BranchTrueFalseTotal
!$places[$seat] ? : 1610 89 1699
grep({$_ == $seat;} $game->finished) ? : 72 17 89
725 : "out $PLACE[ $places[$seat] ]";
726 push @lines, sprintf '%s %s %-6s %-7s %s', $turn, $self->_paint_with($LETTER{$colour}, $PIECE{$colour}),
727 1699 $NAME{$colour}, $who, $where;
728 }
729 509 return \@lines;
730 }
731  
732 509 ▶sub board_lines {
Subroutine board_lines: called CC=6 SCAR=17.9
Pod documented
733 509 my ($self, $mode) = @_;
734 509 ▶ my ($mark, $piece) = $self->marks(defined $mode ? $mode : 'plain');
Branch: 1 branch
BranchTrueFalseTotal
defined $mode ? : 501 8 509
735 509 my $homes = $self->_home_colours;
736 509 my @lines;
737 509 for my $r (0 .. $#BY_ROW) {
738 8653 my @holes = @{ $BY_ROW[$r] };
739 8653 my $first = $self->column($holes[0]) - 1;
740 8653 my $line = (' ' x ($first - 2)) . $self->_paint(chr(97 + $r), 'label') . ' ';
741 8653 for my $i (0 .. $#holes) {
742 61589 ▶ $line .= $self->_paint_with(' ', $GROUND{board}) if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 52936 8653 61589
743 61589 $line .= $self->_cell($holes[$i], $piece->{ $holes[$i] }, $mark->{ $holes[$i] }, $homes);
744 }
745 8653 push @lines, $line;
746 }
747 509 my $panel = $self->_panel;
748 509 for my $i (0 .. $#{$panel}) {
749 2208 my $at = $i + 1;
750 2208 my $pad = $BOARD_WIDTH + 2 - length $plain->($lines[$at]);
751 2208 $lines[$at] .= (' ' x $pad) . $panel->[$i];
752 }
753 509 return \@lines;
754 }
755  
756 498 ▶sub _columns {
Subroutine _columns: called CC=4 SCAR=15.2
757 498 my ($self) = @_;
758 498 ▶ return $self->width if defined $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->width 8 490 498
759 490 ▶ return undef unless $self->interactive && !$self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->keysource 490 0 490
Condition66%2/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
401890
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
760 0 my ($wide) = $self->_screen->measured;
761 0 return $wide;
762 }
763  
764 498 ▶sub _too_narrow {
Subroutine _too_narrow: called CC=4 SCAR=13.9
765 498 my ($self, $lines) = @_;
766 498 $self->_cramped(0);
767 498 my $have = $self->_columns;
768 498 ▶ return undef unless defined $have && $have > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have and $have > 0 490 8 498
Condition66%2/3
defined $have and $have > 0
!ll&&!rl&&r
49008
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
769 8 my ($need) = sort { $b <=> $a } map { length $plain->($_) } @{$lines};
770 8 ▶ return undef if $have >= $need;
Branch: 1 branch
BranchTrueFalseTotal
if $have >= $need 7 1 8
771 1 $self->_cramped(1);
772 1 return "This window is $have columns wide and the board needs $need. Widen it and press a key, or q to stop.";
773 }
774  
775 497 ▶sub _status_lines {
Subroutine _status_lines: called CC=4 SCAR=14.0
776 497 my ($self) = @_;
777 497 my $game = $self->game;
778 497 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 11 486 497
779 11 my @places = $game->places;
780 11 my @order = sort { $places[$a] <=> $places[$b] } 0 .. $#places;
781 11 (my $how = $ENDING{ $game->how }) =~ s/%d/$game->cap/e;
782 return [
783 $self->_paint($how, 'good'),
784 11 $self->_paint(join(', ', map { "$PLACE[ $places[$_] ] " . $self->seat_name($_) } @order) . '.', 'good'),
785 ];
786 }
787 486 my $seat = $game->to_move;
788 486 my $colour = $self->colour_of($seat);
789 my @lines = (sprintf 'Move %d. %s to play, %s.', $game->ply + 1, $NAME{$colour},
790 486 $self->_paint_with($LETTER{$colour}, $PIECE{$colour}));
791 486 ▶ push @lines, "$NAME{$colour} has no move, and must pass." if $game->legal && ($game->legal)[0]{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $game->legal and +($game->legal)[0]->{"kind"} eq "pass" 0 486 486
Condition33%1/3
$game->legal and +($game->legal)[0]->{"kind"} eq "pass"
!ll&&!rl&&r
04860
MC/DC0%0/2
$game->legal and +($game->legal)[0]->{"kind"} eq "pass"
$game->legal +($game->legal)[0]->{"kind"} eq "pass"
Truth table33%1/3
A and B
A= $game->legalB= +($game->legal)[0]->{"kind"} eq "pass"
ABresult
0X0
100
111
792 486 return \@lines;
793 }
794  
795 91 ▶sub _preview_lines {
Subroutine _preview_lines: called CC=7 SCAR=19.6
796 91 my ($self, $mode) = @_;
797 91 ▶ if ($mode eq 'which' && defined $self->focus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "which" and defined $self->focus) 65 26 91
Condition66%2/3
$mode eq "which" and defined $self->focus
!ll&&!rl&&r
26065
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
798 65 my $n = @{ $self->reach($self->focus) };
799 65 ▶ return [ 'The piece at ' . $name->($self->focus) . ' can reach ' . ($n == 1 ? '1 hole' : "$n holes") . '.' ];
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 1 64 65
800 }
801 26 ▶ return [] unless $mode eq 'where' && defined $self->chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "where" and defined $self->chosen 7 19 26
Condition66%2/3
$mode eq "where" and defined $self->chosen
!ll&&!rl&&r
7019
MC/DC50%1/2
$mode eq "where" and defined $self->chosen
$mode eq "where" defined $self->chosen
Truth table66%2/3
A and B
A= $mode eq "where"B= defined $self->chosen
ABresult
0X0
100
111
802 19 my $from = $self->chosen;
803 19 my $to = $self->target;
804 19 ▶ return [ 'Choose where the piece at ' . $name->($from) . ' goes.' ] unless defined $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to 0 19 19
805 19 my $text = $self->game->board->move_text($from, $to);
806 19 return [ 'Enter moves it ' . $self->_paint($text, 'key') . '.' ];
807 }
808  
809 93 ▶sub _legend {
Subroutine _legend: called CC=9 SCAR=22.7
810 93 my ($self, $mode) = @_;
811 93 my $text;
812 93 ▶ if ($mode eq 'which') { $text = 'arrows choose a piece enter pick it up h hint ? help : command q quit' }
Branch: 6 branches
BranchTrueFalseTotal
if ($mode eq 'which') { } 65 28 93
elsif ($mode eq 'where') { } 19 9 28
elsif ($mode eq 'pass') { } 0 9 9
elsif ($mode eq 'ended') { } 2 7 9
elsif ($mode eq 'thinking' or $mode eq 'travel') { } 3 4 7
elsif ($mode eq 'menu') { } 0 4 4
813 19 elsif ($mode eq 'where') { $text = 'arrows choose where it goes enter move it there esc put it back' }
814 0 elsif ($mode eq 'pass') { $text = 'enter pass ? help : command q quit' }
815 2 elsif ($mode eq 'ended') { $text = 'n new game u undo r redo R rules : command q quit' }
816 3 ▶ elsif ($mode eq 'thinking' || $mode eq 'travel') { $text = 'The program is thinking.' }
Condition66%2/3
$mode eq "thinking" or $mode eq "travel"
l!l&&r!l&&!r
304
MC/DC50%1/2
$mode eq "thinking" or $mode eq "travel"
$mode eq "thinking" $mode eq "travel"
Truth table66%2/3
A or B
A= $mode eq "thinking"B= $mode eq "travel"
ABresult
1X1
011
000
817 0 elsif ($mode eq 'menu') { $text = 'up and down choose a line left and right change it enter play q quit' }
818 4 else { $text = 'Any key goes back.' }
819 93 ▶ $text = '' if $mode eq 'travel';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "travel" 0 93 93
820 93 return $self->_paint($text, 'quiet');
821 }
822  
823 65 ▶sub _history_legend {
Subroutine _history_legend: called CC=4 SCAR=13.9
824 65 my ($self) = @_;
825 65 my $history = $self->history;
826 65 ▶ my @keys = (($history->can_undo ? ('u undo') : ()), ($history->can_redo ? ('r redo') : ()));
Branch: 2 branches
BranchTrueFalseTotal
$history->can_undo ? : 8 57 65
$history->can_redo ? : 4 61 65
827 65 ▶ return @keys ? $self->_paint(join(' ', @keys), 'quiet') : ();
Branch: 1 branch
BranchTrueFalseTotal
@keys ? : 11 54 65
828 }
829  
830 2 ▶sub _help_lines {
Subroutine _help_lines: called CC=3 SCAR=11.0
831 2 my ($self) = @_;
832 2 my @lines = ('The keys:', '');
833 2 push @lines, ' ' . $self->_paint(sprintf('%-10s', $_->[0]), 'key') . ' ' . $_->[1] for @KEYS;
834 2 push @lines, '', 'The commands, typed after a colon or at the prompt:', '';
835 2 push @lines, ' ' . $self->_paint(sprintf('%-10s', $_->[0]), 'key') . ' ' . $_->[1] for @COMMANDS;
836 2 return \@lines;
837 }
838  
839 2 ▶sub _rules_lines {
Subroutine _rules_lines: called CC=2 SCAR=6.9
840 2 my ($self) = @_;
841 2 my $cap = $self->game->cap;
842 2 ▶ return [ map { /%d/ ? sprintf($_, $cap) : $_ } @RULES ];
Branch: 1 branch
BranchTrueFalseTotal
/%d/u ? : 2 36 38
843 }
844  
845 0 ▶sub _menu_lines {
Subroutine _menu_lines: not called CC=10 SCAR=47.0
846 0 my ($self) = @_;
847 0 my $choice = $self->_choice;
848 0 my @lines = ('How shall we play?', '');
849 0 for my $n (0 .. $#{ $choice->{rows} }) {
850 0 my ($label, $values) = @{ $choice->{rows}[$n] };
851 0 my $value = $values->[ $choice->{at}{$label} ];
852 0 my $shown = sprintf '%-8s < %s >', $label, $value;
853 0 ▶ $shown .= ' ' . $LEVEL[ $value - 1 ] if $label eq 'level';
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "level" 0 0 0
854 0 ▶ $shown .= ' seat 0 to ' . ($value - 1) . ' at the keys' if $label eq 'people' && $value > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "people" and $value > 1 0 0 0
Condition0%0/3
$label eq "people" and $value > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$label eq "people" and $value > 1
$label eq "people" $value > 1
Truth table0%0/3
A and B
A= $label eq "people"B= $value > 1
ABresult
0X0
100
111
855 0 ▶ $shown .= ' seat 0 at the keys' if $label eq 'people' && $value == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "people" and $value == 1 0 0 0
Condition0%0/3
$label eq "people" and $value == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$label eq "people" and $value == 1
$label eq "people" $value == 1
Truth table0%0/3
A and B
A= $label eq "people"B= $value == 1
ABresult
0X0
100
111
856 0 ▶ $shown .= ' the program plays every seat' if $label eq 'people' && $value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "people" and $value == 0 0 0 0
Condition0%0/3
$label eq "people" and $value == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$label eq "people" and $value == 0
$label eq "people" $value == 0
Truth table0%0/3
A and B
A= $label eq "people"B= $value == 0
ABresult
0X0
100
111
857 0 ▶ push @lines, ($n == $choice->{row} ? '> ' . $self->_paint($shown, 'key') : " $shown");
Branch: 1 branch
BranchTrueFalseTotal
$n == $choice->{'row'} ? : 0 0 0
858 }
859 0 return \@lines;
860 }
861  
862 0 ▶sub _ask_how_to_play {
Subroutine _ask_how_to_play: not called CC=14 SCAR=53.5
863 0 my ($self) = @_;
864 0 my @rows = (
865 [ seats => [ Game::Sternhalma::Layout::seat_counts() ] ],
866 [ people => [ 0 .. 6 ] ],
867 [ level => [ 1 .. scalar @LEVEL ] ],
868 [ pieces => [ 10, 15 ] ],
869 );
870 0 my %now = (seats => $self->seats, people => scalar($self->people), level => $self->level, pieces => $self->pieces);
871 0 my %at;
872 0 for my $row (@rows) {
873 0 my ($label, $values) = @{$row};
874 0 ($at{$label}) = grep { $values->[$_] == $now{$label} } 0 .. $#{$values};
875 0 ▶ $at{$label} = 0 unless defined $at{$label};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at{$label} 0 0 0
876 }
877 0 $self->_choice({ rows => \@rows, at => \%at, row => 0 });
878 0 while (1) {
879 0 $self->show('menu');
880 0 my $key = $self->_read_key;
881 0 ▶ 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 0 0 0
Condition0%0/3
not defined $key or $key =~ /\A(?:eof|interrupt|q|escape)\z/u
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/3
not(A) or B
A= not defined $keyB= $key =~ /\A(?:eof|interrupt|q|escape)\z/u
ABresult
1X1
011
000
882 0 my $choice = $self->_choice;
883 0 my ($label, $values) = @{ $rows[ $choice->{row} ] };
884 0 ▶ if ($key eq 'up') { $choice->{row} = ($choice->{row} - 1) % @rows }
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'up') { } 0 0 0
elsif ($key eq 'down' or $key eq 'tab') { } 0 0 0
elsif ($key eq 'left') { } 0 0 0
elsif ($key eq 'right' or $key eq ' ') { } 0 0 0
elsif ($key eq 'enter') { } 0 0 0
885 0 ▶ elsif ($key eq 'down' || $key eq 'tab') { $choice->{row} = ($choice->{row} + 1) % @rows }
Condition0%0/3
$key eq "down" or $key eq "tab"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "down" or $key eq "tab"
$key eq "down" $key eq "tab"
Truth table0%0/3
A or B
A= $key eq "down"B= $key eq "tab"
ABresult
1X1
011
000
886 0 elsif ($key eq 'left') { $at{$label} = ($at{$label} - 1) % @{$values} }
887 0 ▶ elsif ($key eq 'right' || $key eq ' ') { $at{$label} = ($at{$label} + 1) % @{$values} }
Condition0%0/3
$key eq "right" or $key eq " "
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "right" or $key eq " "
$key eq "right" $key eq " "
Truth table0%0/3
A or B
A= $key eq "right"B= $key eq " "
ABresult
1X1
011
000
888 0 elsif ($key eq 'enter') { last }
889 }
890 0 my %got = map { $rows[$_][0] => $rows[$_][1][ $at{ $rows[$_][0] } ] } 0 .. $#rows;
891 0 ▶ $got{pieces} = 10 unless Game::Sternhalma::Layout::pieces_ok($got{seats}, $got{pieces});
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Sternhalma::Layout::pieces_ok($got{"seats"}, $got{"pieces"}) 0 0 0
892 0 ▶ $got{people} = $got{seats} if $got{people} > $got{seats};
Branch: 1 branch
BranchTrueFalseTotal
if $got{"people"} > $got{"seats"} 0 0 0
893 0 $self->level($got{level});
894 0 $self->seats($got{seats});
895 0 $self->pieces($got{pieces});
896 0 $self->bots([ $got{people} .. $got{seats} - 1 ]);
897 0 $self->swap($self->_fresh_game);
898 0 $self->history->start($self->game);
899 0 $self->_choice(undef);
900 0 return 1;
901 }
902  
903 94 ▶sub screen {
Subroutine screen: called CC=10 SCAR=23.2
Pod documented
904 94 my ($self, $mode) = @_;
905 94 ▶ $mode = 'plain' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 94 94
906 94 my @lines = ($self->_paint('Sternhalma', 'title'), '');
907 94 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 1 93 94
908 1 push @lines, @{ $self->_help_lines }, '', $self->_legend('help');
909 1 return \@lines;
910 }
911 93 ▶ if ($mode eq 'rules') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "rules") 1 92 93
912 1 push @lines, @{ $self->_rules_lines }, '', $self->_legend('rules');
913 1 return \@lines;
914 }
915 92 ▶ if ($mode eq 'menu') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "menu") 0 92 92
916 0 push @lines, @{ $self->_menu_lines }, '', $self->_legend('menu');
917 0 return \@lines;
918 }
919 92 my $board = $self->board_lines($mode);
920 92 ▶ if (defined(my $narrow = $self->_too_narrow($board))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $narrow = $self->_too_narrow($board))) 1 91 92
921 1 push @lines, $narrow;
922 1 return \@lines;
923 }
924 91 my @said = @{ $self->notice };
925 91 ▶ push @said, $self->_paint($self->_complaint, 'warn') if defined $self->_complaint;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_complaint 2 89 91
926 91 ▶ push @lines, @said, '' if @said;
Branch: 1 branch
BranchTrueFalseTotal
if @said 20 71 91
927 91 push @lines, @{$board}, '';
928 91 push @lines, @{ $self->_status_lines };
929 91 push @lines, @{ $self->_preview_lines($mode) };
930 91 push @lines, '', $self->_legend($mode);
931 91 ▶ push @lines, $self->_history_legend if $mode eq 'which' || $mode eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "which" or $mode eq "pass" 65 26 91
Condition66%2/3
$mode eq "which" or $mode eq "pass"
l!l&&r!l&&!r
65026
MC/DC50%1/2
$mode eq "which" or $mode eq "pass"
$mode eq "which" $mode eq "pass"
Truth table66%2/3
A or B
A= $mode eq "which"B= $mode eq "pass"
ABresult
1X1
011
000
932 91 return \@lines;
933 }
934  
935 91 ▶sub show {
Subroutine show: called CC=1 SCAR=0.0
Pod documented
936 91 my ($self, $mode) = @_;
937 91 my $lines = $self->screen($mode);
938 91 $self->_frame($lines);
939 91 $self->_screen->show($lines);
940 91 $self->_redraw(0);
941 91 return $self;
942 }
943  
944 406 ▶sub render {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
945 406 my ($self) = @_;
946 406 my $board = $self->board_lines('plain');
947 406 my $narrow = $self->_too_narrow($board);
948 406 $self->_cramped(0);
949 406 $self->_say($_) for @{ $self->notice };
950 406 ▶ $self->_say($_) for defined $narrow ? ($narrow) : (@{$board}, ''), @{ $self->_status_lines };
Branch: 1 branch
BranchTrueFalseTotal
defined $narrow ? : 0 406 406
951 406 $self->_redraw(0);
952 406 return $self;
953 }
954  
955 1 ▶sub _plain_line { $plain->($_[1]) }
Subroutine _plain_line: called CC=1 SCAR=0.0
956  
957 8248 ▶sub _say {
Subroutine _say: called CC=1 SCAR=0.0
958 8248 my ($self, $line) = @_;
959 8248 $self->_screen->say($line);
960 8248 return $self;
961 }
962  
963 41 ▶sub _remark {
Subroutine _remark: called CC=6 SCAR=17.9
964 41 my ($self, @lines) = @_;
965 41 ▶ if ($self->quiet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->quiet) 1 40 41
966 1 print STDERR $self->_plain_line($_), "\n" for @lines;
967 1 return $self;
968 }
969 40 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 8 32 40
970 8 $self->_log->record($_) for @lines;
971 8 return $self;
972 }
973 32 $self->_say($_) for @lines;
974 32 return $self;
975 }
976  
977 81 ▶sub _read_line {
Subroutine _read_line: called CC=10 SCAR=25.2
978 81 my ($self, $prompt) = @_;
979 81 my $out = $self->out;
980 81 my $game = $self->game;
981 81 ▶ my $ask = defined $prompt ? $prompt : ($game->over ? 'game over' : lc $self->seat_name($game->to_move)) . '> ';
Branch: 2 branches
BranchTrueFalseTotal
defined $prompt ? : 11 70 81
$game->over ? : 0 70 70
982 81 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 81 81
983 0 ▶ my $typed = Game::Terminal::Kit::Line->new(prompt => $ask, head => $self->_frame || []);
Condition0%0/2
$self->_frame || []
l!l
00
MC/DC0%0/1
$self->_frame || []
$self->_frame
Truth table0%0/2
A
A= $self->_frame
Aresult
11
00
984 0 my ($event, $text) = $typed->run($self->_keyboard, $self->_screen);
985 0 ▶ return $event eq 'chosen' ? $text : $event eq 'back' ? '' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$event eq 'chosen' ? : 0 0 0
$event eq 'back' ? : 0 0 0
986 }
987 81 print {$out} $ask;
988 81 my $line = readline $self->in;
989 81 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 27 54 81
990 27 ▶ print {$out} "\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 26 1 27
991 27 return undef;
992 }
993 54 chomp $line;
994 54 $line =~ s/\r\z//;
995 54 ▶ print {$out} "$line\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 49 5 54
996 54 return $line;
997 }
998  
999 # ---- commands -------------------------------------------------------------------------------
1000  
1001 23 my %DO;
1002  
1003 84 ▶sub command {
Subroutine command: called CC=10 SCAR=23.1
Pod documented
1004 84 my ($self, $line) = @_;
1005 84 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 84 84
1006 84 $line =~ s/\A\s+//;
1007 84 $line =~ s/\s+\z//;
1008 84 ▶ return 0 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 10 74 84
1009 74 my ($word, $rest) = split /\s+/, $line, 2;
1010 74 $word = lc $word;
1011 74 ▶ 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" 17 57 74
Condition66%4/6
$word eq "quit" or $word eq "exit" or $word eq "q"
l!l&&r!l&&!r
17057
$word eq "quit" or $word eq "exit"
l!l&&r!l&&!r
17057
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
1012 57 ▶ $self->_redraw(1) if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 6 51 57
1013 57 ▶ if (my $do = $DO{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $do = $DO{$word}) 33 24 57
1014 33 my $was = $self->game->over;
1015 33 ▶ $self->$do(defined $rest ? $rest : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $rest ? : 13 20 33
1016 33 ▶ $self->_redraw(1) if $self->game->over != $was;
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->over != $was 3 30 33
1017 33 return 0;
1018 }
1019 24 $self->_play_text($line);
1020 24 return 0;
1021 }
1022  
1023 24 ▶sub _play_text {
Subroutine _play_text: called CC=4 SCAR=14.0
1024 24 my ($self, $text) = @_;
1025 24 my $game = $self->game;
1026 24 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 1 23 24
1027 1 $self->_remark('The game is over. new starts another.');
1028 1 return 0;
1029 }
1030 23 my $seat = $game->to_move;
1031 23 ▶ if ($self->is_bot($seat)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bot($seat)) 0 23 23
1032 0 $self->_remark('It is the program\'s turn.');
1033 0 return 0;
1034 }
1035 23 ▶ if (defined(my $flag = $game->play($text))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $flag = $game->play($text))) 2 21 23
1036 2 $self->_remark("$text: " . Game::Sternhalma->reason($flag) . '. help lists the commands.');
1037 2 return 0;
1038 }
1039 21 $self->_played($seat);
1040 21 return 1;
1041 }
1042  
1043 9 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod documented
1044 9 my ($self, $file) = @_;
1045 9 ▶ unless (defined $file && length $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and length $file) 1 8 9
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
018
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
1046 1 $self->_remark('load takes the name of a file.');
1047 1 return 0;
1048 }
1049 8 my $fh;
1050 8 ▶ unless (open $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 2 6 8
1051 2 $self->_remark("Cannot read $file: $!.");
1052 2 return 0;
1053 }
1054 6 my $text = do { local $/; <$fh> };
1055 6 close $fh;
1056 6 ▶ my $game = eval { Game::Sternhalma->replay(defined $text ? $text : '') };
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 6 0 6
1057 6 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 5 6
1058 1 (my $why = "$@") =~ s/ at \S+ line \d+\.?\s*\z//;
1059 1 $self->_remark("$file is not a game of sternhalma: $why.");
1060 1 return 0;
1061 }
1062 5 $self->seats($game->seats);
1063 5 $self->pieces($game->pieces);
1064 5 $self->bots([ grep { $_ < $game->seats } @{ $self->bots } ]);
1065 5 $self->swap($game);
1066 5 $self->history->start($game);
1067 5 my @moves = $game->moves;
1068 5 $self->_remark("Loaded $file: " . scalar(@moves) . ' moves.');
1069 5 return 1;
1070 }
1071  
1072 13 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
1073 13 my ($self, $game) = @_;
1074 13 $self->game($game);
1075 13 $self->last(undef);
1076 13 $self->_log->forget;
1077 13 $self->focus(undef);
1078 13 $self->_put_down;
1079 13 $self->_redraw(1);
1080 13 return $self;
1081 }
1082  
1083 5 ▶sub take_back {
Subroutine take_back: called CC=3 SCAR=11.0
Pod documented
1084 5 my ($self) = @_;
1085 5 ▶ my $game = $self->people ? $self->history->undo : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->people ? : 5 0 5
1086 5 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 4 5
1087 1 $self->_remark('There is nothing to take back.');
1088 1 return 0;
1089 }
1090 4 $self->swap($game);
1091 4 $self->last($self->_recall);
1092 4 $self->_remark('Taken back.');
1093 4 return 1;
1094 }
1095  
1096 1 ▶sub play_again {
Subroutine play_again: called CC=3 SCAR=11.8
Pod documented
1097 1 my ($self) = @_;
1098 1 ▶ my $game = $self->people ? $self->history->redo : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->people ? : 1 0 1
1099 1 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 1 1
1100 0 $self->_remark('There is nothing to play again.');
1101 0 return 0;
1102 }
1103 1 $self->swap($game);
1104 1 $self->last($self->_recall);
1105 1 $self->_remark('Played again.');
1106 1 return 1;
1107 }
1108  
1109 5 ▶sub _recall {
Subroutine _recall: called CC=2 SCAR=6.9
1110 5 my ($self) = @_;
1111 5 my @moves = $self->game->moves;
1112 5 ▶ return undef unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 3 2 5
1113 2 return { move => $moves[-1], path => _path_of($moves[-1]) };
1114 }
1115  
1116 3 ▶sub _resigner {
Subroutine _resigner: called CC=4 SCAR=16.0
1117 3 my ($self) = @_;
1118 3 my $game = $self->game;
1119 3 ▶ return undef if $game->over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->over 1 2 3
1120 0 my %out = map { $_ => 1 } grep { ($game->places)[$_] } 0 .. $game->seats - 1;
1121 2 my $seat = $game->to_move;
1122 2 ▶ return $seat unless $self->is_bot($seat) || $out{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_bot($seat) or $out{$seat} 2 0 2
Condition33%1/3
$self->is_bot($seat) or $out{$seat}
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->is_bot($seat) or $out{$seat}
$self->is_bot($seat) $out{$seat}
Truth table33%1/3
A or B
A= $self->is_bot($seat)B= $out{$seat}
ABresult
1X1
011
000
1123 0 my ($person) = grep { !$out{$_} } $self->people;
1124 0 return $person;
1125 }
1126  
1127 %DO = (
1128 1 ▶ help => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
1129 1 my ($self) = @_;
1130 1 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 1 1
1131 0 $self->show('help');
1132 0 $self->_read_key;
1133 0 return;
1134 }
1135 1 $self->_say($_) for @{ $self->_help_lines };
1136 1 return;
1137 },
1138 1 ▶ rules => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
1139 1 my ($self) = @_;
1140 1 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 0 1 1
1141 0 $self->show('rules');
1142 0 $self->_read_key;
1143 0 return;
1144 }
1145 1 $self->_say($_) for @{ $self->_rules_lines };
1146 1 return;
1147 },
1148 1 ▶ board => sub { $_[0]->_redraw(1); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1149 5 ▶ undo => sub { $_[0]->take_back; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1150 1 ▶ redo => sub { $_[0]->play_again; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1151 2 ▶ moves => sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
1152 2 my ($self) = @_;
1153 2 my @legal = $self->game->legal;
1154 2 ▶ return $self->_remark('The game is over.') unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 0 2 2
1155 2 ▶ return $self->_remark('You have no move, and must pass: type pass.') if $legal[0]{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $legal[0]{"kind"} eq "pass" 0 2 2
1156 2 ▶ return $self->_remark('The pieces that can move are the ones the cursor walks.') if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 2 2
1157 2 my %from;
1158 2 push @{ $from{ $_->{from} } }, $_ for @legal;
1159 2 for my $h ($drawn->(keys %from)) {
1160 14 $self->_say(sprintf ' %-4s %s', $name->($h), join ' ', map { $_->{text} } sort { $a->{to} <=> $b->{to} } @{ $from{$h} });
1161 }
1162 2 return;
1163 },
1164 2 ▶ hint => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
1165 2 my ($self) = @_;
1166 2 my $game = $self->game;
1167 2 my $move = Game::Sternhalma::Bot->choose($game, rung => (Game::Sternhalma::Bot->rungs)[-1], seed => $self->seed);
1168 2 ▶ return $self->_remark('The game is over.') unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 2 2
1169 2 ▶ return $self->_remark('The program would pass.') if $move->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $move->{"kind"} eq "pass" 0 2 2
1170 2 return $self->_remark('The program would play ' . $self->_paint($game->board->move_text($move->{from}, $move->{to}), 'key') . '.');
1171 },
1172 4 ▶ level => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1173 4 my ($self, $rest) = @_;
1174 4 ▶ return $self->_remark('A level is a number from 1 to ' . scalar(@LEVEL) . '.') unless $rest =~ /\A[1-4]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ /\A[1-4]\z/u 2 2 4
1175 2 $self->level($rest);
1176 2 return $self->_remark("Level $rest: the program " . $LEVEL[ $rest - 1 ] . '.');
1177 },
1178 4 ▶ new => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1179 4 my ($self, $rest) = @_;
1180 4 ▶ if (length $rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 3 1 4
1181 3 my $why = Game::Sternhalma::Layout::why_not($rest, 10);
1182 3 ▶ return $self->_remark(ucfirst($why) . '.') if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 1 2 3
1183 2 my $people = () = $self->people;
1184 2 $self->seats($rest);
1185 2 ▶ $self->pieces(10) unless Game::Sternhalma::Layout::pieces_ok($rest, $self->pieces);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Sternhalma::Layout::pieces_ok($rest, $self->pieces) 0 2 2
1186 2 ▶ $people = $rest if $people > $rest;
Branch: 1 branch
BranchTrueFalseTotal
if $people > $rest 0 2 2
1187 2 $self->bots([ $people .. $rest - 1 ]);
1188 }
1189 3 $self->swap($self->_fresh_game);
1190 3 $self->history->start($self->game);
1191 3 return $self->_remark('A new game, for ' . $self->seats . ' seats.');
1192 },
1193 4 ▶ save => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
1194 4 my ($self, $rest) = @_;
1195 4 ▶ return $self->_remark('save takes the name of a file.') unless length $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rest 1 3 4
1196 3 my $text = $self->game->log;
1197 3 ▶ 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}: $!.") 3 0 3
1198 3 ▶ my $saved = (print {$fh} $text) && close $fh;
Condition33%1/3
print({$fh;} $text) && close $fh
!ll&&!rl&&r
003
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
1199 3 ▶ return $self->_remark($saved ? "Saved to $rest." : "Cannot write $rest: $!.");
Branch: 1 branch
BranchTrueFalseTotal
$saved ? : 3 0 3
1200 },
1201 5 ▶ load => sub { $_[0]->load($_[1]); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
1202 3 ▶ resign => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1203 3 my ($self) = @_;
1204 3 my $seat = $self->_resigner;
1205 3 ▶ return $self->_remark('There is nobody here to resign.') unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 1 2 3
1206 2 $self->game->resign($seat);
1207 2 $self->_played($seat);
1208 2 return;
1209 },
1210 23 );
1211  
1212 # ---- the pipe -------------------------------------------------------------------------------
1213 #
1214 # ONE LINE IN, ONE LINE OUT, for a program at the other end. Each move read is
1215 # answered `ok POSITION` or `FLAG POSITION`; each move the program makes is
1216 # written `bot MOVE POSITION`; the end is `over HOW PLACES`.
1217  
1218 2 ▶sub pipe {
Subroutine pipe: called CC=8 SCAR=20.8
Pod documented
1219 2 my ($self) = @_;
1220 2 my $game = $self->game;
1221 2 my $out = $self->out;
1222 2 print {$out} 'start ', $game->position, "\n";
1223 2 while (1) {
1224 5 ▶ while (!$game->over && $self->is_bot($game->to_move)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $game->over and $self->is_bot($game->to_move)) 2 5 7
Condition100%3/3
not $game->over and $self->is_bot($game->to_move)
!ll&&!rl&&r
142
MC/DC100%2/2
not $game->over and $self->is_bot($game->to_move)
not $game->over $self->is_bot($game->to_move)
Truth table100%3/3
not(A) and B
A= not $game->overB= $self->is_bot($game->to_move)
ABresult
0X0
100
111
1225 2 my $move = Game::Sternhalma::Bot->choose($game, rung => $self->rung, seed => $self->seed);
1226 2 $game->play($move);
1227 2 print {$out} 'bot ', ($game->moves)[-1], ' ', $game->position, "\n";
1228 }
1229 5 ▶ if ($game->over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->over) 1 4 5
1230 1 print {$out} 'over ', $game->how, ' ', join(',', $game->places), "\n";
1231 1 return 0;
1232 }
1233 4 my $line = readline $self->in;
1234 4 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 4 4
1235 4 $line =~ s/\s+\z//;
1236 4 $line =~ s/\A\s+//;
1237 4 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 4 4
1238 4 ▶ return 0 if $line eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" 1 3 4
1239 3 my $flag = $game->play($line);
1240 3 ▶ print {$out} defined $flag ? $flag : 'ok', ' ', $game->position, "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $flag ? : 1 2 3
1241 }
1242 }
1243  
1244 # ---- the program ----------------------------------------------------------------------------
1245  
1246 14 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
1247 14 return <<'USAGE';
1248 sternhalma - the race across a six-pointed star, in a terminal
1249  
1250 sternhalma [options]
1251  
1252 --seats N 2 (the default), 3, 4 or 6
1253 --pieces N 10 (the default), or 15 for two or three seats
1254 --people N how many play at the keys, as seats 0 up: 1 by default
1255 --bots LIST the seats the program plays instead, as 1,2; none or all
1256 --level N how hard the program plays: 1 to 4, 3 by default
1257 --seed N which of its equal choices the program makes
1258 --load FILE carry on with a saved game
1259 --pipe a move a line in, the position a line out, for programs
1260 --no-colour letters and dots instead of a painted board
1261 --no-pick type moves instead of choosing them with the arrow keys
1262 --no-unicode plain dots for the holes, with colour kept
1263 --version the version, and stop
1264 --help this, and stop
1265  
1266 In a terminal, moves are chosen on the board with the arrow keys; press ? for
1267 the keys. Anywhere else, type a move as the hole a piece leaves and the hole
1268 it ends in, like d4-e5, or help.
1269 USAGE
1270 }
1271  
1272 3 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
1273 return [
1274 3 { name => 'seats', type => 'enum', choices => [qw(2 3 4 6)], help => 'how many seats race' },
1275 { name => 'pieces', type => 'enum', choices => [qw(10 15)], help => 'pieces a seat; fifteen for two or three seats only' },
1276 { name => 'people', type => 'int', min => 0, max => 6, help => 'how many play at the keys, the rest played by the program' },
1277 { name => 'level', type => 'int', min => 1, max => 4, help => 'how hard the program plays' },
1278 ];
1279 }
1280  
1281 31 ▶sub run {
Subroutine run: called CC=26 SCAR=32.7
Pod documented
1282 31 my ($class, @argv) = @_;
1283  
1284 31 my %given;
1285 31 my $parsed = Getopt::Long::GetOptionsFromArray(
1286 \@argv, \%given,
1287 'seats=i', 'pieces=i', 'people=i', 'bots=s', 'level=i', 'seed=i', 'load=s', 'pipe',
1288 'colour|color!', 'pick!', 'unicode!', 'version', 'help',
1289 );
1290  
1291 31 ▶ if (!$parsed || @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed or @argv) 3 28 31
Condition100%3/3
not $parsed or @argv
l!l&&r!l&&!r
2128
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
1292 3 ▶ print STDERR "sternhalma: I do not know '@argv'.\n" if $parsed && @argv;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed and @argv 1 2 3
Condition66%2/3
$parsed and @argv
!ll&&!rl&&r
201
MC/DC50%1/2
$parsed and @argv
$parsed @argv
Truth table66%2/3
A and B
A= $parsedB= @argv
ABresult
0X0
100
111
1293 3 print STDERR _usage();
1294 3 return 2;
1295 }
1296 28 ▶ if ($given{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"help"}) 11 17 28
1297 11 print _usage();
1298 11 return 0;
1299 }
1300 17 ▶ if ($given{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"version"}) 1 16 17
1301 1 print "sternhalma $Game::Sternhalma::VERSION\n";
1302 1 return 0;
1303 }
1304  
1305 8 ▶ my $fail = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1306 8 (my $why = $_[0]) =~ s/ at \S+ line \d+\.?\s*\z//;
1307 8 $why =~ s/\AGame::Sternhalma(?:::\w+)*: //;
1308 8 $why =~ s/\.\z//;
1309 8 print STDERR "sternhalma: $why.\n";
1310 8 return 2;
1311 16 };
1312  
1313 16 my %with;
1314 16 for my $key (qw(seats pieces level seed colour unicode)) {
1315 96 ▶ $with{$key} = $given{$key} if defined $given{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{$key} 11 85 96
1316 }
1317 16 ▶ $with{picking} = $given{pick} if defined $given{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"pick"} 0 16 16
1318 16 ▶ my $seats = defined $given{seats} ? $given{seats} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $given{'seats'} ? : 5 11 16
1319 16 ▶ return $fail->('give --people or --bots, not both') if defined $given{people} && defined $given{bots};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"people"} and defined $given{"bots"} 1 15 16
Condition100%3/3
defined $given{"people"} and defined $given{"bots"}
!ll&&!rl&&r
1411
MC/DC100%2/2
defined $given{"people"} and defined $given{"bots"}
defined $given{"people"} defined $given{"bots"}
Truth table100%3/3
A and B
A= defined $given{"people"}B= defined $given{"bots"}
ABresult
0X0
100
111
1320 15 ▶ if (defined $given{people}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"people"}) 1 14 15
1321 1 ▶ return $fail->("people is 0 to $seats") unless $given{people} =~ /\A[0-9]+\z/ && $given{people} <= $seats;
Branch: 1 branch
BranchTrueFalseTotal
unless $given{"people"} =~ /\A[0-9]+\z/u and $given{"people"} <= $seats 1 0 1
Condition33%1/3
$given{"people"} =~ /\A[0-9]+\z/u and $given{"people"} <= $seats
!ll&&!rl&&r
010
MC/DC0%0/2
$given{"people"} =~ /\A[0-9]+\z/u and $given{"people"} <= $seats
$given{"people"} =~ /\A[0-9]+\z/u $given{"people"} <= $seats
Truth table33%1/3
A and B
A= $given{"people"} =~ /\A[0-9]+\z/uB= $given{"people"} <= $seats
ABresult
0X0
100
111
1322 0 $with{bots} = [ $given{people} .. $seats - 1 ];
1323 }
1324 14 ▶ if (defined $given{bots}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"bots"}) 8 6 14
1325 8 my $list = $given{bots};
1326 8 ▶ return $fail->("bots is a list of seats such as 1,2, or none or all, not '$list'")
Branch: 1 branch
BranchTrueFalseTotal
unless $list =~ /\A(?:none|all|[0-9]+(?:,[0-9]+)*)\z/u 1 7 8
1327 unless $list =~ /\A(?:none|all|[0-9]+(?:,[0-9]+)*)\z/;
1328 7 ▶ $with{bots} = $list eq 'none' ? [] : $list eq 'all' ? [ 0 .. $seats - 1 ] : [ split /,/, $list ];
Branch: 2 branches
BranchTrueFalseTotal
$list eq 'none' ? : 4 3 7
$list eq 'all' ? : 1 2 3
1329 }
1330 13 ▶ if ($given{pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"pipe"}) 2 11 13
1331 2 @with{qw(interactive colour unicode picking pause quiet)} = (0, 0, 0, 0, 0, 1);
1332 }
1333 13 ▶ $with{menu} = 1 unless grep { defined $given{$_} } qw(seats pieces people bots level load pipe);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $given{$_};} "seats", "pieces", "people", "bots", "level", "load", "pipe" 0 13 13
1334  
1335 13 my $terminal = eval { $class->new(%with) };
1336 13 ▶ return $fail->("$@") unless $terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $terminal 5 8 13
1337  
1338 8 ▶ if (defined $given{load}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"load"}) 4 4 8
1339 4 ▶ return 2 unless $terminal->load($given{load});
Branch: 1 branch
BranchTrueFalseTotal
unless $terminal->load($given{"load"}) 1 3 4
1340 }
1341  
1342 7 ▶ return $terminal->pipe if $given{pipe};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"pipe"} 2 5 7
1343 5 return $terminal->start;
1344 }
1345  
1346 23 1;
1347  
1348 __END__
↑ Summary blib/lib/Game/Sternhalma/Notation.pm »