blib/lib/Game/Brandubh/Terminal.pm

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