blib/lib/Game/Oware/Terminal.pm

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