blib/lib/Game/RoyalUr/Terminal.pm

filter: Statementstmt 99.0% 708 / 715 Branchbran 89.7% 393 / 438 Conditioncond 70.9% 117 / 165 MC/DCmcdc 54.0% 54 / 100 Subroutinesub 100% 88 / 88 Podpod 95.4% 21 / 22 totaltotal 90.3% 1381 / 1528 CC 288 SCAR 26.0
CC
288
Cov
92%
command
35.1
BUILD
34.6
run
26.0
CRAP 13.5
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/lib/Game/RoyalUr.pm »
1 package Game::RoyalUr::Terminal;
2  
3 73 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 73 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 73 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 73 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 73 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 73 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
10 73 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 73 ▶use Game::Terminal::Kit::History;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 73 ▶use Game::Terminal::Kit::Keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 73 ▶use Game::Terminal::Kit::Replay;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 73 ▶use Game::Terminal::Kit::Run;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 73 ▶use Game::Terminal::Kit::Save;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 73 ▶use Game::Terminal::Kit::Screen;
Subroutine __ANON__: called CC=1 SCAR=0.0
18 73 ▶use Game::Terminal::Kit::Steer;
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 73 ▶use Game::RoyalUr;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 73 ▶use Game::RoyalUr::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 73 ▶use Game::RoyalUr::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
23 73 ▶use Game::RoyalUr::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 73 our $VERSION = '0.04';
26  
27 73 my (%STYLE, %GLYPH, %OTHER, @FILES);
28 73 ▶BEGIN {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 73 %STYLE = (
30 title => '1;38;5;220',
31 grid => '38;5;241',
32 label => '38;5;245',
33 light => '1;38;5;229',
34 dark => '1;38;5;39',
35 rosette => '1;38;5;205',
36 cursor => '1;38;5;16;48;5;220',
37 landing => '1;38;5;16;48;5;114',
38 capture => '1;38;5;231;48;5;160',
39 marked => '1;38;5;220',
40 unmarked => '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 step => '38;5;109',
46 );
47  
48 73 %GLYPH = (
49 wide => {
50 light => "\x{25CF}", dark => "\x{25CB}", rosette => "\x{2726}",
51 marked => "\x{25B2}", unmarked => "\x{25B3}",
52 h => "\x{2500}", v => "\x{2502}",
53 tl => "\x{256D}", tr => "\x{256E}", bl => "\x{2570}", br => "\x{256F}",
54 td => "\x{252C}", tu => "\x{2534}", lt => "\x{251C}", rt => "\x{2524}", x => "\x{253C}",
55 },
56 ascii => {
57 light => 'O', dark => '@', rosette => '*',
58 marked => '^', unmarked => '.',
59 h => '-', v => '|',
60 tl => '+', tr => '+', bl => '+', br => '+',
61 td => '+', tu => '+', lt => '+', rt => '+', x => '+',
62 },
63 );
64  
65 73 %OTHER = (light => 'dark', dark => 'light');
66 73 @FILES = ('a' .. 'h');
67 }
68  
69 73 has game => (is => 'rw');
70  
71 73 has [qw/in out/] => (is => 'rw');
72  
73 73 has [qw/interactive colour unicode picking/] => (is => 'rw');
74  
75 73 has mode => (is => 'rw', default => 'bot');
76  
77 73 has side => (is => 'rw', default => 'light');
78  
79 73 has level => (is => 'rw');
80  
81 73 has rules => (is => 'rw', default => 'finkel');
82  
83 73 has first => (is => 'rw', default => 'roll');
84  
85 73 has seed => (is => 'rw');
86  
87 73 has pace => (is => 'rw');
88  
89 73 has route => (is => 'rw', default => 0);
90  
91 73 has save_file => (is => 'rw');
92  
93 73 has record => (is => 'rw');
94  
95 73 has keysource => (is => 'rw');
96  
97 73 has history => (is => 'rw');
98  
99 73 has sleeper => (is => 'rw');
100  
101 73 has [qw/_keyboard _screen/] => (is => 'rw');
102  
103 73 has _events => (is => 'rw', private => 1);
104  
105 73 has _told => (is => 'rw', private => 1);
106  
107 73 has _cursor => (is => 'rw', private => 1);
108  
109 73 has _note => (is => 'rw', private => 1);
110  
111 157 ▶sub BUILD {
Subroutine BUILD: called CC=33 SCAR=35.4
Pod undocumented
112 157 my ($self) = @_;
113 157 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 54 103 157
114 157 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 54 103 157
115 157 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 54 103 157
-t $self->in ? : 0 54 54
116 157 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0) unless defined $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 58 99 157
$self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? : 0 58 58
Condition33%1/3
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
5800
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
117 157 ▶ $self->unicode($self->interactive ? 1 : 0) unless defined $self->unicode;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->unicode 58 99 157
$self->interactive ? : 0 58 58
118 157 ▶ $self->_keyboard(Game::Terminal::Kit::Keys->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->keysource ? : 58 99 157
119 in => $self->in,
120 ($self->keysource ? (keysource => $self->keysource) : ()),
121 ));
122 157 ▶ $self->_screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->unicode ? : 2 155 157
123 out => $self->out,
124 interactive => $self->interactive,
125 colour => $self->colour,
126 ascii => $self->unicode ? 0 : 1,
127 ));
128 157 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 60 97 157
$self->interactive ? : 0 60 60
129 157 ▶ $self->picking(0) unless $self->_keyboard->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->keys_available 99 58 157
130 157 ▶ $self->pace($self->interactive && !$self->keysource ? 1 : 0) unless defined $self->pace;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->pace 57 100 157
$self->interactive && !$self->keysource ? : 0 57 57
Condition33%1/3
$self->interactive and not $self->keysource
!ll&&!rl&&r
5700
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
131  
132 157 ▶ 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 155 157
133 unless $self->mode =~ /\A(?:bot|hotseat|watch)\z/;
134 155 ▶ 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 153 155
135 unless $self->side =~ /\A(?:light|dark)\z/;
136 153 ▶ 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 151 153
137 unless $self->first =~ /\A(?:light|dark|roll)\z/;
138 151 ▶ 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 148 151
139 unless $self->pace =~ /\A\d+(?:\.\d+)?\z/;
140  
141 148 my $variant = Game::RoyalUr::Variant->of($self->rules);
142 146 my @levels = Game::RoyalUr::Bot->levels($variant);
143 146 ▶ $self->level($levels[-1]) unless defined $self->level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->level 113 33 146
144 146 ▶ 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 143 146
Condition83%5/6
$self->level =~ /\A\d+\z/ and $self->level >= 1 and $self->level <= $levels[-1]
!ll&&!rl&&r
12143
$self->level =~ /\A\d+\z/ and $self->level >= 1
!ll&&!rl&&r
01145
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
145 unless $self->level =~ /\A\d+\z/ && $self->level >= 1 && $self->level <= $levels[-1];
146  
147 143 ▶ $self->seed(Digest::SHA::sha256(join '|', 'royalur', $$, time, rand)) unless defined $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed 15 128 143
148 143 $self->_events([]);
149 143 $self->_told(0);
150 143 $self->_cursor(0);
151 143 ▶ if ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game) { } 59 84 143
152 59 $self->_told(scalar @{ $self->game->log });
153 }
154 else {
155 84 $self->game($self->_fresh_game);
156 84 $self->_announce_opening;
157 }
158 143 ▶ $self->history($self->_new_history) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 136 7 143
159 143 ▶ $self->history->name('royalur') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 143 143
160 143 $self->history->version($VERSION);
161 143 $self->history->start($self->game);
162 143 ▶ $self->save_file($self->record) if defined $self->record && !defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->record and not defined $self->save_file 1 142 143
Condition66%2/3
defined $self->record and not defined $self->save_file
!ll&&!rl&&r
14201
MC/DC50%1/2
defined $self->record and not defined $self->save_file
defined $self->record not defined $self->save_file
Truth table66%2/3
A and not(B)
A= defined $self->recordB= not defined $self->save_file
ABresult
0X0
100
111
163 143 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 15 128 143
164  
165 143 $self->_screen->utf8->flush_on;
166 143 return;
167 }
168  
169 142 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
170 return Game::Terminal::Kit::History->new(
171 name => 'royalur',
172 version => $VERSION,
173 503 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
174 28 ▶ load => sub { Game::RoyalUr->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 142 );
176 }
177  
178 85 ▶sub _fresh_game {
Subroutine _fresh_game: called CC=2 SCAR=6.9
179 85 my ($self) = @_;
180 85 ▶ return Game::RoyalUr->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->first eq 'roll' ? : 3 82 85
181 seed => $self->seed,
182 rules => $self->rules,
183 ($self->first eq 'roll' ? () : (first => $self->first)),
184 );
185 }
186  
187 85 ▶sub _announce_opening {
Subroutine _announce_opening: called CC=4 SCAR=13.9
188 85 my ($self) = @_;
189 85 my $game = $self->game;
190 85 my @throws = @{ $game->opening };
191 85 ▶ return unless @throws;
Branch: 1 branch
BranchTrueFalseTotal
unless @throws 82 3 85
192 3 ▶ while (@throws) {
Branch: 1 branch
BranchTrueFalseTotal
if (@throws) 6 3 9
193 6 my ($light, $dark) = splice @throws, 0, 2;
194 6 my ($l, $d) = (scalar(grep { $_ } @$light), scalar(grep { $_ } @$dark));
195 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
196 $self->_dice($light), $l, $self->_dice($dark), $d, $l == $d ? ': a tie, again.' : '.');
197 }
198 3 $self->_event($game->first, ucfirst($game->first) . ' moves first.');
199 3 return;
200 }
201  
202 36637 ▶sub _paint {
Subroutine _paint: called CC=2 SCAR=7.1
203 36637 my ($self, $text, $style) = @_;
204 36637 ▶ return $text unless defined $style;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $style 0 36637 36637
205 36637 return $self->_screen->paint($text, $STYLE{$style});
206 }
207  
208 2048 ▶sub _raw { $_[0]->_keyboard->raw }
Subroutine _raw: called CC=1 SCAR=0.0
209  
210 43303 ▶sub _glyph { $GLYPH{ $_[0]->unicode ? 'wide' : 'ascii' }{ $_[1] } }
Subroutine _glyph: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->unicode ? : 112 43191 43303
211  
212 740 ▶sub _dice {
Subroutine _dice: called CC=3 SCAR=11.0
213 740 my ($self, $faces) = @_;
214 740 ▶ return join ' ', map { $self->_paint($self->_glyph($_ ? 'marked' : 'unmarked'), $_ ? 'marked' : 'unmarked') } @$faces;
Branch: 2 branches
BranchTrueFalseTotal
$_ ? : 1332 1492 2824
$_ ? : 1332 1492 2824
215 }
216  
217 1132 ▶sub _event {
Subroutine _event: called CC=1 SCAR=0.0
218 1132 my ($self, $side, $text) = @_;
219 1132 push @{ $self->_events }, { side => $side, text => $text };
220 1132 return;
221 }
222  
223 8 ▶sub events { [ map { $_->{text} } @{ $_[0]->_events } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
224  
225 2046 ▶sub _person {
Subroutine _person: called CC=4 SCAR=13.9
226 2046 my ($self, $side) = @_;
227 2046 ▶ return 0 if $self->mode eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" 1218 828 2046
228 828 ▶ return 1 if $self->mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "hotseat" 150 678 828
229 678 ▶ return $side eq $self->side ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 342 336 678
230 }
231  
232 1829 ▶sub _who {
Subroutine _who: called CC=3 SCAR=11.0
233 1829 my ($self, $side) = @_;
234 1829 ▶ return ucfirst $side if $self->mode ne 'bot';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode ne "bot" 925 904 1829
235 904 ▶ return $side eq $self->side ? 'You' : ucfirst $side;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->side ? : 503 401 904
236 }
237  
238 989 ▶my $said = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
239 989 my ($entry) = @_;
240 989 ▶ return Game::RoyalUr::Notation::format_forfeit($entry->{roll}) unless defined $entry->{move};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"move"} 8 981 989
241 981 my ($from, $to) = split /-/, $entry->{move};
242 981 return Game::RoyalUr::Notation::format_display({ from => $from, to => $to, %$entry });
243 73 };
244  
245 1052 ▶sub _tell {
Subroutine _tell: called CC=6 SCAR=17.9
246 1052 my ($self, $pause) = @_;
247 1052 my $game = $self->game;
248 1052 my $log = $game->log;
249 1052 my $told = $self->_told;
250 1052 ▶ while ($told < @$log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($told < @$log) 1083 1052 2135
251 1083 my $entry = $log->[ $told++ ];
252 1083 $self->_event($entry->{side}, $self->_describe($entry));
253 1083 $self->_told($told);
254 1083 ▶ my $slow = !defined $entry->{move} || !$self->_person($entry->{side});
Condition100%3/3
defined $entry->{'move'} && $self->_person($entry->{'side'})
!ll&&!rl&&r
116777190
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
255 1083 ▶ if ($pause && $slow && $self->_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pause and $slow and $self->_raw) 192 891 1083
Condition83%5/6
$pause and $slow and $self->_raw
!ll&&!rl&&r
190701192
$pause and $slow
!ll&&!rl&&r
0190893
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
256 192 $self->show('wait');
257 192 $self->_wait;
258 }
259 }
260 1052 $self->_told($told);
261 1052 return;
262 }
263  
264 1101 ▶sub _describe {
Subroutine _describe: called CC=6 SCAR=17.9
265 1101 my ($self, $entry) = @_;
266 1101 my $who = $self->_who($entry->{side});
267 1101 ▶ if (!defined $entry->{move}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry->{"move"}) 126 975 1101
268 126 ▶ return $entry->{roll} == 0
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'roll'} == 0 ? : 118 8 126
269 ? "$who rolled nothing and lost the turn."
270 : "$who rolled $entry->{roll} and could not move: the turn is lost.";
271 }
272 975 my $text = "$who played " . $said->($entry);
273 975 ▶ $text .= ', capturing' if $entry->{captures};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"captures"} 96 879 975
274 $text .= $entry->{home} ? ', home.'
275 975 ▶ : $entry->{rosette} ? ', onto a rosette: another roll.' : '.';
Branch: 2 branches
BranchTrueFalseTotal
$entry->{'home'} ? : 75 900 975
$entry->{'rosette'} ? : 193 707 900
276 975 return $text;
277 }
278  
279 192 ▶sub _wait {
Subroutine _wait: called CC=3 SCAR=11.8
280 192 my ($self) = @_;
281 192 my $pace = $self->pace;
282 192 ▶ return unless $pace > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pace > 0 107 85 192
283 85 ▶ return $self->sleeper->($pace) if $self->sleeper;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sleeper 85 0 85
284 0 select undef, undef, undef, $pace;
285 0 return;
286 }
287  
288 112299 ▶my $coordinates = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
289 112299 my ($place, $side) = @_;
290 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
291 93372 return (ord(substr $place, 0, 1) - ord('a'), substr($place, 1, 1) - 1);
292 73 };
293  
294 37084 ▶sub candidates {
Subroutine candidates: called CC=1 SCAR=0.0
Pod documented
295 37084 my ($self) = @_;
296 37084 return $self->game->legal;
297 }
298  
299 36311 ▶sub steer {
Subroutine steer: called CC=11 SCAR=24.0
Pod documented
300 36311 my ($self, $key) = @_;
301 36311 my @moves = $self->candidates;
302 36311 ▶ return $self->_cursor unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 0 36311 36311
303 36311 my $at = $self->_cursor;
304 36311 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 52 36259 36311
305  
306 36311 ▶ if ($key eq 'tab') { $at = Game::Terminal::Kit::Steer::ring(scalar @moves, $at, 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "tab") 7 36304 36311
307 36311 ▶ 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") 2 36309 36311
Condition100%3/3
$key eq "back_tab" or $key eq "backtab"
l!l&&r!l&&!r
1136309
MC/DC100%2/2
$key eq "back_tab" or $key eq "backtab"
$key eq "back_tab" $key eq "backtab"
Truth table100%3/3
A or B
A= $key eq "back_tab"B= $key eq "backtab"
ABresult
1X1
011
000
308 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
309  
310 36311 ▶ if (my ($dx, $dy) = Game::Terminal::Kit::Steer::way($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = Game::Terminal::Kit::Steer::way($key)) 34159 2152 36311
311 34159 my $side = $self->game->side;
312 34159 my @points = map { [ $coordinates->($_->from, $side) ] } @moves;
313 34159 ▶ if ($dy) { $_->[1] = -$_->[1] for @points }
Branch: 1 branch
BranchTrueFalseTotal
if ($dy) 17078 17081 34159
314 34159 $at = Game::Terminal::Kit::Steer::axis(\@points, $at, $dx, $dy);
315 }
316 36311 $self->_cursor($at);
317 36311 return $at;
318 }
319  
320 778 ▶my $cells_after = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
321 778 my ($game, $move) = @_;
322 778 my (%cell, %hand, %home, %mark);
323 778 for my $file (@FILES) {
324 6224 for my $row (1 .. 3) {
325 18672 my $name = "$file$row";
326 18672 ▶ next unless Game::RoyalUr::Notation::square_ok($name);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::RoyalUr::Notation::square_ok($name) 3112 15560 18672
327 15560 $cell{$name} = $game->at($name);
328 }
329 }
330 778 for my $side (qw(light dark)) {
331 1556 $hand{$side} = $game->hand($side);
332 1556 $home{$side} = $game->home($side);
333 }
334 778 ▶ return (\%cell, \%hand, \%home, \%mark) unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 525 253 778
335  
336 253 my $side = $move->side;
337 253 ▶ if ($move->from eq 'hand') { $hand{$side}--; $mark{"hand $side"} = 'cursor' }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->from eq 'hand') { } 116 137 253
338 137 else { $cell{ $move->from } = undef; $mark{ $move->from } = 'cursor' }
339 253 ▶ if ($move->captures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->captures) 21 232 253
340 21 $hand{ $OTHER{$side} }++;
341 21 $mark{"hand $OTHER{$side}"} = 'capture';
342 }
343 253 ▶ if ($move->home) { $home{$side}++; $mark{"home $side"} = 'landing' }
Branch: 1 branch
BranchTrueFalseTotal
if ($move->home) { } 2 251 253
344 251 else { $cell{ $move->to } = $side; $mark{ $move->to } = 'landing' }
345 253 return (\%cell, \%hand, \%home, \%mark);
346 73 };
347  
348 73 my %BRACKET;
349 73 ▶BEGIN { %BRACKET = (cursor => [ '[', ']' ], landing => [ '(', ')' ], capture => [ '<', '>' ], last => [ '_', '_' ]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
350  
351 15699 ▶sub _framed {
Subroutine _framed: called CC=6 SCAR=17.9
352 15699 my ($self, $what, $mark) = @_;
353 15699 ▶ my ($open, $close) = $mark ? @{ $BRACKET{$mark} } : (' ', ' ');
Branch: 1 branch
BranchTrueFalseTotal
$mark ? : 1277 14422 15699
354 15699 ▶ my $inside = defined $what ? $self->_glyph($what) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 3624 12075 15699
355 15699 ▶ return $self->_paint($open . $inside . $close, $mark) if $mark && $mark ne 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $mark and $mark ne "last" 527 15172 15699
Condition100%3/3
$mark and $mark ne "last"
!ll&&!rl&&r
14422750527
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
356 15172 ▶ return $open . (defined $what ? $self->_paint($inside, $what) : $inside) . $close;
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 3350 11822 15172
357 }
358  
359 2334 ▶sub _row {
Subroutine _row: called CC=5 SCAR=16.1
360 2334 my ($self, $row, $cell, $mark) = @_;
361 2334 my $v = $self->_paint($self->_glyph('v'), 'grid');
362 2334 my @parts;
363 2334 for my $file (@FILES) {
364 18672 my $name = "$file$row";
365 18672 ▶ if (!Game::RoyalUr::Notation::square_ok($name)) { push @parts, undef; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (Game::RoyalUr::Notation::square_ok($name)) 3112 15560 18672
366 15560 ▶ 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/ ? : 3890 11670 15560
367 15560 push @parts, $rosette . $self->_framed($cell->{$name}, $mark->{$name}) . ' ';
368 }
369 2334 my $line = $self->_paint($row, 'label') . ' ' . $v;
370 2334 ▶ if (defined $parts[4]) { $line .= join($v, @parts) . $v }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parts[4]) { } 778 1556 2334
371 1556 else { $line .= join($v, @parts[0 .. 3]) . $v . (' ' x 11) . $v . join($v, @parts[6, 7]) . $v }
372 2334 return ' ' . $line;
373 }
374  
375 3112 ▶sub _border {
Subroutine _border: called CC=4 SCAR=13.9
376 3112 my ($self, $kind) = @_;
377 3112 ▶ my $g = sub { $self->_glyph($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
378 3112 my $bar = $g->('h') x 5;
379 3112 ▶ 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
380 3112 ▶ my $line = $kind eq 'top' ? $block->('tl', 'td', 'tr', 4) . (' ' x 11) . $block->('tl', 'td', 'tr', 2)
Branch: 3 branches
BranchTrueFalseTotal
$kind eq 'top' ? : 778 2334 3112
$kind eq 'bottom' ? : 778 1556 2334
$kind eq 'upper' ? : 778 778 1556
381 : $kind eq 'bottom' ? $block->('bl', 'tu', 'br', 4) . (' ' x 11) . $block->('bl', 'tu', 'br', 2)
382 : $g->('lt') . join($g->('x'), ($bar) x 4) . $g->('x') . $bar . $g->($kind eq 'upper' ? 'td' : 'tu') . $bar
383 . $g->('x') . join($g->('x'), ($bar) x 2) . $g->('rt');
384 3112 return ' ' . $self->_paint($line, 'grid');
385 }
386  
387 1556 ▶sub _tray {
Subroutine _tray: called CC=1 SCAR=0.0
388 1556 my ($self, $side, $hand, $home, $mark) = @_;
389 3112 ▶ my $pieces = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
390 3112 my ($count, $which) = @_;
391 3112 my @shown = map { $self->_paint($self->_glyph($side), $side) } 1 .. $count;
392 3112 my $m = $mark->{"$which $side"};
393 3112 ▶ if ($m && $m eq 'cursor') { push @shown, $self->_framed(undef, 'cursor') }
Branch: 2 branches
BranchTrueFalseTotal
if ($m and $m eq 'cursor') { } 116 2996 3112
elsif ($m and @shown) { } 23 5946 5969
Condition100%3/3
$m and $m eq "cursor"
!ll&&!rl&&r
297323116
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
394 23 ▶ elsif ($m && @shown) { $shown[-1] = $self->_framed($side, $m) }
Condition66%2/3
$m and @shown
!ll&&!rl&&r
2973023
MC/DC50%1/2
$m and @shown
$m @shown
Truth table66%2/3
A and B
A= $mB= @shown
ABresult
0X0
100
111
395 3112 ▶ return @shown ? join(' ', @shown) : $self->_paint('none', 'quiet');
Branch: 1 branch
BranchTrueFalseTotal
@shown ? : 1871 1241 3112
396 1556 };
397 return sprintf ' %s hand %s home %s',
398 1556 $self->_paint(sprintf('%-5s', $side), $side), $pieces->($hand->{$side}, 'hand'), $pieces->($home->{$side}, 'home');
399 }
400  
401 778 ▶sub board_lines {
Subroutine board_lines: called CC=9 SCAR=22.0
Pod documented
402 778 my ($self, $move) = @_;
403 778 my $game = $self->game;
404 778 my ($cell, $hand, $home, $mark) = $cells_after->($game, $move);
405 778 ▶ if (!$move) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 525 253 778
406 525 my %latest;
407 525 for my $entry (@{ $game->log }) {
408 24066 ▶ $latest{ $entry->{side} } = $entry if defined $entry->{move};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"move"} 22242 1824 24066
409 }
410 525 for my $entry (values %latest) {
411 815 my (undef, $to) = split /-/, $entry->{move};
412 815 ▶ $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"} 750 65 815
Condition83%5/6
$to ne "home" and defined $cell->{$to} and $cell->{$to} eq $entry->{"side"}
!ll&&!rl&&r
4718750
$to ne "home" and defined $cell->{$to}
!ll&&!rl&&r
470768
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
413 }
414 }
415 778 my @lines = (
416 $self->_tray('dark', $hand, $home, $mark),
417 ' ' . $self->_paint(join(' ', @FILES), 'label'),
418 $self->_border('top'),
419 $self->_row(3, $cell, $mark),
420 $self->_border('upper'),
421 $self->_row(2, $cell, $mark),
422 $self->_border('lower'),
423 $self->_row(1, $cell, $mark),
424 $self->_border('bottom'),
425 $self->_tray('light', $hand, $home, $mark),
426 );
427 778 ▶ push @lines, '', @{ $self->_route_lines } if $self->route;
Branch: 1 branch
BranchTrueFalseTotal
if $self->route 3 775 778
428 778 return \@lines;
429 }
430  
431 3 ▶sub _route_lines {
Subroutine _route_lines: called CC=9 SCAR=22.1
432 3 my ($self) = @_;
433 3 my $game = $self->game;
434 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
435 3 my $variant = $game->variant;
436 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
437 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
438 3 my @lines = (' ' . $self->_paint('The ' . $variant->route . " route, as $side travels it:", 'quiet'));
439 3 for my $row (reverse 0 .. 2) {
440 9 my $line = ' ' . $self->_paint($row + 1, 'label') . ' ';
441 9 for my $file (0 .. 7) {
442 72 my $cell = Game::RoyalUr::Engine->cell_of($file, $row);
443 72 ▶ my $step = $cell < 0 ? 0 : Game::RoyalUr::Engine->route_step($route, $index, $cell);
Branch: 1 branch
BranchTrueFalseTotal
$cell < 0 ? : 12 60 72
444 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
445 }
446 9 push @lines, $line;
447 }
448 3 return \@lines;
449 }
450  
451 743 ▶sub _roll_line {
Subroutine _roll_line: called CC=5 SCAR=16.1
452 743 my ($self) = @_;
453 743 my $game = $self->game;
454 743 ▶ return () if $game->is_over || !defined $game->roll;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over or not defined $game->roll 15 728 743
Condition66%2/3
$game->is_over or not defined $game->roll
l!l&&r!l&&!r
150728
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
455 728 my $throw = $game->throw;
456 728 my $roll = $game->roll;
457 728 my $marked = grep { $_ } @$throw;
458 728 my $text = sprintf ' %s rolled %s %s', $self->_who($game->side), $self->_paint($roll, 'key'), $self->_dice($throw);
459 728 ▶ $text .= $self->_paint(' (nothing marked is worth four)', 'quiet') if !$marked && $roll;
Branch: 1 branch
BranchTrueFalseTotal
if not $marked and $roll 34 694 728
Condition66%2/3
not $marked and $roll
!ll&&!rl&&r
694034
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
460 728 return $text;
461 }
462  
463 743 ▶sub _result_line {
Subroutine _result_line: called CC=4 SCAR=13.9
464 743 my ($self) = @_;
465 743 ▶ my $result = $self->game->result or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->game->result or return () 15 728 743
466 15 my $home = $result->home;
467 15 ▶ 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 15 15
468 15 my $winner = $result->winner;
469 15 ▶ return ' ' . $self->_paint(sprintf('%s won: %s resigned.', ucfirst $winner, $result->loser), 'good')
Branch: 1 branch
BranchTrueFalseTotal
if $result->how eq "resign" 1 14 15
470 if $result->how eq 'resign';
471 return ' ' . $self->_paint(sprintf('%s won, all %d home to %d, in %d plies.',
472 14 ucfirst $winner, $home->{$winner}, $home->{ $result->loser }, $result->plies), 'good');
473 }
474  
475 736 ▶sub _event_lines {
Subroutine _event_lines: called CC=3 SCAR=11.0
476 736 my ($self) = @_;
477 736 my @recent = @{ $self->_events };
478 736 ▶ @recent = @recent[ -4 .. -1 ] if @recent > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @recent > 4 502 234 736
479 736 ▶ return map { ' ' . $self->_paint($_->{text}, $_->{side} || 'quiet') } @recent;
Condition100%2/2
$_->{'side'} || 'quiet'
l!l
2181103
MC/DC100%1/1
$_->{'side'} || 'quiet'
$_->{'side'}
Truth table100%2/2
A
A= $_->{'side'}
Aresult
11
00
480 }
481  
482 377 ▶sub _readout {
Subroutine _readout: called CC=6 SCAR=17.9
483 377 my ($self, $move) = @_;
484 377 my $text = Game::RoyalUr::Notation::format_display($move);
485 377 my @does;
486 377 ▶ push @does, 'enters the board' if $move->from eq 'hand';
Branch: 1 branch
BranchTrueFalseTotal
if $move->from eq "hand" 228 149 377
487 377 ▶ push @does, 'captures' if $move->captures;
Branch: 1 branch
BranchTrueFalseTotal
if $move->captures 16 361 377
488 377 ▶ push @does, 'lands on a rosette: roll again' if $move->rosette;
Branch: 1 branch
BranchTrueFalseTotal
if $move->rosette 27 350 377
489 377 ▶ push @does, 'comes home' if $move->home;
Branch: 1 branch
BranchTrueFalseTotal
if $move->home 0 377 377
490 377 ▶ return ' ' . $self->_paint($text, 'key') . (@does ? ' ' . join(', ', @does) : '');
Branch: 1 branch
BranchTrueFalseTotal
@does ? : 267 110 377
491 }
492  
493 591 ▶sub _legend {
Subroutine _legend: called CC=4 SCAR=13.9
494 591 my ($self, $mode) = @_;
495 591 ▶ my $k = sub { $self->_paint($_[0], 'key') };
Subroutine __ANON__: called CC=1 SCAR=0.0
496 591 ▶ return ' ' . join(' ', $k->('n') . ' new game', $k->('u') . ' take back',
Branch: 2 branches
BranchTrueFalseTotal
if $mode eq "ended" 4 587 591
$self->history->can_redo ? : 0 4 4
497 ($self->history->can_redo ? $k->('r') . ' play again' : ()), $k->('q') . ' leave', $k->('S') . ' save') if $mode eq 'ended';
498 587 ▶ return ' ' . $self->_paint('...', 'quiet') if $mode eq 'wait';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "wait" 356 231 587
499 231 return (' ' . join(' ', $k->('arrows') . ' choose a piece', $k->('enter') . ' move it',
500 $k->('u') . ' take back', $k->('r') . ' play again'),
501 ' ' . join(' ', $k->('R') . ' route', $k->('S') . ' save', $k->('?') . ' help', $k->('q') . ' leave'));
502 }
503  
504 737 ▶sub screen {
Subroutine screen: called CC=14 SCAR=26.4
Pod documented
505 737 my ($self, $mode) = @_;
506 737 ▶ $mode = 'plain' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 737 737
507 737 my $game = $self->game;
508 737 my $variant = $game->variant;
509 737 ▶ my @lines = (
Branch: 3 branches
BranchTrueFalseTotal
defined $variant->name ? : 735 2 737
$self->mode eq 'bot' ? : 559 178 737
$self->mode eq 'hotseat' ? : 173 5 178
510 ' ' . $self->_paint('The Royal Game of Ur', 'title') . ' '
511 . $self->_paint(join(', ', (defined $variant->name ? $variant->name : $variant->describe),
512 ($self->mode eq 'bot' ? 'you are ' . $self->side . ', against level ' . $self->level
513 : $self->mode eq 'hotseat' ? 'two players' : 'level ' . $self->level . ' against itself')), 'quiet'),
514 '',
515 );
516 737 ▶ if ($mode eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "help") 1 736 737
517 1 push @lines, map { " $_" } @{ $self->help_lines };
518 1 return \@lines;
519 }
520 736 ▶ my @moves = $mode eq 'pick' ? $self->candidates : ();
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'pick' ? : 231 505 736
521 736 my $at = $self->_cursor;
522 736 ▶ $at = $#moves if $at > $#moves;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $#moves 505 231 736
523 736 ▶ my $move = @moves ? $moves[$at] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@moves ? : 231 505 736
524  
525 736 push @lines, @{ $self->board_lines($move) }, '';
526 736 push @lines, $self->_roll_line, $self->_result_line;
527 736 ▶ push @lines, $self->_readout($move) if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 231 505 736
528 736 push @lines, '';
529 736 push @lines, $self->_event_lines;
530 736 ▶ push @lines, ' ' . $self->_paint($self->_note, 'bad') if defined $self->_note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_note 30 706 736
531 736 ▶ 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" 591 145 736
Condition100%6/6
$mode eq "pick" or $mode eq "ended" or $mode eq "wait"
l!l&&r!l&&!r
235356145
$mode eq "pick" or $mode eq "ended"
l!l&&r!l&&!r
2314501
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
532 736 return \@lines;
533 }
534  
535 589 ▶sub show {
Subroutine show: called CC=1 SCAR=0.0
Pod documented
536 589 my ($self, $mode) = @_;
537 589 $self->_screen->show($self->screen($mode));
538 589 return $self;
539 }
540  
541 4 ▶sub help_lines {
Subroutine help_lines: called CC=1 SCAR=0.0
Pod documented
542 return [
543 4 'A move is a piece. The roll says how far it goes, so choosing the piece is',
544 'the whole move.',
545 '',
546 ' arrows, tab walk the pieces that can move',
547 ' 1 to 7 go straight to one of them',
548 ' enter, space move it',
549 ' u take back your last turn and every reply to it',
550 ' r play a turn taken back again',
551 ' R show or hide the route',
552 ' S save the game, to carry on with --resume or watch with --replay',
553 ' n a new game',
554 ' q leave (asked twice)',
555 '',
556 'The board shows what the move would leave: [ ] where the piece was, ( ) where',
557 'it lands, and < > round a piece it sends back to its owner\'s hand.',
558 '',
559 'Typing instead: a number from the list, or a move such as hand-b1 or a2-d2;',
560 'and undo, redo, new, route, moves, save [FILE], level N, help, quit.',
561 ];
562 }
563  
564 15 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=4 SCAR=14.6
565 15 my ($self) = @_;
566 15 ▶ return 1 if $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_raw 0 15 15
567 15 ▶ return 0 unless $self->_keyboard->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keyboard->enter_raw 0 15 15
568 15 ▶ $self->_screen->hide_cursor->clear unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 15 15
569 15 return 1;
570 }
571  
572 202 ▶sub pick {
Subroutine pick: called CC=16 SCAR=27.7
Pod documented
573 202 my ($self) = @_;
574 202 my $leaving = 0;
575 202 ▶ $self->_cursor(0) if $self->_cursor > $self->candidates - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_cursor > $self->candidates - 1 0 202 202
576 202 while (1) {
577 227 $self->show('pick');
578 227 my $key = $self->_keyboard->read_key;
579 227 ▶ 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 225 227
Condition83%5/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
20225
not defined $key or $key eq "eof"
l!l&&r!l&&!r
11225
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
580 225 $self->_note(undef);
581 225 ▶ if ($key eq 'q' || $key eq 'escape') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q" or $key eq "escape") 30 195 225
Condition66%2/3
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
300195
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
582 30 ▶ return 'quit' if $leaving;
Branch: 1 branch
BranchTrueFalseTotal
if $leaving 14 16 30
583 16 $leaving = 1;
584 16 $self->_note('Press q again to leave the game, or any other key to stay.');
585 16 next;
586 }
587 195 $leaving = 0;
588 195 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 160 35 195
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
159135
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
589 160 my @moves = $self->candidates;
590 160 return Game::RoyalUr::Notation::format_move($moves[ $self->_cursor ]);
591 }
592 35 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 13 22 35
593 22 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 6 16 22
594 16 ▶ return 'new' if $key eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" 1 15 16
595 15 ▶ return 'route' if $key eq 'R';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "R" 2 13 15
596 13 ▶ return 'save' if $key eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "S" 4 9 13
597 9 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 1 8 9
598 1 $self->show('help');
599 1 $self->_keyboard->read_key;
600 1 next;
601 }
602 8 $self->steer($key);
603 }
604 }
605  
606 4 ▶sub _ended_key {
Subroutine _ended_key: called CC=11 SCAR=27.9
607 4 my ($self) = @_;
608 4 while (1) {
609 4 $self->show('ended');
610 4 my $key = $self->_keyboard->read_key;
611 4 ▶ 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 4 4
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
004
not defined $key or $key eq "eof"
l!l&&r!l&&!r
004
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
612 4 ▶ return 'new' if $key eq 'n' || $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "n" or $key eq "enter" 0 4 4
Condition33%1/3
$key eq "n" or $key eq "enter"
l!l&&r!l&&!r
004
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
613 4 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 1 3 4
614 3 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 0 3 3
615 3 ▶ return 'save' if $key eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "S" 1 2 3
616 2 ▶ return 'quit' if $key eq 'q' || $key eq 'escape';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "escape" 2 0 2
Condition33%1/3
$key eq "q" or $key eq "escape"
l!l&&r!l&&!r
200
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
617 }
618 }
619  
620 249 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
621 249 my ($self, @lines) = @_;
622 249 ▶ $self->_screen->say(@lines) if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 248 1 249
623 249 return;
624 }
625  
626 118 ▶sub _read_line {
Subroutine _read_line: called CC=4 SCAR=14.0
627 118 my ($self) = @_;
628 118 my $game = $self->game;
629 118 my $out = $self->out;
630 118 $self->_say(@{ $self->screen('plain') });
631 118 ▶ if (!$game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->is_over) 118 0 118
632 118 my @moves = $self->candidates;
633 118 $self->_say(map { sprintf ' %s %s', $self->_paint($_ + 1, 'key'), substr($self->_readout($moves[$_]), 4) } 0 .. $#moves);
634 }
635 118 ▶ my $line = $self->_keyboard->read_line(
Branch: 1 branch
BranchTrueFalseTotal
$game->is_over ? : 0 118 118
636 $game->is_over ? 'new, undo or quit> ' : $game->side . ' (' . $game->roll . ')> ', $out);
637 118 ▶ print {$out} "\n" unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 118 0 118
638 118 return $line;
639 }
640  
641 321 ▶sub command {
Subroutine command: called CC=34 SCAR=35.6
Pod documented
642 321 my ($self, $line) = @_;
643 321 my $game = $self->game;
644 321 $self->_note(undef);
645 321 ▶ return 0 unless defined $line && length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 0 321 321
Condition33%1/3
defined $line and length $line
!ll&&!rl&&r
00321
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
646 321 my ($word, $rest) = split ' ', $line, 2;
647 321 $word = lc $word;
648  
649 321 ▶ 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" 70 251 321
Condition66%4/6
$word eq "quit" or $word eq "q" or $word eq "exit"
l!l&&r!l&&!r
700251
$word eq "quit" or $word eq "q"
l!l&&r!l&&!r
700251
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
650 251 ▶ if ($word eq 'help' || $word eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help" or $word eq "?") 1 250 251
Condition66%2/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
10250
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
651 1 $self->_say(map { " $_" } @{ $self->help_lines });
652 1 return 0;
653 }
654 250 ▶ if ($word eq 'new' || $word eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "new" or $word eq "n") 1 249 250
Condition66%2/3
$word eq "new" or $word eq "n"
l!l&&r!l&&!r
10249
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
655 1 $self->seed(Digest::SHA::sha256($self->seed . 'next'));
656 1 $self->game($self->_fresh_game);
657 1 $self->_events([]);
658 1 $self->_told(0);
659 1 $self->_cursor(0);
660 1 $self->history->start($self->game);
661 1 $self->_announce_opening;
662 1 return 0;
663 }
664 249 ▶ if ($word eq 'undo' || $word eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "undo" or $word eq "u") 20 229 249
Condition66%2/3
$word eq "undo" or $word eq "u"
l!l&&r!l&&!r
200229
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
665 20 $self->take_back;
666 20 return 0;
667 }
668 229 ▶ if ($word eq 'redo' || $word eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "redo" or $word eq "r") 8 221 229
Condition100%3/3
$word eq "redo" or $word eq "r"
l!l&&r!l&&!r
62221
MC/DC100%2/2
$word eq "redo" or $word eq "r"
$word eq "redo" $word eq "r"
Truth table100%3/3
A or B
A= $word eq "redo"B= $word eq "r"
ABresult
1X1
011
000
669 8 $self->play_again;
670 8 return 0;
671 }
672 221 ▶ if ($word eq 'route') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "route") 3 218 221
673 3 ▶ $self->route($self->route ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->route ? : 1 2 3
674 3 return 0;
675 }
676 218 ▶ if ($word eq 'moves') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "moves") 5 213 218
677 5 $self->_say(map { ' ' . $said->($_) } @{ $game->log });
678 5 return 0;
679 }
680 213 ▶ if ($word eq 'save' || $word eq 'record') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "save" or $word eq "record") 14 199 213
Condition100%3/3
$word eq "save" or $word eq "record"
l!l&&r!l&&!r
122199
MC/DC100%2/2
$word eq "save" or $word eq "record"
$word eq "save" $word eq "record"
Truth table100%3/3
A or B
A= $word eq "save"B= $word eq "record"
ABresult
1X1
011
000
681 14 ▶ my $file = defined $rest && length $rest ? $rest : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $rest && length $rest ? : 2 12 14
Condition66%2/3
defined $rest and length $rest
!ll&&!rl&&r
1202
MC/DC50%1/2
defined $rest and length $rest
defined $rest length $rest
Truth table66%2/3
A and B
A= defined $restB= length $rest
ABresult
0X0
100
111
682 14 ▶ if (!defined $file && $self->_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file and $self->_raw) 3 11 14
Condition100%3/3
not defined $file and $self->_raw
!ll&&!rl&&r
293
MC/DC100%2/2
not defined $file and $self->_raw
not defined $file $self->_raw
Truth table100%3/3
not(A) and B
A= not defined $fileB= $self->_raw
ABresult
0X0
100
111
683 3 $file = Game::Terminal::Kit::Save::ask_file($self->_keyboard, $self->_screen,
684 default => $self->save_default, head => $self->screen('plain'));
685 3 ▶ return 0 unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 2 3
686 }
687 13 $self->save($file);
688 13 return 0;
689 }
690 199 ▶ if ($word eq 'level') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "level") 2 197 199
691 2 my @levels = Game::RoyalUr::Bot->levels($game->variant);
692 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
693 1 $self->level($rest);
694 1 $self->_event(undef, "The program now plays at level $rest.");
695 }
696 1 else { $self->_note('A level is a number from 1 to ' . $levels[-1] . '.') }
697 2 return 0;
698 }
699 197 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 1 196 197
700 1 $self->_note('The game is over. Type new, undo or quit.');
701 1 return 0;
702 }
703  
704 196 my $move = $line;
705 196 ▶ if ($line =~ /\A[1-7]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A[1-7]\z/) 19 177 196
706 19 my @moves = $self->candidates;
707 19 ▶ if ($line > @moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > @moves) 1 18 19
708 1 $self->_note("There are only " . scalar(@moves) . " moves to choose from.");
709 1 return 0;
710 }
711 18 $move = $moves[ $line - 1 ];
712 }
713 195 ▶ if ($game->play($move)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->play($move)) { } 190 5 195
714 190 $self->_cursor(0);
715 190 $self->_tell(1);
716 }
717 else {
718 5 my $error = $game->error;
719 5 ▶ $self->_note($error->code eq 'bad_move'
Branch: 1 branch
BranchTrueFalseTotal
$error->code eq 'bad_move' ? : 2 3 5
720 ? "I do not know '$line'. A move is two places, like hand-b1 or a2-d2, or its number in the list."
721 : ucfirst($error->message) . '.');
722 }
723 195 return 0;
724 }
725  
726 22 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
727 22 my ($self) = @_;
728 22 $self->_keep;
729 22 my $game = $self->history->undo;
730 22 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 4 18 22
731 4 $self->_note('There is nothing to take back.');
732 4 return 0;
733 }
734 18 $self->swap($game);
735 18 $self->_event(undef, 'Taken back.');
736 18 return 1;
737 }
738  
739 8 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=6.9
Pod documented
740 8 my ($self) = @_;
741 8 $self->_keep;
742 8 my $game = $self->history->redo;
743 8 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 3 5 8
744 3 $self->_note('There is nothing to play again.');
745 3 return 0;
746 }
747 5 $self->swap($game);
748 5 $self->_event(undef, 'Played again.');
749 5 return 1;
750 }
751  
752 30 ▶sub _keep {
Subroutine _keep: called CC=2 SCAR=6.9
753 30 my ($self) = @_;
754 30 ▶ $self->history->record($self->game) if $self->mode ne 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode ne "watch" 29 1 30
755 30 return;
756 }
757  
758 23 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
759 23 my ($self, $game) = @_;
760 23 $self->game($game);
761 23 $self->_told(scalar @{ $game->log });
762 23 $self->_cursor(0);
763 23 $self->_note(undef);
764 23 return $self;
765 }
766  
767 315 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
768 315 my ($self) = @_;
769 315 $self->history->turn($self->game);
770 315 return $self;
771 }
772  
773 17 ▶sub settings {
Subroutine settings: called CC=3 SCAR=11.1
Pod documented
774 17 my ($self) = @_;
775 17 my $rules = $self->game->variant;
776 return {
777 17 ▶ mode => $self->mode,
Branch: 2 branches
BranchTrueFalseTotal
$self->route ? : 1 16 17
defined $rules->name ? : 17 0 17
778 side => $self->side,
779 level => $self->level,
780 first => $self->first,
781 route => $self->route ? 1 : 0,
782 (defined $rules->name ? (rules => $rules->name) : ()),
783 };
784 }
785  
786 16 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=6.9
Pod documented
787 16 my ($self) = @_;
788 16 my $file = $self->history->file;
789 16 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('royalur');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 15 1 16
790 }
791  
792 17 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
793 17 my ($self, $file) = @_;
794 17 ▶ $file = $self->save_default unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 9 8 17
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
908
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
795 17 my $history = $self->history;
796 17 $history->settings($self->settings);
797 17 $history->version($VERSION);
798 17 my $path = eval { $history->saved($self->game)->write_file($file) };
799 17 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 1 16 17
800 1 $self->_note('The game was not saved: ' . _plain($@) . '.');
801 1 return 0;
802 }
803 16 $history->file($path);
804 16 $self->_event(undef, "The game is saved to $path.");
805 16 return 1;
806 }
807  
808 19 ▶sub _plain {
Subroutine _plain: called CC=3 SCAR=11.6
809 19 my ($why) = @_;
810 19 ▶ $why = 'it failed without a reason' unless defined $why && length $why;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why and length $why 0 19 19
Condition33%1/3
defined $why and length $why
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $why and length $why
defined $why length $why
Truth table33%1/3
A and B
A= defined $whyB= length $why
ABresult
0X0
100
111
811 19 $why =~ s/ at \S+ line \d+\.?\s*\z//;
812 19 $why =~ s/\A(?:Game::Terminal::Kit::\w+|Game::RoyalUr(?:::\w+)*): //;
813 19 $why =~ s/\.?\s*\z//;
814 19 return $why;
815 }
816  
817 7 ▶sub replay_lines {
Subroutine replay_lines: called CC=4 SCAR=13.9
Pod documented
818 7 my ($self, $game) = @_;
819 7 my $was = $self->game;
820 7 $self->game($game);
821 7 my @lines = eval {
822 7 my @log = @{ $game->log };
823 7 ▶ @log = @log[ -4 .. -1 ] if @log > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @log > 4 0 7 7
824 7 ▶ (@{ $self->board_lines }, '', $self->_roll_line, $self->_result_line,
Branch: 1 branch
BranchTrueFalseTotal
@log ? : 5 2 7
825 7 (@log ? '' : ()), map { ' ' . $self->_paint($self->_describe($_), $_->{side}) } @log);
826 };
827 7 my $died = $@;
828 7 $self->game($was);
829 7 ▶ die $died unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 7 7
830 7 return @lines;
831 }
832  
833 18 ▶sub read_save {
Subroutine read_save: called CC=16 SCAR=28.0
Pod documented
834 18 my ($class, $file, %option) = @_;
835 18 my $save = eval { Game::Terminal::Kit::Save->read_file($file, game => 'royalur') };
836 18 ▶ return $save if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 7 11 18
837 11 my $why = _plain($@);
838 11 ▶ return (undef, $why) unless $why =~ /is not a saved game\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $why =~ /is not a saved game\z/ 5 6 11
839  
840 # Not a save: a record, as --record wrote before saves were kept.
841 6 ▶ open my $fh, '<', $file or return (undef, "I could not read '$file'");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return undef, "I could not read '${file}'" 6 0 6
842 6 my $text = do { local $/; <$fh> };
843 6 close $fh;
844 6 my ($game, $error) = Game::RoyalUr->replay($text);
845 6 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 2 4 6
846 2 my $detail = $error->detail;
847 return (undef, "'$file' is not a game I can replay: " . $error->message
848 2 ▶ . (defined $detail->{line} ? " (line $detail->{line})" : defined $detail->{ply} ? ' (turn ' . ($detail->{ply} + 1) . ')' : ''));
Branch: 2 branches
BranchTrueFalseTotal
defined $detail->{'line'} ? : 1 1 2
defined $detail->{'ply'} ? : 1 0 1
849 }
850 4 my $current = $game->to_text;
851 4 my @steps;
852 4 ▶ if (($option{steps} || '') eq 'ply') {
Branch: 1 branch
BranchTrueFalseTotal
if (($option{"steps"} || "") eq "ply") 2 2 4
Condition100%2/2
$option{'steps'} || ''
l!l
31
MC/DC100%1/1
$option{'steps'} || ''
$option{'steps'}
Truth table100%2/2
A
A= $option{'steps'}
Aresult
11
00
853 2 my ($record) = Game::RoyalUr::Notation::parse_record($text);
854 2 ▶ my @turns = @{ $record->{turns} || [] };
Condition50%1/2
$record->{'turns'} || []
l!l
20
MC/DC0%0/1
$record->{'turns'} || []
$record->{'turns'}
Truth table50%1/2
A
A= $record->{'turns'}
Aresult
11
00
855 2 for my $n (0 .. $#turns) {
856 8 my ($before) = Game::RoyalUr->replay({ %{$record}, turns => [ @turns[0 .. $n - 1] ], result => undef });
857 8 ▶ next unless $before;
Branch: 1 branch
BranchTrueFalseTotal
unless $before 0 8 8
858 8 my $step = $before->to_text;
859 8 ▶ push @steps, $step unless @steps && $steps[-1] eq $step;
Branch: 1 branch
BranchTrueFalseTotal
unless @steps and $steps[-1] eq $step 4 4 8
Condition100%3/3
@steps and $steps[-1] eq $step
!ll&&!rl&&r
224
MC/DC100%2/2
@steps and $steps[-1] eq $step
@steps $steps[-1] eq $step
Truth table100%3/3
A and B
A= @stepsB= $steps[-1] eq $step
ABresult
0X0
100
111
860 }
861 2 ▶ pop @steps while @steps && $steps[-1] eq $current;
Branch: 1 branch
BranchTrueFalseTotal
@steps and $steps[-1] eq $current and do { pop @steps } 0 2 2
Condition33%1/3
@steps and $steps[-1] eq $current
!ll&&!rl&&r
020
MC/DC0%0/2
@steps and $steps[-1] eq $current
@steps $steps[-1] eq $current
Truth table33%1/3
A and B
A= @stepsB= $steps[-1] eq $current
ABresult
0X0
100
111
862 }
863 4 $save = Game::Terminal::Kit::Save->new(name => 'royalur', past => \@steps, current => $current);
864 4 $save->file($file);
865 4 return $save;
866 }
867  
868 777 ▶sub _bot_move {
Subroutine _bot_move: called CC=3 SCAR=11.0
869 777 my ($self) = @_;
870 777 my $game = $self->game;
871 777 ▶ $self->show('wait') if $self->_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_raw 164 613 777
872 777 my $bot = Game::RoyalUr::Bot->new(level => $self->level);
873 777 ▶ my $move = $bot->choose($game) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $bot->choose($game) or return 777 0 777
874 777 $game->play_or_die($move);
875 777 $self->_tell(1);
876 777 return;
877 }
878  
879 76 ▶sub _turns {
Subroutine _turns: called CC=12 SCAR=24.9
880 76 my ($self) = @_;
881 76 $self->_tell(1);
882 76 while (1) {
883 1087 my $game = $self->game;
884 1087 ▶ my $picking = $self->picking && $self->_raw;
Condition66%2/3
$self->picking && $self->_raw
!ll&&!rl&&r
7360351
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
885 1087 ▶ if ($game->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->is_over) 8 1079 1087
886 8 ▶ last if $self->mode eq 'watch' && !$self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" and not $self->interactive 5 3 8
Condition66%2/3
$self->mode eq "watch" and not $self->interactive
!ll&&!rl&&r
305
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
887 3 ▶ my $line = $picking ? $self->_ended_key : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 3 0 3
888 3 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 3 3
889 3 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 2 1 3
890 1 $self->_tell(1);
891 1 next;
892 }
893 1079 ▶ if (!$self->_person($game->side)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_person($game->side)) 777 302 1079
894 777 $self->_bot_move;
895 777 next;
896 }
897 302 $self->person_turn;
898 302 ▶ my $line = $picking ? $self->pick : $self->_read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 184 118 302
899 302 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 300 302
900 300 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 67 233 300
901 }
902 76 return;
903 }
904  
905 76 ▶sub start {
Subroutine start: called CC=5 SCAR=16.2
Pod documented
906 76 my ($self) = @_;
907 76 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 76 76
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
61150
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
908  
909 76 ▶ Game::Terminal::Kit::Run::guard(sub { $self->_turns }, $self->_keyboard, $self->_screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
910  
911 76 ▶ $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 7 69 76
Condition100%3/3
$self->mode eq "watch" or $self->game->is_over
l!l&&r!l&&!r
5269
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
912 76 return 0;
913 }
914  
915 18 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
916 18 return <<'USAGE';
917 royalur - the Royal Game of Ur, in a terminal
918  
919 royalur [options]
920  
921 --mode HOW bot (the default): you against the program
922 hotseat: two people at one keyboard
923 watch: the program against itself
924 --rules NAME finkel (the default) or masters
925 --level N how well the program plays, 1 upward; the top by default
926 --side WHICH light (the default) or dark: your side against the program
927 --first WHO light, dark, or roll (the default): throw the dice for it
928 --seed HEX hexadecimal; the same seed throws the same dice
929 --pace SECONDS how long a lost turn and the program's move stay up
930 --route show the order the squares are visited in
931 --save FILE where S, or save, writes the game: FILE, else the file it
932 was resumed from, else royalur.save
933 --resume FILE carry on a saved game, with its undo and redo
934 --replay FILE watch a saved game a step at a time; enter plays on from there
935 --record FILE the old name for --save
936 --ascii plain letters and lines, for a terminal with no Unicode
937 --unicode line and shape characters, where they are not the default
938 --no-colour no colour; nothing is lost without it
939 --no-pick type moves instead of choosing them with the arrow keys
940 --version the version, and stop
941 --help this, and stop
942  
943 In a terminal, the pieces that can move are walked with the arrow keys and the
944 board shows what each move would leave; press ? for the keys. Anywhere else,
945 type a number from the list, a move such as hand-b1 or a2-d2, or help.
946  
947 A record written by --record before 0.04 can be given to --resume and
948 --replay as it is.
949 USAGE
950 }
951  
952 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
953 return [
954 2 { name => 'mode', type => 'enum', choices => [qw(bot hotseat watch)], help => 'you against the program, two people, or the program against itself' },
955 { name => 'rules', type => 'enum', choices => [qw(finkel masters)], help => 'the route and the rules' },
956 { name => 'level', type => 'int', min => 1, max => 4, help => 'how well the program plays; the top by default' },
957 { name => 'side', type => 'enum', choices => [qw(light dark)], help => 'your side against the program' },
958 { name => 'first', type => 'enum', choices => [qw(light dark roll)], help => 'who moves first, or throw the dice for it' },
959 { name => 'seed', type => 'str', help => 'hexadecimal; the same seed throws the same dice' },
960 ];
961 }
962  
963 82 ▶sub run {
Subroutine run: called CC=26 SCAR=32.6
Pod documented
964 82 my ($class, @argv) = @_;
965  
966 82 my %given;
967 82 my $parsed = Getopt::Long::GetOptionsFromArray(
968 \@argv, \%given,
969 'mode=s', 'rules=s', 'level=i', 'side=s', 'first=s', 'seed=s', 'pace=s',
970 Game::Terminal::Kit::Save::flags(), 'record=s',
971 'ascii', 'unicode!', 'route', 'colour|color!', 'pick!', 'version', 'help',
972 );
973  
974 82 ▶ if (!$parsed || @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed or @argv) 3 79 82
Condition100%3/3
not $parsed or @argv
l!l&&r!l&&!r
2179
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
975 3 ▶ print STDERR "royalur: 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
976 3 print STDERR _usage();
977 3 return 2;
978 }
979 79 ▶ if ($given{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"help"}) 15 64 79
980 15 print _usage();
981 15 return 0;
982 }
983 64 ▶ if ($given{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given{"version"}) 1 63 64
984 1 print "royalur $Game::RoyalUr::VERSION\n";
985 1 return 0;
986 }
987 63 ▶ if (defined $given{record}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"record"}) 2 61 63
988 2 print STDERR "royalur: --record is now --save, and the game is written when you save it: S, or save.\n";
989 2 ▶ $given{save} = $given{record} unless defined $given{save};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given{"save"} 2 0 2
990 }
991 63 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%given)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%given)) 2 61 63
992 2 print STDERR "royalur: $why.\n";
993 2 return 2;
994 }
995  
996 61 my %with;
997 61 $with{$_} = $given{$_} for grep { defined $given{$_} } qw(mode rules level side first pace route);
998 61 ▶ $with{save_file} = $given{save} if defined $given{save};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"save"} 8 53 61
999 61 ▶ $with{colour} = $given{colour} if defined $given{colour};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"colour"} 3 58 61
1000 61 ▶ $with{picking} = $given{pick} if defined $given{pick};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"pick"} 1 60 61
1001 61 ▶ $with{unicode} = $given{unicode} if defined $given{unicode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"unicode"} 2 59 61
1002 61 ▶ $with{unicode} = 0 if $given{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"ascii"} 1 60 61
1003  
1004 61 ▶ if (defined $given{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $given{"seed"}) 42 19 61
1005 42 ▶ 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 40 42
1006 2 print STDERR "royalur: a seed is hexadecimal, two characters a byte.\n";
1007 2 return 2;
1008 }
1009 40 $with{seed} = pack 'H*', $given{seed};
1010 }
1011  
1012 59 ▶ my $from = defined $given{resume} ? $given{resume} : $given{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $given{'resume'} ? : 9 50 59
1013 59 ▶ return $class->_play(undef, %with) unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 46 13 59
1014  
1015 13 ▶ my ($save, $why) = $class->read_save($from, steps => defined $given{replay} ? 'ply' : 'one');
Branch: 1 branch
BranchTrueFalseTotal
defined $given{'replay'} ? : 4 9 13
1016 13 ▶ unless ($save) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 5 8 13
1017 5 print STDERR "royalur: $why.\n";
1018 5 return 2;
1019 }
1020 # What the game was played with, unless the command line says otherwise.
1021 8 my $settings = $save->settings;
1022 8 for my $key (qw(mode side level first route)) {
1023 40 ▶ $with{$key} = $settings->{$key} if !exists $with{$key} && defined $settings->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $with{$key} and defined $settings->{$key} 27 13 40
Condition100%3/3
not exists $with{$key} and defined $settings->{$key}
!ll&&!rl&&r
4927
MC/DC100%2/2
not exists $with{$key} and defined $settings->{$key}
not exists $with{$key} defined $settings->{$key}
Truth table100%3/3
not(A) and B
A= not exists $with{$key}B= defined $settings->{$key}
ABresult
0X0
100
111
1024 }
1025 8 ▶ return defined $given{replay} ? $class->_replay($save, %with) : $class->_play($save, %with);
Branch: 1 branch
BranchTrueFalseTotal
defined $given{'replay'} ? : 2 6 8
1026 }
1027  
1028 52 ▶sub _play {
Subroutine _play: called CC=5 SCAR=16.3
1029 52 my ($class, $save, %with) = @_;
1030 52 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 6 46 52
1031 6 my $history = $class->_new_history;
1032 6 my $game = eval { $history->resume($save) };
1033 6 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 6 6
1034 0 print STDERR "royalur: '" . $save->file . "' cannot be carried on: " . _plain($@) . ".\n";
1035 0 return 2;
1036 }
1037 6 $with{game} = $game;
1038 6 $with{history} = $history;
1039 6 $with{rules} = $game->variant;
1040 6 ▶ $with{seed} = $game->seed if defined $game->seed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $game->seed 6 0 6
1041 }
1042 52 my $terminal = eval { $class->new(%with) };
1043 52 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 7 45 52
1044 7 print STDERR "royalur: " . _plain($@) . ".\n";
1045 7 return 2;
1046 }
1047 45 return $terminal->start;
1048 }
1049  
1050 2 ▶sub _replay {
Subroutine _replay: called CC=7 SCAR=21.8
1051 2 my ($class, $save, %with) = @_;
1052 2 my $first = eval { Game::RoyalUr->from_text(($save->steps)[0]) };
1053 2 ▶ my $terminal = $first && eval {
Condition33%1/3
$first && eval { do { $class->new(%with, 'game', $first, 'rules', $first->variant, defined $first->seed ? ('seed', $first->seed) : ()) } }
!ll&&!rl&&r
002
MC/DC0%0/2
$first && eval { do { $class->new(%with, 'game', $first, 'rules', $first->variant, defined $first->seed ? ('seed', $first->seed) : ()) } }
$first eval { do { $class->new(%with, 'game', $first, 'rules', $first->variant, defined $first->seed ? ('seed', $first->seed) : ()) } }
Truth table33%1/3
A && B
A= $firstB= eval { do { $class->new(%with, 'game', $first, 'rules', $first->variant, defined $first->seed ? ('seed', $first->seed) : ()) } }
ABresult
0X0
100
111
1054 2 ▶ $class->new(%with, game => $first, rules => $first->variant,
Branch: 1 branch
BranchTrueFalseTotal
defined $first->seed ? : 2 0 2
1055 (defined $first->seed ? (seed => $first->seed) : ()));
1056 };
1057 2 ▶ unless ($terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 0 2 2
1058 0 print STDERR "royalur: '" . $save->file . "' cannot be watched: " . _plain($@) . ".\n";
1059 0 return 2;
1060 }
1061 my $replay = Game::Terminal::Kit::Replay->new(
1062 save => $save,
1063 6 ▶ load => sub { Game::RoyalUr->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 6 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1065 2 ▶ head => [ $terminal->screen('plain')->[0],
Branch: 2 branches
BranchTrueFalseTotal
defined $save->saved_at ? : 1 1 2
$terminal->pace > 0 ? : 0 2 2
1066 ' ' . $terminal->_paint('A replay of ' . $save->file
1067 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), 'quiet'), '' ],
1068 screen => $terminal->_screen,
1069 ($terminal->pace > 0 ? (pace => $terminal->pace) : ()),
1070 );
1071 2 my ($event, $step) = $replay->play($terminal->_keyboard, $terminal->_screen);
1072 2 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 2 0 2
1073 0 return $class->_play($save->from_step($step), %with);
1074 }
1075  
1076 73 1;
1077  
1078 __END__
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/lib/Game/RoyalUr.pm »