blib/lib/Game/Oware/Terminal.pm

filter: Statementstmt 93.4% 626 / 670 Branchbran 79.7% 276 / 346 Conditioncond 77.5% 45 / 58 MC/DCmcdc 61.1% 22 / 36 Subroutinesub 97.9% 94 / 96 Podpod 97.0% 33 / 34 totaltotal 88.3% 1096 / 1240 CC 199 SCAR 25.5
CC
199
Cov
88%
_run
56.6
pick
21.7
_typed_turn
19.5
CRAP 12.8
« blib/lib/Game/Oware.pm ↑ Summary blib/lib/Game/Oware/Result.pm »
1 package Game::Oware::Terminal;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 30 ▶use Game::Oware;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Game::Oware::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Game::Oware::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Game::Oware::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Game::Oware::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Game::Oware::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Game::Oware::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use Game::Oware::Variant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 ▶use Game::Terminal::Kit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 30 ▶use Game::Terminal::Kit::Picker;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 30 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 30 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 30 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 30 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 30 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 30 our $VERSION = '0.05';
27  
28 # The glyph tables. NOTE: this file is NOT under `use utf8`, so the wide
29 # glyphs are byte strings and `length` on anything built from them is a byte
30 # count rather than a column count. Every cell below is therefore padded from
31 # a COUNT the caller already has, never from length(), and nothing here is
32 # ever passed to sprintf's %Ns. Get that wrong and the board shears by two
33 # columns per seed on a UTF-8 terminal and by none on a C-locale one.
34 30 my %GLYPH = (
35 wide => {
36 h => '─', v => '│',
37 tl => '╭', tr => '╮', bl => '╰', br => '╯',
38 td => '┬', tu => '┴', lt => '├', rt => '┤', tx => '┼',
39 seed => '•', more => '…',
40 },
41 ascii => {
42 h => '-', v => '|',
43 tl => '+', tr => '+', bl => '+', br => '+',
44 td => '+', tu => '+', lt => '+', rt => '+', tx => '+',
45 seed => '.', more => '>',
46 },
47 );
48  
49 30 my $STORE_W = 4;
50 30 my $HOUSE_W = 6;
51 30 my $SEED_CAP = 4;
52  
53 30 my %SGR = (
54 frame => '2',
55 letter => '36',
56 mine => '1;36',
57 store => '36',
58 storemine => '1;36',
59 empty => '2',
60 seed => '33',
61 risk => '31',
62 loaded => '1;33',
63 emptied => '2',
64 gained => '32',
65 landed => '1;32',
66 captured => '7;31',
67 forfeited => '1;33',
68 prompt => '1',
69 cursor => '1;7',
70 );
71  
72 30 my @TIMES = ('', 'once', 'twice', 'for the third time', 'for the fourth time');
73  
74 my %KEY = (
75 30 (map { $_ => $_ } 'A' .. 'F', 'a' .. 'f'),
76 'v' => 'live',
77 '?' => 'help',
78 't' => 'type',
79 ':' => 'command',
80 'u' => 'undo',
81 'r' => 'redo',
82 'S' => 'save',
83 );
84  
85 has in => (
86 is => 'rw',
87 isa => Any,
88 72 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
89 30 );
90  
91 has out => (
92 is => 'rw',
93 isa => Any,
94 20 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
95 30 );
96  
97 has level => (
98 is => 'ro',
99 isa => Int,
100 default => 3
101 30 );
102  
103 has variant => (
104 is => 'ro',
105 isa => Str,
106 default => 'abapa'
107 30 );
108  
109 has seat => (
110 is => 'ro',
111 isa => Str,
112 default => 'p1'
113 30 );
114  
115 has seed => (
116 is => 'ro',
117 isa => Str,
118 default => ''
119 30 );
120  
121 has quiet => (
122 is => 'ro',
123 isa => Int,
124 default => 0
125 30 );
126  
127 has ascii => (
128 is => 'ro',
129 isa => Int,
130 default => 0
131 30 );
132  
133 has game => (
134 is => 'rw',
135 isa => Object
136 30 );
137  
138 has bot => (
139 is => 'rw',
140 isa => Object
141 30 );
142  
143 30 has keysource => (is => 'rw');
144  
145 30 has keyboard => (is => 'rw');
146  
147 30 has screen => (is => 'rw');
148  
149 30 has log => (is => 'rw');
150  
151 30 has last_move => (is => 'rw');
152  
153 30 has history => (is => 'rw');
154  
155 30 has save_file => (is => 'rw');
156  
157 has pending => (
158 is => 'rw',
159 isa => ArrayRef,
160 default => []
161 30 );
162  
163 has _wants_ansi => (
164 is => 'ro',
165 init_arg => 'ansi',
166 private => 1
167 30 );
168  
169 has _wants_progress => (
170 is => 'ro',
171 init_arg => 'progress',
172 private => 1
173 30 );
174  
175 has _wants_interactive => (
176 is => 'ro',
177 init_arg => 'interactive',
178 private => 1
179 30 );
180  
181 has _wants_picking => (
182 is => 'rw',
183 init_arg => 'picking',
184 private => 1
185 30 );
186  
187 75 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=25.1
Pod undocumented
188 75 my ($self) = @_;
189  
190 75 Game::Oware::Variant::check_variant($self->variant);
191  
192 73 ▶ die 'Game::Oware::Terminal: seat must be p1 or p2, not ' . $self->seat
Branch: 1 branch
BranchTrueFalseTotal
unless $self->seat eq "p1" or $self->seat eq "p2" 2 71 73
Condition100%3/3
$self->seat eq "p1" or $self->seat eq "p2"
l!l&&r!l&&!r
6922
MC/DC100%2/2
$self->seat eq "p1" or $self->seat eq "p2"
$self->seat eq "p1" $self->seat eq "p2"
Truth table100%3/3
A or B
A= $self->seat eq "p1"B= $self->seat eq "p2"
ABresult
1X1
011
000
193 unless $self->seat eq 'p1' || $self->seat eq 'p2';
194  
195 71 Game::Oware::Bot->setting_for($self->level);
196  
197 69 ▶ $self->game(Game::Oware->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 10 59 69
198 variant => $self->variant,
199 seed => $self->seed,
200 )) unless $self->game;
201  
202 69 ▶ $self->bot(Game::Oware::Bot->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bot 69 0 69
203 level => $self->level,
204 seed => $self->seed . ':bot',
205 )) unless $self->bot;
206  
207 69 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 69 0 69
$self->keysource ? : 26 43 69
208 in => $self->in,
209 pending => $self->pending,
210 ($self->keysource ? (keysource => $self->keysource) : ()),
211 )) unless $self->keyboard;
212  
213 69 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->screen 69 0 69
214 out => $self->out,
215 interactive => $self->interactive,
216 colour => $self->ansi,
217 ascii => $self->ascii,
218 )) unless $self->screen;
219  
220 69 ▶ $self->log(Game::Terminal::Kit::Log->new(keep => 2)) unless $self->log;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->log 69 0 69
221  
222 69 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 55 14 69
223 69 ▶ $self->history->name('oware') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 69 69
224 69 $self->history->version($VERSION);
225 69 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 7 62 69
226 69 $self->history->start($self->game);
227  
228 69 return $self;
229 }
230  
231 72 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
232 return Game::Terminal::Kit::History->new(
233 name => 'oware',
234 version => $VERSION,
235 498 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
236 15 ▶ load => sub { Game::Oware->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
237 72 );
238 }
239  
240 28926 ▶sub ansi {
Subroutine ansi: called CC=5 SCAR=16.1
Pod documented
241 28926 my ($self) = @_;
242 28926 ▶ return 0 if Game::Terminal::Kit::Screen::no_colour();
Branch: 1 branch
BranchTrueFalseTotal
if Game::Terminal::Kit::Screen::no_colour() 1729 27197 28926
243 27197 ▶ return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_ansi 27057 140 27197
$self->_wants_ansi ? : 50 27007 27057
244 140 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 140 140
245 }
246  
247 1809 ▶sub interactive {
Subroutine interactive: called CC=4 SCAR=14.3
Pod documented
248 1809 my ($self) = @_;
249 1809 ▶ return $self->_wants_interactive ? 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_interactive 591 1218 1809
$self->_wants_interactive ? : 0 591 591
250 if defined $self->_wants_interactive;
251 1218 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 1218 1218
252 }
253  
254 662 ▶sub progress {
Subroutine progress: called CC=4 SCAR=14.9
Pod documented
255 662 my ($self) = @_;
256 662 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 76 586 662
257 586 ▶ return $self->_wants_progress ? 1 : 0 if defined $self->_wants_progress;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_progress 0 586 586
$self->_wants_progress ? : 0 0 0
258 586 return $self->interactive;
259 }
260  
261 # Read AND write: the `type` key turns picking off for the rest of the game,
262 # and `pick` turns it back on. Not a plain property because the default has to
263 # be computed from `in`, which the suite sets after construction.
264 1009 ▶sub picking {
Subroutine picking: called CC=6 SCAR=17.9
Pod documented
265 1009 my ($self, @set) = @_;
266 1009 ▶ $self->_wants_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 3 1006 1009
$set[0] ? : 0 3 3
267 1009 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 43 966 1009
268 966 ▶ return $self->_wants_picking ? 1 : 0 if defined $self->_wants_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_picking 400 566 966
$self->_wants_picking ? : 376 24 400
269 566 return $self->keys_available;
270 }
271  
272 19 ▶sub raw {
Subroutine raw: called CC=2 SCAR=6.9
Pod documented
273 19 my ($self) = @_;
274 19 ▶ return $self->keyboard->raw ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->raw ? : 6 13 19
275 }
276  
277 568 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
278 568 my ($self) = @_;
279 568 return $self->_synced_keyboard->keys_available;
280 }
281  
282 159 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=6.9
Pod documented
283 159 my ($self) = @_;
284 159 ▶ return $self->_synced_keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_synced_keyboard->enter_raw ? : 158 1 159
285 }
286  
287 142 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
288 142 my ($self) = @_;
289 142 $self->keyboard->leave_raw;
290 142 return $self;
291 }
292  
293 183 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
294 183 my ($self) = @_;
295 183 return $self->_synced_keyboard->read_key;
296 }
297  
298 558 ▶sub rows_for {
Subroutine rows_for: called CC=2 SCAR=6.9
Pod documented
299 558 my ($self, $seat) = @_;
300 558 ▶ return $seat eq 'p1'
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p1' ? : 555 3 558
301 ? ([ reverse 6 .. 11 ], [ 0 .. 5 ])
302 : ([ reverse 0 .. 5 ], [ 6 .. 11 ]);
303 }
304  
305 546 ▶sub marks_for {
Subroutine marks_for: called CC=6 SCAR=17.9
Pod documented
306 546 my ($self, $move) = @_;
307 546 ▶ return {} unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 33 513 546
308  
309 513 my %mark;
310 513 my $house = $move->house;
311 513 $mark{$house} = '-';
312  
313 # The sowing walk, so the mark on a house of twelve or more shows the
314 # origin staying empty while the hand goes past it.
315 513 my $i = $house;
316 513 my $hand = $move->sown;
317 513 ▶ while ($hand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand) 1399 513 1912
318 1399 $i = ($i + 1) % Game::Oware::Board->HOUSES;
319 1399 ▶ next if $i == $house;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $house 17 1382 1399
320 1382 $mark{$i} = '+';
321 1382 $hand--;
322 }
323  
324 513 $mark{ $move->last } = '*';
325 513 $mark{$_} = '!' for @{ $move->forfeited };
326 513 $mark{$_} = 'x' for @{ $move->captured };
327  
328 513 return \%mark;
329 }
330  
331 558 ▶sub board_text {
Subroutine board_text: called CC=4 SCAR=13.9
Pod documented
332 558 my ($self, $from, $seat, $marks) = @_;
333 558 ▶ $seat ||= $self->seat;
Condition33%1/3
$seat ||= $self->seat
l!l&&r!l&&!r
55800
MC/DC0%0/2
$seat ||= $self->seat
$seat $self->seat
Truth table33%1/3
A ||= B
A= $seatB= $self->seat
ABresult
1X1
011
000
334 558 ▶ $marks ||= {};
Condition100%2/2
$marks ||= {}
l!l
5517
MC/DC100%1/1
$marks ||= {}
$marks
Truth table100%2/2
A
A= $marks
Aresult
11
00
335  
336 # A game or a bare board, because the picker draws a position no game has
337 # been in: the one a candidate move would produce.
338 558 ▶ my $board = ref $from eq 'ARRAY' ? $from : $from->board;
Branch: 1 branch
BranchTrueFalseTotal
ref $from eq 'ARRAY' ? : 164 394 558
339 558 my $captured = Game::Oware::Scoring->captured($board);
340  
341 558 my ($top, $bottom) = $self->rows_for($seat);
342 558 my $them = Game::Oware::Board->other($seat);
343 558 my $g = $self->_glyphs;
344  
345 558 my $v = $self->_paint($g->{v}, 'frame');
346 558 my $house = $g->{h} x $HOUSE_W;
347 558 my $store = $g->{h} x $STORE_W;
348  
349 558 my @left = $self->_store_cell($them, $captured->{$them}, 'store');
350 558 my @right = $self->_store_cell($seat, $captured->{$seat}, 'storemine');
351  
352 558 my @out;
353 558 push @out, $self->_letters($top, 'letter');
354 push @out, $self->_paint($g->{tl} . $store . $g->{td}
355 558 . join($g->{td}, ($house) x 6) . $g->{td} . $store . $g->{tr}, 'frame');
356 558 push @out, $v . $left[0] . $v . $self->_seeds($board, $top, $marks)
357 . $v . $right[0] . $v;
358 558 push @out, $v . $left[1] . $v . $self->_counts($board, $top, $marks)
359 . $v . $right[1] . $v;
360 push @out, $v . $left[2]
361 558 . $self->_paint($g->{lt} . join($g->{tx}, ($house) x 6) . $g->{rt}, 'frame')
362 . $right[2] . $v;
363 558 push @out, $v . $left[3] . $v . $self->_counts($board, $bottom, $marks)
364 . $v . $right[3] . $v;
365 558 push @out, $v . $left[4] . $v . $self->_seeds($board, $bottom, $marks)
366 . $v . $right[4] . $v;
367 push @out, $self->_paint($g->{bl} . $store . $g->{tu}
368 558 . join($g->{tu}, ($house) x 6) . $g->{tu} . $store . $g->{br}, 'frame');
369 558 push @out, $self->_letters($bottom, 'mine');
370  
371 558 return @out;
372 }
373  
374 740 ▶sub preview {
Subroutine preview: called CC=1 SCAR=0.0
Pod documented
375 740 my ($self, $house) = @_;
376 740 return Game::Oware::Rules->resolve(
377 $self->game->board, $house, $self->seat, $self->variant);
378 }
379  
380 577 ▶sub describe {
Subroutine describe: called CC=5 SCAR=16.2
Pod documented
381 577 my ($self, $house) = @_;
382  
383 577 my (undef, $move) = $self->preview($house);
384 577 my $them = Game::Oware::Board->other($self->seat);
385  
386 577 my @note = ('sows ' . $move->sown);
387  
388 577 ▶ push @note, 'right round, skipping '
Branch: 1 branch
BranchTrueFalseTotal
if $move->sown >= 12 44 533 577
389 . Game::Oware::Notation->letter_of($house)
390 if $move->sown >= 12;
391  
392 577 ▶ if (@{ $move->forfeited }) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$move->forfeited;}) { } 0 577 577
elsif (@{$move->captured;}) { } 76 501 577
393 0 push @note, "would take every seed $them has, so takes none";
394 }
395 577 elsif (@{ $move->captured }) {
396 push @note, 'captures ' . $move->taken . ' from ' . join ', ',
397 76 map { Game::Oware::Notation->letter_of($_) } @{ $move->captured };
398 76 ▶ push @note, "and the rest of the board goes to $them" if $move->slammed;
Branch: 1 branch
BranchTrueFalseTotal
if $move->slammed 0 76 76
399 }
400  
401 577 return join ', ', @note;
402 }
403  
404 142 ▶sub picker {
Subroutine picker: called CC=2 SCAR=7.0
Pod documented
405 142 my ($self, $legal, $at) = @_;
406  
407 142 my @items = map { +{
408 577 label => Game::Oware::Notation->letter_of($_),
409 about => $self->describe($_),
410 value => $_,
411 } } @$legal;
412  
413 142 ▶ return Game::Terminal::Kit::Picker->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 0 142 142
414 items => \@items,
415 window => 0,
416 game_keys => { %KEY },
417 log => $self->log,
418 screen => $self->_synced_screen,
419 (defined $at ? (at => $at) : ()),
420 );
421 }
422  
423 0 ▶sub choice_lines {
Subroutine choice_lines: not called CC=1 SCAR=6.9
Pod documented
424 0 my ($self, $legal, $at) = @_;
425 0 return $self->picker($legal, $at)->rows;
426 }
427  
428 164 ▶sub legend {
Subroutine legend: called CC=1 SCAR=0.0
Pod documented
429 164 my ($self) = @_;
430 return (
431 164 'arrows to choose, enter to sow, a letter to jump to that house',
432 'v for the board as it stands, u to undo, r to redo, ? for the rules,',
433 'S to save, t to type, q to quit',
434 );
435 }
436  
437 143 ▶sub pick {
Subroutine pick: called CC=21 SCAR=30.8
Pod documented
438 143 my ($self) = @_;
439  
440 143 my $game = $self->game;
441 143 my $seat = $self->seat;
442 143 my @legal = @{ $game->legal($seat) };
443 143 ▶ return 0 unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 0 143 143
444  
445 # Asked for and not available: fall back to typing for good rather than
446 # failing the same way on every turn of the game.
447 143 ▶ unless ($self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->enter_raw) 1 142 143
448 1 $self->picking(0);
449 1 return $self->_typed_turn;
450 }
451  
452 142 my $picker = $self->picker(\@legal);
453 142 my $log = $self->log;
454 142 my $live = 0;
455  
456 142 while (1) {
457 164 $self->_show_choice($picker, $live);
458  
459 164 my ($event, $value) = $picker->step($self->read_key);
460  
461 164 ▶ if ($event eq 'quit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "quit") 14 150 164
462 14 $self->leave_raw;
463 14 return 0;
464 }
465  
466 150 ▶ if ($event eq 'chosen') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "chosen") 121 29 150
467 121 $self->leave_raw;
468 121 my $move = $game->play($seat, $value);
469 121 $self->_after_ply($move, $seat);
470 121 return 1;
471 }
472  
473 29 ▶ if ($event eq 'key' && ($value eq 'undo' || $value eq 'redo')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "key" and $value eq "undo" || $value eq "redo") 6 23 29
Condition100%6/6
$event eq "key" and $value eq "undo" || $value eq "redo"
!ll&&!rl&&r
1496
$value eq "undo" or $value eq "redo"
l!l&&r!l&&!r
429
MC/DC100%3/3
$event eq "key" and $value eq "undo" || $value eq "redo"
$event eq "key" $value eq "undo" $value eq "redo"
Truth table100%4/4
A and B or C
A= $event eq "key"B= $value eq "undo"C= $value eq "redo"
ABCresult
0XX0
1000
11X1
1011
474 6 ▶ next unless $value eq 'undo' ? $self->take_back : $self->play_again;
Branch: 2 branches
BranchTrueFalseTotal
unless $value eq "undo" ? $self->take_back : $self->play_again 3 3 6
$value eq 'undo' ? : 4 2 6
475 3 $self->leave_raw;
476 3 return 1;
477 }
478  
479 23 ▶ if ($event eq 'back') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "back") 0 23 23
480 0 $log->note('That key does nothing here. Press ? for the ones that do.');
481 0 next;
482 }
483  
484 23 ▶ next unless $event eq 'key';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "key" 14 9 23
485  
486 9 ▶ if ($value eq 'save') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "save") 1 8 9
487 1 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->_synced_screen,
488 default => $self->save_default,
489 head => [ '', $self->board_text($game, $seat, $self->marks_for($self->last_move)), '' ]);
490 1 ▶ $log->note($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 0 1
491 1 next;
492 }
493  
494 # Not `b`, which is a house. See `command`.
495 8 ▶ if ($value eq 'live') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "live") 1 7 8
496 1 $live = !$live;
497 1 next;
498 }
499  
500 7 ▶ if ($value eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "help") 0 7 7
501 0 ▶ $log->note(map { length $_ ? $_ : ' ' } $self->_help_lines);
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 0 0 0
502 0 next;
503 }
504  
505 7 ▶ if ($value eq 'type' || $value eq 'command') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "type" or $value eq "command") 4 3 7
Condition100%3/3
$value eq "type" or $value eq "command"
l!l&&r!l&&!r
223
MC/DC100%2/2
$value eq "type" or $value eq "command"
$value eq "type" $value eq "command"
Truth table100%3/3
A or B
A= $value eq "type"B= $value eq "command"
ABresult
1X1
011
000
506 4 $self->leave_raw;
507 4 ▶ $self->picking(0) if $value eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "type" 2 2 4
508 4 return $self->_typed_turn;
509 }
510  
511 # A HOUSE LETTER IS STILL THE INTERFACE. The letters are printed on the
512 # board and against every option, so typing one has to land on it, and
513 # typing one that is not on the list has to say why it is not.
514 3 my $house = Game::Oware::Notation->index_of($value);
515 3 my ($found) = grep { $legal[$_] == $house } 0 .. $#legal;
516 3 ▶ if (defined $found) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) 1 2 3
517 1 $picker->jump($found);
518 1 next;
519 }
520 2 $log->note(ucfirst($self->_why_not($house, $seat)) . '.');
521 }
522 }
523  
524 553 ▶sub status_text {
Subroutine status_text: called CC=3 SCAR=11.2
Pod documented
525 553 my ($self, $from) = @_;
526 553 ▶ $from ||= $self->game;
Condition33%1/3
$from ||= $self->game
l!l&&r!l&&!r
55300
MC/DC0%0/2
$from ||= $self->game
$from $self->game
Truth table33%1/3
A ||= B
A= $fromB= $self->game
ABresult
1X1
011
000
527  
528 553 ▶ my $board = ref $from eq 'ARRAY' ? $from : $from->board;
Branch: 1 branch
BranchTrueFalseTotal
ref $from eq 'ARRAY' ? : 164 389 553
529 553 my $play = Game::Oware::Board->seeds_on_side($board, 'p1')
530 + Game::Oware::Board->seeds_on_side($board, 'p2');
531  
532 553 return ' ' x ($STORE_W + 4) . $self->_paint($play . ' in play, '
533 . Game::Oware::Variant::target($self->variant) . ' wins', 'frame');
534 }
535  
536 39 ▶sub start {
Subroutine start: called CC=9 SCAR=22.0
Pod documented
537 39 my ($self) = @_;
538  
539 39 ▶ $self->_intro unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 36 3 39
540  
541 39 $self->history->start($self->game);
542 39 ▶ $self->_show unless $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->picking 30 9 39
543  
544 39 ▶ while ($self->game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active") 696 13 709
545 696 my ($turn) = $self->game->waiting_on;
546 696 ▶ last unless defined $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn 0 696 696
547  
548 696 ▶ if ($turn eq $self->seat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn eq $self->seat) { } 365 331 696
549 365 $self->person_turn;
550 365 ▶ last unless $self->_human_turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_human_turn 26 339 365
551 }
552 else {
553 331 ▶ last unless $self->_bot_turn($turn);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_bot_turn($turn) 0 331 331
554 }
555 }
556  
557 39 ▶ $self->person_turn if $self->game->status ne 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->status ne "active" 13 26 39
558 39 $self->_finish;
559  
560 39 return $self->game->result;
561 }
562  
563 378 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
564 378 my ($self) = @_;
565 378 $self->history->turn($self->game);
566 378 return $self;
567 }
568  
569 10 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
570 10 my ($self) = @_;
571 10 my $game = $self->history->undo;
572 10 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 3 7 10
573 3 $self->notify('There is nothing to take back.');
574 3 return 0;
575 }
576 7 $self->swap($game);
577 7 $self->notify('Taken back.');
578 7 return 1;
579 }
580  
581 4 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=6.9
Pod documented
582 4 my ($self) = @_;
583 4 my $game = $self->history->redo;
584 4 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 3 4
585 1 $self->notify('There is nothing to play again.');
586 1 return 0;
587 }
588 3 $self->swap($game);
589 3 $self->notify('Played again.');
590 3 return 1;
591 }
592  
593 10 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
594 10 my ($self, $game) = @_;
595 10 $self->game($game);
596 10 $self->last_move(undef);
597 10 $self->log->forget;
598 10 return $self;
599 }
600  
601 14 ▶sub notify {
Subroutine notify: called CC=2 SCAR=6.9
Pod documented
602 14 my ($self, $line) = @_;
603 14 ▶ if ($self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->raw) { } 6 8 14
604 6 $self->log->note($line);
605 }
606 else {
607 8 $self->_say('', $line);
608 }
609 14 return $self;
610 }
611  
612 257 ▶sub command {
Subroutine command: called CC=10 SCAR=23.1
Pod documented
613 257 my ($self, $line) = @_;
614  
615 257 ▶ return 'quit' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 256 257
616 256 $line =~ s/\A\s+|\s+\z//g;
617  
618 256 ▶ return 'again' unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 2 254 256
619 254 ▶ return 'quit' if $line =~ /\A(?:q|quit|exit)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A(?:q|quit|exit)\z/i 25 229 254
620 229 ▶ return 'help' if $line =~ /\A(?:\?|h|help)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A(?:\?|h|help)\z/i 2 227 229
621 227 ▶ return 'board' if $line =~ /\Aboard\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\Aboard\z/i 1 226 227
622 226 ▶ return 'pick' if $line =~ /\Apick\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\Apick\z/i 0 226 226
623 226 ▶ return 'type' if $line =~ /\Atype\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\Atype\z/i 0 226 226
624 226 ▶ return 'undo' if $line =~ /\Aundo\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\Aundo\z/i 5 221 226
625 221 ▶ return 'redo' if $line =~ /\Aredo\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\Aredo\z/i 1 220 221
626  
627 220 return $line;
628 }
629  
630 6689 ▶sub _glyphs { return $GLYPH{ $_[0]->ascii ? 'ascii' : 'wide' } }
Subroutine _glyphs: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 98 6591 6689
631  
632 1171 ▶sub _synced_keyboard {
Subroutine _synced_keyboard: called CC=1 SCAR=0.0
633 1171 my ($self) = @_;
634 1171 my $keyboard = $self->keyboard;
635 1171 $keyboard->in($self->in);
636 1171 $keyboard->keysource($self->keysource);
637 1171 $keyboard->pending($self->pending);
638 1171 return $keyboard;
639 }
640  
641 1154 ▶sub _synced_screen {
Subroutine _synced_screen: called CC=1 SCAR=0.0
642 1154 my ($self) = @_;
643 1154 my $screen = $self->screen;
644 1154 $screen->out($self->out);
645 1154 $screen->interactive($self->interactive);
646 1154 $screen->colour($self->ansi);
647 1154 return $screen;
648 }
649  
650 847 ▶sub _say {
Subroutine _say: called CC=2 SCAR=7.1
651 847 my ($self, @lines) = @_;
652 847 ▶ $self->_synced_screen->say(@lines) if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 847 0 847
653 847 return;
654 }
655  
656 # A partial line, so the handle has to be flushed or a prompt sits in the
657 # buffer while the bot thinks and the player stares at nothing.
658 0 ▶sub _print {
Subroutine _print: not called CC=1 SCAR=6.9
659 0 my ($self, $text) = @_;
660 0 my $out = $self->out;
661 0 print {$out} $text;
662 0 $self->_synced_screen->flush_on;
663 0 return;
664 }
665  
666 # An undef key is "leave it alone", which is what an ordinary house count
667 # wants: paint every cell and the marked ones stop standing out. Only a call
668 # with no key at all takes the default.
669 29309 ▶sub _paint {
Subroutine _paint: called CC=3 SCAR=11.0
670 29309 my ($self, $text, $key) = @_;
671 29309 ▶ $key = 'prompt' if @_ < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 0 29309 29309
672 29309 ▶ return $text unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1610 27699 29309
673 27699 my $screen = $self->screen;
674 27699 $screen->colour($self->ansi);
675 27699 return $screen->paint($text, $SGR{$key});
676 }
677  
678 1116 ▶sub _letters {
Subroutine _letters: called CC=2 SCAR=6.9
679 1116 my ($self, $houses, $key) = @_;
680 1116 my $row = ' ' x ($STORE_W + 4);
681 $row .= $self->_paint(Game::Oware::Notation->letter_of($_), $key) . ' ' x 6
682 1116 for @$houses;
683 1116 $row =~ s/\s+\z//;
684 1116 return $row;
685 }
686  
687 1116 ▶sub _store_cell {
Subroutine _store_cell: called CC=1 SCAR=0.0
688 1116 my ($self, $who, $count, $key) = @_;
689 1116 my $blank = ' ' x $STORE_W;
690 return (
691 1116 $blank,
692 $self->_paint(sprintf(' %-2s ', $who), $key),
693 $self->_paint(sprintf(' %2d ', $count), $key),
694 $blank,
695 $blank,
696 );
697 }
698  
699 1116 ▶sub _seeds {
Subroutine _seeds: called CC=1 SCAR=0.0
700 1116 my ($self, $board, $houses, $marks) = @_;
701 1116 my $g = $self->_glyphs;
702 return join $self->_paint($g->{v}, 'frame'),
703 1116 map { $self->_paint($self->_seed_text($board->[$_]),
704 6696 $self->_seed_key($board->[$_], $marks->{$_})) } @$houses;
705 }
706  
707 1116 ▶sub _counts {
Subroutine _counts: called CC=2 SCAR=6.9
708 1116 my ($self, $board, $houses, $marks) = @_;
709 1116 my $g = $self->_glyphs;
710 return join $self->_paint($g->{v}, 'frame'),
711 1116 map { $self->_paint(
712 sprintf(' %s %2d ', defined $marks->{$_} ? $marks->{$_} : ' ', $board->[$_]),
713 6696 ▶ $self->_count_key($board->[$_], $marks->{$_})) } @$houses;
Branch: 1 branch
BranchTrueFalseTotal
defined $marks->{$_} ? : 1839 4857 6696
714 }
715  
716 6696 ▶sub _seed_text {
Subroutine _seed_text: called CC=3 SCAR=11.0
717 6696 my ($self, $count) = @_;
718 6696 ▶ return ' ' x $HOUSE_W unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 2797 3899 6696
719  
720 # The overflow glyph is NOT a `+`: that is the mark for a house that gained
721 # a seed, and the two sat one row apart meaning different things.
722 3899 my $g = $self->_glyphs;
723 3899 ▶ return ' ' . ($g->{seed} x $SEED_CAP) . $g->{more} if $count > $SEED_CAP;
Branch: 1 branch
BranchTrueFalseTotal
if $count > $SEED_CAP 1354 2545 3899
724  
725 2545 my $pad = int(($HOUSE_W - $count) / 2);
726 2545 return (' ' x $pad) . ($g->{seed} x $count)
727 . (' ' x ($HOUSE_W - $pad - $count));
728 }
729  
730 6696 ▶sub _seed_key {
Subroutine _seed_key: called CC=8 SCAR=20.8
731 6696 my ($self, $count, $mark) = @_;
732 6696 ▶ return 'captured' if defined $mark && $mark eq 'x';
Branch: 1 branch
BranchTrueFalseTotal
if defined $mark and $mark eq "x" 73 6623 6696
Condition100%3/3
defined $mark and $mark eq "x"
!ll&&!rl&&r
4857176673
MC/DC100%2/2
defined $mark and $mark eq "x"
defined $mark $mark eq "x"
Truth table100%3/3
A and B
A= defined $markB= $mark eq "x"
ABresult
0X0
100
111
733 6623 ▶ return 'forfeited' if defined $mark && $mark eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if defined $mark and $mark eq "!" 0 6623 6623
Condition66%2/3
defined $mark and $mark eq "!"
!ll&&!rl&&r
485717660
MC/DC0%0/2
defined $mark and $mark eq "!"
defined $mark $mark eq "!"
Truth table66%2/3
A and B
A= defined $markB= $mark eq "!"
ABresult
0X0
100
111
734 6623 ▶ return 'empty' unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 2724 3899 6623
735 3899 ▶ return 'risk' if $count <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 2 1544 2355 3899
736 2355 ▶ return 'loaded' if $count >= 12;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 12 165 2190 2355
737 2190 return 'seed';
738 }
739  
740 6696 ▶sub _count_key {
Subroutine _count_key: called CC=10 SCAR=23.1
741 6696 my ($self, $count, $mark) = @_;
742  
743 6696 ▶ if (defined $mark) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mark) 1839 4857 6696
744 1839 ▶ return 'captured' if $mark eq 'x';
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "x" 73 1766 1839
745 1766 ▶ return 'forfeited' if $mark eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "!" 0 1766 1766
746 1766 ▶ return 'landed' if $mark eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "*" 470 1296 1766
747 1296 ▶ return 'gained' if $mark eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "+" 783 513 1296
748 513 ▶ return 'emptied' if $mark eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "-" 513 0 513
749 }
750  
751 4857 ▶ return 'empty' unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 2211 2646 4857
752 2646 ▶ return 'risk' if $count <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 2 917 1729 2646
753 1729 ▶ return 'loaded' if $count >= 12;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 12 119 1610 1729
754 1610 return undef;
755 }
756  
757 389 ▶sub _show {
Subroutine _show: called CC=5 SCAR=16.1
758 389 my ($self, $move) = @_;
759 389 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 3 386 389
760  
761 386 my $game = $self->game;
762 386 my $result = $game->result;
763  
764 # A sweep rewrote the board after the move, so the move's marks would be
765 # pointing at counts that are no longer the ones it produced.
766 386 ▶ my $swept = $result
Condition100%6/6
$result && ($result->reason eq 'cycle' || $result->reason eq 'no_feed')
!ll&&!rl&&r
37565
$result->reason eq "cycle" or $result->reason eq "no_feed"
l!l&&r!l&&!r
146
MC/DC100%3/3
$result && ($result->reason eq 'cycle' || $result->reason eq 'no_feed')
$result $result->reason eq "cycle" $result->reason eq "no_feed"
Truth table100%4/4
A && B or C
A= $resultB= $result->reason eq "cycle"C= $result->reason eq "no_feed"
ABCresult
0XX0
1000
11X1
1011
767 && ($result->reason eq 'cycle' || $result->reason eq 'no_feed');
768  
769 386 ▶ $self->_say('',
Branch: 1 branch
BranchTrueFalseTotal
$swept ? : 5 381 386
770 $self->board_text($game, $self->seat, $swept ? {} : $self->marks_for($move)),
771 $self->status_text($game));
772  
773 386 return;
774 }
775  
776 37 ▶sub _legend {
Subroutine _legend: called CC=1 SCAR=0.0
777 37 my ($self) = @_;
778 return (
779 37 ' ' . $self->_paint('-', 'emptied') . ' emptied '
780 . $self->_paint('+', 'gained') . ' gained a seed '
781 . $self->_paint('*', 'landed') . ' the last seed landed',
782 ' ' . $self->_paint('x', 'captured') . ' captured '
783 . $self->_paint('!', 'forfeited') . ' capture forfeited',
784 );
785 }
786  
787 36 ▶sub _intro {
Subroutine _intro: called CC=1 SCAR=0.0
788 36 my ($self) = @_;
789 36 $self->_say(
790 'Oware, ' . $self->variant . ' rules. You are ' . $self->seat . '.',
791 'Your six houses are the bottom row and your store is on the right.',
792 'Sow from one of them by typing its letter.',
793 '',
794 'The marks say what the last move did:',
795 $self->_legend,
796 '',
797 'Type help for the rules that surprise people, or quit to stop.',
798 );
799 36 return;
800 }
801  
802 1 ▶sub _help {
Subroutine _help: called CC=1 SCAR=0.0
803 1 my ($self) = @_;
804 1 $self->_say($self->_help_lines);
805 1 return;
806 }
807  
808 1 ▶sub _help_lines {
Subroutine _help_lines: called CC=1 SCAR=0.0
809 1 my ($self) = @_;
810 return (
811 1 'Take every seed from one of your houses and drop one in each house',
812 'counter-clockwise, skipping the house you took them from.',
813 '',
814 'If your last seed brings one of your opponent houses to exactly two or',
815 'three, you capture it, and the walk continues backwards while that',
816 'holds. Twenty-five seeds wins. Twenty-four each is a draw.',
817 '',
818 'Three rules surprise people:',
819 ' - a move that would take EVERY seed your opponent has takes none;',
820 ' - if your opponent has no seeds you must play a house that reaches',
821 ' them, and if you cannot, you take your own seeds and the game ends;',
822 ' - a game that will not end is stopped by a house rule, not a rule of',
823 ' Oware. See the note when it happens.',
824 '',
825 'The marks on the board:',
826 $self->_legend,
827 '',
828 'A house of one or two seeds is drawn in red, because that is what an',
829 'opponent captures by bringing it to two or three. A house of twelve or',
830 'more is drawn bright, because it laps the board.',
831 );
832 }
833  
834 244 ▶sub _prompt_for {
Subroutine _prompt_for: called CC=2 SCAR=6.9
835 244 my ($self, $seat) = @_;
836  
837 244 my $game = $self->game;
838 244 my @legal = @{ $game->legal($seat) };
839 244 my @sowable = Game::Oware::Rules->sowable($game->board, $seat);
840  
841 244 ▶ if (@sowable > @legal) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sowable > @legal) 3 241 244
842 3 my $them = Game::Oware::Board->other($seat);
843 $self->_say('', $them . ' has no seeds, so you must play a house that'
844 . ' reaches them: '
845 3 . join(' ', map { Game::Oware::Notation->letter_of($_) } @legal));
846 }
847  
848 244 return join ' ', map { Game::Oware::Notation->letter_of($_) } @legal;
849 }
850  
851 365 ▶sub _human_turn {
Subroutine _human_turn: called CC=2 SCAR=6.9
852 365 my ($self) = @_;
853 365 ▶ return $self->picking ? $self->pick : $self->_typed_turn;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking ? : 126 239 365
854 }
855  
856 164 ▶sub _show_choice {
Subroutine _show_choice: called CC=5 SCAR=16.1
857 164 my ($self, $picker, $live) = @_;
858  
859 164 my $game = $self->game;
860 164 my $seat = $self->seat;
861 164 my $log = $self->log;
862  
863 # THE ROUND SO FAR, every frame. The picker redraws the screen on every
864 # keystroke, so the narration printed when a move was played has already
865 # gone by the time there is anything to choose. Both plies, not just the
866 # opponent's: a player who cannot see their own capture, or what it was
867 # answered with, is choosing in the dark.
868 164 my @lines = map { @$_ } @{ $log->recent };
869 164 ▶ push @lines, '' if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 121 43 164
870  
871 # THE BOARD DRAWN HERE IS THE ONE THE MOVE WOULD PRODUCE, not the one on
872 # the table with a path drawn over it. The first version did the latter,
873 # the way Game::Checkers highlights a jump, and it was wrong for this
874 # game: the marks said where the seeds went while every count was still
875 # the count from before they moved, so the cursor house read `- 1` for a
876 # house it had just emptied. Here a capture shows the store going up.
877 #
878 # `v` swaps it for the position as it stands, carrying the opponent's own
879 # marks, because a preview is not a substitute for the board you are
880 # actually playing from.
881 164 my ($board, $marks, $title);
882 164 ▶ if ($live) {
Branch: 1 branch
BranchTrueFalseTotal
if ($live) { } 1 163 164
883 1 $board = $game->board;
884 1 $marks = $self->marks_for($self->last_move);
885 1 $title = ' as it stands:';
886 }
887 else {
888 163 my $house = $picker->current;
889 163 my $move;
890 163 ($board, $move) = $self->preview($house);
891 163 $marks = $self->marks_for($move);
892 163 $title = ' if you sow '
893 . Game::Oware::Notation->letter_of($house) . ':';
894 }
895  
896 164 push @lines,
897 $title,
898 $self->board_text($board, $seat, $marks),
899 $self->status_text($board),
900 '';
901  
902 # The list holds only legal moves, so a player looking for a house the
903 # feeding rule took away has nothing to read. Say it where the list is.
904 164 my @sowable = Game::Oware::Rules->sowable($game->board, $seat);
905 push @lines, Game::Oware::Board->other($seat) . ' has no seeds, so only a'
906 . ' house that reaches them can be played:', ''
907 164 ▶ if @sowable > @{ $picker->items };
Branch: 1 branch
BranchTrueFalseTotal
if @sowable > @{$picker->items;} 5 159 164
908  
909 164 push @lines, $picker->rows;
910  
911 164 my @notes = @{ $log->notes };
912 164 ▶ push @lines, '', @notes if @notes;
Branch: 1 branch
BranchTrueFalseTotal
if @notes 10 154 164
913 164 push @lines, '', map { $self->_paint($_, 'frame') } $self->legend;
914  
915 164 $self->_synced_screen->flush_on->show(\@lines);
916 164 $log->shown;
917  
918 164 return;
919 }
920  
921 2 ▶sub _why_not {
Subroutine _why_not: called CC=3 SCAR=11.1
922 2 my ($self, $house, $seat) = @_;
923 2 ▶ my $flag = Game::Oware::Board->owner_of($house) ne $seat ? 'not_your_house'
Branch: 2 branches
BranchTrueFalseTotal
'Game::Oware::Board'->owner_of($house) ne $seat ? : 1 1 2
!$self->game->board->[$house] ? : 1 0 1
924 : !$self->game->board->[$house] ? 'empty_house'
925 : 'must_feed';
926 2 return Game::Oware::Error->throw($flag)->message;
927 }
928  
929 244 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=17 SCAR=29.7
930 244 my ($self) = @_;
931  
932 244 my $game = $self->game;
933 244 my $seat = $self->seat;
934  
935 244 my $legal = $self->_prompt_for($seat);
936  
937 244 while (1) {
938 # The newline belongs to the PROMPT, not to whatever answers it. On a
939 # terminal the echoed Return supplies one; on a pipe it does not, and
940 # the first version of this loop printed every refusal, every help
941 # screen and every redrawn board onto the prompt's own line.
942 261 my $line = $self->_synced_keyboard->read_line(
943 "\n" . $self->_paint('your move', 'prompt') . " [$legal] > ",
944 $self->out);
945 261 ▶ if (my (undef, $file) = Game::Terminal::Kit::Save::command($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $file) = Game::Terminal::Kit::Save::command($line)) 11 250 261
946 11 $self->_say('', $self->save($file));
947 11 next;
948 }
949 250 my $command = $self->command($line);
950  
951 250 ▶ return 0 if $command eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "quit" 24 226 250
952  
953 226 ▶ if ($command eq 'help') { $self->_say(''); $self->_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "help") 1 225 226
954 225 ▶ if ($command eq 'again') { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "again") 1 224 225
955 0 ▶ if ($command eq 'board') { $self->_show; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "board") 0 224 224
956 0 ▶ if ($command eq 'type') { $self->picking(0); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "type") 0 224 224
957  
958 224 ▶ if ($command eq 'undo' || $command eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "undo" or $command eq "redo") 6 218 224
Condition100%3/3
$command eq "undo" or $command eq "redo"
l!l&&r!l&&!r
51218
MC/DC100%2/2
$command eq "undo" or $command eq "redo"
$command eq "undo" $command eq "redo"
Truth table100%3/3
A or B
A= $command eq "undo"B= $command eq "redo"
ABresult
1X1
011
000
959 6 ▶ next unless $command eq 'undo' ? $self->take_back : $self->play_again;
Branch: 2 branches
BranchTrueFalseTotal
unless $command eq "undo" ? $self->take_back : $self->play_again 1 5 6
$command eq 'undo' ? : 5 1 6
960 5 ▶ $self->_show unless $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->picking 5 0 5
961 5 return 1;
962 }
963  
964 218 ▶ if ($command eq 'pick') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "pick") 0 218 218
965 0 $self->picking(1);
966 0 ▶ return $self->pick if $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keys_available 0 0 0
967 0 $self->picking(0);
968 0 $self->_say('', 'The keys are not available here, so this game is'
969 . ' typed. Term::ReadKey and a terminal are what it takes.');
970 0 next;
971 }
972  
973 218 my $house = eval { Game::Oware::Notation->index_of($command) };
974 218 ▶ if (!defined $house) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $house) 2 216 218
975 2 $self->_say('', 'That is not a house. Type one of: ' . $legal);
976 2 next;
977 }
978  
979 216 my $out_of = $game->play($seat, $house);
980 216 ▶ if (ref $out_of && $out_of->isa('Game::Oware::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out_of and $out_of->isa("Game::Oware::Error")) 1 215 216
Condition66%2/3
ref $out_of and $out_of->isa("Game::Oware::Error")
!ll&&!rl&&r
02151
MC/DC50%1/2
ref $out_of and $out_of->isa("Game::Oware::Error")
ref $out_of $out_of->isa("Game::Oware::Error")
Truth table66%2/3
A and B
A= ref $out_ofB= $out_of->isa("Game::Oware::Error")
ABresult
0X0
100
111
981 1 $self->_say('', ucfirst($out_of->message) . '.');
982 1 next;
983 }
984  
985 215 $self->_after_ply($out_of, $seat);
986 215 return 1;
987 }
988 }
989  
990 331 ▶sub _bot_turn {
Subroutine _bot_turn: called CC=3 SCAR=12.0
991 331 my ($self, $seat) = @_;
992  
993 331 my $game = $self->game;
994  
995 331 $self->_thinking($seat);
996 331 my $house = $self->bot->choose($game, $seat);
997 331 $self->_thought;
998  
999 # Unreachable while the engine ends a game the moment a seat has no legal
1000 # move. A `return` here instead would hand control back to a loop whose
1001 # condition has not changed, so the program would spin in silence.
1002 331 ▶ unless (defined $house) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $house) 0 331 331
1003 0 ▶ $self->_say('', "$seat has no move, so the game cannot go on.")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
1004 unless $self->quiet;
1005 0 return 0;
1006 }
1007  
1008 331 my $move = $game->play($seat, $house);
1009 331 $self->_after_ply($move, $seat);
1010  
1011 331 return 1;
1012 }
1013  
1014 331 ▶sub _thinking {
Subroutine _thinking: called CC=2 SCAR=9.2
1015 331 my ($self, $seat) = @_;
1016 331 ▶ return unless $self->progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->progress 331 0 331
1017 0 $self->_print($self->_paint("$seat is thinking", 'frame'));
1018 0 return;
1019 }
1020  
1021 331 ▶sub _thought {
Subroutine _thought: called CC=2 SCAR=9.2
1022 331 my ($self) = @_;
1023 331 ▶ return unless $self->progress;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->progress 331 0 331
1024 0 $self->_print("\r" . (' ' x 20) . "\r");
1025 0 return;
1026 }
1027  
1028 # Everything one ply owes the screen, in one place, because the two front ends
1029 # want opposite things from it. The typed game scrolls, so it narrates and
1030 # redraws; the picker owns the screen and reprints both plies in its own header
1031 # on every keystroke, so anything drawn here would be a flash and nothing more.
1032 667 ▶sub _after_ply {
Subroutine _after_ply: called CC=4 SCAR=13.9
1033 667 my ($self, $move, $seat) = @_;
1034  
1035 # Recorded whether or not it is printed. The picker redraws the screen on
1036 # every keystroke, so this is the only surviving copy of the round, and a
1037 # player who cannot see their own capture or the reply to it is choosing
1038 # in the dark.
1039 667 $self->last_move($move);
1040 667 $self->log->record($self->_narrate_lines($move, $seat, 1));
1041  
1042 667 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 76 591 667
1043 591 ▶ return if $self->picking && $self->game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and $self->game->status eq "active" 237 354 591
Condition100%3/3
$self->picking and $self->game->status eq "active"
!ll&&!rl&&r
3522237
MC/DC100%2/2
$self->picking and $self->game->status eq "active"
$self->picking $self->game->status eq "active"
Truth table100%3/3
A and B
A= $self->pickingB= $self->game->status eq "active"
ABresult
0X0
100
111
1044  
1045 354 $self->_say('', $self->_narrate_lines($move, $seat));
1046 354 $self->_show($move);
1047  
1048 354 return;
1049 }
1050  
1051 1021 ▶sub _narrate_lines {
Subroutine _narrate_lines: called CC=6 SCAR=17.9
1052 1021 my ($self, $move, $seat, $past) = @_;
1053  
1054 1021 my $name = Game::Oware::Notation->letter_of($move->house);
1055 1021 my $them = Game::Oware::Board->other($seat);
1056 1021 ▶ my $sows = $past ? 'sowed' : 'sows';
Branch: 1 branch
BranchTrueFalseTotal
$past ? : 667 354 1021
1057  
1058 1021 my @lines = ($self->_paint("$seat $sows $name.", 'prompt'));
1059  
1060 1021 ▶ push @lines, ' The sow went right round, so it skipped '
Branch: 1 branch
BranchTrueFalseTotal
if $move->sown >= 12 38 983 1021
1061 . "$name on the way past."
1062 if $move->sown >= 12;
1063  
1064 1021 ▶ if (@{ $move->forfeited }) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$move->forfeited;}) { } 2 1019 1021
elsif (@{$move->captured;}) { } 96 923 1019
1065 my $houses = join ', ',
1066 2 map { Game::Oware::Notation->letter_of($_) } @{ $move->forfeited };
1067 2 push @lines, " That would have taken every seed $them has, from "
1068 . "$houses, so it takes none and they stay on the board.";
1069 }
1070 1019 elsif (@{ $move->captured }) {
1071 my $houses = join ', ',
1072 96 map { Game::Oware::Notation->letter_of($_) } @{ $move->captured };
1073 96 push @lines, ' It captures ' . $move->taken . " seeds, from $houses.";
1074 96 ▶ push @lines, ' Every seed left on the board goes to ' . $them . '.'
Branch: 1 branch
BranchTrueFalseTotal
if $move->slammed 0 96 96
1075 if $move->slammed;
1076 }
1077  
1078 1021 return @lines;
1079 }
1080  
1081 39 ▶sub _finish {
Subroutine _finish: called CC=8 SCAR=20.9
1082 39 my ($self) = @_;
1083  
1084 39 my $game = $self->game;
1085 39 my $result = $game->result;
1086  
1087 39 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 26 13 39
1088 26 ▶ $self->_say('', 'Stopped. Nobody won.') unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 25 1 26
1089 26 return;
1090 }
1091  
1092 13 my $reason = $result->reason;
1093  
1094 13 ▶ if ($reason eq 'no_feed' && !$self->quiet) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reason eq 'no_feed' and not $self->quiet) { } 4 9 13
elsif ($reason eq 'cycle') { } 2 7 9
Condition66%2/3
$reason eq "no_feed" and not $self->quiet
!ll&&!rl&&r
904
MC/DC50%1/2
$reason eq "no_feed" and not $self->quiet
$reason eq "no_feed" not $self->quiet
Truth table66%2/3
A and not(B)
A= $reason eq "no_feed"B= not $self->quiet
ABresult
0X0
100
111
1095 4 my ($swept) = map { $_->{payload}{p} }
1096 4 grep { $_->{kind} eq 'sweep' } @{ $game->events };
1097 4 $self->_say('', "$swept had no move that could give the other side seeds,"
1098 . ' so it took every seed in its own territory and the game ended.');
1099 }
1100 elsif ($reason eq 'cycle') {
1101 # NOT narration, so quiet does not silence it. A game ended by a rule
1102 # that no book contains has to say so however terse the output is.
1103 2 my ($cycle) = grep { $_->{kind} eq 'cycle' } @{ $game->events };
1104 my $times = $cycle->{payload}{times}
1105 2 ▶ || Game::Oware::Variant::repetition_limit($self->variant);
Condition33%1/3
$cycle->{'payload'}{'times'} || Game::Oware::Variant::repetition_limit($self->variant)
l!l&&r!l&&!r
200
MC/DC0%0/2
$cycle->{'payload'}{'times'} || Game::Oware::Variant::repetition_limit($self->variant)
$cycle->{'payload'}{'times'} Game::Oware::Variant::repetition_limit($self->variant)
Truth table33%1/3
A || B
A= $cycle->{'payload'}{'times'}B= Game::Oware::Variant::repetition_limit($self->variant)
ABresult
1X1
011
000
1106 my $why = $cycle->{payload}{why} eq 'repetition'
1107 ? 'the same position came round ' . _times($times)
1108 2 ▶ : $cycle->{payload}{plies} . ' moves passed with nothing captured';
Branch: 1 branch
BranchTrueFalseTotal
$cycle->{'payload'}{'why'} eq 'repetition' ? : 2 0 2
1109 2 $self->_say('', "This game was going nowhere: $why.",
1110 'So each side took the seeds on its own half.',
1111 'THAT IS A HOUSE RULE AND NOT A RULE OF OWARE. The published rule',
1112 'ends a cycle when both players agree, which is not something this',
1113 'program can ask you.');
1114 }
1115  
1116 13 $self->_say('', $self->_paint($result->stringify . '.', 'prompt'));
1117  
1118 13 return;
1119 }
1120  
1121 2 ▶sub _times {
Subroutine _times: called CC=3 SCAR=14.2
1122 2 my ($count) = @_;
1123 2 ▶ return $TIMES[$count] if $count && $count <= $#TIMES;
Branch: 1 branch
BranchTrueFalseTotal
if $count and $count <= $#TIMES 2 0 2
Condition33%1/3
$count and $count <= $#TIMES
!ll&&!rl&&r
002
MC/DC0%0/2
$count and $count <= $#TIMES
$count $count <= $#TIMES
Truth table33%1/3
A and B
A= $countB= $count <= $#TIMES
ABresult
0X0
100
111
1124 0 return "for the ${count}th time";
1125 }
1126  
1127 12 ▶sub settings {
Subroutine settings: called CC=1 SCAR=0.0
Pod documented
1128 12 my ($self) = @_;
1129 12 return { level => $self->level, variant => $self->variant, seat => $self->seat, seed => $self->seed };
1130 }
1131  
1132 7 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=7.1
Pod documented
1133 7 my ($self) = @_;
1134 7 my $file = $self->history->file;
1135 7 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('oware');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 7 0 7
1136 }
1137  
1138 12 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
1139 12 my ($self, $file) = @_;
1140 12 ▶ $file = $self->save_default unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 6 6 12
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
606
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
1141 12 my $history = $self->history;
1142 12 $history->settings($self->settings);
1143 12 $history->version($VERSION);
1144 12 my $path = eval { $history->saved($self->game)->write_file($file) };
1145 12 ▶ return 'The game was not saved: ' . Game::Terminal::Kit::Save::why($@) . '.' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 12 12
1146 12 $history->file($path);
1147 12 return "The game is saved to $path.";
1148 }
1149  
1150 9 ▶sub read_save {
Subroutine read_save: called CC=1 SCAR=0.0
Pod documented
1151 9 my ($class, $file) = @_;
1152 9 return Game::Terminal::Kit::Save->read_game($file, 'oware');
1153 }
1154  
1155 3 ▶sub replay_lines {
Subroutine replay_lines: called CC=5 SCAR=16.5
Pod documented
1156 3 my ($self, $game) = @_;
1157 3 my @events = grep { $_->{kind} eq 'sow' } @{ $game->log };
1158 3 ▶ my $last = @events ? $events[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@events ? : 2 1 3
1159 3 my $result = $game->result;
1160 return ($self->board_text($game, $self->seat), $self->status_text($game),
1161 3 ▶ ($last ? ('', ' ' . $last->{actor} . ' sowed ' . Game::Oware::Notation->letter_of($last->{payload}{house}) . '.') : ()),
Branch: 3 branches
BranchTrueFalseTotal
$last ? : 2 1 3
$result ? : 0 3 3
defined $result->winner ? : 0 0 0
1162 ($result ? (' The game is over' . (defined $result->winner ? ': ' . $result->winner . ' won.' : ': a draw.')) : ()));
1163 }
1164  
1165 44 ▶sub _defaults {
Subroutine _defaults: called CC=1 SCAR=0.0
1166 return (
1167 44 level => 3,
1168 variant => 'abapa',
1169 seat => 'p1',
1170 seed => '',
1171 quiet => 0,
1172 ascii => 0,
1173 );
1174 }
1175  
1176 11 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
1177 11 my %opt = _defaults();
1178  
1179 11 my @levels = Game::Oware::Bot->levels;
1180 11 my @variants = Game::Oware::Variant::variants();
1181  
1182 11 my $levels = $levels[0] . ' to ' . $levels[-1];
1183 11 my $variants = join ' or ', @variants;
1184  
1185 11 return <<"USAGE";
1186 oware - play Oware against a bot
1187  
1188 Usage: oware [options]
1189  
1190 -l, --level N bot level, $levels (default $opt{level})
1191 -s, --seat SEAT the seat you play, p1 or p2 (default $opt{seat})
1192 -V, --variant V $variants (default $opt{variant})
1193 --seed STRING the seed, so a game can be replayed
1194 -q, --quiet only the result: no banner, narration or board
1195 -a, --ascii plain box drawing, for a terminal without the glyphs
1196 --ansi force colour on, --no-ansi forces it off
1197 --no-pick type the moves out instead of choosing them with the keys
1198 --save FILE where S, or "save", writes the game: FILE, else the file
1199 it was resumed from, else oware.save
1200 --resume FILE carry on a saved game, with its undo and redo
1201 --replay FILE watch a saved game a turn at a time; enter plays on
1202 -v, --version print the version
1203 -h, --help this
1204  
1205 On a terminal the legal moves are listed under the board and chosen with the
1206 arrow keys: the board above the list is the one the move under the cursor
1207 would produce, so a capture is seen before it is played. Enter sows it, a
1208 house letter jumps to that house, u takes back your last move and the reply,
1209 r plays them again, S saves the game, t goes back to typing and ? prints the
1210 rules. Needs
1211 Term::ReadKey; without it, or off a terminal, the moves are typed out as a
1212 letter at a time, and undo, redo and save are typed too.
1213  
1214 The board marks what the last move did: - emptied, + gained a seed, * where
1215 the last seed landed, x captured, ! capture forfeited. Those are characters
1216 rather than colours, so they survive --no-ansi and a redirected handle.
1217  
1218 Levels $levels[-2] and $levels[-1] search a great deal deeper and take noticeably
1219 longer a move. The budget is in nodes rather than seconds, so a loaded
1220 machine plays the same game as an idle one.
1221  
1222 Exit status is 0 if you won or drew, 1 if you lost, 2 for a bad option.
1223 USAGE
1224 }
1225  
1226 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
1227 return [
1228 2 { name => 'level', type => 'int', min => 1, max => 5, help => 'bot level' },
1229 { name => 'seat', type => 'enum', choices => [qw(p1 p2)], help => 'the seat you play' },
1230 { name => 'variant', type => 'enum', choices => [qw(abapa awari)], help => 'the rules' },
1231 { name => 'seed', type => 'str', help => 'so a game can be replayed' },
1232 ];
1233 }
1234  
1235 33 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
1236 33 my ($class, @argv) = @_;
1237 33 my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case));
1238 33 my $status = $class->_run(\@argv);
1239 33 Getopt::Long::Configure($saved);
1240 33 return $status;
1241 }
1242  
1243 33 ▶sub _run {
Subroutine _run: called CC=34 SCAR=40.4
1244 33 my ($class, $argv) = @_;
1245 33 my %default = _defaults();
1246 33 my %opt;
1247  
1248 Getopt::Long::GetOptionsFromArray($argv, \%opt,
1249 'level|l=i',
1250 'variant|V=s',
1251 'seat|s=s',
1252 'seed=s',
1253 'quiet|q',
1254 'ascii|a',
1255 'ansi!',
1256 'pick!',
1257 'version|v',
1258 'help|h',
1259 Game::Terminal::Kit::Save::flags(),
1260 33 ▶ ) or do { print STDERR _usage(); return 2 };
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray($argv, \%opt, "level|l=i", "variant|V=s", "seat|s=s", "seed=s", "quiet|q", "ascii|a", "ansi!", "pick!", "version|v", "help|h", Game::Terminal::Kit::Save::flags()) or do { print STDERR _usage(); return 2 } 31 2 33
1261  
1262 31 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 9 22 31
1263  
1264 22 ▶ if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 1 21 22
1265 1 print "oware $Game::Oware::VERSION\n";
1266 1 return 0;
1267 }
1268  
1269 21 ▶ if (@{$argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv;}) 0 21 21
1270 0 print STDERR "oware: there is nothing to do with '$argv->[0]': options begin with --\n", _usage();
1271 0 return 2;
1272 }
1273 21 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) 2 19 21
1274 2 print STDERR "oware: $why.\n";
1275 2 return 2;
1276 }
1277  
1278 19 ▶ my $from = defined $opt{resume} ? $opt{resume} : $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 6 13 19
1279 19 my $save;
1280 19 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 8 11 19
1281 8 ($save, my $why) = $class->read_save($from);
1282 8 ▶ unless ($save) { print STDERR "oware: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 2 6 8
1283 6 my $settings = $save->settings;
1284 6 ▶ defined $opt{$_} or $opt{$_} = $settings->{$_} for grep { defined $settings->{$_} } qw(level variant seat seed);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $settings->{$_} 1 23 24
1285 }
1286 17 ▶ defined $opt{$_} or $opt{$_} = $default{$_} for keys %default;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $default{$_} 43 59 102
1287  
1288 17 my $history = _new_history();
1289 17 my $game;
1290 17 ▶ if ($save && !defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save and not defined $opt{"replay"}) 5 12 17
Condition100%3/3
$save and not defined $opt{"replay"}
!ll&&!rl&&r
1115
MC/DC100%2/2
$save and not defined $opt{"replay"}
$save not defined $opt{"replay"}
Truth table100%3/3
A and not(B)
A= $saveB= not defined $opt{"replay"}
ABresult
0X0
100
111
1291 5 $game = eval { $history->resume($save) };
1292 5 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 5 5
1293 0 print STDERR "oware: '" . $save->file . "' cannot be carried on: " . Game::Terminal::Kit::Save::why($@) . ".\n";
1294 0 return 2;
1295 }
1296 }
1297  
1298 17 my $terminal = eval {
1299 $class->new(
1300 level => $opt{level},
1301 variant => $opt{variant},
1302 seat => $opt{seat},
1303 seed => $opt{seed},
1304 quiet => $opt{quiet} ? 1 : 0,
1305 ascii => $opt{ascii} ? 1 : 0,
1306 history => $history,
1307 ($game ? (game => $game) : ()),
1308 (defined $opt{save} ? (save_file => $opt{save}) : ()),
1309 (exists $opt{ansi} ? (ansi => $opt{ansi} ? 1 : 0) : ()),
1310 17 ▶ (exists $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 8 branches
BranchTrueFalseTotal
$opt{'quiet'} ? : 1 16 17
$opt{'ascii'} ? : 0 17 17
$game ? : 5 12 17
defined $opt{'save'} ? : 6 11 17
exists $opt{'ansi'} ? : 0 17 17
$opt{'ansi'} ? : 0 0 0
exists $opt{'pick'} ? : 0 17 17
$opt{'pick'} ? : 0 0 0
1311 );
1312 };
1313  
1314 17 ▶ unless ($terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 3 14 17
1315 3 ▶ my $why = $@ || 'could not start';
Condition50%1/2
$@ || 'could not start'
l!l
30
MC/DC0%0/1
$@ || 'could not start'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1316 3 $why =~ s/ at \S+ line \d+\.?\s*\z//;
1317 3 $why =~ s/\A[\w:]+: //;
1318 3 chomp $why;
1319 3 print STDERR "oware: $why\n";
1320 3 return 2;
1321 }
1322  
1323 14 ▶ if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"replay"}) 1 13 14
1324 my $replay = Game::Terminal::Kit::Replay->new(
1325 save => $save,
1326 3 ▶ load => sub { Game::Oware->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1327 3 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1328 1 ▶ head => [ 'Oware, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 1 0 1
1329 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
1330 screen => $terminal->screen,
1331 );
1332 my ($event, $step) = Game::Terminal::Kit::Run::guard(
1333 1 ▶ sub { $replay->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
1334 1 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 0 1
1335 0 $game = eval { $terminal->history->resume($save->from_step($step)) };
1336 0 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 0 0
1337 0 print STDERR "oware: '" . $save->file . "' cannot be carried on: " . Game::Terminal::Kit::Save::why($@) . ".\n";
1338 0 return 2;
1339 }
1340 0 $terminal->swap($game);
1341 0 ▶ $terminal->history->file($opt{save}) if defined $opt{save};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"save"} 0 0 0
1342 }
1343  
1344 my $result = Game::Terminal::Kit::Run::guard(
1345 13 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
1346  
1347 13 ▶ return 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 13 0 13
1348 0 ▶ return 0 if !defined $result->winner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result->winner 0 0 0
1349 0 ▶ return 0 if $result->winner eq $opt{seat};
Branch: 1 branch
BranchTrueFalseTotal
if $result->winner eq $opt{"seat"} 0 0 0
1350 0 return 1;
1351 }
1352  
1353 30 1;
1354  
1355 __END__
« blib/lib/Game/Oware.pm ↑ Summary blib/lib/Game/Oware/Result.pm »