blib/lib/Game/Brandubh/Terminal.pm

filter: Statementstmt 93.1% 838 / 900 Branchbran 84.5% 465 / 550 Conditioncond 67.7% 128 / 189 MC/DCmcdc 49.0% 54 / 110 Subroutinesub 97.4% 75 / 77 Podpod 98.3% 61 / 62 totaltotal 85.8% 1621 / 1888 CC 361 SCAR 26.8
CC
361
Cov
87%
choose
40.6
pick
34.5
command
30.6
CRAP 14.6
↑ Summary blib/lib/Game/Brandubh/Variant.pm »
1 package Game::Brandubh::Terminal;
2  
3 38 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 38 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 38 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 38 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 38 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
10 38 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 38 ▶use Game::Brandubh;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 38 ▶use Game::Brandubh::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 38 ▶use Game::Brandubh::Notation qw(square_name square_parse);
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 38 our $VERSION = '0.02';
17  
18 38 our (@COMMANDS, %SEQUENCE, %CONTROL, %KEY, %GROUND, %INK, %GLYPH, %ENDING);
19  
20 38 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 38 @COMMANDS = (
22 [ 'd1d3' => 'play a move: the square a piece leaves, then where it goes' ],
23 [ 'moves' => 'every legal move, and what each would capture' ],
24 [ 'hint' => 'what the program would play here' ],
25 [ 'undo' => 'take back your last move, and the reply to it' ],
26 [ 'board' => 'draw the board again' ],
27 [ 'pick' => 'choose moves on the board with the arrow keys' ],
28 [ 'type' => 'go back to typing moves' ],
29 [ 'level N' => 'how hard the program plays, 1 to 3' ],
30 [ 'side WHO' => 'attackers, defenders, both or none: which side is yours' ],
31 [ 'new' => 'start another game' ],
32 [ 'save FILE' => 'write the game to a file' ],
33 [ 'load FILE' => 'read a game back from a file' ],
34 [ 'draw' => 'offer a draw' ],
35 [ 'resign' => 'give the game up' ],
36 [ 'rules' => 'the rules of the game' ],
37 [ 'colour' => 'turn colour on or off' ],
38 [ 'help' => 'this list' ],
39 [ 'quit' => 'stop playing' ],
40 );
41  
42 38 %SEQUENCE = (
43 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
44 'H' => 'home', 'F' => 'end',
45 '1~' => 'home', '4~' => 'end', '7~' => 'home', '8~' => 'end',
46 '5~' => 'page_up', '6~' => 'page_down', 'Z' => 'back_tab',
47 );
48  
49 38 %CONTROL = (
50 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
51 "\x7f" => 'backspace', "\x08" => 'backspace',
52 "\x03" => 'interrupt', "\x04" => 'eof',
53 );
54  
55 38 %KEY = (
56 u => 'undo',
57 r => 'rules',
58 n => 'new',
59 q => 'quit',
60 c => 'colour',
61 );
62  
63 38 %GROUND = (
64 light => '48;5;180',
65 dark => '48;5;137',
66 throne => '48;5;94',
67 corner => '48;5;58',
68 cursor => '48;5;33',
69 from => '48;5;240',
70 to => '48;5;34',
71 reach => '48;5;108',
72 taken => '48;5;160',
73 trail => '48;5;222',
74 );
75  
76 38 %INK = (
77 attacker => '1;38;5;16',
78 defender => '1;38;5;231',
79 king => '1;38;5;165',
80 mark => '38;5;236',
81 reach => '1;38;5;22',
82 taken => '1;38;5;231',
83 title => '1;38;5;220',
84 good => '1;38;5;34',
85 bad => '1;38;5;160',
86 quiet => '38;5;245',
87 key => '1;38;5;33',
88 warn => '1;38;5;208',
89 );
90  
91 38 %GLYPH = (
92 wide => { attacker => "\x{25B2}", defender => "\x{25CF}", king => "\x{265A}",
93 throne => "\x{2726}", corner => "\x{25C8}", reach => "\x{2022}", taken => "\x{2715}", from => "\x{25E6}" },
94 ascii => { attacker => 'A', defender => 'D', king => 'K',
95 throne => '#', corner => 'X', reach => '.', taken => 'x', from => '_' },
96 );
97  
98 38 %ENDING = (
99 corner => 'The king has reached a corner. The defenders win.',
100 edge => 'The king has reached the edge. The defenders win.',
101 capture => 'The king is captured. The attackers win.',
102 no_pieces => 'The attackers have no piece left. The defenders win.',
103 repetition => 'The same position has come round once too often. The game is drawn.',
104 no_move => 'The side to move cannot move. The game is drawn.',
105 ply_cap => 'The game has run its full length with no end. It is drawn.',
106 agreed => 'A draw, by agreement.',
107 );
108 }
109  
110 38 has game => (is => 'rw');
111  
112 38 has [qw/in out/] => (is => 'rw');
113  
114 38 has [qw/interactive colour unicode picking/] => (is => 'rw');
115  
116 38 has human => (is => 'rw', default => 'attackers');
117  
118 38 has level => (is => 'rw', default => 2);
119  
120 38 has seed => (is => 'rw');
121  
122 38 has variant => (is => 'rw');
123  
124 38 has keysource => (is => 'rw');
125  
126 38 has raw => (is => 'rw', default => 0);
127  
128 38 has pending => (is => 'rw', default => []);
129  
130 38 has notice => (is => 'rw', default => []);
131  
132 38 has focus => (is => 'rw');
133  
134 38 has chosen => (is => 'rw');
135  
136 38 has redraw => (is => 'rw', default => 1);
137  
138 38 has pause => (is => 'rw');
139  
140 38 has menu => (is => 'rw');
141  
142 91 ▶sub BUILD {
Subroutine BUILD: called CC=22 SCAR=31.4
Pod undocumented
143 91 my ($self) = @_;
144 91 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 58 33 91
145 91 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 31 60 91
146 91 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 30 61 91
-t $self->in ? : 0 30 30
147 91 ▶ $self->colour($self->interactive && !$ENV{NO_COLOR} ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 26 65 91
$self->interactive && !$ENV{'NO_COLOR'} ? : 0 26 26
Condition33%1/3
$self->interactive and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
2600
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
148 91 ▶ $self->unicode($self->colour ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 61 30 91
$self->colour ? : 3 58 61
149 91 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 28 63 91
$self->interactive ? : 0 28 28
150 91 ▶ $self->picking(0) unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 64 27 91
151 91 ▶ $self->pause($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pause;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pause 63 28 91
$self->interactive && !$self->keysource ? : 0 63 63
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
6300
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
152 91 ▶ $self->menu(0) unless defined $self->menu;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->menu 85 6 91
153  
154 91 ▶ Carp::croak("Game::Brandubh::Terminal: side is attackers, defenders, both or none, not '" . $self->human . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->human =~ /\A(?:attackers|defenders|both|none)\z/ 2 89 91
155 unless $self->human =~ /\A(?:attackers|defenders|both|none)\z/;
156 89 ▶ Carp::croak('Game::Brandubh::Terminal: level is 1, 2 or 3')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->level =~ /\A[123]\z/ 2 87 89
157 unless $self->level =~ /\A[123]\z/;
158  
159 87 ▶ $self->seed(Digest::SHA::sha256(join '|', 'brandubh', $$, time, rand)) unless defined $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed 22 65 87
160 87 ▶ $self->game($self->fresh_game) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 76 11 87
161  
162 86 ▶ binmode $self->out, ':encoding(UTF-8)' if $self->unicode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->unicode 6 80 86
163 86 my $previous = select $self->out;
164 86 $| = 1;
165 86 select $previous;
166 86 return;
167 }
168  
169 79 ▶sub fresh_game {
Subroutine fresh_game: called CC=2 SCAR=6.9
Pod documented
170 79 my ($self) = @_;
171 79 ▶ return Game::Brandubh->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $self->variant ? : 10 69 79
172 seed => $self->seed,
173 (defined $self->variant ? (variant => $self->variant) : ()),
174 );
175 }
176  
177 168 ▶sub budget { (Game::Brandubh::Bot->levels)[ $_[0]->level - 1 ] }
Subroutine budget: called CC=1 SCAR=0.0
Pod documented
178  
179 84 ▶sub start {
Subroutine start: called CC=8 SCAR=21.8
Pod documented
180 84 my ($self) = @_;
181 84 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 84 84
Condition66%2/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
57270
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
182  
183 84 my $interrupt = $SIG{INT};
184 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
185 0 $self->leave_raw;
186 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
187 0 kill 'INT', $$;
188 84 };
189  
190 84 ▶ $self->remark($self->paint('Brandubh.', 'title') . ' Type help for the commands.') unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 57 27 84
191 84 my $played = eval {
192 84 ▶ $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 82 2 84
Condition100%6/6
not $self->menu && $self->raw or $self->ask_how_to_play
l!l&&r!l&&!r
8022
$self->menu && $self->raw
!ll&&!rl&&r
7914
MC/DC100%3/3
not $self->menu && $self->raw or $self->ask_how_to_play
$self->menu $self->raw $self->ask_how_to_play
Truth table100%4/4
not(A && B) or C
A= $self->menuB= $self->rawC= $self->ask_how_to_play
ABCresult
0XX1
10X1
1111
1100
193 84 1;
194 };
195 84 my $error = $@;
196 84 $self->leave_raw;
197 84 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 84 84
198 84 return 0;
199 }
200  
201 82 ▶sub turns {
Subroutine turns: called CC=12 SCAR=24.9
Pod documented
202 82 my ($self) = @_;
203 82 while (1) {
204 350 ▶ my $picking = $self->picking && $self->raw;
Condition66%2/3
$self->picking && $self->raw
!ll&&!rl&&r
306044
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
205 350 my $game = $self->game;
206  
207 350 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "finished") 20 330 350
208 20 ▶ my $line = $picking ? $self->pick_ended : $self->read_ended;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 3 17 20
209 20 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 5 15 20
210 15 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 8 7 15
211 7 next;
212 }
213 330 ▶ if ($self->bot_plays($game->side_to_move)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bot_plays($game->side_to_move)) 166 164 330
214 166 $self->bot_move;
215 166 next;
216 }
217  
218 164 ▶ $self->render if $self->redraw && !$picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->redraw and not $picking 94 70 164
Condition100%3/3
$self->redraw and not $picking
!ll&&!rl&&r
343694
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
219 164 ▶ my $line = $picking ? $self->pick : $self->read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 38 126 164
220 164 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 2 162 164
221 2 $self->say('Bye.');
222 2 last;
223 }
224 162 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 67 95 162
225 }
226 82 return $self;
227 }
228  
229 17 ▶sub read_ended {
Subroutine read_ended: called CC=2 SCAR=6.9
Pod documented
230 17 my ($self) = @_;
231 17 ▶ $self->render if $self->redraw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->redraw 9 8 17
232 17 return $self->read_line('new, undo or quit> ');
233 }
234  
235 3 ▶sub pick_ended {
Subroutine pick_ended: called CC=11 SCAR=29.8
Pod documented
236 3 my ($self) = @_;
237 3 $self->focus(undef);
238 3 $self->chosen(undef);
239 3 while (1) {
240 3 $self->show('ended');
241 3 my $key = $self->read_key;
242 3 ▶ 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 3 3
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
003
not defined $key or $key eq "eof"
l!l&&r!l&&!r
003
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
243 3 ▶ return 'new' if $key eq 'n' || $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" or $key eq "enter" 1 2 3
Condition66%2/3
$key eq "n" or $key eq "enter"
l!l&&r!l&&!r
102
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
244 2 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 1 1 2
245 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
246 0 ▶ return 'rules' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 0 0
247 0 ▶ return $self->read_line(': ') if $key eq ':';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq ":" 0 0 0
248 }
249 }
250  
251 330 ▶sub bot_plays {
Subroutine bot_plays: called CC=4 SCAR=13.9
Pod documented
252 330 my ($self, $side) = @_;
253 330 ▶ return 0 if $self->human eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "both" 137 193 330
254 193 ▶ return 1 if $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "none" 155 38 193
255 38 ▶ return $side eq $self->human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->human ? : 27 11 38
256 }
257  
258 166 ▶sub bot_move {
Subroutine bot_move: called CC=9 SCAR=22.0
Pod documented
259 166 my ($self) = @_;
260 166 my $game = $self->game;
261 166 my $side = $game->side_to_move;
262 166 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 3 163 166
263 3 $self->show('thinking');
264 }
265 166 my $thought = Game::Brandubh::Bot->think($game, seed => $self->seed, level => $self->budget);
266 166 ▶ return undef unless $thought;
Branch: 1 branch
BranchTrueFalseTotal
unless $thought 0 166 166
267 166 $game->play_or_die($thought->{move});
268  
269 166 my $shown = $game->shown->[-1];
270 166 my $line = sprintf 'The %s played %s', $side, $self->paint($shown, 'key');
271 166 my ($taken) = $shown =~ /x([a-g1-7,]+)/;
272 166 ▶ $line .= ', taking ' . join(' and ', split /,/, $taken) if defined $taken;
Branch: 1 branch
BranchTrueFalseTotal
if defined $taken 22 144 166
273 166 ▶ $line .= $thought->{slipped} ? ' (without much thought).' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$thought->{'slipped'} ? : 29 137 166
274 166 ▶ $self->remark($line) if $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "none" 155 11 166
275 166 ▶ $self->notice([$line]) unless $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->human eq "none" 11 155 166
276 166 $self->redraw(1);
277 166 ▶ if ($self->human eq 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->human eq "none") 155 11 166
278 155 $self->render;
279 155 ▶ select undef, undef, undef, 0.4 if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 155 155
280 }
281 166 return $thought->{move};
282 }
283  
284 91 ▶sub keys_available {
Subroutine keys_available: called CC=5 SCAR=21.7
Pod documented
285 91 my ($self) = @_;
286 91 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 27 64 91
287 64 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 64 0 64
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0640
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
288 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
289 }
290  
291 27 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=25.6
Pod documented
292 27 my ($self) = @_;
293 27 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 27 27
294 27 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 27 0 27
295 27 $self->raw(1);
296 27 return $self;
297 }
298 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
299 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
300 0 $self->raw(1);
301 0 my $out = $self->out;
302 0 print {$out} "\e[?25l";
303 0 return $self;
304 }
305  
306 84 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=13.1
Pod documented
307 84 my ($self) = @_;
308 84 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 57 27 84
309 27 ▶ unless ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->keysource) 0 27 27
310 0 eval { Term::ReadKey::ReadMode(0, $self->in) };
311 0 my $out = $self->out;
312 0 print {$out} "\e[?25h";
313 }
314 27 $self->raw(0);
315 27 return $self;
316 }
317  
318 191 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=25.4
Pod documented
319 191 my ($self, $wait) = @_;
320 191 my $pending = $self->pending;
321 191 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 4 187 191
322 187 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 187 0 187
323 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
324 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
325 0 select undef, undef, undef, 0.05;
326 0 return Term::ReadKey::ReadKey(-1, $self->in);
327 }
328  
329 128 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
330 128 my ($self) = @_;
331 128 my $char = $self->read_char(1);
332 128 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 127 128
333 127 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Brandubh::Terminal::CONTROL{$char} 39 88 127
334 88 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 32 56 88
335 56 return $char;
336 }
337  
338 32 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=20.9
Pod documented
339 32 my ($self) = @_;
340 32 my $opener = $self->read_char(0);
341 32 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 32 32
342 32 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 4 28 32
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
2714
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
343 4 unshift @{ $self->pending }, $opener;
344 4 return 'escape';
345 }
346 28 my $tail = '';
347 28 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 31 0 31
348 31 my $char = $self->read_char(0);
349 31 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 31 31
350 31 $tail .= $char;
351 31 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 28 3 31
352 }
353 28 ▶ return $SEQUENCE{$tail} || 'unknown';
Condition100%2/2
$Game::Brandubh::Terminal::SEQUENCE{$tail} || 'unknown'
l!l
253
MC/DC100%1/1
$Game::Brandubh::Terminal::SEQUENCE{$tail} || 'unknown'
$Game::Brandubh::Terminal::SEQUENCE{$tail}
Truth table100%2/2
A
A= $Game::Brandubh::Terminal::SEQUENCE{$tail}
Aresult
11
00
354 }
355  
356 207 ▶sub coordinates {
Subroutine coordinates: called CC=1 SCAR=0.0
Pod documented
357 207 my ($name) = @_;
358 207 my ($file, $rank) = square_parse($name);
359 207 return ($file, $rank);
360 }
361  
362 27 ▶sub steer {
Subroutine steer: called CC=11 SCAR=24.0
Pod documented
363 27 my ($self, $from, $key, $among) = @_;
364 27 my ($ff, $fr) = coordinates($from);
365 27 my ($best, $cost);
366 27 for my $name (@{$among}) {
367 201 ▶ next if $name eq $from;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $from 21 180 201
368 180 my ($f, $r) = coordinates($name);
369 180 my ($along, $across);
370 180 ▶ if ($key eq 'left') { ($along, $across) = ($ff - $f, abs($r - $fr)) }
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'left') { } 12 168 180
elsif ($key eq 'right') { } 13 155 168
elsif ($key eq 'up') { } 56 99 155
371 13 elsif ($key eq 'right') { ($along, $across) = ($f - $ff, abs($r - $fr)) }
372 56 elsif ($key eq 'up') { ($along, $across) = ($r - $fr, abs($f - $ff)) }
373 99 else { ($along, $across) = ($fr - $r, abs($f - $ff)) }
374 180 ▶ next if $along <= 0 || $across > $along;
Branch: 1 branch
BranchTrueFalseTotal
if $along <= 0 or $across > $along 108 72 180
Condition100%3/3
$along <= 0 or $across > $along
l!l&&r!l&&!r
106272
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
375 72 my $this = $along + 2 * $across;
376 72 ▶ ($best, $cost) = ($name, $this) if !defined $cost || $this < $cost;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cost or $this < $cost 30 42 72
Condition100%3/3
not defined $cost or $this < $cost
l!l&&r!l&&!r
25542
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
377 }
378 27 ▶ return defined $best ? $best : $from;
Branch: 1 branch
BranchTrueFalseTotal
defined $best ? : 25 2 27
379 }
380  
381 39 ▶sub movable {
Subroutine movable: called CC=1 SCAR=0.0
Pod documented
382 39 my ($self) = @_;
383 39 my %seen;
384 39 return [ grep { !$seen{$_}++ } map { $_->{from} } @{ $self->game->legal } ];
385 }
386  
387 164 ▶sub reach {
Subroutine reach: called CC=1 SCAR=0.0
Pod documented
388 164 my ($self, $from) = @_;
389 164 return [ grep { $_->{from} eq $from } @{ $self->game->legal } ];
390 }
391  
392 38 ▶sub pick {
Subroutine pick: called CC=33 SCAR=35.4
Pod documented
393 38 my ($self) = @_;
394 38 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 38 38
395 38 my $movable = $self->movable;
396 38 ▶ return undef unless @{$movable};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$movable;} 0 38 38
397  
398 38 $self->chosen(undef);
399 38 ▶ $self->focus($movable->[0]) unless defined $self->focus && grep { $_ eq $self->focus } @{$movable};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->focus and grep {$_ eq $self->focus;} @{$movable;} 34 4 38
Condition66%2/3
defined $self->focus and grep {$_ eq $self->focus;} @{$movable;}
!ll&&!rl&&r
3404
MC/DC50%1/2
defined $self->focus and grep {$_ eq $self->focus;} @{$movable;}
defined $self->focus grep {$_ eq $self->focus;} @{$movable;}
Truth table66%2/3
A and B
A= defined $self->focusB= grep {$_ eq $self->focus;} @{$movable;}
ABresult
0X0
100
111
400  
401 38 while (1) {
402 94 my $chosen = $self->chosen;
403 94 ▶ my $entries = defined $chosen ? $self->reach($chosen) : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $chosen ? : 29 65 94
404 94 ▶ my $among = defined $chosen ? [ map { $_->{to} } @{$entries} ] : $movable;
Branch: 1 branch
BranchTrueFalseTotal
defined $chosen ? : 29 65 94
405  
406 94 ▶ $self->show(defined $chosen ? 'where' : 'which');
Branch: 1 branch
BranchTrueFalseTotal
defined $chosen ? : 29 65 94
407 94 my $key = $self->read_key;
408 94 ▶ 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" 1 93 94
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
1093
not defined $key or $key eq "eof"
l!l&&r!l&&!r
1093
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
409  
410 93 ▶ if ($key =~ /\A(?:up|down|left|right)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A(?:up|down|left|right)\z/) 20 73 93
411 20 $self->focus($self->steer($self->focus, $key, $among));
412 20 next;
413 }
414 73 ▶ if ($key eq 'tab' || $key eq 'back_tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tab" or $key eq "back_tab") 11 62 73
Condition66%2/3
$key eq "tab" or $key eq "back_tab"
l!l&&r!l&&!r
11062
MC/DC50%1/2
$key eq "tab" or $key eq "back_tab"
$key eq "tab" $key eq "back_tab"
Truth table66%2/3
A or B
A= $key eq "tab"B= $key eq "back_tab"
ABresult
1X1
011
000
415 11 my ($at) = grep { $among->[$_] eq $self->focus } 0 .. $#{$among};
416 11 ▶ $at = 0 unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 0 11 11
417 11 ▶ $at = ($at + ($key eq 'tab' ? 1 : -1)) % @{$among};
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'tab' ? : 11 0 11
418 11 $self->focus($among->[$at]);
419 11 next;
420 }
421 62 ▶ if ($key eq 'home') { $self->focus($among->[0]); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home") 0 62 62
422 62 ▶ if ($key eq 'end') { $self->focus($among->[-1]); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end") 0 62 62
423  
424 62 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 20 42 62
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
20042
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
425 20 ▶ if (!defined $chosen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chosen) 12 8 20
426 12 $self->chosen($self->focus);
427 12 $self->focus($self->reach($self->focus)->[0]{to});
428 12 next;
429 }
430 8 my ($entry) = grep { $_->{to} eq $self->focus } @{$entries};
431 8 ▶ next unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 8 8
432 8 $self->game->play_or_die($entry->{move});
433 8 $self->notice([]);
434 8 $self->chosen(undef);
435 8 $self->focus(undef);
436 8 $self->redraw(1);
437 8 return '';
438 }
439 42 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 5 37 42
Condition100%3/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
4137
MC/DC100%2/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table100%3/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
440 5 ▶ if (defined $chosen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chosen) 3 2 5
441 3 $self->focus($chosen);
442 3 $self->chosen(undef);
443 }
444 5 next;
445 }
446 37 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 1 36 37
447 1 my $move = $self->hint_move;
448 1 ▶ if (defined $move) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $move) 1 0 1
449 1 my ($from, $to) = (substr($move, 0, 2), substr($move, 2, 2));
450 1 $self->chosen($from);
451 1 $self->focus($to);
452 1 $self->notice([ @{ $self->notice }, 'The program would play ' . $self->paint("$from-$to", 'key') . '. Enter to play it.' ]);
453 }
454 1 next;
455 }
456 36 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 1 35 36
457 1 $self->show('help');
458 1 $self->read_key;
459 1 next;
460 }
461 35 ▶ if ($key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":") 2 33 35
462 2 $self->chosen(undef);
463 2 return $self->read_line(': ');
464 }
465 33 ▶ if ($key =~ /\A[123]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[123]\z/) 1 32 33
466 1 $self->set_level($key);
467 1 next;
468 }
469 32 ▶ if ($KEY{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Game::Brandubh::Terminal::KEY{$key}) 27 5 32
470 27 $self->chosen(undef);
471 27 return $KEY{$key};
472 }
473 5 ▶ next if $key eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "unknown" 3 2 5
474 1 $self->notice([ @{ $self->notice }, 'That key does nothing here. Press ? for the ones that do.' ])
475 2 ▶ unless grep { /does nothing here/ } @{ $self->notice };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/does nothing here/;} @{$self->notice;} 1 1 2
476 }
477 }
478  
479 358 ▶sub marks {
Subroutine marks: called CC=13 SCAR=25.9
Pod documented
480 358 my ($self, $mode) = @_;
481 358 my $game = $self->game;
482 358 my %mark;
483  
484 358 my $shown = $game->shown;
485 358 ▶ if (@{$shown}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$shown;}) 217 141 358
486 217 my ($from, $to) = $shown->[-1] =~ /([a-g][1-7])-([a-g][1-7])/;
487 217 ▶ $mark{$from} = $mark{$to} = 'trail' if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 217 0 217
488 }
489 358 ▶ return (\%mark, $game->pieces) unless $mode eq 'which' || $mode eq 'where';
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "which" or $mode eq "where" 264 94 358
Condition100%3/3
$mode eq "which" or $mode eq "where"
l!l&&r!l&&!r
6529264
MC/DC100%2/2
$mode eq "which" or $mode eq "where"
$mode eq "which" $mode eq "where"
Truth table100%3/3
A or B
A= $mode eq "which"B= $mode eq "where"
ABresult
1X1
011
000
490  
491 94 my $pieces = $game->pieces;
492 94 my $chosen = $self->chosen;
493 94 ▶ if (!defined $chosen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chosen) 65 29 94
494 65 ▶ $mark{ $self->focus } = 'cursor' if defined $self->focus;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->focus 65 0 65
495 65 ▶ if (defined $self->focus) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->focus) 65 0 65
496 65 ▶ $mark{ $_->{to} } ||= 'reach' for @{ $self->reach($self->focus) };
Condition50%1/2
$mark{$_->{"to"}} ||= "reach"
l!l
0342
MC/DC0%0/1
$mark{$_->{"to"}} ||= "reach"
$mark{$_->{"to"}}
Truth table50%1/2
A
A= $mark{$_->{"to"}}
Aresult
11
00
497 65 $mark{ $self->focus } = 'cursor';
498 }
499 65 return (\%mark, $pieces);
500 }
501  
502 29 my $entries = $self->reach($chosen);
503 29 $mark{ $_->{to} } = 'reach' for @{$entries};
504 29 my ($entry) = grep { $_->{to} eq $self->focus } @{$entries};
505 29 ▶ if ($entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 29 0 29
506 29 my %after = %{$pieces};
507 29 $after{ $entry->{to} } = delete $after{$chosen};
508 29 $mark{$chosen} = 'from';
509 29 $mark{ $entry->{to} } = 'to';
510 29 $mark{$_} = 'taken' for @{ $entry->{captures} };
511 29 return (\%mark, \%after);
512 }
513 0 $mark{$chosen} = 'from';
514 0 return (\%mark, $pieces);
515 }
516  
517 2169 ▶sub paint {
Subroutine paint: called CC=3 SCAR=11.3
Pod documented
518 2169 my ($self, $text, $ink) = @_;
519 2169 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 2063 106 2169
520 106 ▶ my $code = $INK{$ink} || $ink;
Condition33%1/3
$Game::Brandubh::Terminal::INK{$ink} || $ink
l!l&&r!l&&!r
10600
MC/DC0%0/2
$Game::Brandubh::Terminal::INK{$ink} || $ink
$Game::Brandubh::Terminal::INK{$ink} $ink
Truth table33%1/3
A || B
A= $Game::Brandubh::Terminal::INK{$ink}B= $ink
ABresult
1X1
011
000
521 106 return "\e[${code}m$text\e[0m";
522 }
523  
524 7098 ▶sub glyph {
Subroutine glyph: called CC=2 SCAR=6.9
Pod documented
525 7098 my ($self, $what) = @_;
526 7098 ▶ return $GLYPH{ $self->unicode ? 'wide' : 'ascii' }{$what};
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 196 6902 7098
527 }
528  
529 17542 ▶sub special {
Subroutine special: called CC=3 SCAR=11.0
Pod documented
530 17542 my ($name) = @_;
531 17542 ▶ return 'throne' if $name eq 'd4';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "d4" 358 17184 17542
532 17184 ▶ return 'corner' if $name =~ /\A[ag][17]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A[ag][17]\z/ 1432 15752 17184
533 15752 return '';
534 }
535  
536 358 ▶sub board_lines {
Subroutine board_lines: called CC=3 SCAR=11.1
Pod documented
537 358 my ($self, $mode) = @_;
538 358 ▶ my ($mark, $pieces) = $self->marks($mode || 'plain');
Condition50%1/2
$mode || 'plain'
l!l
3580
MC/DC0%0/1
$mode || 'plain'
$mode
Truth table50%1/2
A
A= $mode
Aresult
11
00
539 358 ▶ return $self->colour ? $self->painted_board($mark, $pieces) : $self->plain_board($mark, $pieces);
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 12 346 358
540 }
541  
542 12 ▶sub painted_board {
Subroutine painted_board: called CC=14 SCAR=26.4
Pod documented
543 12 my ($self, $mark, $pieces) = @_;
544 12 my @lines;
545 12 for my $rank (reverse 0 .. 6) {
546 84 my ($top, $bottom) = ('', '');
547 84 for my $file (0 .. 6) {
548 588 my $name = square_name($file, $rank);
549 588 ▶ my $what = $mark->{$name} || '';
Condition100%2/2
$mark->{$name} || ''
l!l
51537
MC/DC100%1/1
$mark->{$name} || ''
$mark->{$name}
Truth table100%2/2
A
A= $mark->{$name}
Aresult
11
00
550 588 my $piece = $pieces->{$name};
551 588 my $place = special($name);
552  
553 my $ground = $what && $what ne 'reach' ? $GROUND{$what}
554 : $what eq 'reach' ? $GROUND{reach}
555 : $place ? $GROUND{$place}
556 588 ▶ : (($file + $rank) % 2 ? $GROUND{light} : $GROUND{dark});
Branch: 4 branches
BranchTrueFalseTotal
$what && $what ne 'reach' ? : 10 578 588
$what eq 'reach' ? : 41 537 578
$place ? : 60 477 537
($file + $rank) % 2 ? : 259 218 477
Condition100%3/3
$what and $what ne "reach"
!ll&&!rl&&r
5374110
MC/DC100%2/2
$what and $what ne "reach"
$what $what ne "reach"
Truth table100%3/3
A and B
A= $whatB= $what ne "reach"
ABresult
0X0
100
111
557  
558 588 my ($glyph, $ink);
559 588 ▶ if ($what eq 'taken') { ($glyph, $ink) = ($self->glyph('taken'), $INK{taken}) }
Branch: 5 branches
BranchTrueFalseTotal
if ($what eq 'taken') { } 2 586 588
elsif (defined $piece) { } 126 460 586
elsif ($what eq 'from') { } 3 457 460
elsif ($what eq 'reach') { } 41 416 457
elsif ($place) { } 52 364 416
560 126 elsif (defined $piece) { ($glyph, $ink) = ($self->glyph($piece), $INK{$piece}) }
561 3 elsif ($what eq 'from') { ($glyph, $ink) = ($self->glyph('from'), $INK{mark}) }
562 41 elsif ($what eq 'reach'){ ($glyph, $ink) = ($self->glyph('reach'), $INK{reach}) }
563 52 elsif ($place) { ($glyph, $ink) = ($self->glyph($place), $INK{mark}) }
564 364 else { ($glyph, $ink) = (' ', $INK{mark}) }
565  
566 588 $top .= "\e[${ground};${ink}m $glyph \e[0m";
567 588 $bottom .= "\e[${ground}m \e[0m";
568 }
569 84 push @lines, sprintf(' %d ', $rank + 1) . $top, ' ' . $bottom;
570 }
571 12 push @lines, ' ' . join('', map { " $_ " } 'a' .. 'g');
572 12 return \@lines;
573 }
574  
575 346 ▶sub plain_board {
Subroutine plain_board: called CC=12 SCAR=24.8
Pod documented
576 346 my ($self, $mark, $pieces) = @_;
577 346 my $rule = ' +' . ('---+' x 7);
578 346 my @lines = (' ' . join(' ', 'a' .. 'g'), $rule);
579 346 for my $rank (reverse 0 .. 6) {
580 2422 my $row = sprintf ' %d |', $rank + 1;
581 2422 for my $file (0 .. 6) {
582 16954 my $name = square_name($file, $rank);
583 16954 ▶ my $what = $mark->{$name} || '';
Condition100%2/2
$mark->{$name} || ''
l!l
106315891
MC/DC100%1/1
$mark->{$name} || ''
$mark->{$name}
Truth table100%2/2
A
A= $mark->{$name}
Aresult
11
00
584 16954 my $piece = $pieces->{$name};
585 16954 my $place = special($name);
586 16954 ▶ my $glyph = $what eq 'taken' ? $self->glyph('taken')
Branch: 5 branches
BranchTrueFalseTotal
$what eq 'taken' ? : 2 16952 16954
defined $piece ? : 3775 13177 16952
$what eq 'from' ? : 26 13151 13177
$what eq 'reach' ? : 512 12639 13151
$place ? : 1485 11154 12639
587 : defined $piece ? $self->glyph($piece)
588 : $what eq 'from' ? $self->glyph('from')
589 : $what eq 'reach' ? $self->glyph('reach')
590 : $place ? $self->glyph($place)
591 : ' ';
592 16954 ▶ $row .= $what eq 'cursor' ? "[$glyph]|"
Branch: 3 branches
BranchTrueFalseTotal
$what eq 'cursor' ? : 63 16891 16954
$what eq 'to' ? : 26 16865 16891
$what eq 'trail' ? : 434 16431 16865
593 : $what eq 'to' ? "($glyph)|"
594 : $what eq 'trail' ? "'$glyph'|"
595 : " $glyph |";
596 }
597 2422 push @lines, $row, $rule;
598 }
599 346 return \@lines;
600 }
601  
602 341 ▶sub who {
Subroutine who: called CC=3 SCAR=11.0
Pod documented
603 341 my ($self, $side) = @_;
604 341 ▶ return 'you' if $self->human eq $side;
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq $side 30 311 341
605 311 ▶ return 'either of you' if $self->human eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "both" 158 153 311
606 153 return 'the program, level ' . $self->level;
607 }
608  
609 358 ▶sub status_lines {
Subroutine status_lines: called CC=13 SCAR=25.7
Pod documented
610 358 my ($self) = @_;
611 358 my $game = $self->game;
612 358 my $pieces = $game->pieces;
613 358 my %count;
614 358 $count{$_}++ for values %{$pieces};
615  
616 358 my @lines;
617 358 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq 'finished') { } 17 341 358
618 17 push @lines, @{ $self->result_lines };
619 }
620 else {
621 341 my $side = $game->side_to_move;
622 341 ▶ push @lines, sprintf '%s to move: %s.',
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'attackers' ? : 208 133 341
623 $self->paint('The ' . $side, $side eq 'attackers' ? 'warn' : 'good'), $self->who($side);
624 }
625 push @lines, sprintf '%s %s %s %s %s %s',
626 'Attackers', $self->tray('attacker', $count{attacker} || 0, 8),
627 'Defenders', $self->tray('defender', $count{defender} || 0, 4),
628 358 ▶ 'King', $self->tray('king', $count{king} || 0, 1);
Condition83%5/6
$count{'attacker'} || 0
l!l
3580
$count{'defender'} || 0
l!l
33028
$count{'king'} || 0
l!l
3562
MC/DC0%0/1
$count{'attacker'} || 0
$count{'attacker'}
MC/DC100%1/1
$count{'defender'} || 0
$count{'defender'}
MC/DC100%1/1
$count{'king'} || 0
$count{'king'}
Truth table50%1/2
A
A= $count{'attacker'}
Aresult
11
00
Truth table100%2/2
A
A= $count{'defender'}
Aresult
11
00
Truth table100%2/2
A
A= $count{'king'}
Aresult
11
00
629  
630 358 push @lines, $self->paint($self->table_line, 'quiet');
631  
632 358 my $shown = $game->shown;
633 358 ▶ my $from = @{$shown} > 6 ? @{$shown} - 6 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$shown;} > 6 ? : 127 231 358
634 push @lines, $self->paint('Move ' . ($game->ply + 1) . '.', 'quiet')
635 358 ▶ . (@{$shown} ? ' ' . $self->paint(join(' ', map { ($_ + 1) . '. ' . $shown->[$_] } $from .. $#{$shown}), 'quiet') : '');
Branch: 1 branch
BranchTrueFalseTotal
@{$shown;} ? : 217 141 358
636  
637 358 my $repeats = $game->repeats;
638 358 my $limit = $game->variant->repeat;
639 358 ▶ push @lines, $self->paint(sprintf('This position has now stood %d time%s. At %d the game is drawn.',
Branch: 2 branches
BranchTrueFalseTotal
if $game->status eq "active" and $repeats > 1 5 353 358
$repeats == 1 ? : 0 5 5
Condition100%3/3
$game->status eq "active" and $repeats > 1
!ll&&!rl&&r
173365
MC/DC100%2/2
$game->status eq "active" and $repeats > 1
$game->status eq "active" $repeats > 1
Truth table100%3/3
A and B
A= $game->status eq "active"B= $repeats > 1
ABresult
0X0
100
111
640 $repeats, ($repeats == 1 ? '' : 's'), $limit), 'warn')
641 if $game->status eq 'active' && $repeats > 1;
642 358 ▶ push @lines, $self->paint('The ' . $game->side_of($game->draw_offered_by) . ' have offered a draw.', 'warn')
Branch: 1 branch
BranchTrueFalseTotal
if defined $game->draw_offered_by 0 358 358
643 if defined $game->draw_offered_by;
644 358 return \@lines;
645 }
646  
647 358 ▶sub table_line {
Subroutine table_line: called CC=4 SCAR=13.9
Pod documented
648 358 my ($self) = @_;
649 358 my $human = $self->human;
650 358 ▶ return 'Two players at one keyboard.' if $human eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $human eq "both" 167 191 358
651 191 ▶ return 'The program plays both sides, at level ' . $self->level . '.' if $human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $human eq "none" 155 36 191
652 36 ▶ return sprintf 'You have the %s; the program has the %s, at level %d.',
Branch: 1 branch
BranchTrueFalseTotal
$human eq 'attackers' ? : 16 20 36
653 $human, ($human eq 'attackers' ? 'defenders' : 'attackers'), $self->level;
654 }
655  
656 17 ▶sub result_lines {
Subroutine result_lines: called CC=10 SCAR=23.0
Pod documented
657 17 my ($self) = @_;
658 17 ▶ my $result = $self->game->result or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->game->result or return [] 17 0 17
659 17 my $how = $result->how;
660 my $line = $how eq 'resign'
661 ? sprintf('The %s resign. The %s win.', $result->loser, $result->winner)
662 17 ▶ : $ENDING{$how};
Branch: 1 branch
BranchTrueFalseTotal
$how eq 'resign' ? : 3 14 17
663 17 ▶ my $ink = !defined $result->winner ? 'warn'
Branch: 3 branches
BranchTrueFalseTotal
!defined($result->winner) ? : 4 13 17
$self->human eq $result->winner ? : 1 12 13
$self->human eq $result->loser ? : 1 11 12
664 : $self->human eq $result->winner ? 'good'
665 : $self->human eq $result->loser ? 'bad'
666 : 'title';
667 17 my @lines = ($self->paint($line, $ink));
668 17 ▶ push @lines, $self->paint('You win.', 'good') if defined $result->winner && $self->human eq $result->winner;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result->winner and $self->human eq $result->winner 1 16 17
Condition100%3/3
defined $result->winner and $self->human eq $result->winner
!ll&&!rl&&r
4121
MC/DC100%2/2
defined $result->winner and $self->human eq $result->winner
defined $result->winner $self->human eq $result->winner
Truth table100%3/3
A and B
A= defined $result->winnerB= $self->human eq $result->winner
ABresult
0X0
100
111
669 17 ▶ push @lines, $self->paint('The program wins.', 'bad') if defined $result->winner && $self->human eq $result->loser;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result->winner and $self->human eq $result->loser 1 16 17
Condition100%3/3
defined $result->winner and $self->human eq $result->loser
!ll&&!rl&&r
4121
MC/DC100%2/2
defined $result->winner and $self->human eq $result->loser
defined $result->winner $self->human eq $result->loser
Truth table100%3/3
A and B
A= defined $result->winnerB= $self->human eq $result->loser
ABresult
0X0
100
111
670 17 return \@lines;
671 }
672  
673 102 ▶sub legend {
Subroutine legend: called CC=5 SCAR=16.1
Pod documented
674 102 my ($self, $mode) = @_;
675 102 ▶ my $k = sub { $self->paint($_[0], 'key') };
Subroutine __ANON__: called CC=1 SCAR=0.0
676 102 ▶ return $k->('arrows') . ' choose a piece ' . $k->('enter') . ' pick it up ' . $k->('h') . ' hint '
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "which" 65 37 102
677 . $k->('u') . ' undo ' . $k->('?') . ' help ' . $k->('q') . ' quit'
678 if $mode eq 'which';
679 37 ▶ return $k->('arrows') . ' choose where ' . $k->('enter') . ' play it ' . $k->('esc') . ' put it back '
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "where" 29 8 37
680 . $k->('?') . ' help'
681 if $mode eq 'where';
682 8 ▶ return $k->('n') . ' new game ' . $k->('u') . ' undo ' . $k->('r') . ' rules ' . $k->('q') . ' quit'
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "ended" 3 5 8
683 if $mode eq 'ended';
684 5 ▶ return 'The program is thinking...' if $mode eq 'thinking';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "thinking" 3 2 5
685 2 return 'Press any key to go back to the board.';
686 }
687  
688 29 ▶sub preview_lines {
Subroutine preview_lines: called CC=5 SCAR=16.1
Pod documented
689 29 my ($self) = @_;
690 29 my $chosen = $self->chosen;
691 29 ▶ return [] unless defined $chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chosen 0 29 29
692 29 my ($entry) = grep { $_->{to} eq $self->focus } @{ $self->reach($chosen) };
693 29 ▶ return [] unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 29 29
694 29 my $line = sprintf 'The %s from %s to %s', $entry->{piece}, $chosen, $entry->{to};
695 29 ▶ $line .= ', taking ' . join(' and ', @{ $entry->{captures} }) if @{ $entry->{captures} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$entry->{"captures"};} 2 27 29
696 29 $line .= '.';
697 29 ▶ $line .= ' ' . $self->paint('This wins the game.', 'good') if $entry->{wins};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"wins"} 3 26 29
698 29 return [$line];
699 }
700  
701 113 ▶sub banner {
Subroutine banner: called CC=2 SCAR=6.9
Pod documented
702 113 my ($self) = @_;
703 113 ▶ return [' BRANDUBH'] unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 108 5 113
704 5 my $bar = join '', map { "\e[48;5;${_}m \e[0m" } 94, 130, 136, 172, 178, 220, 178, 172, 136, 130, 94;
705 5 return [ ' ' . $self->paint('B R A N D U B H', 'title') . ' ' . $bar ];
706 }
707  
708 1074 ▶sub tray {
Subroutine tray: called CC=5 SCAR=16.1
Pod documented
709 1074 my ($self, $piece, $left, $of) = @_;
710 1074 my $glyph = $self->glyph($piece);
711 1074 ▶ my $gone = $self->unicode ? "\x{00B7}" : '.';
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 27 1047 1074
712 1074 ▶ return ($glyph x $left) . ($gone x ($of - $left)) . " $left" unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 1038 36 1074
713 36 ▶ my $ground = '48;5;' . ($piece eq 'attacker' ? '180' : '94');
Branch: 1 branch
BranchTrueFalseTotal
$piece eq 'attacker' ? : 12 24 36
714 36 ▶ return "\e[${ground};$INK{$piece}m " . join(' ', ($glyph) x $left) . ($left ? ' ' : '') . "\e[0m"
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 36 0 36
715 . $self->paint(join('', (" $gone") x ($of - $left)), 'quiet');
716 }
717  
718 6 ▶sub choose {
Subroutine choose: called CC=22 SCAR=37.0
Pod documented
719 6 my ($self, $question, $options, $at) = @_;
720 6 ▶ $at = 0 unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 0 6 6
721 6 while (1) {
722 11 my @lines = (@{ $self->banner }, '', $question, '');
723 11 for my $i (0 .. $#{$options}) {
724 41 my ($label, $about) = @{ $options->[$i] }[1, 2];
725 41 ▶ push @lines, $i == $at
Branch: 2 branches
BranchTrueFalseTotal
$i == $at ? : 11 30 41
$self->colour ? : 0 11 11
726 ? ' ' . $self->paint(sprintf(' > %-16s ', $label), $self->colour ? '1;48;5;33;38;5;231' : 'key') . ' ' . $about
727 : sprintf(' %-16s %s', $label, $self->paint($about, 'quiet'));
728 }
729 11 push @lines, '', $self->paint('up', 'key') . ' and ' . $self->paint('down', 'key') . ' to choose, '
730 . $self->paint('enter', 'key') . ' to take it, ' . $self->paint('q', 'key') . ' to leave';
731 11 my $out = $self->out;
732 11 ▶ if ($self->interactive) { print {$out} "\e[H", join('', map { "$_\e[K\n" } @lines), "\e[J" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 11 0 11
733 0 else { print {$out} "$_\n" for @lines }
734  
735 11 my $key = $self->read_key;
736 11 ▶ return undef if !defined $key || $key eq 'eof' || $key eq 'interrupt' || $key eq 'q' || $key eq 'escape';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape" 2 9 11
Condition50%6/12
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape"
l!l&&r!l&&!r
209
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
l!l&&r!l&&!r
029
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0011
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0011
MC/DC20%1/5
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" or $key eq "escape"
not defined $key $key eq "eof" $key eq "interrupt" $key eq "q" $key eq "escape"
Truth table33%2/6
not(A) or B or C or D or E
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"D= $key eq "q"E= $key eq "escape"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
737 9 ▶ if ($key eq 'up' || $key eq 'back_tab') { $at = ($at - 1) % @{$options}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "up" or $key eq "back_tab") 1 8 9
Condition66%2/3
$key eq "up" or $key eq "back_tab"
l!l&&r!l&&!r
108
MC/DC50%1/2
$key eq "up" or $key eq "back_tab"
$key eq "up" $key eq "back_tab"
Truth table66%2/3
A or B
A= $key eq "up"B= $key eq "back_tab"
ABresult
1X1
011
000
738 8 ▶ if ($key eq 'down' || $key eq 'tab') { $at = ($at + 1) % @{$options}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "down" or $key eq "tab") 4 4 8
Condition66%2/3
$key eq "down" or $key eq "tab"
l!l&&r!l&&!r
404
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
739 4 ▶ if ($key eq 'home') { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home") 0 4 4
740 4 ▶ if ($key eq 'end') { $at = $#{$options}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end") 0 4 4
741 4 ▶ if ($key =~ /\A[1-9]\z/ && $key <= @{$options}) { $at = $key - 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-9]\z/ and $key <= @{$options;}) 0 4 4
Condition33%1/3
$key =~ /\A[1-9]\z/ and $key <= @{$options;}
!ll&&!rl&&r
400
MC/DC0%0/2
$key =~ /\A[1-9]\z/ and $key <= @{$options;}
$key =~ /\A[1-9]\z/ $key <= @{$options;}
Truth table33%1/3
A and B
A= $key =~ /\A[1-9]\z/B= $key <= @{$options;}
ABresult
0X0
100
111
742 4 ▶ return $options->[$at][0] if $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" or $key eq " " 4 0 4
Condition33%1/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
400
MC/DC0%0/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table33%1/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
743 }
744 }
745  
746 4 ▶sub ask_how_to_play {
Subroutine ask_how_to_play: called CC=7 SCAR=19.7
Pod documented
747 4 my ($self) = @_;
748 4 ▶ my $side = $self->choose('Which side will you take?', [
Branch: 3 branches
BranchTrueFalseTotal
$self->human eq 'defenders' ? : 0 4 4
$self->human eq 'both' ? : 0 4 4
$self->human eq 'none' ? : 0 4 4
749 [ 'attackers', 'The attackers', 'eight pieces round the board; they move first and must capture the king' ],
750 [ 'defenders', 'The defenders', 'the king and his four; he must reach a corner. The gentler side to learn on' ],
751 [ 'both', 'Both', 'two people at this keyboard' ],
752 [ 'none', 'Neither', 'watch the program play itself' ],
753 ], $self->human eq 'defenders' ? 1 : $self->human eq 'both' ? 2 : $self->human eq 'none' ? 3 : 0);
754 4 ▶ return 0 unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 1 3 4
755 3 $self->human($side);
756 3 ▶ return 1 if $side eq 'both';
Branch: 1 branch
BranchTrueFalseTotal
if $side eq "both" 1 2 3
757  
758 2 my $level = $self->choose('How hard should the program play?', [
759 [ 1, 'Level 1', 'looks a move or two ahead, and now and then does not look at all' ],
760 [ 2, 'Level 2', 'looks three moves ahead' ],
761 [ 3, 'Level 3', 'looks four or five moves ahead: its best' ],
762 ], $self->level - 1);
763 2 ▶ return 0 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 1 1 2
764 1 $self->level($level);
765 1 return 1;
766 }
767  
768 102 ▶sub screen {
Subroutine screen: called CC=5 SCAR=16.1
Pod documented
769 102 my ($self, $mode) = @_;
770 102 my @lines = (@{ $self->banner }, '');
771 102 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 1 101 102
772 1 push @lines, @{ $self->help_lines }, '', $self->legend('help');
773 1 return \@lines;
774 }
775 101 ▶ if ($mode eq 'rules') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "rules") 1 100 101
776 1 push @lines, @{ $self->rules_lines }, '', $self->legend('rules');
777 1 return \@lines;
778 }
779 100 ▶ push @lines, @{ $self->notice }, ('') x (@{ $self->notice } ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
@{$self->notice;} ? : 19 81 100
780 100 push @lines, @{ $self->board_lines($mode) }, '';
781 100 push @lines, @{ $self->status_lines };
782 100 ▶ push @lines, @{ $self->preview_lines } if $mode eq 'where';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "where" 29 71 100
783 100 push @lines, '', $self->legend($mode);
784 100 return \@lines;
785 }
786  
787 102 ▶sub show {
Subroutine show: called CC=3 SCAR=11.5
Pod documented
788 102 my ($self, $mode) = @_;
789 102 my $out = $self->out;
790 102 my $lines = $self->screen($mode);
791 102 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 102 0 102
792 102 print {$out} "\e[H", join('', map { "$_\e[K\n" } @{$lines}), "\e[J";
793 }
794 else {
795 0 print {$out} "$_\n" for @{$lines};
796 }
797 102 $self->redraw(0);
798 102 return $self;
799 }
800  
801 258 ▶sub render {
Subroutine render: called CC=4 SCAR=14.0
Pod documented
802 258 my ($self) = @_;
803 258 my $out = $self->out;
804 258 ▶ print {$out} "\e[H\e[2J" if $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->interactive 0 258 258
805 258 $self->say($_) for @{ $self->notice };
806 258 $self->say($_) for @{ $self->board_lines('plain') }, '', @{ $self->status_lines };
807 258 $self->redraw(0);
808 258 return $self;
809 }
810  
811 5873 ▶sub say {
Subroutine say: called CC=2 SCAR=7.0
Pod documented
812 5873 my ($self, $line) = @_;
813 5873 my $out = $self->out;
814 5873 ▶ print {$out} (defined $line ? $line : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 5873 0 5873
815 5873 return $self;
816 }
817  
818 248 ▶sub remark {
Subroutine remark: called CC=2 SCAR=6.9
Pod documented
819 248 my ($self, $line) = @_;
820 248 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 5 243 248
821 5 $self->notice([ @{ $self->notice }, $line ]);
822 5 return $self;
823 }
824 243 return $self->say($line);
825 }
826  
827 145 ▶sub read_line {
Subroutine read_line: called CC=5 SCAR=16.1
Pod documented
828 145 my ($self, $prompt) = @_;
829 145 my $out = $self->out;
830 145 my $game = $self->game;
831 145 ▶ print {$out} defined $prompt ? $prompt : ($game->side_to_move || 'game over') . '> ';
Branch: 1 branch
BranchTrueFalseTotal
defined $prompt ? : 19 126 145
Condition50%1/2
$game->side_to_move || 'game over'
l!l
1260
MC/DC0%0/1
$game->side_to_move || 'game over'
$game->side_to_move
Truth table50%1/2
A
A= $game->side_to_move
Aresult
11
00
832 145 ▶ return $self->keyed_line if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 2 143 145
833 143 my $line = readline $self->in;
834 143 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 6 137 143
835 137 chomp $line;
836 137 return $line;
837 }
838  
839 2 ▶sub keyed_line {
Subroutine keyed_line: called CC=10 SCAR=24.9
Pod documented
840 2 my ($self) = @_;
841 2 my $out = $self->out;
842 2 my $line = '';
843 2 while (1) {
844 18 my $key = $self->read_key;
845 18 ▶ 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 18 18
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0018
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0018
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
846 18 ▶ last if $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" 2 16 18
847 16 ▶ if ($key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape") 0 16 16
848 0 $line = '';
849 0 last;
850 }
851 16 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 1 15 16
852 1 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 1 1
853 1 chop $line;
854 1 print {$out} "\b \b";
855 1 next;
856 }
857 15 ▶ next if length $key > 1 || $key lt ' ';
Branch: 1 branch
BranchTrueFalseTotal
if length $key > 1 or $key lt " " 0 15 15
Condition33%1/3
length $key > 1 or $key lt " "
l!l&&r!l&&!r
0015
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
858 15 $line .= $key;
859 15 print {$out} $key;
860 }
861 2 print {$out} "\n";
862 2 return $line;
863 }
864  
865 177 ▶sub command {
Subroutine command: called CC=27 SCAR=34.2
Pod documented
866 177 my ($self, $line) = @_;
867 177 $line =~ s/\A\s+//;
868 177 $line =~ s/\s+\z//;
869 177 ▶ return 0 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 8 169 177
870 169 my ($word, $rest) = split /\s+/, $line, 2;
871 169 $word = lc $word;
872 169 ▶ $rest = '' unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 156 13 169
873  
874 169 ▶ 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" 75 94 169
Condition66%4/6
$word eq "quit" or $word eq "exit" or $word eq "q"
l!l&&r!l&&!r
75094
$word eq "quit" or $word eq "exit"
l!l&&r!l&&!r
75094
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
875 94 ▶ if ($word eq 'help' || $word eq '?') { $self->page('help'); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help" or $word eq "?") 2 92 94
Condition66%2/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
2092
MC/DC50%1/2
$word eq "help" or $word eq "?"
$word eq "help" $word eq "?"
Truth table66%2/3
A or B
A= $word eq "help"B= $word eq "?"
ABresult
1X1
011
000
876 92 ▶ if ($word eq 'rules') { $self->page('rules'); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "rules") 5 87 92
877 87 ▶ if ($word eq 'board') { $self->redraw(1); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "board") 2 85 87
878 85 ▶ if ($word eq 'moves') { $self->show_moves; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "moves") 4 81 85
879 81 ▶ if ($word eq 'hint') { $self->show_hint; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "hint") 1 80 81
880 80 ▶ if ($word eq 'undo') { $self->take_back; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "undo") 6 74 80
881 74 ▶ if ($word eq 'new') { $self->new_game; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "new") 3 71 74
882 71 ▶ if ($word eq 'save') { $self->save($rest); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "save") 3 68 71
883 68 ▶ if ($word eq 'load') { $self->load($rest); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "load") 5 63 68
884 63 ▶ if ($word eq 'level') { $self->set_level($rest); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "level") 5 58 63
885 58 ▶ if ($word eq 'side') { $self->set_side($rest); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "side") 2 56 58
886 56 ▶ if ($word eq 'draw') { $self->offer_draw; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "draw") 4 52 56
887 52 ▶ if ($word eq 'resign') { $self->resign; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "resign") 4 48 52
888 48 ▶ if ($word eq 'colour' || $word eq 'color') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "colour" or $word eq "color") 2 46 48
Condition66%2/3
$word eq "colour" or $word eq "color"
l!l&&r!l&&!r
2046
MC/DC50%1/2
$word eq "colour" or $word eq "color"
$word eq "colour" $word eq "color"
Truth table66%2/3
A or B
A= $word eq "colour"B= $word eq "color"
ABresult
1X1
011
000
889 2 ▶ $self->colour($self->colour ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 0 2 2
890 2 $self->redraw(1);
891 2 return 0;
892 }
893 46 ▶ if ($word eq 'pick') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "pick") 0 46 46
894 0 ▶ if ($self->keys_available && $self->enter_raw) { $self->picking(1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keys_available and $self->enter_raw) { } 0 0 0
Condition0%0/3
$self->keys_available and $self->enter_raw
!ll&&!rl&&r
000
MC/DC0%0/2
$self->keys_available and $self->enter_raw
$self->keys_available $self->enter_raw
Truth table0%0/3
A and B
A= $self->keys_availableB= $self->enter_raw
ABresult
0X0
100
111
895 0 else { $self->remark('The arrow keys need Term::ReadKey and a terminal. Still typing.') }
896 0 $self->redraw(1);
897 0 return 0;
898 }
899 46 ▶ if ($word eq 'type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "type") 0 46 46
900 0 $self->picking(0);
901 0 $self->leave_raw;
902 0 $self->redraw(1);
903 0 return 0;
904 }
905  
906 46 my $move = $line;
907 46 $move =~ s/\A([a-gA-G][1-7])\s+([a-gA-G][1-7])\z/$1$2/;
908 46 $self->play_text($move);
909 46 return 0;
910 }
911  
912 46 ▶sub play_text {
Subroutine play_text: called CC=4 SCAR=13.9
Pod documented
913 46 my ($self, $move) = @_;
914 46 my $game = $self->game;
915 46 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "finished") 1 45 46
916 1 $self->remark('The game is over. new starts another.');
917 1 return $self;
918 }
919 45 my $refused = $game->play($move);
920 45 ▶ if ($refused) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refused) 9 36 45
921 9 my $code = $refused->code;
922 9 ▶ $self->remark($code eq 'bad_move'
Branch: 1 branch
BranchTrueFalseTotal
$code eq 'bad_move' ? : 2 7 9
923 ? "I do not know '$move'. A move is two squares, like d1d3. help lists the commands."
924 : ucfirst($refused->message) . '.');
925 9 return $self;
926 }
927 36 $self->notice([]);
928 36 $self->redraw(1);
929 36 return $self;
930 }
931  
932 7 ▶sub page {
Subroutine page: called CC=4 SCAR=13.9
Pod documented
933 7 my ($self, $which) = @_;
934 7 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) 1 6 7
935 1 $self->show($which);
936 1 $self->read_key;
937 1 $self->redraw(1);
938 1 return $self;
939 }
940 6 ▶ $self->say($_) for @{ $which eq 'help' ? $self->help_lines : $self->rules_lines };
Branch: 1 branch
BranchTrueFalseTotal
$which eq 'help' ? : 2 4 6
941 6 return $self;
942 }
943  
944 4 ▶sub show_moves {
Subroutine show_moves: called CC=6 SCAR=18.0
Pod documented
945 4 my ($self) = @_;
946 4 my $legal = $self->game->legal;
947 4 ▶ unless (@{$legal}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$legal;}) 0 4 4
948 0 $self->remark('There are no moves: the game is over.');
949 0 return $self;
950 }
951 4 my %by;
952 4 push @{ $by{ $_->{from} } }, $_ for @{$legal};
953 4 for my $from (sort keys %by) {
954 my @to = map {
955 135 ▶ $_->{to} . (@{ $_->{captures} } ? 'x' . join(',', @{ $_->{captures} }) : '') . ($_->{wins} ? '!' : '')
Branch: 2 branches
BranchTrueFalseTotal
@{$_->{'captures'};} ? : 3 132 135
$_->{'wins'} ? : 2 133 135
956 25 } sort { $a->{to} cmp $b->{to} } @{ $by{$from} };
957 25 $self->say(sprintf ' %-8s %s %s', $by{$from}[0]{piece}, $from, join(' ', @to));
958 }
959 4 $self->say(sprintf '%d moves. x marks a capture, ! a move that wins.', scalar @{$legal});
960 4 return $self;
961 }
962  
963 2 ▶sub hint_move {
Subroutine hint_move: called CC=1 SCAR=0.0
Pod documented
964 2 my ($self) = @_;
965 2 return Game::Brandubh::Bot->hint($self->game, seed => $self->seed);
966 }
967  
968 1 ▶sub show_hint {
Subroutine show_hint: called CC=2 SCAR=7.0
Pod documented
969 1 my ($self) = @_;
970 1 my $move = $self->hint_move;
971 1 ▶ $self->remark(defined $move
Branch: 1 branch
BranchTrueFalseTotal
defined $move ? : 1 0 1
972 ? 'The program would play ' . substr($move, 0, 2) . '-' . substr($move, 2, 2) . '.'
973 : 'The game is over: there is nothing to play.');
974 1 return $self;
975 }
976  
977 6 ▶sub take_back {
Subroutine take_back: called CC=12 SCAR=25.0
Pod documented
978 6 my ($self) = @_;
979 6 my $game = $self->game;
980 6 my $took = 0;
981 6 ▶ $took++ if $game->status eq 'finished' && $game->result->by_players && $game->undo;
Branch: 1 branch
BranchTrueFalseTotal
if $game->status eq "finished" and $game->result->by_players and $game->undo 1 5 6
Condition83%5/6
$game->status eq "finished" and $game->result->by_players and $game->undo
!ll&&!rl&&r
501
$game->status eq "finished" and $game->result->by_players
!ll&&!rl&&r
411
MC/DC66%2/3
$game->status eq "finished" and $game->result->by_players and $game->undo
$game->status eq "finished" $game->result->by_players $game->undo
Truth table75%3/4
A and B and C
A= $game->status eq "finished"B= $game->result->by_playersC= $game->undo
ABCresult
0XX0
10X0
1100
1111
982 6 ▶ if (!$took) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($took) 5 1 6
983 5 ▶ $took++ if $game->undo;
Branch: 1 branch
BranchTrueFalseTotal
if $game->undo 4 1 5
984 5 ▶ $took++ if $took && $self->human !~ /\A(?:both|none)\z/
Branch: 1 branch
BranchTrueFalseTotal
if $took and not $self->human =~ /\A(?:both|none)\z/ and $game->status eq "active" and $game->side_to_move ne $self->human and $game->undo 1 4 5
Condition75%9/12
$took and not $self->human =~ /\A(?:both|none)\z/ and $game->status eq "active" and $game->side_to_move ne $self->human and $game->undo
!ll&&!rl&&r
401
$took and not $self->human =~ /\A(?:both|none)\z/ and $game->status eq "active" and $game->side_to_move ne $self->human
!ll&&!rl&&r
401
$took and not $self->human =~ /\A(?:both|none)\z/ and $game->status eq "active"
!ll&&!rl&&r
401
$took and not $self->human =~ /\A(?:both|none)\z/
!ll&&!rl&&r
131
MC/DC40%2/5
$took and not $self->human =~ /\A(?:both|none)\z/ and $game->status eq "active" and $game->side_to_move ne $self->human and $game->undo
$took not $self->human =~ /\A(?:both|none)\z/ $game->status eq "active" $game->side_to_move ne $self->human $game->undo
Truth table50%3/6
A and not(B) and C and D and E
A= $tookB= not $self->human =~ /\A(?:both|none)\z/C= $game->status eq "active"D= $game->side_to_move ne $self->humanE= $game->undo
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
985 && $game->status eq 'active' && $game->side_to_move ne $self->human && $game->undo;
986 }
987 6 $self->notice([]);
988 6 ▶ $self->remark($took ? 'Taken back.' : 'There is nothing to take back.');
Branch: 1 branch
BranchTrueFalseTotal
$took ? : 5 1 6
989 6 $self->focus(undef);
990 6 $self->chosen(undef);
991 6 $self->redraw(1);
992 6 return $took;
993 }
994  
995 3 ▶sub new_game {
Subroutine new_game: called CC=1 SCAR=0.0
Pod documented
996 3 my ($self) = @_;
997 3 $self->seed(Digest::SHA::sha256($self->seed . 'again'));
998 3 $self->game($self->fresh_game);
999 3 $self->notice(['A new game.']);
1000 3 $self->focus(undef);
1001 3 $self->chosen(undef);
1002 3 $self->redraw(1);
1003 3 return $self;
1004 }
1005  
1006 3 ▶sub save {
Subroutine save: called CC=3 SCAR=11.0
Pod documented
1007 3 my ($self, $file) = @_;
1008 3 ▶ unless (length $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $file) 1 2 3
1009 1 $self->remark('save needs a file name.');
1010 1 return 0;
1011 }
1012 2 my $handle;
1013 2 ▶ unless (open $handle, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $handle, ">", $file) 1 1 2
1014 1 $self->remark("Cannot write $file: $!.");
1015 1 return 0;
1016 }
1017 1 print {$handle} $self->game->as_text;
1018 1 close $handle;
1019 1 $self->remark("Saved to $file.");
1020 1 return 1;
1021 }
1022  
1023 7 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod documented
1024 7 my ($self, $file) = @_;
1025 7 ▶ unless (length $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $file) 1 6 7
1026 1 $self->remark('load needs a file name.');
1027 1 return 0;
1028 }
1029 6 my $handle;
1030 6 ▶ unless (open $handle, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $handle, "<", $file) 2 4 6
1031 2 $self->remark("Cannot read $file: $!.");
1032 2 return 0;
1033 }
1034 4 my $text = do { local $/; <$handle> };
1035 4 close $handle;
1036 4 my ($game, $refused, $at) = Game::Brandubh->from_text($text, seed => $self->seed);
1037 4 ▶ if (!$game || $refused) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $game or $refused) 2 2 4
Condition100%3/3
not $game or $refused
l!l&&r!l&&!r
112
MC/DC100%2/2
not $game or $refused
not $game $refused
Truth table100%3/3
not(A) or B
A= not $gameB= $refused
ABresult
1X1
011
000
1038 2 ▶ $self->remark($game
Branch: 1 branch
BranchTrueFalseTotal
$game ? : 1 1 2
1039 ? sprintf('%s is not a game I can play: move %d is refused (%s).', $file, $at + 1, $refused->message)
1040 : "$file is not a saved game.");
1041 2 return 0;
1042 }
1043 2 $self->game($game);
1044 2 $self->notice([ sprintf 'Loaded %s: %d moves.', $file, $game->ply ]);
1045 2 ▶ $self->say($self->notice->[0]) unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 2 0 2
1046 2 $self->focus(undef);
1047 2 $self->chosen(undef);
1048 2 $self->redraw(1);
1049 2 return 1;
1050 }
1051  
1052 6 ▶sub set_level {
Subroutine set_level: called CC=3 SCAR=11.0
Pod documented
1053 6 my ($self, $level) = @_;
1054 6 ▶ unless (defined $level && $level =~ /\A[123]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level and $level =~ /\A[123]\z/) 2 4 6
Condition66%2/3
defined $level and $level =~ /\A[123]\z/
!ll&&!rl&&r
024
MC/DC50%1/2
defined $level and $level =~ /\A[123]\z/
defined $level $level =~ /\A[123]\z/
Truth table66%2/3
A and B
A= defined $levelB= $level =~ /\A[123]\z/
ABresult
0X0
100
111
1055 2 $self->remark('A level is 1, 2 or 3.');
1056 2 return 0;
1057 }
1058 4 $self->level($level);
1059 4 $self->remark("The program now plays at level $level.");
1060 4 return 1;
1061 }
1062  
1063 2 ▶sub set_side {
Subroutine set_side: called CC=5 SCAR=16.4
Pod documented
1064 2 my ($self, $side) = @_;
1065 2 ▶ $side = lc(defined $side ? $side : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 2 0 2
1066 2 ▶ unless ($side =~ /\A(?:attackers|defenders|both|none)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($side =~ /\A(?:attackers|defenders|both|none)\z/) 1 1 2
1067 1 $self->remark('A side is attackers, defenders, both or none.');
1068 1 return 0;
1069 }
1070 1 $self->human($side);
1071 1 ▶ $self->remark($side eq 'both' ? 'Both sides are yours.'
Branch: 2 branches
BranchTrueFalseTotal
$side eq 'both' ? : 0 1 1
$side eq 'none' ? : 0 1 1
1072 : $side eq 'none' ? 'The program plays both sides.'
1073 : "You have the $side.");
1074 1 $self->redraw(1);
1075 1 return 1;
1076 }
1077  
1078 6 ▶sub seat_of_human {
Subroutine seat_of_human: called CC=3 SCAR=11.0
Pod documented
1079 6 my ($self) = @_;
1080 6 my $game = $self->game;
1081 6 ▶ return $game->turn if $self->human eq 'both' || $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "both" or $self->human eq "none" 3 3 6
Condition66%2/3
$self->human eq "both" or $self->human eq "none"
l!l&&r!l&&!r
303
MC/DC50%1/2
$self->human eq "both" or $self->human eq "none"
$self->human eq "both" $self->human eq "none"
Truth table66%2/3
A or B
A= $self->human eq "both"B= $self->human eq "none"
ABresult
1X1
011
000
1082 3 return $game->seat_of($self->human);
1083 }
1084  
1085 4 ▶sub offer_draw {
Subroutine offer_draw: called CC=7 SCAR=19.5
Pod documented
1086 4 my ($self) = @_;
1087 4 my $game = $self->game;
1088 4 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "finished") 1 3 4
1089 1 $self->remark('The game is over.');
1090 1 return 0;
1091 }
1092 3 my $seat = $self->seat_of_human;
1093 3 ▶ my $other = $seat eq 'p1' ? 'p2' : 'p1';
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p1' ? : 2 1 3
1094 3 ▶ if ($self->human eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->human eq "both") 1 2 3
1095 1 $game->offer_draw($seat);
1096 1 $game->accept_draw($other);
1097 1 $self->redraw(1);
1098 1 return 1;
1099 }
1100 2 my $score = $game->search(budget => $self->budget, salt => 'draw');
1101 2 ▶ my $for_program = $score ? ($game->turn eq $seat ? -$score->{score} : $score->{score}) : 0;
Branch: 2 branches
BranchTrueFalseTotal
$score ? : 2 0 2
$game->turn eq $seat ? : 2 0 2
1102 2 ▶ if ($for_program > 150) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_program > 150) 1 1 2
1103 1 $self->remark('The program declines the draw.');
1104 1 return 0;
1105 }
1106 1 $game->offer_draw($seat);
1107 1 $game->accept_draw($other);
1108 1 $self->redraw(1);
1109 1 return 1;
1110 }
1111  
1112 4 ▶sub resign {
Subroutine resign: called CC=2 SCAR=6.9
Pod documented
1113 4 my ($self) = @_;
1114 4 my $game = $self->game;
1115 4 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "finished") 1 3 4
1116 1 $self->remark('The game is over.');
1117 1 return 0;
1118 }
1119 3 $game->resign($self->seat_of_human);
1120 3 $self->redraw(1);
1121 3 return 1;
1122 }
1123  
1124 3 ▶sub help_lines {
Subroutine help_lines: called CC=2 SCAR=6.9
Pod documented
1125 3 my ($self) = @_;
1126 3 my @lines = ('Typed commands:', '');
1127 3 push @lines, sprintf(' %-10s %s', $_->[0], $_->[1]) for @COMMANDS;
1128 3 push @lines, '', 'On the board, with the arrow keys:', '',
1129 ' arrows move to a piece, then to where it should go',
1130 ' tab the next one, in order',
1131 ' enter pick the piece up, then put it down',
1132 ' esc put the piece back',
1133 ' h show the move the program would play',
1134 ' 1 2 3 how hard the program plays',
1135 ' u r n c q undo, rules, new game, colour, quit',
1136 ' : type a command';
1137 3 return \@lines;
1138 }
1139  
1140 5 ▶sub rules_lines {
Subroutine rules_lines: called CC=4 SCAR=14.9
Pod documented
1141 5 my ($self) = @_;
1142 5 my $repeat = $self->game->variant->repeat;
1143 5 ▶ my $nth = $repeat == 2 ? 'second' : $repeat == 3 ? 'third' : $repeat == 4 ? 'fourth' : "${repeat}th";
Branch: 3 branches
BranchTrueFalseTotal
$repeat == 2 ? : 2 3 5
$repeat == 3 ? : 3 0 3
$repeat == 4 ? : 0 0 0
1144 return [
1145 5 'Brandubh is played on seven squares by seven. The middle square is the',
1146 'throne and the four corners are the king\'s way out.',
1147 '',
1148 'The attackers have eight pieces and move first. The defenders have four,',
1149 'and the king, who starts on the throne.',
1150 '',
1151 'Every piece moves like a rook: any distance along a row or a column, not',
1152 'onto another piece and not over one. No piece may stop on the throne, not',
1153 'even the king once he has left it, though a piece may slide across it',
1154 'while it is empty. Only the king may stop on a corner.',
1155 '',
1156 'A piece is captured when the enemy moves so that it stands between two',
1157 'enemy pieces, one on each side, along a row or a column. An empty corner,',
1158 'and the throne while it is empty, each count as an enemy to both sides.',
1159 'Only the piece that moved captures: a piece may move between two enemies',
1160 'and stand there unharmed. One move can capture up to three. Nothing is',
1161 'captured against the edge of the board.',
1162 '',
1163 'The king is captured by attackers: four round him on the throne, three',
1164 'when he stands beside it, and two, like any piece, anywhere else.',
1165 '',
1166 'The king wins by reaching a corner. The attackers win by capturing him,',
1167 'and lose if they have no piece left.',
1168 '',
1169 "The game is drawn when a position comes round for the $nth time, when",
1170 'the side to move cannot move, and when it has gone on too long to end.',
1171 ];
1172 }
1173  
1174 4 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
1175 4 return <<'USAGE';
1176 brandubh - the Irish tafl game, in a terminal
1177  
1178 brandubh [options]
1179  
1180 --side WHO attackers (the default), defenders, both, or none
1181 --level N how hard the program plays: 1, 2 (the default) or 3
1182 --variant TEXT the rule set, for example 'custom repeat=2'
1183 --seed HEX sixty-four hexadecimal characters; the same seed plays the same game
1184 --load FILE carry on with a saved game
1185 --no-colour letters and lines instead of a painted board
1186 --no-pick type moves instead of choosing them with the arrow keys
1187 --no-unicode letters for the pieces, with colour kept
1188 --version the version, and stop
1189 --help this, and stop
1190  
1191 In a terminal, moves are chosen on the board with the arrow keys; press ? for
1192 the keys. Anywhere else, type a move as two squares, like d1d3, or help.
1193 USAGE
1194 }
1195  
1196 35 ▶sub run {
Subroutine run: called CC=22 SCAR=30.9
Pod documented
1197 35 my ($class, @argv) = @_;
1198  
1199 35 my %given;
1200 35 my $parsed = Getopt::Long::GetOptionsFromArray(
1201 \@argv, \%given,
1202 'level=i', 'side=s', 'seed=s', 'variant=s', 'load=s',
1203 'colour|color!', 'pick!', 'unicode!', 'version', 'help',
1204 );
1205  
1206 35 ▶ if (!$parsed || @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed or @argv) 3 32 35
Condition100%3/3
not $parsed or @argv
l!l&&r!l&&!r
2132
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
1207 3 ▶ print STDERR "brandubh: 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
1208 3 print STDERR _usage();
1209 3 return 2;
1210 }
1211 32 ▶ if ($given{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"help"}) 1 31 32
1212 1 print _usage();
1213 1 return 0;
1214 }
1215 31 ▶ if ($given{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"version"}) 1 30 31
1216 1 print "brandubh $Game::Brandubh::VERSION\n";
1217 1 return 0;
1218 }
1219  
1220 30 my %with;
1221 30 ▶ $with{level} = $given{level} if defined $given{level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"level"} 10 20 30
1222 30 ▶ $with{human} = $given{side} if defined $given{side};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"side"} 24 6 30
1223 30 ▶ $with{variant} = $given{variant} if defined $given{variant};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"variant"} 7 23 30
1224 30 ▶ $with{colour} = $given{colour} if defined $given{colour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"colour"} 5 25 30
1225 30 ▶ $with{picking} = $given{pick} if defined $given{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"pick"} 2 28 30
1226 30 ▶ $with{unicode} = $given{unicode} if defined $given{unicode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"unicode"} 2 28 30
1227 30 ▶ $with{menu} = 1 unless defined $given{side} || defined $given{level} || defined $given{load};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given{"side"} or defined $given{"level"} or defined $given{"load"} 4 26 30
Condition100%6/6
defined $given{"side"} or defined $given{"level"} or defined $given{"load"}
l!l&&r!l&&!r
2514
defined $given{"side"} or defined $given{"level"}
l!l&&r!l&&!r
2415
MC/DC100%3/3
defined $given{"side"} or defined $given{"level"} or defined $given{"load"}
defined $given{"side"} defined $given{"level"} defined $given{"load"}
Truth table100%4/4
A or B or C
A= defined $given{"side"}B= defined $given{"level"}C= defined $given{"load"}
ABCresult
1XX1
01X1
0011
0000
1228  
1229 30 ▶ if (defined $given{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"seed"}) 6 24 30
1230 6 ▶ if ($given{seed} !~ /\A[0-9a-fA-F]{64}\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($given{"seed"} =~ /\A[0-9a-fA-F]{64}\z/) 2 4 6
1231 2 print STDERR "brandubh: a seed is sixty-four hexadecimal characters.\n";
1232 2 return 2;
1233 }
1234 4 $with{seed} = pack 'H*', $given{seed};
1235 }
1236  
1237 28 my $terminal = eval { $class->new(%with) };
1238 28 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 3 25 28
1239 3 (my $why = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
1240 3 $why =~ s/\AGame::Brandubh(?:::\w+)*: //;
1241 3 print STDERR "brandubh: $why.\n";
1242 3 return 2;
1243 }
1244  
1245 25 local @SIG{qw(INT TERM HUP)};
1246 25 for my $sig (qw(INT TERM HUP)) {
1247 75 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
1248 }
1249  
1250 25 ▶ if (defined $given{load}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"load"}) 2 23 25
1251 2 ▶ return 2 unless $terminal->load($given{load});
Branch: 1 branch
BranchTrueFalseTotal
unless $terminal->load($given{"load"}) 1 1 2
1252 }
1253  
1254 24 return $terminal->start;
1255 }
1256  
1257 38 1;
1258  
1259 __END__
↑ Summary blib/lib/Game/Brandubh/Variant.pm »