blib/lib/Game/RoyalUr/Terminal.pm

filter: Statementstmt 94.2% 610 / 647 Branchbran 85.6% 346 / 404 Conditioncond 67.4% 112 / 166 MC/DCmcdc 47.0% 47 / 100 Subroutinesub 96.9% 64 / 66 Podpod 92.3% 12 / 13 totaltotal 85.3% 1191 / 1396 CC 271 SCAR 26.2
CC
271
Cov
88%
command
37.0
BUILD
28.0
run
22.2
CRAP 13.8
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/lib/Game/RoyalUr.pm »
1 package Game::RoyalUr::Terminal;
2  
3 51 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 51 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 51 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 51 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 51 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 51 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
10 51 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 51 ▶use Game::RoyalUr;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 51 ▶use Game::RoyalUr::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 51 ▶use Game::RoyalUr::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 51 ▶use Game::RoyalUr::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 51 our $VERSION = '0.02';
18  
19 51 my (%CONTROL, %SEQUENCE, %STYLE, %GLYPH, %OTHER, @FILES);
20 51 ▶BEGIN {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 51 %CONTROL = (
22 "\n" => 'enter', "\r" => 'enter', "\t" => 'tab', ' ' => 'space',
23 "\x7f" => 'backspace', "\b" => 'backspace', "\x03" => 'interrupt', "\x04" => 'eof',
24 );
25 51 %SEQUENCE = (A => 'up', B => 'down', C => 'right', D => 'left', Z => 'backtab');
26  
27 51 %STYLE = (
28 title => '1;38;5;220',
29 grid => '38;5;241',
30 label => '38;5;245',
31 light => '1;38;5;229',
32 dark => '1;38;5;39',
33 rosette => '1;38;5;205',
34 cursor => '1;38;5;16;48;5;220',
35 landing => '1;38;5;16;48;5;114',
36 capture => '1;38;5;231;48;5;160',
37 marked => '1;38;5;220',
38 unmarked => '38;5;240',
39 key => '1;38;5;214',
40 good => '1;38;5;114',
41 bad => '1;38;5;203',
42 quiet => '38;5;245',
43 step => '38;5;109',
44 );
45  
46 51 %GLYPH = (
47 wide => {
48 light => "\x{25CF}", dark => "\x{25CB}", rosette => "\x{2726}",
49 marked => "\x{25B2}", unmarked => "\x{25B3}",
50 h => "\x{2500}", v => "\x{2502}",
51 tl => "\x{256D}", tr => "\x{256E}", bl => "\x{2570}", br => "\x{256F}",
52 td => "\x{252C}", tu => "\x{2534}", lt => "\x{251C}", rt => "\x{2524}", x => "\x{253C}",
53 },
54 ascii => {
55 light => 'O', dark => '@', rosette => '*',
56 marked => '^', unmarked => '.',
57 h => '-', v => '|',
58 tl => '+', tr => '+', bl => '+', br => '+',
59 td => '+', tu => '+', lt => '+', rt => '+', x => '+',
60 },
61 );
62  
63 51 %OTHER = (light => 'dark', dark => 'light');
64 51 @FILES = ('a' .. 'h');
65 }
66  
67 51 has game => (is => 'rw');
68  
69 51 has [qw/in out/] => (is => 'rw');
70  
71 51 has [qw/interactive colour unicode picking/] => (is => 'rw');
72  
73 51 has mode => (is => 'rw', default => 'bot');
74  
75 51 has side => (is => 'rw', default => 'light');
76  
77 51 has level => (is => 'rw');
78  
79 51 has rules => (is => 'rw', default => 'finkel');
80  
81 51 has first => (is => 'rw', default => 'roll');
82  
83 51 has seed => (is => 'rw');
84  
85 51 has pace => (is => 'rw');
86  
87 51 has route => (is => 'rw', default => 0);
88  
89 51 has record => (is => 'rw');
90  
91 51 has keysource => (is => 'rw');
92  
93 51 has sleeper => (is => 'rw');
94  
95 51 has _raw => (is => 'rw', private => 1);
96  
97 51 has _pending => (is => 'rw', private => 1);
98  
99 51 has _events => (is => 'rw', private => 1);
100  
101 51 has _told => (is => 'rw', private => 1);
102  
103 51 has _cursor => (is => 'rw', private => 1);
104  
105 51 has _note => (is => 'rw', private => 1);
106  
107 113 ▶sub BUILD {
Subroutine BUILD: called CC=27 SCAR=33.3
Pod undocumented
108 113 my ($self) = @_;
109 113 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 40 73 113
110 113 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 40 73 113
111 113 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 40 73 113
-t $self->in ? : 0 40 40
112 113 ▶ $self->colour($self->interactive && !$ENV{NO_COLOR} ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 44 69 113
$self->interactive && !$ENV{'NO_COLOR'} ? : 0 44 44
Condition33%1/3
$self->interactive and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
4400
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
113 113 ▶ $self->unicode($self->interactive ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 44 69 113
$self->interactive ? : 0 44 44
114 113 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 46 67 113
$self->interactive ? : 0 46 46
115 113 ▶ $self->picking(0) unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 74 39 113
116 113 ▶ $self->pace($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pace;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pace 43 70 113
$self->interactive && !$self->keysource ? : 0 43 43
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
4300
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
117  
118 113 ▶ Carp::croak("Game::RoyalUr::Terminal: mode is bot, hotseat or watch, not '" . $self->mode . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mode =~ /\A(?:bot|hotseat|watch)\z/ 2 111 113
119 unless $self->mode =~ /\A(?:bot|hotseat|watch)\z/;
120 111 ▶ Carp::croak("Game::RoyalUr::Terminal: side is light or dark, not '" . $self->side . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->side =~ /\A(?:light|dark)\z/ 2 109 111
121 unless $self->side =~ /\A(?:light|dark)\z/;
122 109 ▶ Carp::croak("Game::RoyalUr::Terminal: first is light, dark or roll, not '" . $self->first . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->first =~ /\A(?:light|dark|roll)\z/ 2 107 109
123 unless $self->first =~ /\A(?:light|dark|roll)\z/;
124 107 ▶ Carp::croak('Game::RoyalUr::Terminal: pace is a number of seconds, 0 or more')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pace =~ /\A\d+(?:\.\d+)?\z/ 3 104 107
125 unless $self->pace =~ /\A\d+(?:\.\d+)?\z/;
126  
127 104 my $variant = Game::RoyalUr::Variant->of($self->rules);
128 102 my @levels = Game::RoyalUr::Bot->levels($variant);
129 102 ▶ $self->level($levels[-1]) unless defined $self->level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->level 89 13 102
130 102 ▶ Carp::croak('Game::RoyalUr::Terminal: level is a whole number from 1 to ' . $levels[-1])
Branch: 1 branch
BranchTrueFalseTotal
unless $self->level =~ /\A\d+\z/ and $self->level >= 1 and $self->level <= $levels[-1] 3 99 102
Condition83%5/6
$self->level =~ /\A\d+\z/ and $self->level >= 1 and $self->level <= $levels[-1]
!ll&&!rl&&r
1299
$self->level =~ /\A\d+\z/ and $self->level >= 1
!ll&&!rl&&r
01101
MC/DC66%2/3
$self->level =~ /\A\d+\z/ and $self->level >= 1 and $self->level <= $levels[-1]
$self->level =~ /\A\d+\z/ $self->level >= 1 $self->level <= $levels[-1]
Truth table75%3/4
A and B and C
A= $self->level =~ /\A\d+\z/B= $self->level >= 1C= $self->level <= $levels[-1]
ABCresult
0XX0
10X0
1100
1111
131 unless $self->level =~ /\A\d+\z/ && $self->level >= 1 && $self->level <= $levels[-1];
132  
133 99 ▶ $self->seed(Digest::SHA::sha256(join '|', 'royalur', $$, time, rand)) unless defined $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed 15 84 99
134 99 $self->_raw(0);
135 99 $self->_pending([]);
136 99 $self->_events([]);
137 99 $self->_told(0);
138 99 $self->_cursor(0);
139 99 ▶ if ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game) { } 49 50 99
140 49 $self->_told(scalar @{ $self->game->log });
141 }
142 else {
143 50 $self->game($self->_fresh_game);
144 50 $self->_announce_opening;
145 }
146  
147 99 ▶ binmode $self->out, ':encoding(UTF-8)' if $self->unicode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->unicode 2 97 99
148 99 my $previous = select $self->out;
149 99 $| = 1;
150 99 select $previous;
151 99 return;
152 }
153  
154 51 ▶sub _fresh_game {
Subroutine _fresh_game: called CC=2 SCAR=6.9
155 51 my ($self) = @_;
156 51 ▶ return Game::RoyalUr->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->first eq 'roll' ? : 3 48 51
157 seed => $self->seed,
158 rules => $self->rules,
159 ($self->first eq 'roll' ? () : (first => $self->first)),
160 );
161 }
162  
163 51 ▶sub _announce_opening {
Subroutine _announce_opening: called CC=4 SCAR=13.9
164 51 my ($self) = @_;
165 51 my $game = $self->game;
166 51 my @throws = @{ $game->opening };
167 51 ▶ return unless @throws;
Branch: 1 branch
BranchTrueFalseTotal
unless @throws 48 3 51
168 3 ▶ while (@throws) {
Branch: 1 branch
BranchTrueFalseTotal
if (@throws) 6 3 9
169 6 my ($light, $dark) = splice @throws, 0, 2;
170 6 my ($l, $d) = (scalar(grep { $_ } @$light), scalar(grep { $_ } @$dark));
171 6 ▶ $self->_event(undef, sprintf 'To see who starts, light threw %s %d and dark %s %d%s',
Branch: 1 branch
BranchTrueFalseTotal
$l == $d ? : 3 3 6
172 $self->_dice($light), $l, $self->_dice($dark), $d, $l == $d ? ': a tie, again.' : '.');
173 }
174 3 $self->_event($game->first, ucfirst($game->first) . ' moves first.');
175 3 return;
176 }
177  
178 17839 ▶sub _paint {
Subroutine _paint: called CC=4 SCAR=14.1
179 17839 my ($self, $text, $style) = @_;
180 17839 ▶ return $text unless $self->colour && defined $style && $STYLE{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour and defined $style and $STYLE{$style} 17605 234 17839
Condition66%4/6
$self->colour and defined $style and $STYLE{$style}
!ll&&!rl&&r
176050234
$self->colour and defined $style
!ll&&!rl&&r
176050234
MC/DC33%1/3
$self->colour and defined $style and $STYLE{$style}
$self->colour defined $style $STYLE{$style}
Truth table50%2/4
A and B and C
A= $self->colourB= defined $styleC= $STYLE{$style}
ABCresult
0XX0
10X0
1100
1111
181 234 return "\e[$STYLE{$style}m$text\e[0m";
182 }
183  
184 21655 ▶sub _glyph { $GLYPH{ $_[0]->unicode ? 'wide' : 'ascii' }{ $_[1] } }
Subroutine _glyph: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->unicode ? : 112 21543 21655
185  
186 359 ▶sub _dice {
Subroutine _dice: called CC=3 SCAR=11.0
187 359 my ($self, $faces) = @_;
188 359 ▶ return join ' ', map { $self->_paint($self->_glyph($_ ? 'marked' : 'unmarked'), $_ ? 'marked' : 'unmarked') } @$faces;
Branch: 2 branches
BranchTrueFalseTotal
$_ ? : 650 658 1308
$_ ? : 650 658 1308
189 }
190  
191 743 ▶sub _event {
Subroutine _event: called CC=1 SCAR=0.0
192 743 my ($self, $side, $text) = @_;
193 743 push @{ $self->_events }, { side => $side, text => $text };
194 743 return;
195 }
196  
197 3 ▶sub events { [ map { $_->{text} } @{ $_[0]->_events } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
198  
199 1372 ▶sub _person {
Subroutine _person: called CC=4 SCAR=13.9
200 1372 my ($self, $side) = @_;
201 1372 ▶ return 0 if $self->mode eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" 986 386 1372
202 386 ▶ return 1 if $self->mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "hotseat" 76 310 386
203 310 ▶ return $side eq $self->side ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 162 148 310
204 }
205  
206 1076 ▶sub _who {
Subroutine _who: called CC=3 SCAR=11.0
207 1076 my ($self, $side) = @_;
208 1076 ▶ return ucfirst $side if $self->mode ne 'bot';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode ne "bot" 673 403 1076
209 403 ▶ return $side eq $self->side ? 'You' : ucfirst $side;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 228 175 403
210 }
211  
212 661 ▶my $said = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 661 my ($entry) = @_;
214 661 ▶ return Game::RoyalUr::Notation::format_forfeit($entry->{roll}) unless defined $entry->{move};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"move"} 4 657 661
215 657 my ($from, $to) = split /-/, $entry->{move};
216 657 return Game::RoyalUr::Notation::format_display({ from => $from, to => $to, %$entry });
217 51 };
218  
219 703 ▶sub _tell {
Subroutine _tell: called CC=11 SCAR=24.0
220 703 my ($self, $pause) = @_;
221 703 my $game = $self->game;
222 703 my $log = $game->log;
223 703 my $told = $self->_told;
224 703 ▶ while ($told < @$log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($told < @$log) 729 703 1432
225 729 my $entry = $log->[ $told++ ];
226 729 my $who = $self->_who($entry->{side});
227 729 my $text;
228 729 ▶ if (!defined $entry->{move}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $entry->{'move'}) { } 75 654 729
229 75 ▶ $text = $entry->{roll} == 0
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'roll'} == 0 ? : 69 6 75
230 ? "$who rolled nothing and lost the turn."
231 : "$who rolled $entry->{roll} and could not move: the turn is lost.";
232 }
233 else {
234 654 $text = "$who played " . $said->($entry);
235 654 ▶ $text .= ', capturing' if $entry->{captures};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"captures"} 66 588 654
236 $text .= $entry->{home} ? ', home.'
237 654 ▶ : $entry->{rosette} ? ', onto a rosette: another roll.' : '.';
Branch: 2 branches
BranchTrueFalseTotal
$entry->{'home'} ? : 55 599 654
$entry->{'rosette'} ? : 133 466 599
238 }
239 729 $self->_event($entry->{side}, $text);
240 729 $self->_told($told);
241 729 ▶ my $slow = !defined $entry->{move} || !$self->_person($entry->{side});
Condition100%3/3
defined $entry->{'move'} && $self->_person($entry->{'side'})
!ll&&!rl&&r
7556787
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
242 729 ▶ if ($pause && $slow && $self->_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pause and $slow and $self->_raw) 87 642 729
Condition83%5/6
$pause and $slow and $self->_raw
!ll&&!rl&&r
8755587
$pause and $slow
!ll&&!rl&&r
087642
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
243 87 $self->show('wait');
244 87 $self->_wait;
245 }
246 }
247 703 $self->_told($told);
248 703 return;
249 }
250  
251 87 ▶sub _wait {
Subroutine _wait: called CC=3 SCAR=11.8
252 87 my ($self) = @_;
253 87 my $pace = $self->pace;
254 87 ▶ return unless $pace > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pace > 0 2 85 87
255 85 ▶ return $self->sleeper->($pace) if $self->sleeper;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sleeper 85 0 85
256 0 select undef, undef, undef, $pace;
257 0 return;
258 }
259  
260 112299 ▶my $coordinates = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
261 112299 my ($place, $side) = @_;
262 112299 ▶ return (4.5, $side eq 'light' ? 0 : 2) if $place eq 'hand';
Branch: 2 branches
BranchTrueFalseTotal
if $place eq "hand" 18927 93372 112299
$side eq 'light' ? : 11759 7168 18927
263 93372 return (ord(substr $place, 0, 1) - ord('a'), substr($place, 1, 1) - 1);
264 51 };
265  
266 36706 ▶sub candidates {
Subroutine candidates: called CC=1 SCAR=0.0
Pod documented
267 36706 my ($self) = @_;
268 36706 return $self->game->legal;
269 }
270  
271 36311 ▶sub steer {
Subroutine steer: called CC=16 SCAR=27.7
Pod documented
272 36311 my ($self, $key) = @_;
273 36311 my @moves = $self->candidates;
274 36311 ▶ return $self->_cursor unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 0 36311 36311
275 36311 my $at = $self->_cursor;
276 36311 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 52 36259 36311
277  
278 36311 ▶ if ($key eq 'tab') { $at = ($at + 1) % @moves }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tab") 7 36304 36311
279 36311 ▶ if ($key eq 'backtab') { $at = ($at - 1) % @moves }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backtab") 2 36309 36311
280 36311 ▶ if ($key =~ /\A[1-7]\z/ && $key <= @moves) { $at = $key - 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-7]\z/ and $key <= @moves) 2140 34171 36311
Condition100%3/3
$key =~ /\A[1-7]\z/ and $key <= @moves
!ll&&!rl&&r
3416922140
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
281  
282 36311 ▶ if ($key =~ /\A(?:left|right|up|down)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A(?:left|right|up|down)\z/) 34159 2152 36311
283 34159 my $side = $self->game->side;
284 34159 my @xy = map { [ $coordinates->($_->from, $side) ] } @moves;
285 my @order = $key eq 'left' || $key eq 'right'
286 61553 ▶ ? sort { $xy[$a][0] <=> $xy[$b][0] || $xy[$a][1] <=> $xy[$b][1] } 0 .. $#moves
Condition66%2/3
$xy[$a][0] <=> $xy[$b][0] || $xy[$a][1] <=> $xy[$b][1]
l!l&&r!l&&!r
5843331200
MC/DC0%0/2
$xy[$a][0] <=> $xy[$b][0] || $xy[$a][1] <=> $xy[$b][1]
$xy[$a][0] <=> $xy[$b][0] $xy[$a][1] <=> $xy[$b][1]
Truth table66%2/3
A || B
A= $xy[$a][0] <=> $xy[$b][0]B= $xy[$a][1] <=> $xy[$b][1]
ABresult
1X1
011
000
287 34159 ▶ : sort { $xy[$b][1] <=> $xy[$a][1] || $xy[$a][0] <=> $xy[$b][0] } 0 .. $#moves;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'left' || $key eq 'right' ? : 17081 17078 34159
Condition83%5/6
$key eq "left" or $key eq "right"
l!l&&r!l&&!r
8537854417078
$xy[$b][1] <=> $xy[$a][1] || $xy[$a][0] <=> $xy[$b][0]
l!l&&r!l&&!r
30760247120
MC/DC100%2/2
$key eq "left" or $key eq "right"
$key eq "left" $key eq "right"
MC/DC0%0/2
$xy[$b][1] <=> $xy[$a][1] || $xy[$a][0] <=> $xy[$b][0]
$xy[$b][1] <=> $xy[$a][1] $xy[$a][0] <=> $xy[$b][0]
Truth table100%3/3
A or B
A= $key eq "left"B= $key eq "right"
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $xy[$b][1] <=> $xy[$a][1]B= $xy[$a][0] <=> $xy[$b][0]
ABresult
1X1
011
000
288 34159 my ($where) = grep { $order[$_] == $at } 0 .. $#order;
289 34159 ▶ $where += ($key eq 'right' || $key eq 'down') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'right' || $key eq 'down' ? : 17085 17074 34159
Condition100%3/3
$key eq "right" or $key eq "down"
l!l&&r!l&&!r
8544854117074
MC/DC100%2/2
$key eq "right" or $key eq "down"
$key eq "right" $key eq "down"
Truth table100%3/3
A or B
A= $key eq "right"B= $key eq "down"
ABresult
1X1
011
000
290 34159 ▶ $at = $order[$where] if $where >= 0 && $where <= $#order;
Branch: 1 branch
BranchTrueFalseTotal
if $where >= 0 and $where <= $#order 7331 26828 34159
Condition100%3/3
$where >= 0 and $where <= $#order
!ll&&!rl&&r
14634121947331
MC/DC100%2/2
$where >= 0 and $where <= $#order
$where >= 0 $where <= $#order
Truth table100%3/3
A and B
A= $where >= 0B= $where <= $#order
ABresult
0X0
100
111
291 }
292 36311 $self->_cursor($at);
293 36311 return $at;
294 }
295  
296 391 ▶my $cells_after = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
297 391 my ($game, $move) = @_;
298 391 my (%cell, %hand, %home, %mark);
299 391 for my $file (@FILES) {
300 3128 for my $row (1 .. 3) {
301 9384 my $name = "$file$row";
302 9384 ▶ next unless Game::RoyalUr::Notation::square_ok($name);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::RoyalUr::Notation::square_ok($name) 1564 7820 9384
303 7820 $cell{$name} = $game->at($name);
304 }
305 }
306 391 for my $side (qw(light dark)) {
307 782 $hand{$side} = $game->hand($side);
308 782 $home{$side} = $game->home($side);
309 }
310 391 ▶ return (\%cell, \%hand, \%home, \%mark) unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 266 125 391
311  
312 125 my $side = $move->side;
313 125 ▶ if ($move->from eq 'hand') { $hand{$side}--; $mark{"hand $side"} = 'cursor' }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->from eq 'hand') { } 49 76 125
314 76 else { $cell{ $move->from } = undef; $mark{ $move->from } = 'cursor' }
315 125 ▶ if ($move->captures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->captures) 13 112 125
316 13 $hand{ $OTHER{$side} }++;
317 13 $mark{"hand $OTHER{$side}"} = 'capture';
318 }
319 125 ▶ if ($move->home) { $home{$side}++; $mark{"home $side"} = 'landing' }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->home) { } 2 123 125
320 123 else { $cell{ $move->to } = $side; $mark{ $move->to } = 'landing' }
321 125 return (\%cell, \%hand, \%home, \%mark);
322 51 };
323  
324 51 my %BRACKET;
325 51 ▶BEGIN { %BRACKET = (cursor => [ '[', ']' ], landing => [ '(', ')' ], capture => [ '<', '>' ], last => [ '_', '_' ]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
326  
327 7884 ▶sub _framed {
Subroutine _framed: called CC=6 SCAR=17.9
328 7884 my ($self, $what, $mark) = @_;
329 7884 ▶ my ($open, $close) = $mark ? @{ $BRACKET{$mark} } : (' ', ' ');
Branch: 1 branch
BranchTrueFalseTotal
$mark ? : 594 7290 7884
330 7884 ▶ my $inside = defined $what ? $self->_glyph($what) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 1861 6023 7884
331 7884 ▶ return $self->_paint($open . $inside . $close, $mark) if $mark && $mark ne 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $mark and $mark ne "last" 263 7621 7884
Condition100%3/3
$mark and $mark ne "last"
!ll&&!rl&&r
7290331263
MC/DC100%2/2
$mark and $mark ne "last"
$mark $mark ne "last"
Truth table100%3/3
A and B
A= $markB= $mark ne "last"
ABresult
0X0
100
111
332 7621 ▶ return $open . (defined $what ? $self->_paint($inside, $what) : $inside) . $close;
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 1723 5898 7621
333 }
334  
335 1173 ▶sub _row {
Subroutine _row: called CC=5 SCAR=16.1
336 1173 my ($self, $row, $cell, $mark) = @_;
337 1173 my $v = $self->_paint($self->_glyph('v'), 'grid');
338 1173 my @parts;
339 1173 for my $file (@FILES) {
340 9384 my $name = "$file$row";
341 9384 ▶ if (!Game::RoyalUr::Notation::square_ok($name)) { push @parts, undef; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (Game::RoyalUr::Notation::square_ok($name)) 1564 7820 9384
342 7820 ▶ my $rosette = $name =~ /\A(?:a1|g1|a3|g3|d2)\z/ ? $self->_paint($self->_glyph('rosette'), 'rosette') : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A(?:a1|g1|a3|g3|d2)\z/ ? : 1955 5865 7820
343 7820 push @parts, $rosette . $self->_framed($cell->{$name}, $mark->{$name}) . ' ';
344 }
345 1173 my $line = $self->_paint($row, 'label') . ' ' . $v;
346 1173 ▶ if (defined $parts[4]) { $line .= join($v, @parts) . $v }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parts[4]) { } 391 782 1173
347 782 else { $line .= join($v, @parts[0 .. 3]) . $v . (' ' x 11) . $v . join($v, @parts[6, 7]) . $v }
348 1173 return ' ' . $line;
349 }
350  
351 1564 ▶sub _border {
Subroutine _border: called CC=4 SCAR=13.9
352 1564 my ($self, $kind) = @_;
353 1564 ▶ my $g = sub { $self->_glyph($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
354 1564 my $bar = $g->('h') x 5;
355 1564 ▶ my $block = sub { my ($left, $mid, $right, $n) = @_; $g->($left) . join($g->($mid), ($bar) x $n) . $g->($right) };
Subroutine __ANON__: called CC=1 SCAR=0.0
356 1564 ▶ my $line = $kind eq 'top' ? $block->('tl', 'td', 'tr', 4) . (' ' x 11) . $block->('tl', 'td', 'tr', 2)
Branch: 3 branches
BranchTrueFalseTotal
$kind eq 'top' ? : 391 1173 1564
$kind eq 'bottom' ? : 391 782 1173
$kind eq 'upper' ? : 391 391 782
357 : $kind eq 'bottom' ? $block->('bl', 'tu', 'br', 4) . (' ' x 11) . $block->('bl', 'tu', 'br', 2)
358 : $g->('lt') . join($g->('x'), ($bar) x 4) . $g->('x') . $bar . $g->($kind eq 'upper' ? 'td' : 'tu') . $bar
359 . $g->('x') . join($g->('x'), ($bar) x 2) . $g->('rt');
360 1564 return ' ' . $self->_paint($line, 'grid');
361 }
362  
363 782 ▶sub _tray {
Subroutine _tray: called CC=1 SCAR=0.0
364 782 my ($self, $side, $hand, $home, $mark) = @_;
365 1564 ▶ my $pieces = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
366 1564 my ($count, $which) = @_;
367 1564 my @shown = map { $self->_paint($self->_glyph($side), $side) } 1 .. $count;
368 1564 my $m = $mark->{"$which $side"};
369 1564 ▶ if ($m && $m eq 'cursor') { push @shown, $self->_framed(undef, 'cursor') }
Branch: 2 branches
BranchTrueFalseTotal
if ($m and $m eq 'cursor') { } 49 1515 1564
elsif ($m and @shown) { } 15 3000 3015
Condition100%3/3
$m and $m eq "cursor"
!ll&&!rl&&r
15001549
MC/DC100%2/2
$m and $m eq "cursor"
$m $m eq "cursor"
Truth table100%3/3
A and B
A= $mB= $m eq "cursor"
ABresult
0X0
100
111
370 15 ▶ elsif ($m && @shown) { $shown[-1] = $self->_framed($side, $m) }
Condition66%2/3
$m and @shown
!ll&&!rl&&r
1500015
MC/DC50%1/2
$m and @shown
$m @shown
Truth table66%2/3
A and B
A= $mB= @shown
ABresult
0X0
100
111
371 1564 ▶ return @shown ? join(' ', @shown) : $self->_paint('none', 'quiet');
Branch: 1 branch
BranchTrueFalseTotal
@shown ? : 966 598 1564
372 782 };
373 return sprintf ' %s hand %s home %s',
374 782 $self->_paint(sprintf('%-5s', $side), $side), $pieces->($hand->{$side}, 'hand'), $pieces->($home->{$side}, 'home');
375 }
376  
377 391 ▶sub board_lines {
Subroutine board_lines: called CC=9 SCAR=22.0
Pod documented
378 391 my ($self, $move) = @_;
379 391 my $game = $self->game;
380 391 my ($cell, $hand, $home, $mark) = $cells_after->($game, $move);
381 391 ▶ if (!$move) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 266 125 391
382 266 my %latest;
383 266 for my $entry (@{ $game->log }) {
384 12088 ▶ $latest{ $entry->{side} } = $entry if defined $entry->{move};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"move"} 11174 914 12088
385 }
386 266 for my $entry (values %latest) {
387 368 my (undef, $to) = split /-/, $entry->{move};
388 368 ▶ $mark->{$to} = 'last' if $to ne 'home' && defined $cell->{$to} && $cell->{$to} eq $entry->{side};
Branch: 1 branch
BranchTrueFalseTotal
if $to ne "home" and defined $cell->{$to} and $cell->{$to} eq $entry->{"side"} 331 37 368
Condition83%5/6
$to ne "home" and defined $cell->{$to} and $cell->{$to} eq $entry->{"side"}
!ll&&!rl&&r
289331
$to ne "home" and defined $cell->{$to}
!ll&&!rl&&r
280340
MC/DC66%2/3
$to ne "home" and defined $cell->{$to} and $cell->{$to} eq $entry->{"side"}
$to ne "home" defined $cell->{$to} $cell->{$to} eq $entry->{"side"}
Truth table75%3/4
A and B and C
A= $to ne "home"B= defined $cell->{$to}C= $cell->{$to} eq $entry->{"side"}
ABCresult
0XX0
10X0
1100
1111
389 }
390 }
391 391 my @lines = (
392 $self->_tray('dark', $hand, $home, $mark),
393 ' ' . $self->_paint(join(' ', @FILES), 'label'),
394 $self->_border('top'),
395 $self->_row(3, $cell, $mark),
396 $self->_border('upper'),
397 $self->_row(2, $cell, $mark),
398 $self->_border('lower'),
399 $self->_row(1, $cell, $mark),
400 $self->_border('bottom'),
401 $self->_tray('light', $hand, $home, $mark),
402 );
403 391 ▶ push @lines, '', @{ $self->_route_lines } if $self->route;
Branch: 1 branch
BranchTrueFalseTotal
if $self->route 3 388 391
404 391 return \@lines;
405 }
406  
407 3 ▶sub _route_lines {
Subroutine _route_lines: called CC=9 SCAR=22.1
408 3 my ($self) = @_;
409 3 my $game = $self->game;
410 3 ▶ my $side = $game->side || $game->first;
Condition33%1/3
$game->side || $game->first
l!l&&r!l&&!r
300
MC/DC0%0/2
$game->side || $game->first
$game->side $game->first
Truth table33%1/3
A || B
A= $game->sideB= $game->first
ABresult
1X1
011
000
411 3 my $variant = $game->variant;
412 3 ▶ my $route = $variant->route eq 'long' ? Game::RoyalUr::Engine::ROUTE_LONG() : Game::RoyalUr::Engine::ROUTE_SHORT();
Branch: 1 branch
BranchTrueFalseTotal
$variant->route eq 'long' ? : 2 1 3
413 3 ▶ my $index = $side eq 'light' ? Game::RoyalUr::Engine::SIDE_LIGHT() : Game::RoyalUr::Engine::SIDE_DARK();
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'light' ? : 3 0 3
414 3 my @lines = (' ' . $self->_paint('The ' . $variant->route . " route, as $side travels it:", 'quiet'));
415 3 for my $row (reverse 0 .. 2) {
416 9 my $line = ' ' . $self->_paint($row + 1, 'label') . ' ';
417 9 for my $file (0 .. 7) {
418 72 my $cell = Game::RoyalUr::Engine->cell_of($file, $row);
419 72 ▶ my $step = $cell < 0 ? 0 : Game::RoyalUr::Engine->route_step($route, $index, $cell);
Branch: 1 branch
BranchTrueFalseTotal
$cell < 0 ? : 12 60 72
420 72 ▶ $line .= $cell < 0 ? ' ' : $step ? $self->_paint(sprintf(' %2d ', $step), 'step') : $self->_paint(' . ', 'quiet');
Branch: 2 branches
BranchTrueFalseTotal
$cell < 0 ? : 12 60 72
$step ? : 46 14 60
421 }
422 9 push @lines, $line;
423 }
424 3 return \@lines;
425 }
426  
427 356 ▶sub _roll_line {
Subroutine _roll_line: called CC=5 SCAR=16.1
428 356 my ($self) = @_;
429 356 my $game = $self->game;
430 356 ▶ return () if $game->is_over || !defined $game->roll;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over or not defined $game->roll 9 347 356
Condition66%2/3
$game->is_over or not defined $game->roll
l!l&&r!l&&!r
90347
MC/DC50%1/2
$game->is_over or not defined $game->roll
$game->is_over not defined $game->roll
Truth table66%2/3
A or not(B)
A= $game->is_overB= not defined $game->roll
ABresult
1X1
011
000
431 347 my $throw = $game->throw;
432 347 my $roll = $game->roll;
433 347 my $marked = grep { $_ } @$throw;
434 347 my $text = sprintf ' %s rolled %s %s', $self->_who($game->side), $self->_paint($roll, 'key'), $self->_dice($throw);
435 347 ▶ $text .= $self->_paint(' (nothing marked is worth four)', 'quiet') if !$marked && $roll;
Branch: 1 branch
BranchTrueFalseTotal
if not $marked and $roll 32 315 347
Condition66%2/3
not $marked and $roll
!ll&&!rl&&r
315032
MC/DC50%1/2
not $marked and $roll
not $marked $roll
Truth table66%2/3
not(A) and B
A= not $markedB= $roll
ABresult
0X0
100
111
436 347 return $text;
437 }
438  
439 356 ▶sub _result_line {
Subroutine _result_line: called CC=4 SCAR=14.0
440 356 my ($self) = @_;
441 356 ▶ my $result = $self->game->result or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->game->result or return () 9 347 356
442 9 my $home = $result->home;
443 9 ▶ 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 9 9
444 9 my $winner = $result->winner;
445 9 ▶ return ' ' . $self->_paint(sprintf('%s won: %s resigned.', ucfirst $winner, $result->loser), 'good')
Branch: 1 branch
BranchTrueFalseTotal
if $result->how eq "resign" 0 9 9
446 if $result->how eq 'resign';
447 return ' ' . $self->_paint(sprintf('%s won, all %d home to %d, in %d plies.',
448 9 ucfirst $winner, $home->{$winner}, $home->{ $result->loser }, $result->plies), 'good');
449 }
450  
451 356 ▶sub _event_lines {
Subroutine _event_lines: called CC=3 SCAR=11.0
452 356 my ($self) = @_;
453 356 my @recent = @{ $self->_events };
454 356 ▶ @recent = @recent[ -4 .. -1 ] if @recent > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @recent > 4 234 122 356
455 356 ▶ return map { ' ' . $self->_paint($_->{text}, $_->{side} || 'quiet') } @recent;
Condition100%2/2
$_->{'side'} || 'quiet'
l!l
101426
MC/DC100%1/1
$_->{'side'} || 'quiet'
$_->{'side'}
Truth table100%2/2
A
A= $_->{'side'}
Aresult
11
00
456 }
457  
458 188 ▶sub _readout {
Subroutine _readout: called CC=6 SCAR=17.9
459 188 my ($self, $move) = @_;
460 188 my $text = Game::RoyalUr::Notation::format_display($move);
461 188 my @does;
462 188 ▶ push @does, 'enters the board' if $move->from eq 'hand';
Branch: 1 branch
BranchTrueFalseTotal
if $move->from eq "hand" 118 70 188
463 188 ▶ push @does, 'captures' if $move->captures;
Branch: 1 branch
BranchTrueFalseTotal
if $move->captures 8 180 188
464 188 ▶ push @does, 'lands on a rosette: roll again' if $move->rosette;
Branch: 1 branch
BranchTrueFalseTotal
if $move->rosette 18 170 188
465 188 ▶ push @does, 'comes home' if $move->home;
Branch: 1 branch
BranchTrueFalseTotal
if $move->home 0 188 188
466 188 ▶ return ' ' . $self->_paint($text, 'key') . (@does ? ' ' . join(', ', @does) : '');
Branch: 1 branch
BranchTrueFalseTotal
@does ? : 140 48 188
467 }
468  
469 262 ▶sub _legend {
Subroutine _legend: called CC=3 SCAR=11.0
470 262 my ($self, $mode) = @_;
471 262 ▶ my $k = sub { $self->_paint($_[0], 'key') };
Subroutine __ANON__: called CC=1 SCAR=0.0
472 262 ▶ return ' ' . join(' ', $k->('n') . ' new game', $k->('u') . ' take back', $k->('q') . ' leave') if $mode eq 'ended';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "ended" 1 261 262
473 261 ▶ return ' ' . $self->_paint('...', 'quiet') if $mode eq 'wait';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "wait" 158 103 261
474 103 return ' ' . join(' ', $k->('arrows') . ' choose a piece', $k->('enter') . ' move it',
475 $k->('u') . ' take back', $k->('r') . ' route', $k->('?') . ' help', $k->('q') . ' leave');
476 }
477  
478 357 ▶sub screen {
Subroutine screen: called CC=14 SCAR=26.4
Pod documented
479 357 my ($self, $mode) = @_;
480 357 ▶ $mode = 'plain' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 357 357
481 357 my $game = $self->game;
482 357 my $variant = $game->variant;
483 357 ▶ my @lines = (
Branch: 3 branches
BranchTrueFalseTotal
defined $variant->name ? : 355 2 357
$self->mode eq 'bot' ? : 248 109 357
$self->mode eq 'hotseat' ? : 105 4 109
484 ' ' . $self->_paint('The Royal Game of Ur', 'title') . ' '
485 . $self->_paint(join(', ', (defined $variant->name ? $variant->name : $variant->describe),
486 ($self->mode eq 'bot' ? 'you are ' . $self->side . ', against level ' . $self->level
487 : $self->mode eq 'hotseat' ? 'two players' : 'level ' . $self->level . ' against itself')), 'quiet'),
488 '',
489 );
490 357 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 1 356 357
491 1 push @lines, map { " $_" } @{ $self->help_lines };
492 1 return \@lines;
493 }
494 356 ▶ my @moves = $mode eq 'pick' ? $self->candidates : ();
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'pick' ? : 103 253 356
495 356 my $at = $self->_cursor;
496 356 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 253 103 356
497 356 ▶ my $move = @moves ? $moves[$at] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@moves ? : 103 253 356
498  
499 356 push @lines, @{ $self->board_lines($move) }, '';
500 356 push @lines, $self->_roll_line, $self->_result_line;
501 356 ▶ push @lines, $self->_readout($move) if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 103 253 356
502 356 push @lines, '';
503 356 push @lines, $self->_event_lines;
504 356 ▶ push @lines, ' ' . $self->_paint($self->_note, 'bad') if defined $self->_note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_note 15 341 356
505 356 ▶ 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" 262 94 356
Condition100%6/6
$mode eq "pick" or $mode eq "ended" or $mode eq "wait"
l!l&&r!l&&!r
10415894
$mode eq "pick" or $mode eq "ended"
l!l&&r!l&&!r
1031252
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
506 356 return \@lines;
507 }
508  
509 261 ▶sub show {
Subroutine show: called CC=3 SCAR=11.4
Pod documented
510 261 my ($self, $mode) = @_;
511 261 my $out = $self->out;
512 261 my $lines = $self->screen($mode);
513 261 ▶ if ($self->interactive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 261 0 261
514 261 print {$out} "\e[H", join('', map { "$_\e[K\n" } @$lines), "\e[J";
515 }
516 else {
517 0 print {$out} "$_\n" for @$lines;
518 }
519 261 return $self;
520 }
521  
522 2 ▶sub help_lines {
Subroutine help_lines: called CC=1 SCAR=0.0
Pod documented
523 return [
524 2 'A move is a piece. The roll says how far it goes, so choosing the piece is',
525 'the whole move.',
526 '',
527 ' arrows, tab walk the pieces that can move',
528 ' 1 to 7 go straight to one of them',
529 ' enter, space move it',
530 ' u take back your last move',
531 ' r show or hide the route',
532 ' n a new game',
533 ' q leave (asked twice)',
534 '',
535 'The board shows what the move would leave: [ ] where the piece was, ( ) where',
536 'it lands, and < > round a piece it sends back to its owner\'s hand.',
537 '',
538 'Typing instead: a number from the list, or a move such as hand-b1 or a2-d2;',
539 'and undo, new, route, moves, record FILE, level N, help, quit.',
540 ];
541 }
542  
543 113 ▶sub _keys_available {
Subroutine _keys_available: called CC=5 SCAR=21.7
544 113 my ($self) = @_;
545 113 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 39 74 113
546 74 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 74 0 74
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0740
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
547 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
548 }
549  
550 3 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=5 SCAR=25.6
551 3 my ($self) = @_;
552 3 ▶ return 1 if $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_raw 0 3 3
553 3 ▶ if ($self->keysource) { $self->_raw(1); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 3 0 3
554 0 ▶ return 0 unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 0 0 0
555 0 ▶ return 0 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
556 0 $self->_raw(1);
557 0 my $out = $self->out;
558 0 print {$out} "\e[?25l\e[2J";
559 0 return 1;
560 }
561  
562 49 ▶sub _leave_raw {
Subroutine _leave_raw: called CC=3 SCAR=13.1
563 49 my ($self) = @_;
564 49 ▶ return unless $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_raw 46 3 49
565 3 ▶ unless ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->keysource) 0 3 3
566 0 eval { Term::ReadKey::ReadMode(0, $self->in) };
567 0 my $out = $self->out;
568 0 print {$out} "\e[?25h";
569 }
570 3 $self->_raw(0);
571 3 return;
572 }
573  
574 111 ▶sub _read_char {
Subroutine _read_char: called CC=6 SCAR=28.6
575 111 my ($self, $wait) = @_;
576 111 my $pending = $self->_pending;
577 111 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 0 111 111
578 111 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 111 0 111
579 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
580 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
581 0 select undef, undef, undef, 0.05;
582 0 return Term::ReadKey::ReadKey(-1, $self->in);
583 }
584  
585 103 ▶sub _read_key {
Subroutine _read_key: called CC=11 SCAR=26.2
586 103 my ($self) = @_;
587 103 my $char = $self->_read_char(1);
588 103 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 102 103
589 102 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 81 21 102
590 21 ▶ return $char unless $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
unless $char eq "\e" 17 4 21
591 4 my $opener = $self->_read_char(0);
592 4 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 4 4
593 4 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 0 4 4
Condition33%1/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
400
MC/DC0%0/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table33%1/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
594 0 unshift @{ $self->_pending }, $opener;
595 0 return 'escape';
596 }
597 4 my $tail = '';
598 4 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 4 0 4
599 4 my $next = $self->_read_char(0);
600 4 ▶ last unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 4 4
601 4 $tail .= $next;
602 4 ▶ last if $next =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $next =~ /[A-Za-z~]/ 4 0 4
603 }
604 4 ▶ return $SEQUENCE{$tail} || 'unknown';
Condition50%1/2
$SEQUENCE{$tail} || 'unknown'
l!l
40
MC/DC0%0/1
$SEQUENCE{$tail} || 'unknown'
$SEQUENCE{$tail}
Truth table50%1/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
605 }
606  
607 87 ▶sub pick {
Subroutine pick: called CC=14 SCAR=26.4
Pod documented
608 87 my ($self) = @_;
609 87 my $leaving = 0;
610 87 ▶ $self->_cursor(0) if $self->_cursor > $self->candidates - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cursor > $self->candidates - 1 0 87 87
611 87 while (1) {
612 101 $self->show('pick');
613 101 my $key = $self->_read_key;
614 101 ▶ 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 99 101
Condition83%5/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
2099
not defined $key or $key eq "eof"
l!l&&r!l&&!r
1199
MC/DC66%2/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table75%3/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
615 99 $self->_note(undef);
616 99 ▶ if ($key eq 'q' || $key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q" or $key eq "escape") 8 91 99
Condition66%2/3
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
8091
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
617 8 ▶ return 'quit' if $leaving;
Branch: 1 branch
BranchTrueFalseTotal
if $leaving 3 5 8
618 5 $leaving = 1;
619 5 $self->_note('Press q again to leave the game, or any other key to stay.');
620 5 next;
621 }
622 91 $leaving = 0;
623 91 ▶ if ($key eq 'enter' || $key eq 'space') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq "space") 78 13 91
Condition100%3/3
$key eq "enter" or $key eq "space"
l!l&&r!l&&!r
77113
MC/DC100%2/2
$key eq "enter" or $key eq "space"
$key eq "enter" $key eq "space"
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq "space"
ABresult
1X1
011
000
624 78 my @moves = $self->candidates;
625 78 return Game::RoyalUr::Notation::format_move($moves[ $self->_cursor ]);
626 }
627 13 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 2 11 13
628 11 ▶ return 'new' if $key eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" 1 10 11
629 10 ▶ return 'route' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 1 9 10
630 9 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 1 8 9
631 1 $self->show('help');
632 1 $self->_read_key;
633 1 next;
634 }
635 8 $self->steer($key);
636 }
637 }
638  
639 1 ▶sub _ended_key {
Subroutine _ended_key: called CC=9 SCAR=27.3
640 1 my ($self) = @_;
641 1 while (1) {
642 1 $self->show('ended');
643 1 my $key = $self->_read_key;
644 1 ▶ 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 1 1
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
001
not defined $key or $key eq "eof"
l!l&&r!l&&!r
001
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
645 1 ▶ return 'new' if $key eq 'n' || $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" or $key eq "enter" 0 1 1
Condition33%1/3
$key eq "n" or $key eq "enter"
l!l&&r!l&&!r
001
MC/DC0%0/2
$key eq "n" or $key eq "enter"
$key eq "n" $key eq "enter"
Truth table33%1/3
A or B
A= $key eq "n"B= $key eq "enter"
ABresult
1X1
011
000
646 1 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 0 1 1
647 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
648 }
649 }
650  
651 159 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
652 159 my ($self, @lines) = @_;
653 159 my $out = $self->out;
654 159 print {$out} "$_\n" for @lines;
655 159 return;
656 }
657  
658 75 ▶sub _read_line {
Subroutine _read_line: called CC=5 SCAR=16.2
659 75 my ($self) = @_;
660 75 my $game = $self->game;
661 75 my $out = $self->out;
662 75 $self->_say(@{ $self->screen('plain') });
663 75 ▶ if (!$game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->is_over) 75 0 75
664 75 my @moves = $self->candidates;
665 75 $self->_say(map { sprintf ' %s %s', $self->_paint($_ + 1, 'key'), substr($self->_readout($moves[$_]), 4) } 0 .. $#moves);
666 }
667 75 ▶ print {$out} $game->is_over ? 'new, undo or quit> ' : $game->side . ' (' . $game->roll . ')> ';
Branch: 1 branch
BranchTrueFalseTotal
$game->is_over ? : 0 75 75
668 75 my $line = readline $self->in;
669 75 ▶ print {$out} "\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 75 0 75
670 75 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 73 75
671 73 $line =~ s/\s+\z//;
672 73 $line =~ s/\A\s+//;
673 73 return $line;
674 }
675  
676 153 ▶sub command {
Subroutine command: called CC=34 SCAR=36.1
Pod documented
677 153 my ($self, $line) = @_;
678 153 my $game = $self->game;
679 153 $self->_note(undef);
680 153 ▶ return 0 unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 0 153 153
Condition33%1/3
defined $line and length $line
!ll&&!rl&&r
00153
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
681 153 my ($word, $rest) = split ' ', $line, 2;
682 153 $word = lc $word;
683  
684 153 ▶ 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" 44 109 153
Condition66%4/6
$word eq "quit" or $word eq "q" or $word eq "exit"
l!l&&r!l&&!r
440109
$word eq "quit" or $word eq "q"
l!l&&r!l&&!r
440109
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
685 109 ▶ if ($word eq 'help' || $word eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help" or $word eq "?") 1 108 109
Condition66%2/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
10108
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
686 1 $self->_say(map { " $_" } @{ $self->help_lines });
687 1 return 0;
688 }
689 108 ▶ if ($word eq 'new' || $word eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "new" or $word eq "n") 1 107 108
Condition66%2/3
$word eq "new" or $word eq "n"
l!l&&r!l&&!r
10107
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
690 1 $self->seed(Digest::SHA::sha256($self->seed . 'next'));
691 1 $self->game($self->_fresh_game);
692 1 $self->_events([]);
693 1 $self->_told(0);
694 1 $self->_cursor(0);
695 1 $self->_announce_opening;
696 1 return 0;
697 }
698 107 ▶ if ($word eq 'undo' || $word eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "undo" or $word eq "u") 4 103 107
Condition66%2/3
$word eq "undo" or $word eq "u"
l!l&&r!l&&!r
40103
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
699 4 my $taken = 0;
700 4 ▶ while ($game->undo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->undo) 3 1 4
701 3 $taken++;
702 3 ▶ last if $game->is_over || $self->_person($game->side);
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over or $self->_person($game->side) 3 0 3
Condition33%1/3
$game->is_over or $self->_person($game->side)
l!l&&r!l&&!r
030
MC/DC0%0/2
$game->is_over or $self->_person($game->side)
$game->is_over $self->_person($game->side)
Truth table33%1/3
A or B
A= $game->is_overB= $self->_person($game->side)
ABresult
1X1
011
000
703 }
704 4 $self->_told(scalar @{ $game->log });
705 4 $self->_cursor(0);
706 4 ▶ $taken ? $self->_event(undef, 'Taken back.') : $self->_note('There is nothing to take back.');
Branch: 1 branch
BranchTrueFalseTotal
$taken ? : 3 1 4
707 4 return 0;
708 }
709 103 ▶ if ($word eq 'route' || $word eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "route" or $word eq "r") 2 101 103
Condition66%2/3
$word eq "route" or $word eq "r"
l!l&&r!l&&!r
20101
MC/DC50%1/2
$word eq "route" or $word eq "r"
$word eq "route" $word eq "r"
Truth table66%2/3
A or B
A= $word eq "route"B= $word eq "r"
ABresult
1X1
011
000
710 2 ▶ $self->route($self->route ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->route ? : 1 1 2
711 2 return 0;
712 }
713 101 ▶ if ($word eq 'moves') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "moves") 3 98 101
714 3 $self->_say(map { ' ' . $said->($_) } @{ $game->log });
715 3 return 0;
716 }
717 98 ▶ if ($word eq 'record') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "record") 2 96 98
718 2 ▶ if (!defined $rest || !length $rest) { $self->_note('record wants the name of a file.') }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rest && length $rest) { } 1 1 2
elsif ($self->save($rest)) { } 1 0 1
Condition66%2/3
defined $rest && length $rest
!ll&&!rl&&r
101
MC/DC50%1/2
defined $rest && length $rest
defined $rest length $rest
Truth table66%2/3
A && B
A= defined $restB= length $rest
ABresult
0X0
100
111
719 1 elsif ($self->save($rest)) { $self->_event(undef, "The game is written to $rest.") }
720 0 else { $self->_note("I could not write '$rest'.") }
721 2 return 0;
722 }
723 96 ▶ if ($word eq 'level') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "level") 2 94 96
724 2 my @levels = Game::RoyalUr::Bot->levels($game->variant);
725 2 ▶ 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]) { } 1 1 2
Condition44%4/9
defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1 and $rest <= $levels[-1]
!ll&&!rl&&r
011
defined $rest and $rest =~ /\A\d+\z/ and $rest >= 1
!ll&&!rl&&r
002
defined $rest and $rest =~ /\A\d+\z/
!ll&&!rl&&r
002
MC/DC25%1/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 table40%2/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
726 1 $self->level($rest);
727 1 $self->_event(undef, "The program now plays at level $rest.");
728 }
729 1 else { $self->_note('A level is a number from 1 to ' . $levels[-1] . '.') }
730 2 return 0;
731 }
732 94 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 1 93 94
733 1 $self->_note('The game is over. Type new, undo or quit.');
734 1 return 0;
735 }
736  
737 93 my $move = $line;
738 93 ▶ if ($line =~ /\A[1-7]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A[1-7]\z/) 9 84 93
739 9 my @moves = $self->candidates;
740 9 ▶ if ($line > @moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > @moves) 1 8 9
741 1 $self->_note("There are only " . scalar(@moves) . " moves to choose from.");
742 1 return 0;
743 }
744 8 $move = $moves[ $line - 1 ];
745 }
746 92 ▶ if ($game->play($move)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->play($move)) { } 87 5 92
747 87 $self->_cursor(0);
748 87 $self->_tell(1);
749 }
750 else {
751 5 my $error = $game->error;
752 5 ▶ $self->_note($error->code eq 'bad_move'
Branch: 1 branch
BranchTrueFalseTotal
$error->code eq 'bad_move' ? : 2 3 5
753 ? "I do not know '$line'. A move is two places, like hand-b1 or a2-d2, or its number in the list."
754 : ucfirst($error->message) . '.');
755 }
756 92 return 0;
757 }
758  
759 2 ▶sub save {
Subroutine save: called CC=3 SCAR=11.3
Pod documented
760 2 my ($self, $file) = @_;
761 2 ▶ open my $fh, '>', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or return 0 2 0 2
762 2 print {$fh} $self->game->to_record;
763 2 ▶ return close $fh ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh ? : 2 0 2
764 }
765  
766 567 ▶sub _bot_move {
Subroutine _bot_move: called CC=3 SCAR=11.0
767 567 my ($self) = @_;
768 567 my $game = $self->game;
769 567 ▶ $self->show('wait') if $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_raw 71 496 567
770 567 my $bot = Game::RoyalUr::Bot->new(level => $self->level);
771 567 ▶ my $move = $bot->choose($game) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $bot->choose($game) or return 567 0 567
772 567 $game->play_or_die($move);
773 567 $self->_tell(1);
774 567 return;
775 }
776  
777 49 ▶sub _turns {
Subroutine _turns: called CC=12 SCAR=25.4
778 49 my ($self) = @_;
779 49 $self->_tell(1);
780 49 while (1) {
781 720 my $game = $self->game;
782 720 ▶ my $picking = $self->picking && $self->_raw;
Condition66%2/3
$self->picking && $self->_raw
!ll&&!rl&&r
5750145
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
783 720 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 5 715 720
784 5 ▶ last if $self->mode eq 'watch' && !$self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" and not $self->interactive 4 1 5
Condition66%2/3
$self->mode eq "watch" and not $self->interactive
!ll&&!rl&&r
104
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
785 1 ▶ my $line = $picking ? $self->_ended_key : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 1 0 1
786 1 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1 1
787 1 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 1 0 1
788 0 $self->_tell(1);
789 0 next;
790 }
791 715 ▶ if (!$self->_person($game->side)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_person($game->side)) 567 148 715
792 567 $self->_bot_move;
793 567 next;
794 }
795 148 ▶ my $line = $picking ? $self->pick : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 73 75 148
796 148 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 146 148
797 146 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 42 104 146
798 }
799 49 return;
800 }
801  
802 49 ▶sub start {
Subroutine start: called CC=7 SCAR=20.4
Pod documented
803 49 my ($self) = @_;
804 49 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 49 49
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
4630
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
805  
806 49 my $interrupt = $SIG{INT};
807 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
808 0 $self->_leave_raw;
809 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
810 0 kill 'INT', $$;
811 49 };
812  
813 49 my $played = eval { $self->_turns; 1 };
814 49 my $error = $@;
815 49 $self->_leave_raw;
816 49 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 49 49
817  
818 49 ▶ $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 5 44 49
Condition100%3/3
$self->mode eq "watch" or $self->game->is_over
l!l&&r!l&&!r
4144
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
819 49 ▶ $self->save($self->record) if defined $self->record;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->record 1 48 49
820 49 return 0;
821 }
822  
823 3 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
824 3 return <<'USAGE';
825 royalur - the Royal Game of Ur, in a terminal
826  
827 royalur [options]
828  
829 --mode HOW bot (the default): you against the program
830 hotseat: two people at one keyboard
831 watch: the program against itself
832 --rules NAME finkel (the default) or masters
833 --level N how well the program plays, 1 upward; the top by default
834 --side WHICH light (the default) or dark: your side against the program
835 --first WHO light, dark, or roll (the default): throw the dice for it
836 --seed HEX hexadecimal; the same seed throws the same dice
837 --pace SECONDS how long a lost turn and the program's move stay up
838 --route show the order the squares are visited in
839 --record FILE write the game to FILE when you leave
840 --replay FILE carry on from a game written by --record
841 --ascii plain letters and lines, for a terminal with no Unicode
842 --unicode line and shape characters, where they are not the default
843 --no-colour no colour; nothing is lost without it
844 --no-pick type moves instead of choosing them with the arrow keys
845 --version the version, and stop
846 --help this, and stop
847  
848 In a terminal, the pieces that can move are walked with the arrow keys and the
849 board shows what each move would leave; press ? for the keys. Anywhere else,
850 type a number from the list, a move such as hand-b1 or a2-d2, or help.
851 USAGE
852 }
853  
854 48 ▶sub run {
Subroutine run: called CC=22 SCAR=31.0
Pod documented
855 48 my ($class, @argv) = @_;
856  
857 48 my %given;
858 48 my $parsed = Getopt::Long::GetOptionsFromArray(
859 \@argv, \%given,
860 'mode=s', 'rules=s', 'level=i', 'side=s', 'first=s', 'seed=s', 'pace=s',
861 'record=s', 'replay=s', 'ascii', 'unicode!', 'route', 'colour|color!', 'pick!', 'version', 'help',
862 );
863  
864 48 ▶ if (!$parsed || @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed or @argv) 2 46 48
Condition100%3/3
not $parsed or @argv
l!l&&r!l&&!r
1146
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
865 2 ▶ print STDERR "royalur: I do not know '@argv'.\n" if $parsed && @argv;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed and @argv 1 1 2
Condition66%2/3
$parsed and @argv
!ll&&!rl&&r
101
MC/DC50%1/2
$parsed and @argv
$parsed @argv
Truth table66%2/3
A and B
A= $parsedB= @argv
ABresult
0X0
100
111
866 2 print STDERR _usage();
867 2 return 2;
868 }
869 46 ▶ if ($given{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"help"}) 1 45 46
870 1 print _usage();
871 1 return 0;
872 }
873 45 ▶ if ($given{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"version"}) 1 44 45
874 1 print "royalur $Game::RoyalUr::VERSION\n";
875 1 return 0;
876 }
877  
878 44 my %with;
879 44 $with{$_} = $given{$_} for grep { defined $given{$_} } qw(mode rules level side first pace record route);
880 44 ▶ $with{colour} = $given{colour} if defined $given{colour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"colour"} 3 41 44
881 44 ▶ $with{picking} = $given{pick} if defined $given{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"pick"} 1 43 44
882 44 ▶ $with{unicode} = $given{unicode} if defined $given{unicode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"unicode"} 2 42 44
883 44 ▶ $with{unicode} = 0 if $given{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"ascii"} 1 43 44
884  
885 44 ▶ if (defined $given{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"seed"}) 35 9 44
886 35 ▶ 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 33 35
887 2 print STDERR "royalur: a seed is hexadecimal, two characters a byte.\n";
888 2 return 2;
889 }
890 33 $with{seed} = pack 'H*', $given{seed};
891 }
892  
893 42 ▶ if (defined $given{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"replay"}) 3 39 42
894 3 ▶ open my $fh, '<', $given{replay} or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $given{"replay"} or do { print STDERR qq[royalur: I could not read '$given{"replay"}'.\n]; return 2 } 2 1 3
895 1 print STDERR "royalur: I could not read '$given{replay}'.\n";
896 1 return 2;
897 };
898 2 my $text = do { local $/; <$fh> };
899 2 close $fh;
900 2 my ($game, $error) = Game::RoyalUr->replay($text);
901 2 ▶ if (!$game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 1 2
902 1 my $detail = $error->detail;
903 print STDERR "royalur: '$given{replay}' is not a game I can replay: ", $error->message,
904 1 ▶ (defined $detail->{line} ? " (line $detail->{line})" : defined $detail->{ply} ? ' (turn ' . ($detail->{ply} + 1) . ')' : ''), ".\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $detail->{'line'} ? : 0 1 1
defined $detail->{'ply'} ? : 1 0 1
905 1 return 2;
906 }
907 1 $with{game} = $game;
908 1 $with{rules} = $game->variant;
909 1 ▶ $with{seed} = $game->seed if defined $game->seed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $game->seed 1 0 1
910 }
911  
912 40 my $terminal = eval { $class->new(%with) };
913 40 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 7 33 40
914 7 (my $why = $@) =~ s/ at \S+ line \d+\.?\s*\z//;
915 7 $why =~ s/\AGame::RoyalUr(?:::\w+)*: //;
916 7 print STDERR "royalur: $why.\n";
917 7 return 2;
918 }
919  
920 33 local @SIG{qw(INT TERM HUP)};
921 33 for my $sig (qw(INT TERM HUP)) {
922 99 ▶ $SIG{$sig} = sub { $terminal->_leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
923 }
924  
925 33 return $terminal->start;
926 }
927  
928 51 1;
929  
930 __END__
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/lib/Game/RoyalUr.pm »