blib/lib/Game/Senet/Terminal.pm

filter: Statementstmt 93.0% 587 / 631 Branchbran 82.5% 322 / 390 Conditioncond 68.7% 108 / 157 MC/DCmcdc 50.5% 46 / 91 Subroutinesub 97.5% 79 / 81 Podpod 95.4% 21 / 22 totaltotal 84.7% 1163 / 1372 CC 261 SCAR 30.6
CC
261
Cov
86%
command
129.9
BUILD
31.0
run
21.0
CRAP 21.3
↑ Summary blib/lib/Game/Senet/Notation.pm »
1 package Game::Senet::Terminal;
2  
3 54 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 54 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 54 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 54 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 54 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
10 54 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 54 ▶use Game::Terminal::Kit::Frame;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 54 ▶use Game::Terminal::Kit::History;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 54 ▶use Game::Terminal::Kit::Keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 54 ▶use Game::Terminal::Kit::Run;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 54 ▶use Game::Terminal::Kit::Screen;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 54 ▶use Game::Terminal::Kit::Steer;
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 54 ▶use Game::Senet;
Subroutine __ANON__: called CC=1 SCAR=0.0
20 54 ▶use Game::Senet::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 54 ▶use Game::Senet::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 54 ▶use Game::Senet::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 54 our $VERSION = '0.02';
25  
26 54 my (%STYLE, %GLYPH, %SIGN, %OTHER, %ROW_OF, %COL_OF);
27 54 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
28 54 %STYLE = (
29 title => '1;38;5;220',
30 grid => '38;5;241',
31 label => '38;5;245',
32 sign => '1;38;5;205',
33 light => '1;38;5;229',
34 dark => '1;38;5;39',
35 cursor => '1;38;5;16;48;5;220',
36 target => '1;38;5;16;48;5;114',
37 from => '1;38;5;231;48;5;160',
38 recent => '38;5;250',
39 face => '1;38;5;229',
40 back => '38;5;240',
41 key => '1;38;5;214',
42 good => '1;38;5;114',
43 bad => '1;38;5;203',
44 quiet => '38;5;245',
45 );
46  
47 54 %GLYPH = (
48 wide => {
49 light => "\x{25CF}", dark => "\x{25CB}", face => "\x{25AF}", back => "\x{25AE}",
50 h => "\x{2500}", v => "\x{2502}", tl => "\x{250C}", tr => "\x{2510}", bl => "\x{2514}",
51 br => "\x{2518}", td => "\x{252C}", tu => "\x{2534}", lt => "\x{251C}", rt => "\x{2524}",
52 x => "\x{253C}", ankh => "\x{2625}", turn_a => "\x{2500}\x{2510}", turn_b => "\x{2502}",
53 turn_c => "\x{2500}\x{2518}", turn_d => "\x{250C}\x{2500}", turn_e => "\x{2502}",
54 turn_f => "\x{2514}\x{2500}", into => "\x{25B6}", from => "\x{25C0}",
55 },
56 ascii => {
57 light => 'O', dark => '@', face => '|', back => '#',
58 h => '-', v => '|', tl => '+', tr => '+', bl => '+', br => '+', td => '+', tu => '+',
59 lt => '+', rt => '+', x => '+', ankh => '+', turn_a => '-.', turn_b => '|', turn_c => "-'",
60 turn_d => '.-', turn_e => '|', turn_f => "'-", into => '>', from => '<',
61 },
62 );
63  
64 54 %SIGN = (26 => 'X', 27 => '~', 28 => 'III', 29 => 'II', 30 => 'I');
65 54 %OTHER = (light => 'dark', dark => 'light');
66 54 for my $h (1 .. 30) {
67 1620 my $row = int(($h - 1) / 10);
68 1620 $ROW_OF{$h} = $row;
69 1620 ▶ $COL_OF{$h} = $row == 1 ? 20 - $h : ($h - 1) % 10;
Branch: 1 branch
BranchTrueFalseTotal
$row == 1 ? : 540 1080 1620
70 }
71 }
72  
73 54 has game => (is => 'rw');
74  
75 54 has [qw/in out/] => (is => 'rw');
76  
77 54 has [qw/interactive colour unicode picking/] => (is => 'rw');
78  
79 54 has mode => (is => 'rw', default => 'bot');
80  
81 54 has side => (is => 'rw', default => 'light');
82  
83 54 has level => (is => 'rw');
84  
85 54 has rules => (is => 'rw', default => 'kendall');
86  
87 54 has pieces => (is => 'rw', default => 5);
88  
89 54 has first => (is => 'rw', default => 'light');
90  
91 54 has seed => (is => 'rw');
92  
93 54 has pace => (is => 'rw');
94  
95 54 has record => (is => 'rw');
96  
97 54 has keysource => (is => 'rw');
98  
99 54 has history => (is => 'rw');
100  
101 54 has sleeper => (is => 'rw');
102  
103 54 has [qw/_keyboard _screen/] => (is => 'rw');
104  
105 54 has _events => (is => 'rw', private => 1);
106  
107 54 has _told => (is => 'rw', private => 1);
108  
109 54 has _cursor => (is => 'rw', private => 1);
110  
111 54 has _note => (is => 'rw', private => 1);
112  
113 900 ▶sub BUILD {
Subroutine BUILD: called CC=30 SCAR=34.4
Pod undocumented
114 900 my ($self) = @_;
115 900 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 47 853 900
116 900 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 47 853 900
117 900 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 47 853 900
-t $self->in ? : 0 47 47
118 900 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 36 864 900
$self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? : 0 36 36
Condition33%1/3
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
3600
MC/DC0%0/2
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
$self->interactive not Game::Terminal::Kit::Screen::no_colour()
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not Game::Terminal::Kit::Screen::no_colour()
ABresult
0X0
100
111
119 900 ▶ $self->unicode($self->interactive ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 37 863 900
$self->interactive ? : 0 37 37
120 900 ▶ $self->_keyboard(Game::Terminal::Kit::Keys->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->keysource ? : 37 863 900
121 in => $self->in,
122 ($self->keysource ? (keysource => $self->keysource) : ()),
123 ));
124 900 ▶ $self->_screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 406 494 900
125 out => $self->out,
126 interactive => $self->interactive,
127 colour => $self->colour,
128 ascii => $self->unicode ? 0 : 1,
129 ));
130 900 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 38 862 900
$self->interactive ? : 0 38 38
131 900 ▶ $self->picking(0) unless $self->_keyboard->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->keys_available 863 37 900
132 900 ▶ $self->pace($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pace;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pace 36 864 900
$self->interactive && !$self->keysource ? : 0 36 36
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
3600
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
133  
134 900 ▶ Carp::croak("Game::Senet::Terminal: mode is bot, hotseat or watch, not '" . $self->mode . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mode =~ /\A(?:bot|hotseat|watch)\z/ 1 899 900
135 unless $self->mode =~ /\A(?:bot|hotseat|watch)\z/;
136 899 ▶ Carp::croak("Game::Senet::Terminal: side is light or dark, not '" . $self->side . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->side =~ /\A(?:light|dark)\z/ 1 898 899
137 unless $self->side =~ /\A(?:light|dark)\z/;
138 898 ▶ Carp::croak("Game::Senet::Terminal: first is light or dark, not '" . $self->first . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->first =~ /\A(?:light|dark)\z/ 1 897 898
139 unless $self->first =~ /\A(?:light|dark)\z/;
140 897 ▶ Carp::croak('Game::Senet::Terminal: pieces is 5 or 7')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pieces =~ /\A[57]\z/ 1 896 897
141 unless $self->pieces =~ /\A[57]\z/;
142 896 ▶ Carp::croak('Game::Senet::Terminal: pace is a number of seconds, 0 or more')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pace =~ /\A\d+(?:\.\d+)?\z/ 2 894 896
143 unless $self->pace =~ /\A\d+(?:\.\d+)?\z/;
144  
145 894 ▶ my $variant = $self->game ? $self->game->variant : $self->_variant;
Branch: 1 branch
BranchTrueFalseTotal
$self->game ? : 848 46 894
146 893 my @levels = Game::Senet::Bot->levels($variant);
147 893 ▶ $self->level($levels[-1]) unless defined $self->level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->level 880 13 893
148 893 ▶ Carp::croak('Game::Senet::Terminal: level is a whole number from 1')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->level =~ /\A[1-9][0-9]*\z/ 1 892 893
149 unless $self->level =~ /\A[1-9][0-9]*\z/;
150 892 ▶ $self->level($levels[-1]) if $self->level > $levels[-1];
Branch: 1 branch
BranchTrueFalseTotal
if $self->level > $levels[-1] 2 890 892
151  
152 892 ▶ $self->seed($self->_system_seed) unless defined $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed 816 76 892
153 892 $self->_events([]);
154 892 $self->_told(0);
155 892 $self->_cursor(0);
156 892 ▶ if ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game) { } 848 44 892
157 848 $self->_told(scalar @{ $self->game->log });
158 }
159 else {
160 44 $self->game($self->_fresh_game);
161 }
162 $self->history(Game::Terminal::Kit::History->new(
163 1071 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
164 1 ▶ load => sub { Game::Senet->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 892 ▶ )) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 892 0 892
166 892 $self->history->start($self->game);
167  
168 892 $self->_screen->utf8->flush_on;
169 892 return;
170 }
171  
172 816 ▶sub _system_seed {
Subroutine _system_seed: called CC=3 SCAR=11.3
173 816 my $bytes = '';
174 816 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 816 0 816
175 816 read $fh, $bytes, 16;
176 816 close $fh;
177 }
178 816 ▶ return length $bytes == 16 ? $bytes : Digest::SHA::sha256(join '|', 'senet', $$, time, rand);
Branch: 1 branch
BranchTrueFalseTotal
length $bytes == 16 ? : 816 0 816
179 }
180  
181 91 ▶sub _variant {
Subroutine _variant: called CC=2 SCAR=7.0
182 91 my ($self) = @_;
183 91 my $rules = $self->rules;
184 91 ▶ return $rules if ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules 0 91 91
185 91 return Game::Senet::Variant->named($rules, pieces => $self->pieces);
186 }
187  
188 45 ▶sub _fresh_game {
Subroutine _fresh_game: called CC=1 SCAR=0.0
189 45 my ($self) = @_;
190 45 return Game::Senet->new(seed => $self->seed, rules => $self->_variant, first => $self->first);
191 }
192  
193 304046 ▶sub _paint {
Subroutine _paint: called CC=2 SCAR=7.1
194 304046 my ($self, $text, $style) = @_;
195 304046 ▶ return $text unless defined $style;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $style 0 304046 304046
196 304046 return $self->_screen->paint($text, $STYLE{$style});
197 }
198  
199 2410 ▶sub _raw { $_[0]->_keyboard->raw }
Subroutine _raw: called CC=1 SCAR=0.0
200  
201 133174 ▶sub _glyph { $GLYPH{ $_[0]->unicode ? 'wide' : 'ascii' }{ $_[1] } }
Subroutine _glyph: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->unicode ? : 55377 77797 133174
202  
203 1250 ▶sub _event {
Subroutine _event: called CC=1 SCAR=0.0
204 1250 my ($self, $side, $text) = @_;
205 1250 push @{ $self->_events }, { side => $side, text => $text };
206 1250 return;
207 }
208  
209 2 ▶sub events { [ map { $_->{text} } @{ $_[0]->_events } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
210  
211 2411 ▶sub _person {
Subroutine _person: called CC=4 SCAR=13.9
212 2411 my ($self, $side) = @_;
213 2411 ▶ return 0 if $self->mode eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" 1820 591 2411
214 591 ▶ return 1 if $self->mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "hotseat" 37 554 591
215 554 ▶ return $side eq $self->side ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 282 272 554
216 }
217  
218 4106 ▶sub _who {
Subroutine _who: called CC=3 SCAR=11.0
219 4106 my ($self, $side) = @_;
220 4106 ▶ return ucfirst $side if $self->mode ne 'bot';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode ne "bot" 3417 689 4106
221 689 ▶ return $side eq $self->side ? 'You' : ucfirst $side;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 358 331 689
222 }
223  
224 2876 ▶sub sticks_text {
Subroutine sticks_text: called CC=3 SCAR=11.0
Pod documented
225 2876 my ($self, $sticks) = @_;
226 2876 ▶ return '[ ' . join(' ', map { $self->_paint($self->_glyph($_ ? 'face' : 'back'), $_ ? 'face' : 'back') } @$sticks) . ' ]';
Branch: 2 branches
BranchTrueFalseTotal
$_ ? : 5773 5651 11424
$_ ? : 5773 5651 11424
227 }
228  
229 2872 ▶sub roll_words {
Subroutine roll_words: called CC=4 SCAR=13.9
Pod documented
230 2872 my ($self, $roll, $again, $bound) = @_;
231 2872 ▶ my $words = $roll == 5 ? 'no light face: a roll of 5' : "a roll of $roll";
Branch: 1 branch
BranchTrueFalseTotal
$roll == 5 ? : 215 2657 2872
232 2872 ▶ $words .= ', and throws again' if $again;
Branch: 1 branch
BranchTrueFalseTotal
if $again 1109 1763 2872
233 2872 ▶ $words .= ', for the piece on 26' if $bound;
Branch: 1 branch
BranchTrueFalseTotal
if $bound 1 2871 2872
234 2872 return $words;
235 }
236  
237 1151 ▶sub _what_it_does {
Subroutine _what_it_does: called CC=7 SCAR=19.5
238 1151 my ($self, $entry, $rules) = @_;
239 1151 my @does;
240 1151 ▶ push @does, 'a backward move: nothing can go forward' if $entry->{back};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"back"} 67 1084 1151
241 1151 ▶ push @does, 'swaps with ' . $OTHER{ $entry->{side} } if $entry->{swap};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"swap"} 202 949 1151
242 1151 ▶ if ($entry->{walk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"walk"}) 15 1136 1151
243 15 my ($from, $to) = split /-/, $entry->{move};
244 15 ▶ push @does, ($from == 26 ? 'into the House of Water' : 'out of the water') . ": back to $to";
Branch: 1 branch
BranchTrueFalseTotal
$from == 26 ? : 11 4 15
245 }
246 1151 ▶ push @does, 'bears off' if $entry->{off};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"off"} 31 1120 1151
247 1151 my (undef, $to) = split /-/, $entry->{move};
248 1151 ▶ push @does, 'onto the House of Happiness' if $to eq '26';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq 26 48 1103 1151
249 1151 return @does;
250 }
251  
252 1188 ▶sub _briefly {
Subroutine _briefly: called CC=7 SCAR=19.5
253 1188 my ($self, $entry) = @_;
254 1188 my ($from, $to) = split /-/, $entry->{move};
255 1188 my @does;
256 1188 ▶ push @does, 'back' if $entry->{back};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"back"} 66 1122 1188
257 1188 ▶ push @does, 'swaps' if $entry->{swap};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"swap"} 203 985 1188
258 1188 ▶ push @does, ($from == 26 ? 'into the water' : 'out of the water') . ", to $to" if $entry->{walk};
Branch: 2 branches
BranchTrueFalseTotal
if $entry->{"walk"} 24 1164 1188
$from == 26 ? : 16 8 24
259 1188 ▶ push @does, 'bears off' if $entry->{off};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"off"} 63 1125 1188
260 1188 ▶ push @does, 'onto the House of Happiness' if $to eq '26';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq 26 85 1103 1188
261 1188 return @does;
262 }
263  
264 1234 ▶sub _tell {
Subroutine _tell: called CC=15 SCAR=27.1
265 1234 my ($self, $pause) = @_;
266 1234 my $game = $self->game;
267 1234 my $log = $game->log;
268 1234 my $told = $self->_told;
269 1234 ▶ while ($told < @$log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($told < @$log) 1223 1234 2457
270 1223 my $entry = $log->[ $told++ ];
271 1223 my $who = $self->_who($entry->{side});
272 1223 my $text;
273 1223 ▶ if (!defined $entry->{move}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $entry->{'move'}) { } 35 1188 1223
274 $text = "$who threw " . (defined $entry->{sticks} ? $self->_plain_sticks($entry->{sticks}) . " (a $entry->{roll})" : "a $entry->{roll}")
275 35 ▶ . ' and could not move' . ($entry->{again} ? ': throws again.' : '.');
Branch: 2 branches
BranchTrueFalseTotal
defined $entry->{'sticks'} ? : 34 1 35
$entry->{'again'} ? : 13 22 35
276 }
277 else {
278 1188 my @does = $self->_briefly($entry);
279 $text = "$who played " . Game::Senet::Notation::format_display({ %$entry, from => (split /-/, $entry->{move})[0],
280 to => (split /-/, $entry->{move})[1] }, 0)
281 . (@does ? ', ' . join(', ', @does) : '')
282 1188 ▶ . ($entry->{again} ? '; throws again.' : '.');
Branch: 2 branches
BranchTrueFalseTotal
@does ? : 431 757 1188
$entry->{'again'} ? : 462 726 1188
283 }
284 1223 $self->_event($entry->{side}, $text);
285 1223 $self->_told($told);
286 1223 ▶ my $slow = !defined $entry->{move} || !$self->_person($entry->{side});
Condition100%3/3
defined $entry->{'move'} && $self->_person($entry->{'side'})
!ll&&!rl&&r
351046142
MC/DC100%2/2
defined $entry->{'move'} && $self->_person($entry->{'side'})
defined $entry->{'move'} $self->_person($entry->{'side'})
Truth table100%3/3
A && B
A= defined $entry->{'move'}B= $self->_person($entry->{'side'})
ABresult
0X0
100
111
287 1223 ▶ if ($pause && $slow && $self->_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pause and $slow and $self->_raw) 140 1083 1223
Condition83%5/6
$pause and $slow and $self->_raw
!ll&&!rl&&r
142941140
$pause and $slow
!ll&&!rl&&r
01421081
MC/DC66%2/3
$pause and $slow and $self->_raw
$pause $slow $self->_raw
Truth table75%3/4
A and B and C
A= $pauseB= $slowC= $self->_raw
ABCresult
0XX0
10X0
1100
1111
288 140 $self->show('wait');
289 140 $self->_wait;
290 }
291 }
292 1234 my $turn = $game->turn;
293 1234 ▶ if ($turn && $turn->{bound}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn and $turn->{"bound"}) 17 1217 1234
Condition100%3/3
$turn and $turn->{"bound"}
!ll&&!rl&&r
11120617
MC/DC100%2/2
$turn and $turn->{"bound"}
$turn $turn->{"bound"}
Truth table100%3/3
A and B
A= $turnB= $turn->{"bound"}
ABresult
0X0
100
111
294 17 my $last = $self->_events->[-1];
295 $self->_event($turn->{side}, $self->_who($turn->{side}) . ' throws again for the piece on 26.')
296 17 ▶ unless $last && $last->{text} =~ /for the piece on 26/;
Branch: 1 branch
BranchTrueFalseTotal
unless $last and $last->{"text"} =~ /for the piece on 26/ 17 0 17
Condition33%1/3
$last and $last->{"text"} =~ /for the piece on 26/
!ll&&!rl&&r
0170
MC/DC0%0/2
$last and $last->{"text"} =~ /for the piece on 26/
$last $last->{"text"} =~ /for the piece on 26/
Truth table33%1/3
A and B
A= $lastB= $last->{"text"} =~ /for the piece on 26/
ABresult
0X0
100
111
297 }
298 1234 $self->_told($told);
299 1234 return;
300 }
301  
302 34 ▶sub _plain_sticks {
Subroutine _plain_sticks: called CC=3 SCAR=11.1
303 34 my ($self, $sticks) = @_;
304 34 ▶ return defined $sticks ? join('', map { $_ ? $self->_glyph('face') : $self->_glyph('back') } split //, $sticks) : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $sticks ? : 34 0 34
$_ ? : 71 65 136
305 }
306  
307 273 ▶sub _wait {
Subroutine _wait: called CC=3 SCAR=12.7
308 273 my ($self) = @_;
309 273 my $pace = $self->pace;
310 273 ▶ return unless $pace > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pace > 0 0 273 273
311 273 ▶ return $self->sleeper->($pace) if $self->sleeper;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sleeper 273 0 273
312 0 select undef, undef, undef, $pace;
313 0 return;
314 }
315  
316 56824 ▶sub candidates {
Subroutine candidates: called CC=1 SCAR=0.0
Pod documented
317 56824 my ($self) = @_;
318 56824 return $self->game->legal;
319 }
320  
321 169983 ▶sub place_of {
Subroutine place_of: called CC=1 SCAR=0.0
Pod documented
322 169983 my ($class, $house) = @_;
323 169983 return ($COL_OF{$house}, $ROW_OF{$house});
324 }
325  
326 55243 ▶sub steer {
Subroutine steer: called CC=9 SCAR=22.0
Pod documented
327 55243 my ($self, $key) = @_;
328 55243 my @moves = $self->candidates;
329 55243 ▶ return $self->_cursor unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 0 55243 55243
330 55243 my $at = $self->_cursor;
331 55243 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 175 55068 55243
332  
333 55243 ▶ if ($key eq 'tab') { $at = Game::Terminal::Kit::Steer::ring(scalar @moves, $at, 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tab") 8 55235 55243
334 55243 ▶ if ($key eq 'back_tab' || $key eq 'backtab') { $at = Game::Terminal::Kit::Steer::ring(scalar @moves, $at, -1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "back_tab" or $key eq "backtab") 1 55242 55243
Condition66%2/3
$key eq "back_tab" or $key eq "backtab"
l!l&&r!l&&!r
0155242
MC/DC50%1/2
$key eq "back_tab" or $key eq "backtab"
$key eq "back_tab" $key eq "backtab"
Truth table66%2/3
A or B
A= $key eq "back_tab"B= $key eq "backtab"
ABresult
1X1
011
000
335 55243 ▶ if ($key =~ /\A[1-7]\z/ && $key <= @moves) { $at = $key - 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-7]\z/ and $key <= @moves) 3250 51993 55243
Condition100%3/3
$key =~ /\A[1-7]\z/ and $key <= @moves
!ll&&!rl&&r
5199213250
MC/DC100%2/2
$key =~ /\A[1-7]\z/ and $key <= @moves
$key =~ /\A[1-7]\z/ $key <= @moves
Truth table100%3/3
A and B
A= $key =~ /\A[1-7]\z/B= $key <= @moves
ABresult
0X0
100
111
336  
337 55243 ▶ if (my ($dx, $dy) = Game::Terminal::Kit::Steer::way($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = Game::Terminal::Kit::Steer::way($key)) 51983 3260 55243
338 51983 my @points = map { [ $self->place_of($_->from) ] } @moves;
339 51983 $at = Game::Terminal::Kit::Steer::axis(\@points, $at, $dx, $dy);
340 }
341 55243 $self->_cursor($at);
342 55243 return $at;
343 }
344  
345 2918 ▶sub _cells_after {
Subroutine _cells_after: called CC=7 SCAR=19.5
346 2918 my ($self, $move) = @_;
347 2918 my $game = $self->game;
348 2918 my (%cell, %home, %mark);
349 2918 $cell{$_} = $game->at($_) for 1 .. 30;
350 2918 $home{$_} = $game->home($_) for qw(light dark);
351 2918 ▶ return (\%cell, \%home, \%mark) unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 1812 1106 2918
352  
353 1106 my $side = $move->side;
354 1106 $cell{ $move->from } = undef;
355 1106 ▶ if ($move->off) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->off) { } 34 1072 1106
356 34 $home{$side}++;
357 34 $mark{"home $side"} = 'target';
358 }
359 else {
360 1072 ▶ if ($move->swap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->swap) 191 881 1072
361 191 $cell{ $move->from } = $OTHER{$side};
362 191 $mark{ $move->from } = 'from';
363 }
364 1072 $cell{ $move->to } = $side;
365 1072 $mark{ $move->to } = 'target';
366 }
367 1106 ▶ $mark{ $move->from } //= 'cursor';
Condition100%2/2
$mark{$move->from} //= "cursor"
l!l
191915
MC/DC100%1/1
$mark{$move->from} //= "cursor"
$mark{$move->from}
Truth table100%2/2
A
A= $mark{$move->from}
Aresult
11
00
368 1106 return (\%cell, \%home, \%mark);
369 }
370  
371 87574 ▶sub _marked {
Subroutine _marked: called CC=4 SCAR=13.9
372 87574 my ($self, $text, $mark) = @_;
373 87574 ▶ my ($open, $close) = Game::Terminal::Kit::Frame::brackets($mark // 'plain');
Condition100%2/2
$mark // "plain"
l!l
699380581
MC/DC100%1/1
$mark // "plain"
$mark
Truth table100%2/2
A
A= $mark
Aresult
11
00
374 87574 ▶ return $self->_paint($open . $text . $close, $mark) if $mark && $mark ne 'recent';
Branch: 1 branch
BranchTrueFalseTotal
if $mark and $mark ne "recent" 2212 85362 87574
Condition100%3/3
$mark and $mark ne "recent"
!ll&&!rl&&r
8058147812212
MC/DC100%2/2
$mark and $mark ne "recent"
$mark $mark ne "recent"
Truth table100%3/3
A and B
A= $markB= $mark ne "recent"
ABresult
0X0
100
111
375 85362 return $open . $text . $close;
376 }
377  
378 87540 ▶sub _house_top {
Subroutine _house_top: called CC=3 SCAR=11.0
379 87540 my ($self, $h) = @_;
380 87540 ▶ my $sign = $h == 15 ? $self->_glyph('ankh') : $SIGN{$h} // '';
Branch: 1 branch
BranchTrueFalseTotal
$h == 15 ? : 2918 84622 87540
Condition100%2/2
$SIGN{$h} // ""
l!l
1459070032
MC/DC100%1/1
$SIGN{$h} // ""
$SIGN{$h}
Truth table100%2/2
A
A= $SIGN{$h}
Aresult
11
00
381 87540 my $number = sprintf '%-2s ', $h;
382 87540 my $pad = 3 - length $sign;
383 87540 return $self->_paint($number, 'label') . $self->_paint($sign, 'sign') . (' ' x $pad);
384 }
385  
386 87540 ▶sub _house_piece {
Subroutine _house_piece: called CC=4 SCAR=13.9
387 87540 my ($self, $who, $mark) = @_;
388 87540 ▶ my $painted = $mark && $mark ne 'recent';
Condition100%3/3
$mark && $mark ne 'recent'
!ll&&!rl&&r
8058147812178
MC/DC100%2/2
$mark && $mark ne 'recent'
$mark $mark ne 'recent'
Truth table100%3/3
A && B
A= $markB= $mark ne 'recent'
ABresult
0X0
100
111
389 87540 ▶ my $inside = !defined $who ? ' ' : $painted ? $self->_glyph($who) : $self->_paint($self->_glyph($who), $who);
Branch: 2 branches
BranchTrueFalseTotal
!defined($who) ? : 62706 24834 87540
$painted ? : 1263 23571 24834
390 87540 return ' ' . $self->_marked($inside, $mark) . ' ';
391 }
392  
393 11672 ▶sub _border {
Subroutine _border: called CC=1 SCAR=0.0
394 11672 my ($self, $left, $mid, $right) = @_;
395 11672 ▶ my $g = sub { $self->_glyph($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
396 11672 return $self->_paint($g->($left) . join($g->($mid), ($g->('h') x 6) x 10) . $g->($right), 'grid');
397 }
398  
399 8754 ▶sub _row_houses {
Subroutine _row_houses: called CC=3 SCAR=11.0
400 8754 my ($row) = @_;
401 8754 ▶ return $row == 0 ? (1 .. 10) : $row == 1 ? (reverse 11 .. 20) : (21 .. 30);
Branch: 2 branches
BranchTrueFalseTotal
$row == 0 ? : 2918 5836 8754
$row == 1 ? : 2918 2918 5836
402 }
403  
404 5836 ▶sub _home_line {
Subroutine _home_line: called CC=4 SCAR=13.9
405 5836 my ($self, $side, $home, $mark) = @_;
406 5836 my @shown = map { $self->_paint($self->_glyph($side), $side) } 1 .. $home->{$side};
407 $shown[-1] = $self->_marked($self->_glyph($side), 'target')
408 5836 ▶ if @shown && $mark->{"home $side"};
Branch: 1 branch
BranchTrueFalseTotal
if @shown and $$mark{"home $side"} 34 5802 5836
Condition100%3/3
@shown and $$mark{"home $side"}
!ll&&!rl&&r
2065373734
MC/DC100%2/2
@shown and $$mark{"home $side"}
@shown $$mark{"home $side"}
Truth table100%3/3
A and B
A= @shownB= $$mark{"home $side"}
ABresult
0X0
100
111
409 5836 ▶ return sprintf ' %s home: %s', $self->_paint(sprintf('%-5s', $side), $side),
Branch: 1 branch
BranchTrueFalseTotal
@shown ? : 3771 2065 5836
410 @shown ? join(' ', @shown) : $self->_paint('none', 'quiet');
411 }
412  
413 2918 ▶sub board_lines {
Subroutine board_lines: called CC=8 SCAR=20.8
Pod documented
414 2918 my ($self, $move) = @_;
415 2918 my $game = $self->game;
416 2918 my ($cell, $home, $mark) = $self->_cells_after($move);
417 2918 my %recent;
418 2918 for my $entry (@{ $game->log }) {
419 293934 ▶ next unless defined $entry->{move};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"move"} 3207 290727 293934
420 290727 my (undef, $to) = split /-/, $entry->{move};
421 290727 $recent{ $entry->{side} } = $to;
422 }
423 2918 for my $side (keys %recent) {
424 5617 my $to = $recent{$side};
425 5617 ▶ next if $to eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "off" 315 5302 5617
426 5302 ▶ $mark->{$to} //= 'recent' if defined $cell->{$to} && $cell->{$to} eq $side;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell->{$to} and $cell->{$to} eq $side 4787 515 5302
Condition100%5/5
defined $cell->{$to} and $cell->{$to} eq $side
!ll&&!rl&&r
3331824787
$mark->{$to} //= "recent"
l!l
64781
MC/DC100%2/2
defined $cell->{$to} and $cell->{$to} eq $side
defined $cell->{$to} $cell->{$to} eq $side
MC/DC100%1/1
$mark->{$to} //= "recent"
$mark->{$to}
Truth table100%3/3
A and B
A= defined $cell->{$to}B= $cell->{$to} eq $side
ABresult
0X0
100
111
Truth table100%2/2
A
A= $mark->{$to}
Aresult
11
00
427 }
428  
429 2918 my $v = $self->_paint($self->_glyph('v'), 'grid');
430 8754 ▶ my $row_lines = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
431 8754 my ($row) = @_;
432 8754 my @houses = _row_houses($row);
433 8754 my $top = $v . join($v, map { $self->_house_top($_) } @houses) . $v;
434 8754 my $low = $v . join($v, map { $self->_house_piece($cell->{$_}, $mark->{$_}) } @houses) . $v;
435 8754 return ($top, $low);
436 2918 };
437 2918 my ($r0a, $r0b) = $row_lines->(0);
438 2918 my ($r1a, $r1b) = $row_lines->(1);
439 2918 my ($r2a, $r2b) = $row_lines->(2);
440 2918 ▶ my $g = sub { $self->_paint($self->_glyph($_[0]), 'grid') };
Subroutine __ANON__: called CC=1 SCAR=0.0
441 2918 my $sp = ' ';
442  
443 return [
444 2918 $self->_home_line('dark', $home, $mark),
445 '',
446 $sp . $self->_border('tl', 'td', 'tr'),
447 $sp . $r0a,
448 $sp . $r0b . $g->('into') . $g->('turn_a'),
449 $sp . $self->_border('lt', 'x', 'rt') . ' ' . $g->('turn_b'),
450 ' ' . $g->('turn_d') . $g->('from') . $r1a . $g->('from') . $g->('turn_c'),
451 ' ' . $g->('turn_e') . ' ' . $r1b,
452 ' ' . $g->('turn_e') . ' ' . $self->_border('lt', 'x', 'rt'),
453 ' ' . $g->('turn_f') . $g->('into') . $r2a . $g->('into') . $self->_paint('off', 'grid'),
454 $sp . $r2b,
455 $sp . $self->_border('bl', 'tu', 'br'),
456 '',
457 $self->_home_line('light', $home, $mark),
458 ];
459 }
460  
461 2884 ▶sub _shield_lines {
Subroutine _shield_lines: called CC=15 SCAR=27.1
462 2884 my ($self) = @_;
463 2884 my $game = $self->game;
464 2884 my $variant = $game->variant;
465 2884 my @lines;
466 2884 for my $side (qw(light dark)) {
467 5768 my (@runs, $run);
468 5768 for my $h (1 .. 30) {
469 173040 ▶ if (($game->at($h) // '') eq $side) { push @{ $run //= [] }, $h; next }
Branch: 1 branch
BranchTrueFalseTotal
if (($game->at($h) // "") eq $side) 24711 148329 173040
Condition100%4/4
$game->at($h) // ""
l!l
49422123618
$run //= []
l!l
593018781
MC/DC100%1/1
$game->at($h) // ""
$game->at($h)
MC/DC100%1/1
$run //= []
$run
Truth table100%2/2
A
A= $game->at($h)
Aresult
11
00
Truth table100%2/2
A
A= $run
Aresult
11
00
470 148329 ▶ push @runs, $run if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 18745 129584 148329
471 148329 undef $run;
472 }
473 5768 ▶ push @runs, $run if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 36 5732 5768
474 5768 my (@kept, @blocks);
475 5768 for my $r (grep { @$_ >= 2 } @runs) {
476 4606 ▶ push @kept, $variant->protect eq 'run' ? @$r : $variant->protect eq 'ends' ? ($r->[0], $r->[-1]) : ();
Branch: 2 branches
BranchTrueFalseTotal
$variant->protect eq 'run' ? : 610 3996 4606
$variant->protect eq 'ends' ? : 3996 0 3996
477 4606 ▶ push @blocks, "$r->[0]-$r->[-1]" if $variant->blockade && @$r >= $variant->blockade;
Branch: 1 branch
BranchTrueFalseTotal
if $variant->blockade and @$r >= $variant->blockade 4207 399 4606
Condition66%2/3
$variant->blockade and @$r >= $variant->blockade
!ll&&!rl&&r
03994207
MC/DC50%1/2
$variant->blockade and @$r >= $variant->blockade
$variant->blockade @$r >= $variant->blockade
Truth table66%2/3
A and B
A= $variant->blockadeB= @$r >= $variant->blockade
ABresult
0X0
100
111
478 }
479 5768 ▶ my @said = ((@kept ? 'protected ' . join(' ', @kept) : 'nothing protected'),
Branch: 2 branches
BranchTrueFalseTotal
@kept ? : 3734 2034 5768
@blocks ? : 3463 2305 5768
480 (@blocks ? 'blockades ' . join(' ', @blocks) : ()));
481 5768 push @lines, ' ' . $self->_paint(sprintf('%-5s ', $side) . join('; ', @said), 'quiet');
482 }
483 2884 return @lines;
484 }
485  
486 2884 ▶sub _sticks_line {
Subroutine _sticks_line: called CC=4 SCAR=13.9
487 2884 my ($self) = @_;
488 2884 my $game = $self->game;
489 2884 ▶ return () if $game->is_over || !defined $game->roll;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over or not defined $game->roll 18 2866 2884
Condition100%3/3
$game->is_over or not defined $game->roll
l!l&&r!l&&!r
1622866
MC/DC100%2/2
$game->is_over or not defined $game->roll
$game->is_over not defined $game->roll
Truth table100%3/3
A or not(B)
A= $game->is_overB= not defined $game->roll
ABresult
1X1
011
000
490 2866 my $turn = $game->turn;
491 return (sprintf(' %s threw %d %s %s', $self->_who($game->side), $game->throw,
492 $self->sticks_text($game->sticks), $self->roll_words($game->roll, $turn->{extra})),
493 2866 ▶ ($turn->{bound} ? ' ' . $self->_paint('This throw is for the piece on 26: it alone may move.', 'key') : ()));
Branch: 1 branch
BranchTrueFalseTotal
$turn->{'bound'} ? : 209 2657 2866
494 }
495  
496 2884 ▶sub _result_line {
Subroutine _result_line: called CC=4 SCAR=14.0
497 2884 my ($self) = @_;
498 2884 ▶ my $result = $self->game->result or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->game->result or return () 16 2868 2884
499 16 ▶ return ' ' . $self->_paint('The game ran its full length with no end. It is drawn.', 'bad') if $result->is_draw;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_draw 0 16 16
500 16 my $winner = $result->winner;
501 16 ▶ return ' ' . $self->_paint(sprintf('%s won: %s resigned.', ucfirst $winner, $result->loser), 'good')
Branch: 1 branch
BranchTrueFalseTotal
if $result->how eq "resign" 0 16 16
502 if $result->how eq 'resign';
503 16 my $home = $result->home;
504 return ' ' . $self->_paint(sprintf('%s won, all %d home to %d, in %d plies.',
505 16 ucfirst $winner, $home->{$winner}, $home->{ $result->loser }, $result->plies), 'good');
506 }
507  
508 2884 ▶sub _event_lines {
Subroutine _event_lines: called CC=3 SCAR=11.0
509 2884 my ($self) = @_;
510 2884 my @recent = @{ $self->_events };
511 2884 ▶ @recent = @recent[ -4 .. -1 ] if @recent > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @recent > 4 405 2479 2884
512 2884 ▶ return map { ' ' . $self->_paint($_->{text}, $_->{side} || 'quiet') } @recent;
Condition100%2/2
$_->{'side'} || 'quiet'
l!l
16609
MC/DC100%1/1
$_->{'side'} || 'quiet'
$_->{'side'}
Truth table100%2/2
A
A= $_->{'side'}
Aresult
11
00
513 }
514  
515 1151 ▶sub readout {
Subroutine readout: called CC=2 SCAR=6.9
Pod documented
516 1151 my ($self, $move) = @_;
517 1151 my %entry = (side => $move->side, move => Game::Senet::Notation::format_move($move),
518 back => $move->back, swap => $move->swap, walk => $move->walk, off => $move->off);
519 1151 my @does = $self->_what_it_does(\%entry);
520 1151 ▶ return $self->_paint(Game::Senet::Notation::format_display($move, 0), 'key') . (@does ? ' ' . join(', ', @does) : '');
Branch: 1 branch
BranchTrueFalseTotal
@does ? : 357 794 1151
521 }
522  
523 2031 ▶sub _legend {
Subroutine _legend: called CC=4 SCAR=13.9
524 2031 my ($self, $mode) = @_;
525 2031 ▶ my $k = sub { $self->_paint($_[0], 'key') };
Subroutine __ANON__: called CC=1 SCAR=0.0
526 2031 ▶ return ' ' . join(' ', $k->('enter') . ' leave', $k->('n') . ' new game', $k->('u') . ' take back',
Branch: 2 branches
BranchTrueFalseTotal
if $mode eq "ended" 5 2026 2031
$self->history->can_redo ? : 0 5 5
527 ($self->history->can_redo ? $k->('r') . ' play again' : ())) if $mode eq 'ended';
528 2026 ▶ return ' ' . $self->_paint('...', 'quiet') if $mode eq 'wait';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "wait" 938 1088 2026
529 1088 return ' ' . join(' ', $k->('arrows') . ' choose', $k->('enter') . ' move',
530 $k->('u') . ' back', $k->('r') . ' again', $k->('?') . ' help', $k->('q') . ' leave');
531 }
532  
533 2884 ▶sub screen {
Subroutine screen: called CC=13 SCAR=25.9
Pod documented
534 2884 my ($self, $mode) = @_;
535 2884 ▶ $mode = 'plain' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 2884 2884
536 2884 my $game = $self->game;
537 2884 my $variant = $game->variant;
538 2884 ▶ my @lines = (
Branch: 2 branches
BranchTrueFalseTotal
$self->mode eq 'bot' ? : 423 2461 2884
$self->mode eq 'hotseat' ? : 2455 6 2461
539 ' ' . $self->_paint('Senet', 'title') . ' '
540 . $self->_paint(join(', ', $variant->describe,
541 ($self->mode eq 'bot' ? 'you are ' . $self->side . ', against level ' . $self->level
542 : $self->mode eq 'hotseat' ? 'two players' : 'level ' . $self->level . ' against itself')), 'quiet'),
543 '',
544 );
545 2884 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 0 2884 2884
546 0 push @lines, map { " $_" } @{ $self->help_lines };
547 0 return \@lines;
548 }
549 2884 ▶ my @moves = $mode eq 'pick' ? $self->candidates : ();
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'pick' ? : 1088 1796 2884
550 2884 my $at = $self->_cursor;
551 2884 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 1796 1088 2884
552 2884 ▶ my $move = @moves ? $moves[$at] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@moves ? : 1088 1796 2884
553  
554 2884 push @lines, @{ $self->board_lines($move) }, $self->_shield_lines, '';
555 2884 push @lines, $self->_sticks_line, $self->_result_line;
556 2884 ▶ push @lines, ' ' . $self->readout($move) if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 1088 1796 2884
557 2884 push @lines, '';
558 2884 push @lines, $self->_event_lines;
559 2884 ▶ push @lines, ' ' . $self->_paint($self->_note, 'bad') if defined $self->_note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_note 3 2881 2884
560 2884 ▶ push @lines, '', $self->_legend($mode) if $mode eq 'pick' || $mode eq 'ended' || $mode eq 'wait';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "pick" or $mode eq "ended" or $mode eq "wait" 2031 853 2884
Condition100%6/6
$mode eq "pick" or $mode eq "ended" or $mode eq "wait"
l!l&&r!l&&!r
1093938853
$mode eq "pick" or $mode eq "ended"
l!l&&r!l&&!r
108851791
MC/DC100%3/3
$mode eq "pick" or $mode eq "ended" or $mode eq "wait"
$mode eq "pick" $mode eq "ended" $mode eq "wait"
Truth table100%4/4
A or B or C
A= $mode eq "pick"B= $mode eq "ended"C= $mode eq "wait"
ABCresult
1XX1
01X1
0011
0000
561 2884 return \@lines;
562 }
563  
564 433 ▶sub show {
Subroutine show: called CC=1 SCAR=0.0
Pod documented
565 433 my ($self, $mode) = @_;
566 433 $self->_screen->show($self->screen($mode));
567 433 return $self;
568 }
569  
570 0 ▶sub help_lines {
Subroutine help_lines: not called CC=1 SCAR=6.9
Pod documented
571 return [
572 0 'A move is a piece. The sticks say how far it goes, so choosing the piece is',
573 'the whole move. A roll of 1, 4 or 5 throws again.',
574 '',
575 ' arrows, tab walk the pieces that can move',
576 ' 1 to 7 go straight to one of them',
577 ' enter, space move it',
578 ' u take back your last turn and every reply to it',
579 ' r play a turn taken back again',
580 ' n a new game',
581 ' q leave (asked twice)',
582 ' enter, q leave, once the game is over',
583 '',
584 'The board shows what the move would leave: [ ] the house the piece leaves,',
585 '< > where it lands, ( ) the house a swapped piece goes back to, and { } the',
586 'last move each side made.',
587 '',
588 'Typing instead: a number from the list, or a move such as 12-15 or 26-off;',
589 'and undo, redo, new, moves, record FILE, level N, help, quit.',
590 ];
591 }
592  
593 6 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=4 SCAR=14.6
594 6 my ($self) = @_;
595 6 ▶ return 1 if $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_raw 0 6 6
596 6 ▶ return 0 unless $self->_keyboard->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->enter_raw 0 6 6
597 6 ▶ $self->_screen->hide_cursor->clear unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 6 6
598 6 return 1;
599 }
600  
601 145 ▶sub pick {
Subroutine pick: called CC=15 SCAR=28.1
Pod documented
602 145 my ($self) = @_;
603 145 my $leaving = 0;
604 145 ▶ $self->_cursor(0) if $self->_cursor > $self->candidates - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cursor > $self->candidates - 1 0 145 145
605 145 while (1) {
606 155 $self->show('pick');
607 155 my $key = $self->_keyboard->read_key;
608 155 ▶ return undef if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 2 153 155
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
20153
not defined $key or $key eq "eof"
l!l&&r!l&&!r
20153
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
609 153 $self->_note(undef);
610 153 ▶ if ($key eq 'q' || $key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q" or $key eq "escape") 5 148 153
Condition66%2/3
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
50148
MC/DC50%1/2
$key eq "q" or $key eq "escape"
$key eq "q" $key eq "escape"
Truth table66%2/3
A or B
A= $key eq "q"B= $key eq "escape"
ABresult
1X1
011
000
611 5 ▶ return 'quit' if $leaving;
Branch: 1 branch
BranchTrueFalseTotal
if $leaving 2 3 5
612 3 $leaving = 1;
613 3 $self->_note('Press q again to leave the game, or any other key to stay.');
614 3 next;
615 }
616 148 $leaving = 0;
617 148 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 140 8 148
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
13918
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
618 140 my @moves = $self->candidates;
619 140 ▶ next unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 0 140 140
620 140 return Game::Senet::Notation::format_move($moves[ $self->_cursor ]);
621 }
622 8 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 1 7 8
623 7 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 7 7
624 7 ▶ return 'new' if $key eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" 0 7 7
625 7 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 7 7
626 0 $self->show('help');
627 0 $self->_keyboard->read_key;
628 0 next;
629 }
630 7 $self->steer($key);
631 }
632 }
633  
634 5 ▶sub _ended_key {
Subroutine _ended_key: called CC=11 SCAR=26.4
635 5 my ($self) = @_;
636 5 while (1) {
637 5 $self->show('ended');
638 5 my $key = $self->_keyboard->read_key;
639 5 ▶ 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 5 5
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
005
not defined $key or $key eq "eof"
l!l&&r!l&&!r
005
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
640 5 ▶ return 'new' if $key eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" 1 4 5
641 4 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 0 4 4
642 4 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 4 4
643 4 ▶ return 'quit' if $key eq 'q' || $key eq 'escape' || $key eq 'enter' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "escape" or $key eq "enter" or $key eq " " 4 0 4
Condition77%7/9
$key eq "q" or $key eq "escape" or $key eq "enter" or $key eq " "
l!l&&r!l&&!r
310
$key eq "q" or $key eq "escape" or $key eq "enter"
l!l&&r!l&&!r
211
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
202
MC/DC0%0/4
$key eq "q" or $key eq "escape" or $key eq "enter" or $key eq " "
$key eq "q" $key eq "escape" $key eq "enter" $key eq " "
Truth table60%3/5
A or B or C or D
A= $key eq "q"B= $key eq "escape"C= $key eq "enter"D= $key eq " "
ABCDresult
1XXX1
01XX1
001X1
00011
00000
644 }
645 }
646  
647 87 ▶sub _say {
Subroutine _say: called CC=2 SCAR=7.1
648 87 my ($self, @lines) = @_;
649 87 ▶ $self->_screen->say(@lines) if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 87 0 87
650 87 return;
651 }
652  
653 38 ▶sub _read_line {
Subroutine _read_line: called CC=4 SCAR=14.0
654 38 my ($self) = @_;
655 38 my $game = $self->game;
656 38 my $out = $self->out;
657 38 $self->_say(@{ $self->screen('plain') });
658 38 ▶ if (!$game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->is_over) 38 0 38
659 38 my @moves = $self->candidates;
660 38 $self->_say(map { sprintf ' %s %s', $self->_paint($_ + 1, 'key'), $self->readout($moves[$_]) } 0 .. $#moves);
661 }
662 38 ▶ my $line = $self->_keyboard->read_line(
Branch: 1 branch
BranchTrueFalseTotal
$game->is_over ? : 0 38 38
663 $game->is_over ? 'new, undo, or enter to leave> ' : $game->side . ' (' . $game->roll . ')> ', $out);
664 38 ▶ print {$out} "\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 38 0 38
665 38 return $line;
666 }
667  
668 183 ▶sub command {
Subroutine command: called CC=30 SCAR=48.7
Pod documented
669 183 my ($self, $line) = @_;
670 183 my $game = $self->game;
671 183 $self->_note(undef);
672 183 ▶ return 0 unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 0 183 183
Condition33%1/3
defined $line and length $line
!ll&&!rl&&r
00183
MC/DC0%0/2
defined $line and length $line
defined $line length $line
Truth table33%1/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
673 183 my ($word, $rest) = split ' ', $line, 2;
674 183 $word = lc $word;
675  
676 183 ▶ return 1 if $word eq 'quit' || $word eq 'q' || $word eq 'exit';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "quit" or $word eq "q" or $word eq "exit" 38 145 183
Condition66%4/6
$word eq "quit" or $word eq "q" or $word eq "exit"
l!l&&r!l&&!r
380145
$word eq "quit" or $word eq "q"
l!l&&r!l&&!r
380145
MC/DC33%1/3
$word eq "quit" or $word eq "q" or $word eq "exit"
$word eq "quit" $word eq "q" $word eq "exit"
Truth table50%2/4
A or B or C
A= $word eq "quit"B= $word eq "q"C= $word eq "exit"
ABCresult
1XX1
01X1
0011
0000
677 145 ▶ if ($word eq 'help' || $word eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help" or $word eq "?") 0 145 145
Condition33%1/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
00145
MC/DC0%0/2
$word eq "help" or $word eq "?"
$word eq "help" $word eq "?"
Truth table33%1/3
A or B
A= $word eq "help"B= $word eq "?"
ABresult
1X1
011
000
678 0 $self->_say(map { " $_" } @{ $self->help_lines });
679 0 return 0;
680 }
681 145 ▶ if ($word eq 'new' || $word eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "new" or $word eq "n") 1 144 145
Condition66%2/3
$word eq "new" or $word eq "n"
l!l&&r!l&&!r
10144
MC/DC50%1/2
$word eq "new" or $word eq "n"
$word eq "new" $word eq "n"
Truth table66%2/3
A or B
A= $word eq "new"B= $word eq "n"
ABresult
1X1
011
000
682 1 $self->seed(Digest::SHA::sha256($self->seed . 'next'));
683 1 $self->game($self->_fresh_game);
684 1 $self->_events([]);
685 1 $self->_told(0);
686 1 $self->_cursor(0);
687 1 $self->history->start($self->game);
688 1 return 0;
689 }
690 144 ▶ if ($word eq 'undo' || $word eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "undo" or $word eq "u") 1 143 144
Condition66%2/3
$word eq "undo" or $word eq "u"
l!l&&r!l&&!r
10143
MC/DC50%1/2
$word eq "undo" or $word eq "u"
$word eq "undo" $word eq "u"
Truth table66%2/3
A or B
A= $word eq "undo"B= $word eq "u"
ABresult
1X1
011
000
691 1 $self->take_back;
692 1 return 0;
693 }
694 143 ▶ if ($word eq 'redo' || $word eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "redo" or $word eq "r") 0 143 143
Condition33%1/3
$word eq "redo" or $word eq "r"
l!l&&r!l&&!r
00143
MC/DC0%0/2
$word eq "redo" or $word eq "r"
$word eq "redo" $word eq "r"
Truth table33%1/3
A or B
A= $word eq "redo"B= $word eq "r"
ABresult
1X1
011
000
695 0 $self->play_again;
696 0 return 0;
697 }
698 143 ▶ if ($word eq 'moves') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "moves") 1 142 143
699 $self->_say(map {
700 ' ' . (defined $_->{move}
701 ? Game::Senet::Notation::format_display({ %$_, from => (split /-/, $_->{move})[0], to => (split /-/, $_->{move})[1] }, $_->{again})
702 2 ▶ : Game::Senet::Notation::format_forfeit($_->{roll}, $_->{again}))
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'move'} ? : 2 0 2
703 1 } @{ $game->log });
704 1 return 0;
705 }
706 142 ▶ if ($word eq 'record') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "record") 0 142 142
707 0 ▶ if (!defined $rest || !length $rest) { $self->_note('record wants the name of a file.') }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rest && length $rest) { } 0 0 0
elsif ($self->save($rest)) { } 0 0 0
Condition0%0/3
defined $rest && length $rest
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rest && length $rest
defined $rest length $rest
Truth table0%0/3
A && B
A= defined $restB= length $rest
ABresult
0X0
100
111
708 0 elsif ($self->save($rest)) { $self->_event(undef, "The game is written to $rest.") }
709 0 else { $self->_note("I could not write '$rest'.") }
710 0 return 0;
711 }
712 142 ▶ if ($word eq 'level') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "level") 0 142 142
713 0 my @levels = Game::Senet::Bot->levels($game->variant);
714 0 ▶ if (defined $rest && $rest =~ /\A\d+\z/ && $rest >= 1 && $rest <= $levels[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1 and $rest <= $levels[-1]) { } 0 0 0
Condition0%0/9
defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1 and $rest <= $levels[-1]
!ll&&!rl&&r
000
defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1
!ll&&!rl&&r
000
defined $rest and $rest =~ /\A\d+\z/
!ll&&!rl&&r
000
MC/DC0%0/4
defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1 and $rest <= $levels[-1]
defined $rest $rest =~ /\A\d+\z/ $rest >= 1 $rest <= $levels[-1]
Truth table0%0/5
A and B and C and D
A= defined $restB= $rest =~ /\A\d+\z/C= $rest >= 1D= $rest <= $levels[-1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
715 0 $self->level($rest);
716 0 $self->_event(undef, "The program now plays at level $rest.");
717 }
718 0 else { $self->_note('A level is a number from 1 to ' . $levels[-1] . '.') }
719 0 return 0;
720 }
721 142 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 0 142 142
722 0 $self->_note('The game is over. Type new, undo or quit.');
723 0 return 0;
724 }
725  
726 142 my $move = $line;
727 142 ▶ if ($line =~ /\A[1-7]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A[1-7]\z/) 4 138 142
728 4 my @moves = $self->candidates;
729 4 ▶ if ($line > @moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > @moves) 0 4 4
730 0 $self->_note('There are only ' . scalar(@moves) . ' moves to choose from.');
731 0 return 0;
732 }
733 4 $move = $moves[ $line - 1 ];
734 }
735 142 ▶ if ($game->play($move)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->play($move)) { } 142 0 142
736 142 $self->_cursor(0);
737 142 $self->_tell(1);
738 }
739 else {
740 0 my $error = $game->error;
741 0 ▶ $self->_note($error->code eq 'bad_move'
Branch: 1 branch
BranchTrueFalseTotal
$error->code eq 'bad_move' ? : 0 0 0
742 ? "I do not know '$line'. A move is two houses, like 12-15 or 26-off, or its number in the list."
743 : ucfirst($error->message) . '.');
744 }
745 142 return 0;
746 }
747  
748 1 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=7.3
Pod documented
749 1 my ($self) = @_;
750 1 $self->_keep;
751 1 my $game = $self->history->undo;
752 1 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 1 1
753 0 $self->_note('There is nothing to take back.');
754 0 return 0;
755 }
756 1 $self->swap($game);
757 1 $self->_event(undef, 'Taken back.');
758 1 return 1;
759 }
760  
761 0 ▶sub play_again {
Subroutine play_again: not called CC=2 SCAR=17.9
Pod documented
762 0 my ($self) = @_;
763 0 $self->_keep;
764 0 my $game = $self->history->redo;
765 0 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 0 0
766 0 $self->_note('There is nothing to play again.');
767 0 return 0;
768 }
769 0 $self->swap($game);
770 0 $self->_event(undef, 'Played again.');
771 0 return 1;
772 }
773  
774 1 ▶sub _keep {
Subroutine _keep: called CC=2 SCAR=7.1
775 1 my ($self) = @_;
776 1 ▶ $self->history->record($self->game) if $self->mode ne 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode ne "watch" 1 0 1
777 1 return;
778 }
779  
780 1 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
781 1 my ($self, $game) = @_;
782 1 $self->game($game);
783 1 $self->_told(scalar @{ $game->log });
784 1 $self->_cursor(0);
785 1 $self->_note(undef);
786 1 return $self;
787 }
788  
789 177 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
790 177 my ($self) = @_;
791 177 $self->history->turn($self->game);
792 177 return $self;
793 }
794  
795 1 ▶sub save {
Subroutine save: called CC=3 SCAR=11.3
Pod documented
796 1 my ($self, $file) = @_;
797 1 ▶ open my $fh, '>', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or return 0 1 0 1
798 1 print {$fh} $self->game->to_record;
799 1 ▶ return close $fh ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh ? : 1 0 1
800 }
801  
802 1046 ▶sub _bot_move {
Subroutine _bot_move: called CC=4 SCAR=13.9
803 1046 my ($self) = @_;
804 1046 my $game = $self->game;
805 1046 my $bot = Game::Senet::Bot->new(level => $self->level);
806 1046 ▶ my $move = $bot->choose($game) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $bot->choose($game) or return 1046 0 1046
807 1046 ▶ if ($self->_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_raw) 133 913 1046
808 133 my @moves = $self->candidates;
809 133 my ($at) = grep { $moves[$_] == $move } 0 .. $#moves;
810 133 ▶ $self->_cursor($at // 0);
Condition50%1/2
$at // 0
l!l
1330
MC/DC0%0/1
$at // 0
$at
Truth table50%1/2
A
A= $at
Aresult
11
00
811 133 $self->show('pick');
812 133 $self->_wait;
813 }
814 1046 $game->play_or_die($move);
815 1046 $self->_cursor(0);
816 1046 $self->_tell(1);
817 1046 return;
818 }
819  
820 45 ▶sub _turns {
Subroutine _turns: called CC=14 SCAR=26.8
821 45 my ($self) = @_;
822 45 $self->_tell(1);
823 45 while (1) {
824 1234 my $game = $self->game;
825 1234 ▶ my $picking = $self->picking && $self->_raw;
Condition66%2/3
$self->picking && $self->_raw
!ll&&!rl&&r
9570277
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
826 1234 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 11 1223 1234
827 11 ▶ last if $self->mode eq 'watch' && !$self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" and not $self->interactive 6 5 11
Condition66%2/3
$self->mode eq "watch" and not $self->interactive
!ll&&!rl&&r
506
MC/DC50%1/2
$self->mode eq "watch" and not $self->interactive
$self->mode eq "watch" not $self->interactive
Truth table66%2/3
A and not(B)
A= $self->mode eq "watch"B= not $self->interactive
ABresult
0X0
100
111
828 5 ▶ my $line = $picking ? $self->_ended_key : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 5 0 5
829 5 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 5 5
830 5 ▶ last if !$picking && $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not $picking and $line eq "" 0 5 5
Condition33%1/3
not $picking and $line eq ""
!ll&&!rl&&r
500
MC/DC0%0/2
not $picking and $line eq ""
not $picking $line eq ""
Truth table33%1/3
not(A) and B
A= not $pickingB= $line eq ""
ABresult
0X0
100
111
831 5 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 4 1 5
832 1 $self->_tell(1);
833 1 next;
834 }
835 1223 ▶ if (!$self->_person($game->side)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_person($game->side)) 1046 177 1223
836 1046 $self->_bot_move;
837 1046 next;
838 }
839 177 $self->person_turn;
840 177 ▶ my $line = $picking ? $self->pick : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 139 38 177
841 177 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 176 177
842 176 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 34 142 176
843 }
844 45 return;
845 }
846  
847 45 ▶sub start {
Subroutine start: called CC=10 SCAR=23.2
Pod documented
848 45 my ($self) = @_;
849 45 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 45 45
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
3960
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
850  
851 45 ▶ Game::Terminal::Kit::Run::guard(sub { $self->_turns }, $self->_keyboard, $self->_screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
852  
853 45 ▶ $self->_say(@{ $self->screen('plain') }) if $self->mode eq 'watch' || $self->game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" or $self->game->is_over 10 35 45
Condition100%3/3
$self->mode eq "watch" or $self->game->is_over
l!l&&r!l&&!r
6435
MC/DC100%2/2
$self->mode eq "watch" or $self->game->is_over
$self->mode eq "watch" $self->game->is_over
Truth table100%3/3
A or B
A= $self->mode eq "watch"B= $self->game->is_over
ABresult
1X1
011
000
854 45 ▶ $self->save($self->record) if defined $self->record;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->record 1 44 45
855 45 my $result = $self->game->result;
856 45 ▶ return 1 if $self->mode eq 'bot' && $result && defined $result->winner && $result->winner ne $self->side;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "bot" and $result and defined $result->winner and $result->winner ne $self->side 1 44 45
Condition77%7/9
$self->mode eq "bot" and $result and defined $result->winner and $result->winner ne $self->side
!ll&&!rl&&r
4401
$self->mode eq "bot" and $result and defined $result->winner
!ll&&!rl&&r
4401
$self->mode eq "bot" and $result
!ll&&!rl&&r
22221
MC/DC50%2/4
$self->mode eq "bot" and $result and defined $result->winner and $result->winner ne $self->side
$self->mode eq "bot" $result defined $result->winner $result->winner ne $self->side
Truth table60%3/5
A and B and C and D
A= $self->mode eq "bot"B= $resultC= defined $result->winnerD= $result->winner ne $self->side
ABCDresult
0XXX0
10XX0
110X0
11100
11111
857 44 return 0;
858 }
859  
860 5 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
861 5 return <<'USAGE';
862 senet - Senet, the race game of ancient Egypt, in a terminal
863  
864 senet [options]
865  
866 --mode HOW bot (the default): you against the program
867 hotseat: two people at one keyboard
868 watch: the program against itself
869 --rules NAME kendall (the default) or skyruk
870 --pieces N 5 (the default) or 7 a side
871 --level N how well the program plays, 1 upward; the top by default,
872 and above the top plays the top
873 --side WHICH light (the default) or dark: your side against the program
874 --first WHO light (the default) or dark: who throws first
875 --seed HEX hexadecimal; the same seed throws the same sticks
876 --pace SECONDS how long a lost turn and the program's move stay up
877 --record FILE write the game to FILE when you leave
878 --replay FILE carry on from a game written by --record
879 --ascii plain letters and lines, for a terminal with no Unicode
880 --unicode line and shape characters, where they are not the default
881 --no-colour no colour; nothing is lost without it
882 --no-pick type moves instead of choosing them with the arrow keys
883 --version the version, and stop
884 --help this, and stop
885  
886 In a terminal, the pieces that can move are walked with the arrow keys and the
887 board shows what each move would leave; press ? for the keys. Anywhere else,
888 type a number from the list, a move such as 12-15 or 26-off, or help.
889 USAGE
890 }
891  
892 1 ▶sub options {
Subroutine options: called CC=3 SCAR=11.0
Pod documented
893 1 my $top = 0;
894 1 for my $name (Game::Senet::Variant->names) {
895 2 my @levels = Game::Senet::Bot->levels($name);
896 2 ▶ $top = $levels[-1] if $levels[-1] > $top;
Branch: 1 branch
BranchTrueFalseTotal
if $levels[-1] > $top 1 1 2
897 }
898 return [
899 1 { name => 'mode', type => 'enum', choices => [qw(bot hotseat watch)],
900 help => 'you against the program, two people, or the program against itself' },
901 { name => 'rules', type => 'enum', choices => [ Game::Senet::Variant->names ], help => 'the rules' },
902 { name => 'pieces', type => 'enum', choices => [qw(5 7)], help => 'pieces a side' },
903 { name => 'level', type => 'int', min => 1, max => $top, help => 'how well the program plays; the top by default' },
904 { name => 'side', type => 'enum', choices => [qw(light dark)], help => 'your side against the program' },
905 { name => 'first', type => 'enum', choices => [qw(light dark)], help => 'who throws first' },
906 { name => 'seed', type => 'str', help => 'hexadecimal; the same seed throws the same sticks' },
907 ];
908 }
909  
910 59 ▶sub run {
Subroutine run: called CC=21 SCAR=30.5
Pod documented
911 59 my ($class, @argv) = @_;
912  
913 59 my $saved = Getopt::Long::Configure(qw(default no_ignore_case no_auto_abbrev));
914 59 my %given;
915 59 my $parsed = Getopt::Long::GetOptionsFromArray(
916 \@argv, \%given,
917 'mode=s', 'rules=s', 'pieces=i', 'level=i', 'side=s', 'first=s', 'seed=s', 'pace=s',
918 'record=s', 'replay=s', 'ascii', 'unicode!', 'colour|color!', 'pick!', 'version', 'help',
919 );
920 59 Getopt::Long::Configure($saved);
921  
922 59 ▶ if (!$parsed || @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed or @argv) 3 56 59
Condition100%3/3
not $parsed or @argv
l!l&&r!l&&!r
2156
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
923 3 ▶ print STDERR "senet: 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
924 3 print STDERR _usage();
925 3 return 2;
926 }
927 56 ▶ if ($given{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"help"}) 2 54 56
928 2 print _usage();
929 2 return 0;
930 }
931 54 ▶ if ($given{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"version"}) 3 51 54
932 3 print "senet $Game::Senet::VERSION\n";
933 3 return 0;
934 }
935  
936 51 my %with;
937 51 $with{$_} = $given{$_} for grep { defined $given{$_} } qw(mode rules pieces level side first pace record);
938 51 ▶ $with{colour} = $given{colour} if defined $given{colour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"colour"} 11 40 51
939 51 ▶ $with{picking} = $given{pick} if defined $given{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"pick"} 9 42 51
940 51 ▶ $with{unicode} = $given{unicode} if defined $given{unicode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"unicode"} 1 50 51
941 51 ▶ $with{unicode} = 0 if $given{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"ascii"} 9 42 51
942  
943 51 ▶ if (defined $given{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"seed"}) 41 10 51
944 41 ▶ if ($given{seed} !~ /\A(?:[0-9a-fA-F]{2})+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($given{"seed"} =~ /\A(?:[0-9a-fA-F]{2})+\z/) 2 39 41
945 2 print STDERR "senet: a seed is hexadecimal, two characters a byte.\n";
946 2 return 2;
947 }
948 39 $with{seed} = pack 'H*', $given{seed};
949 }
950  
951 49 ▶ if (defined $given{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"replay"}) 3 46 49
952 3 ▶ open my $fh, '<', $given{replay} or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $given{"replay"} or do { print STDERR qq[senet: I could not read '$given{"replay"}'.\n]; return 2 } 2 1 3
953 1 print STDERR "senet: I could not read '$given{replay}'.\n";
954 1 return 2;
955 };
956 2 my $text = do { local $/; <$fh> };
957 2 close $fh;
958 2 my ($game, $error) = Game::Senet->replay($text);
959 2 ▶ if (!$game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 1 2
960 1 my $detail = $error->detail;
961 print STDERR "senet: '$given{replay}' is not a game I can replay: ", $error->message,
962 1 ▶ (defined $detail->{line} ? " (line $detail->{line})" : defined $detail->{ply} ? ' (ply ' . ($detail->{ply} + 1) . ')' : ''), ".\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $detail->{'line'} ? : 0 1 1
defined $detail->{'ply'} ? : 1 0 1
963 1 return 2;
964 }
965 1 $with{game} = $game;
966 1 $with{rules} = $game->variant;
967 1 ▶ $with{seed} = $game->seed if defined $game->seed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $game->seed 1 0 1
968 }
969  
970 47 my $terminal = eval { $class->new(%with) };
971 47 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 8 39 47
972 8 (my $why = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
973 8 $why =~ s/\AGame::Senet(?:::\w+)*: //;
974 8 print STDERR "senet: $why.\n";
975 8 return 2;
976 }
977  
978 39 return $terminal->start;
979 }
980  
981 54 1;
982  
983 __END__
↑ Summary blib/lib/Game/Senet/Notation.pm »