blib/lib/Game/Reversi/Terminal.pm

filter: Statementstmt 92.7% 409 / 441 Branchbran 81.4% 202 / 248 Conditioncond 64.2% 45 / 70 MC/DCmcdc 46.6% 21 / 45 Subroutinesub 94.1% 64 / 68 Podpod 96.0% 24 / 25 totaltotal 85.2% 765 / 897 CC 154 SCAR 27.2
CC
154
Cov
86%
run
44.4
pick_square
36.1
_typed_turn
19.3
CRAP 15.2
↑ Summary blib/lib/Game/Reversi.pm »
1 package Game::Reversi::Terminal;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use Game::Reversi;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Game::Reversi::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Game::Reversi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Game::Reversi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Game::Reversi::Opening;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Game::Terminal::Kit::Steer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 23 our $VERSION = '0.05';
23  
24 23 my %GLYPH = (
25 b => { wide => "\x{25CF}", ascii => 'X' },
26 w => { wide => "\x{25CB}", ascii => 'O' },
27 );
28  
29 23 my %TURNED = (
30 b => { wide => "\x{25C9}", ascii => 'x' },
31 w => { wide => "\x{25CE}", ascii => 'o' },
32 );
33 23 my %NAME = (b => 'dark', w => 'light');
34  
35 23 my %LINE = (
36 wide => { h => "\x{2500}", v => "\x{2502}", tl => "\x{250C}", tr => "\x{2510}",
37 bl => "\x{2514}", br => "\x{2518}", t => "\x{252C}", b => "\x{2534}",
38 l => "\x{251C}", r => "\x{2524}", x => "\x{253C}" },
39 ascii => { h => '-', v => '|', tl => '+', tr => '+', bl => '+', br => '+',
40 t => '+', b => '+', l => '+', r => '+', x => '+' },
41 );
42  
43 23 my %INK = (b => '1', w => '2', '*' => '33', grid => '2',
44 turned => '1;32', placed => '1;36', cursor => '1;7');
45  
46 23 my %STEP = (left => -1, up => -1, back_tab => -1,
47 right => 1, down => 1, tab => 1);
48  
49 has in => (
50 is => 'rw',
51 isa => Any,
52 31 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
53 23 );
54  
55 has out => (
56 is => 'rw',
57 isa => Any,
58 21 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
59 23 );
60  
61 has level => (
62 is => 'ro',
63 isa => Int,
64 default => 3
65 23 );
66  
67 has variant => (
68 is => 'ro',
69 isa => Str,
70 default => 'historic'
71 23 );
72  
73 has colour => (
74 is => 'ro',
75 isa => Str,
76 default => 'b'
77 23 );
78  
79 has seed => (
80 is => 'ro',
81 default => 'terminal'
82 23 );
83  
84 has [qw/quiet ascii/] => (
85 is => 'rw',
86 isa => Bool,
87 default => 0
88 23 );
89  
90 has game => (
91 is => 'rw',
92 isa => Any
93 23 );
94  
95 23 has last_move => (is => 'rw');
96  
97 23 has keysource => (is => 'rw');
98  
99 55 ▶has pending => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
100  
101 23 has [qw/keyboard screen/] => (is => 'rw');
102  
103 23 has history => (is => 'rw');
104  
105 23 has save_file => (is => 'rw');
106  
107 23 has _picking => (is => 'rw', init_arg => 'picking', private => 1);
108  
109 has _wants_ansi => (
110 is => 'rw',
111 init_arg => 'ansi',
112 private => 1
113 23 );
114  
115 55 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.2
Pod undocumented
116 55 my ($self) = @_;
117  
118 55 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 55 0 55
$self->keysource ? : 21 34 55
119 in => $self->in,
120 pending => $self->pending,
121 ($self->keysource ? (keysource => $self->keysource) : ()),
122 )) unless $self->keyboard;
123 55 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->screen 55 0 55
124 out => $self->out,
125 interactive => $self->interactive,
126 colour => $self->ansi,
127 ascii => $self->ascii,
128 )) unless $self->screen;
129 55 $self->screen->utf8->flush_on;
130 55 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 55 0 55
131 55 ▶ $self->history->name('reversi') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 55 55
132 55 $self->history->version($VERSION);
133 55 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 7 48 55
134  
135 55 ▶ die "Game::Reversi::Terminal: colour must be b or w"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour eq "b" or $self->colour eq "w" 1 54 55
Condition100%3/3
$self->colour eq "b" or $self->colour eq "w"
l!l&&r!l&&!r
5131
MC/DC100%2/2
$self->colour eq "b" or $self->colour eq "w"
$self->colour eq "b" $self->colour eq "w"
Truth table100%3/3
A or B
A= $self->colour eq "b"B= $self->colour eq "w"
ABresult
1X1
011
000
136 unless $self->colour eq 'b' || $self->colour eq 'w';
137 54 ▶ die "Game::Reversi::Terminal: there is no variant '" . $self->variant . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless "Game::Reversi::Opening"->describes($self->variant) 1 53 54
138 unless Game::Reversi::Opening->describes($self->variant);
139  
140 die 'Game::Reversi::Terminal: there is no level ' . $self->level
141 53 ▶ unless grep { $_ == $self->level } Game::Reversi::Bot->levels;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $self->level;} "Game::Reversi::Bot"->levels 1 52 53
142  
143 52 return $self;
144 }
145  
146 55 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
147 return Game::Terminal::Kit::History->new(
148 name => 'reversi',
149 version => $VERSION,
150 242 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 12 ▶ load => sub { Game::Reversi->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 55 );
153 }
154  
155 14346 ▶sub ansi {
Subroutine ansi: called CC=4 SCAR=13.9
Pod documented
156 14346 my ($self) = @_;
157 14346 ▶ return 0 if Game::Terminal::Kit::Screen::no_colour();
Branch: 1 branch
BranchTrueFalseTotal
if Game::Terminal::Kit::Screen::no_colour() 1527 12819 14346
158 12819 ▶ return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_ansi 2164 10655 12819
$self->_wants_ansi ? : 29 2135 2164
159 10655 return $self->interactive;
160 }
161  
162 497 ▶sub _reader {
Subroutine _reader: called CC=2 SCAR=6.9
163 497 my ($self) = @_;
164 497 my $keyboard = $self->keyboard;
165 497 $keyboard->in($self->in);
166 497 $keyboard->pending($self->pending);
167 497 ▶ $keyboard->keysource($self->keysource) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 103 394 497
168 497 return $keyboard;
169 }
170  
171 14286 ▶sub _writer {
Subroutine _writer: called CC=1 SCAR=0.0
172 14286 my ($self) = @_;
173 14286 my $screen = $self->screen;
174 14286 $screen->out($self->out);
175 14286 $screen->interactive($self->interactive);
176 14286 $screen->colour($self->ansi);
177 14286 return $screen;
178 }
179  
180 13097 ▶sub _paint {
Subroutine _paint: called CC=1 SCAR=0.0
181 13097 my ($self, $glyph, $key) = @_;
182 13097 return $self->_writer->paint($glyph, $INK{$key});
183 }
184  
185 1127 ▶sub _say {
Subroutine _say: called CC=1 SCAR=0.0
186 1127 my ($self, @lines) = @_;
187 1127 $self->_writer->say(@lines);
188 1127 return;
189 }
190  
191 208 ▶sub _ask {
Subroutine _ask: called CC=3 SCAR=11.0
192 208 my ($self, $prompt) = @_;
193 208 my $line = $self->_reader->read_line($prompt, $self->out);
194 208 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 207 208
195 207 $self->_say('');
196 207 ▶ return wantarray ? (lc $line, $line) : lc $line;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 207 0 207
197 }
198  
199 6409 ▶sub glyph {
Subroutine glyph: called CC=2 SCAR=6.9
Pod documented
200 6409 my ($self, $colour) = @_;
201 6409 ▶ return $GLYPH{$colour}{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1639 4770 6409
202 }
203  
204 308 ▶sub board_text {
Subroutine board_text: called CC=8 SCAR=20.8
Pod documented
205 308 my ($self, $game, $colour, %o) = @_;
206 1616 my %legal = map { $_->square => 1 }
207 308 ▶ @{ defined $colour ? $game->legal($colour) : [] };
Branch: 1 branch
BranchTrueFalseTotal
defined $colour ? : 275 33 308
208 308 ▶ my %flipped = map { $_ => 1 } @{ $o{flips} || [] };
Condition100%2/2
$o{'flips'} || []
l!l
187121
MC/DC100%1/1
$o{'flips'} || []
$o{'flips'}
Truth table100%2/2
A
A= $o{'flips'}
Aresult
11
00
209 308 ▶ my $line = $LINE{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 112 196 308
210 308 ▶ my $kind = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 112 196 308
211  
212 308 my $files = ' ' . join ' ', 'a' .. 'h';
213 2772 ▶ my $rule = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 2772 my ($left, $join, $right) = @_;
215 return ' ' . $self->_paint(
216 2772 $left . join($join, ($line->{h} x 3) x 8) . $right, 'grid');
217 308 };
218  
219 308 my @lines = ($files, $rule->(@{$line}{qw/tl t tr/}));
220 308 for my $row (0 .. 7) {
221 2464 my $rank = 8 - $row;
222 2464 my @cells;
223 2464 for my $col (0 .. 7) {
224 19712 my $square = $row * 8 + $col;
225 19712 my $cell = $game->board->[$square];
226 19712 push @cells, $self->_cell($square, $cell, $kind, \%legal,
227 \%flipped, %o);
228 }
229 2464 my $bar = $self->_paint($line->{v}, 'grid');
230 2464 push @lines, " $rank " . $bar . join($bar, @cells) . $bar . " $rank";
231 2464 ▶ push @lines, $rule->(@{$line}{qw/l x r/}) unless $row == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $row == 7 2156 308 2464
232 }
233 308 push @lines, $rule->(@{$line}{qw/bl b br/}), $files;
234 308 return @lines;
235 }
236  
237 271 ▶sub since {
Subroutine since: called CC=2 SCAR=6.9
Pod documented
238 271 my ($self) = @_;
239 271 ▶ my $move = $self->last_move or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $move = $self->last_move or return () 187 84 271
240 187 return (last => $move->square, flips => $move->flips);
241 }
242  
243 349 ▶sub remember {
Subroutine remember: called CC=3 SCAR=12.5
Pod documented
244 349 my ($self, $move) = @_;
245 349 ▶ $self->last_move(ref $move && $move->can('square') ? $move : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $move && $move->can('square') ? : 349 0 349
Condition33%1/3
ref $move and $move->can("square")
!ll&&!rl&&r
00349
MC/DC0%0/2
ref $move and $move->can("square")
ref $move $move->can("square")
Truth table33%1/3
A and B
A= ref $moveB= $move->can("square")
ABresult
0X0
100
111
246 349 return $self;
247 }
248  
249 19712 ▶sub _cell {
Subroutine _cell: called CC=12 SCAR=24.9
250 19712 my ($self, $square, $cell, $kind, $legal, $flipped, %o) = @_;
251  
252 19712 ▶ my $cursor = defined $o{cursor} && $o{cursor} == $square;
Condition100%3/3
defined $o{'cursor'} && $o{'cursor'} == $square
!ll&&!rl&&r
15808384361
MC/DC100%2/2
defined $o{'cursor'} && $o{'cursor'} == $square
defined $o{'cursor'} $o{'cursor'} == $square
Truth table100%3/3
A && B
A= defined $o{'cursor'}B= $o{'cursor'} == $square
ABresult
0X0
100
111
253 19712 ▶ my $placed = defined $o{last} && $o{last} == $square;
Condition100%3/3
defined $o{'last'} && $o{'last'} == $square
!ll&&!rl&&r
774411781187
MC/DC100%2/2
defined $o{'last'} && $o{'last'} == $square
defined $o{'last'} $o{'last'} == $square
Truth table100%3/3
A && B
A= defined $o{'last'}B= $o{'last'} == $square
ABresult
0X0
100
111
254  
255 19712 ▶ if ($cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor) 61 19651 19712
256 my $face = defined $cell ? $self->glyph($cell)
257 61 ▶ : $o{to_play} ? $GLYPH{ $o{to_play} }{$kind} : '*';
Branch: 2 branches
BranchTrueFalseTotal
defined $cell ? : 0 61 61
$o{'to_play'} ? : 61 0 61
258 61 return $self->_paint('>' . $face . '<', 'cursor');
259 }
260  
261 19651 ▶ return $self->_paint('[' . $self->glyph($cell) . ']', 'placed')
Branch: 1 branch
BranchTrueFalseTotal
if $placed and defined $cell 187 19464 19651
Condition66%2/3
$placed and defined $cell
!ll&&!rl&&r
194640187
MC/DC50%1/2
$placed and defined $cell
$placed defined $cell
Truth table66%2/3
A and B
A= $placedB= defined $cell
ABresult
0X0
100
111
262 if $placed && defined $cell;
263  
264 return ' ' . $self->_paint($TURNED{$cell}{$kind}, 'turned') . ' '
265 19464 ▶ if defined $cell && $flipped->{$square};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell and $flipped->{$square} 414 19050 19464
Condition100%3/3
defined $cell and $flipped->{$square}
!ll&&!rl&&r
134675583414
MC/DC100%2/2
defined $cell and $flipped->{$square}
defined $cell $flipped->{$square}
Truth table100%3/3
A and B
A= defined $cellB= $flipped->{$square}
ABresult
0X0
100
111
266  
267 return ' ' . (defined $cell ? $self->_paint($self->glyph($cell), $cell)
268 19050 ▶ : $legal->{$square} ? $self->_paint('*', '*')
Branch: 2 branches
BranchTrueFalseTotal
defined $cell ? : 5583 13467 19050
$legal->{$square} ? : 1555 11912 13467
269 : ' ') . ' ';
270 }
271  
272 27 ▶sub start {
Subroutine start: called CC=11 SCAR=24.0
Pod documented
273 27 my ($self) = @_;
274  
275 27 ▶ $self->game(Game::Reversi->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 21 6 27
276 variant => $self->variant, seed => $self->seed)) unless $self->game;
277 27 my $bot = Game::Reversi::Bot->new(
278 level => $self->level, seed => $self->seed . ':bot');
279 27 my $me = $self->colour;
280  
281 27 $self->_intro;
282 27 $self->history->start($self->game);
283 27 my $seen = scalar @{ $self->game->events };
284  
285 27 ▶ while ($self->game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active") 374 5 379
286 374 my $game = $self->game;
287 374 my $turn = $game->turn;
288  
289 374 ▶ if ($turn eq $me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn eq $me) { } 203 171 374
290 203 $self->history->turn($game);
291 203 my $outcome = $self->_human_turn($game, $me);
292 203 ▶ if ($outcome && $outcome eq 'swap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($outcome and $outcome eq "swap") 7 196 203
Condition100%3/3
$outcome and $outcome eq "swap"
!ll&&!rl&&r
174227
MC/DC100%2/2
$outcome and $outcome eq "swap"
$outcome $outcome eq "swap"
Truth table100%3/3
A and B
A= $outcomeB= $outcome eq "swap"
ABresult
0X0
100
111
293 7 $seen = scalar @{ $self->game->events };
294 7 next;
295 }
296 196 ▶ if ($outcome) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outcome) 22 174 196
297 22 $game->resign($me);
298 22 last;
299 }
300 }
301 else {
302 171 my $move = $bot->choose($game, $turn);
303 171 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 171 171
304 171 my $done = $game->play($turn, $move->square);
305 171 $self->remember($done);
306 171 ▶ $self->_say('', $NAME{$turn} . ' plays ' . $move->name
Branch: 2 branches
BranchTrueFalseTotal
if $move->phase eq "play" 163 8 171
$move->turned == 1 ? : 69 94 163
307 . ', turning ' . $move->turned
308 . ($move->turned == 1 ? ' disc' : ' discs'))
309 if $move->phase eq 'play';
310 171 ▶ $self->_say('', $NAME{$turn} . ' places on ' . $move->name)
Branch: 1 branch
BranchTrueFalseTotal
if $move->phase eq "place" 8 163 171
311 if $move->phase eq 'place';
312 }
313  
314 345 $seen = $self->_announce_passes($game, $seen);
315 }
316  
317 27 $self->_finish($self->game);
318 27 return $self->game->result;
319 }
320  
321 12 ▶sub _travel {
Subroutine _travel: called CC=4 SCAR=13.9
322 12 my ($self, $way) = @_;
323 12 my $history = $self->history;
324 12 ▶ my $rebuilt = $way eq 'redo' ? $history->redo : $history->undo;
Branch: 1 branch
BranchTrueFalseTotal
$way eq 'redo' ? : 4 8 12
325 12 ▶ return $way eq 'redo' ? 'nothing to redo' : 'nothing to take back'
Branch: 2 branches
BranchTrueFalseTotal
unless $rebuilt 5 7 12
$way eq 'redo' ? : 2 3 5
326 unless $rebuilt;
327 7 $self->game($rebuilt);
328 7 $self->last_move(undef);
329 7 return;
330 }
331  
332 27 ▶sub _intro {
Subroutine _intro: called CC=3 SCAR=11.0
333 27 my ($self) = @_;
334 27 my $me = $self->colour;
335 $self->_say(
336 27 ▶ 'Reversi. You are ' . $self->glyph($me) . ', ' . $NAME{$me} . '.',
Branch: 2 branches
BranchTrueFalseTotal
unless $self->quiet 25 2 27
$self->variant eq 'historic' ? : 4 21 25
337 $self->variant eq 'historic'
338 ? 'The historic opening: the first four discs are placed by the '
339 . 'players, on the centre four, and turn nothing.'
340 : 'The Othello opening: four discs are already on the board.',
341 'Enter a square such as d3. "help" for help, "quit" to give up.',
342 ) unless $self->quiet;
343 27 return;
344 }
345  
346 185 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
347 185 my ($self) = @_;
348 185 return $self->_reader->keys_available;
349 }
350  
351 205 ▶sub picking {
Subroutine picking: called CC=6 SCAR=17.9
Pod documented
352 205 my ($self, @set) = @_;
353 205 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 1 204 205
$set[0] ? : 0 1 1
354 205 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 2 203 205
355 203 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 19 184 203
$self->_picking ? : 13 6 19
356 184 return $self->keys_available;
357 }
358  
359 0 ▶sub raw {
Subroutine raw: not called CC=1 SCAR=6.9
Pod documented
360 0 my ($self) = @_;
361 0 return $self->keyboard->raw;
362 }
363  
364 24 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=6.9
Pod documented
365 24 my ($self) = @_;
366 24 ▶ return $self->_reader->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_reader->enter_raw ? : 23 1 24
367 }
368  
369 0 ▶sub leave_raw {
Subroutine leave_raw: not called CC=1 SCAR=6.9
Pod documented
370 0 my ($self) = @_;
371 0 $self->_reader->leave_raw;
372 0 return $self;
373 }
374  
375 80 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
376 80 my ($self) = @_;
377 80 return $self->_reader->read_key;
378 }
379  
380 24996 ▶sub interactive {
Subroutine interactive: called CC=3 SCAR=11.4
Pod documented
381 24996 my ($self) = @_;
382 24996 ▶ my $out = $self->out or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or return 0 24996 0 24996
383 24996 ▶ return (eval { -t $out }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $out } } ? : 0 24996 24996
384 }
385  
386 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
387 0 my ($self) = @_;
388 0 $self->_writer->clear(scrollback => 1);
389 0 return $self;
390 }
391  
392 62 ▶sub square_line {
Subroutine square_line: called CC=6 SCAR=18.5
Pod documented
393 62 my ($self, $game, $me, $square) = @_;
394  
395 62 my $name = Game::Reversi::Board->name_of($square);
396 62 my $trial = $game->clone;
397 62 my $move = $trial->play($me, $square);
398  
399 62 ▶ return "$name: " . $move->message
Branch: 1 branch
BranchTrueFalseTotal
if ref $move and $move->isa("Game::Reversi::Error") 0 62 62
Condition33%1/3
ref $move and $move->isa("Game::Reversi::Error")
!ll&&!rl&&r
0620
MC/DC0%0/2
ref $move and $move->isa("Game::Reversi::Error")
ref $move $move->isa("Game::Reversi::Error")
Truth table33%1/3
A and B
A= ref $moveB= $move->isa("Game::Reversi::Error")
ABresult
0X0
100
111
400 if ref $move && $move->isa('Game::Reversi::Error');
401  
402 62 ▶ return "$name: placed, turning nothing yet" if $move->phase eq 'place';
Branch: 1 branch
BranchTrueFalseTotal
if $move->phase eq "place" 0 62 62
403  
404 62 my $count = $trial->counts;
405 return sprintf '%s: turns %d %s, and you would be %d to %d',
406 $name, $move->turned, ($move->turned == 1 ? 'disc' : 'discs'),
407 62 ▶ $count->{$me}, $count->{ $me eq 'b' ? 'w' : 'b' };
Branch: 2 branches
BranchTrueFalseTotal
$move->turned == 1 ? : 58 4 62
$me eq 'b' ? : 60 2 62
408 }
409  
410 0 ▶sub pick_help {
Subroutine pick_help: not called CC=1 SCAR=6.9
Pod documented
411 return (
412 0 'arrows walk the squares marked *, which are the only ones that play.',
413 'the line under the board says what the move would turn.',
414 'enter plays it, t goes back to typing, q gives up.',
415 'u takes your last turn back, r plays it again.',
416 'S saves the game, to carry on with --resume or watch with --replay.',
417 );
418 }
419  
420 23 ▶sub pick_square {
Subroutine pick_square: called CC=25 SCAR=35.9
Pod documented
421 23 my ($self, $game, $me) = @_;
422  
423 23 my @legal = sort { $a <=> $b } map { $_->square } @{ $game->legal($me) };
424 23 ▶ return $self->_typed_turn($game, $me) unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 0 23 23
425  
426 23 my $at = 0;
427 23 ▶ if (my $last = $self->last_move) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->last_move) 5 18 23
428 5 my ($gap) = sort { $a <=> $b } map { abs($_ - $last->square) } @legal;
429 $at = Game::Terminal::Kit::Steer::rest(\@legal,
430 5 ▶ sub { abs($_[0] - $last->square) == $gap });
Subroutine __ANON__: called CC=1 SCAR=0.0
431 }
432  
433 23 my @notice;
434  
435 23 while (1) {
436 61 my $count = $game->counts;
437 $self->_writer->show([
438 '',
439 $self->board_text($game, $me, $self->since,
440 cursor => $legal[$at], to_play => $me),
441 ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b}
442 . ' ' . $self->glyph(q{w}) . ' ' . $count->{w},
443 ' ' . $self->square_line($game, $me, $legal[$at]),
444 61 ▶ (map { ' ' . $_ } @notice),
Condition66%2/3
$self->history->legend || ()
l!l&&r!l&&!r
8530
MC/DC0%0/2
$self->history->legend || ()
$self->history->legend ()
Truth table66%2/3
A || B
A= $self->history->legendB= ()
ABresult
1X1
011
000
445 ' ' . $self->_paint(join(', ', 'arrows to choose, enter to play, ? for the keys',
446 $self->history->legend || (), 'S save'), 'grid'),
447 ]);
448 61 @notice = ();
449  
450 61 my $key = $self->read_key;
451 61 ▶ return 1 if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 0 61 61
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0061
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0061
MC/DC0%0/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
452  
453 61 ▶ if ($STEP{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STEP{$key}) 32 29 61
454 32 $at = Game::Terminal::Kit::Steer::ring(scalar @legal, $at, $STEP{$key});
455 32 next;
456 }
457 0 ▶ if ($key eq 'home') { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home") 0 29 29
458 0 ▶ if ($key eq 'end') { $at = $#legal; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end") 0 29 29
459  
460 29 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 4 25 29
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
4025
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
461 4 my $played = $game->play($me, $legal[$at]);
462 4 ▶ if (ref $played && $played->isa('Game::Reversi::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $played and $played->isa("Game::Reversi::Error")) 0 4 4
Condition33%1/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
040
MC/DC0%0/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played $played->isa("Game::Reversi::Error")
Truth table33%1/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
ABresult
0X0
100
111
463 0 @notice = ($played->message . '.');
464 0 next;
465 }
466 4 $self->remember($played);
467 4 ▶ $self->_say(' you play ' . $played->name
Branch: 2 branches
BranchTrueFalseTotal
$played->phase eq 'play' ? : 4 0 4
$played->turned == 1 ? : 4 0 4
468 . ($played->phase eq 'play'
469 ? ', turning ' . $played->turned
470 . ($played->turned == 1 ? ' disc' : ' discs')
471 : ''));
472 4 return 0;
473 }
474  
475 25 ▶ if ($key eq 'u' || $key eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "u" or $key eq "r") 7 18 25
Condition100%3/3
$key eq "u" or $key eq "r"
l!l&&r!l&&!r
5218
MC/DC100%2/2
$key eq "u" or $key eq "r"
$key eq "u" $key eq "r"
Truth table100%3/3
A or B
A= $key eq "u"B= $key eq "r"
ABresult
1X1
011
000
476 7 ▶ my $nothing = $self->_travel($key eq 'u' ? 'undo' : 'redo');
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'u' ? : 5 2 7
477 7 ▶ return 'swap' unless $nothing;
Branch: 1 branch
BranchTrueFalseTotal
unless $nothing 3 4 7
478 4 @notice = ($nothing . '.');
479 4 next;
480 }
481 18 ▶ if ($key eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "S") 1 17 18
482 1 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->_writer,
483 default => $self->save_default,
484 head => [ '', $self->board_text($game, $me, $self->since, to_play => $me), '' ]);
485 1 ▶ @notice = ($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 0 1
486 1 next;
487 }
488 17 ▶ if ($key eq 'q') { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 16 1 17
489 0 ▶ if ($key eq 't') { $self->picking(0); $self->leave_raw; return $self->_typed_turn($game, $me) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t") 0 1 1
490 0 ▶ if ($key eq '?') { @notice = $self->pick_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 1 1
491  
492 1 @notice = ('that key does nothing here. ? for the ones that do');
493 }
494 }
495  
496 203 ▶sub _human_turn {
Subroutine _human_turn: called CC=3 SCAR=11.0
497 203 my ($self, $game, $me) = @_;
498  
499 203 ▶ if ($self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking) 13 190 203
500 13 ▶ return $self->pick_square($game, $me) if $self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enter_raw 12 1 13
501 1 $self->picking(0);
502 }
503  
504 191 return $self->_typed_turn($game, $me);
505 }
506  
507 191 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=19 SCAR=29.6
508 191 my ($self, $game, $me) = @_;
509  
510 191 while (1) {
511 208 my $count = $game->counts;
512 208 $self->_say("", $self->board_text($game, $me, $self->since));
513 $self->_say(
514 ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b} . ' '
515 . $self->glyph(q{w}) . ' ' . $count->{w},
516 208 ▶ ' ' . ($game->phase eq 'opening'
Branch: 1 branch
BranchTrueFalseTotal
$game->phase eq 'opening' ? : 15 193 208
517 ? 'Place a disc on one of the centre four.'
518 : 'Your move. The squares marked * are the ones that outflank something.'),
519 );
520  
521 208 my ($answer, $typed) = $self->_ask(' > ');
522  
523 208 ▶ return 1 unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 1 207 208
524 207 ▶ return 1 if $answer eq 'quit' || $answer eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "quit" or $answer eq "resign" 15 192 207
Condition66%2/3
$answer eq "quit" or $answer eq "resign"
l!l&&r!l&&!r
150192
MC/DC50%1/2
$answer eq "quit" or $answer eq "resign"
$answer eq "quit" $answer eq "resign"
Truth table66%2/3
A or B
A= $answer eq "quit"B= $answer eq "resign"
ABresult
1X1
011
000
525  
526 192 ▶ if ($answer eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer eq "help") 1 191 192
527 1 $self->_help($game, $me);
528 1 next;
529 }
530 191 ▶ if ($answer eq 'board' || $answer eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer eq "board" or $answer eq "") 0 191 191
Condition33%1/3
$answer eq "board" or $answer eq ""
l!l&&r!l&&!r
00191
MC/DC0%0/2
$answer eq "board" or $answer eq ""
$answer eq "board" $answer eq ""
Truth table33%1/3
A or B
A= $answer eq "board"B= $answer eq ""
ABresult
1X1
011
000
531 0 next;
532 }
533 191 ▶ if ($answer eq 'undo' || $answer eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer eq "undo" or $answer eq "redo") 5 186 191
Condition100%3/3
$answer eq "undo" or $answer eq "redo"
l!l&&r!l&&!r
32186
MC/DC100%2/2
$answer eq "undo" or $answer eq "redo"
$answer eq "undo" $answer eq "redo"
Truth table100%3/3
A or B
A= $answer eq "undo"B= $answer eq "redo"
ABresult
1X1
011
000
534 5 my $nothing = $self->_travel($answer);
535 5 ▶ return 'swap' unless $nothing;
Branch: 1 branch
BranchTrueFalseTotal
unless $nothing 4 1 5
536 1 $self->_say(' ' . $nothing . '.');
537 1 next;
538 }
539 186 ▶ if (my ($saving, $file) = Game::Terminal::Kit::Save::command($typed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = Game::Terminal::Kit::Save::command($typed)) 11 175 186
540 11 $self->_say(' ' . $self->save($file));
541 11 next;
542 }
543  
544 175 my $square = Game::Reversi::Notation->text_to_square($answer);
545 175 ▶ if (!defined $square) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $square) 3 172 175
546 3 $self->_say(' "' . $answer . '" is not a square. Try d3, or "help".');
547 3 next;
548 }
549  
550 172 my $played = $game->play($me, $square);
551 172 ▶ $self->remember($played) unless ref $played && $played->isa("Game::Reversi::Error");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $played and $played->isa("Game::Reversi::Error") 171 1 172
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
01711
MC/DC50%1/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played $played->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
ABresult
0X0
100
111
552 172 ▶ if (ref $played && $played->isa('Game::Reversi::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $played and $played->isa("Game::Reversi::Error")) 1 171 172
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
01711
MC/DC50%1/2
ref $played and $played->isa("Game::Reversi::Error")
ref $played $played->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $playedB= $played->isa("Game::Reversi::Error")
ABresult
0X0
100
111
553 1 $self->_say(' ' . $played->message . '.');
554 1 next;
555 }
556  
557 171 ▶ $self->_say(' you play ' . $played->name
Branch: 2 branches
BranchTrueFalseTotal
$played->phase eq 'play' ? : 163 8 171
$played->turned == 1 ? : 76 87 163
558 . ($played->phase eq 'play'
559 ? ', turning ' . $played->turned
560 . ($played->turned == 1 ? ' disc' : ' discs')
561 : ''));
562 171 return 0;
563 }
564 }
565  
566 1 ▶sub _help {
Subroutine _help: called CC=1 SCAR=0.0
567 1 my ($self, $game, $me) = @_;
568 1 $self->_say(
569 '',
570 ' Enter a square as a file and a rank, such as d3 or f5.',
571 ' A move must outflank at least one of the other side\'s discs;',
572 ' every disc it outflanks is turned, and you do not get to choose.',
573 ' The squares marked * are the ones you can play.',
574 ' A turn with no legal move is forfeited for you automatically.',
575 ' The game ends when neither side can move, which can happen with',
576 ' the board not full. "undo" takes your last turn back, "redo"',
577 ' plays it again, "save" or "save FILE" saves the game to carry',
578 ' on with --resume, and "quit" gives up.',
579 '',
580 );
581 1 return;
582 }
583  
584 345 ▶sub _announce_passes {
Subroutine _announce_passes: called CC=3 SCAR=11.0
585 345 my ($self, $game, $seen) = @_;
586 345 my $events = $game->events;
587 345 for my $i ($seen .. $#$events) {
588 354 my $event = $events->[$i];
589 354 ▶ next unless $event->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"kind"} eq "pass" 350 4 354
590 4 my $colour = $event->{payload}{colour};
591 4 $self->_say(' ' . $NAME{$colour} . ' has no legal move, so that turn is '
592 . 'forfeited and the other side plays again.');
593 }
594 345 return scalar @$events;
595 }
596  
597 28 ▶sub _finish {
Subroutine _finish: called CC=7 SCAR=19.6
598 28 my ($self, $game) = @_;
599 28 my $result = $game->result;
600 28 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 28 28
601  
602 28 $self->_say('', $self->board_text($game, undef), '');
603  
604 28 my $count = $result->counts;
605 $self->_say(' discs on the board: ' . $self->glyph(q{b}) . ' ' . $count->{b}
606 28 . ' ' . $self->glyph(q{w}) . ' ' . $count->{w});
607  
608 28 my $score = $result->score;
609 28 ▶ if ($score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score) 6 22 28
610 6 my $empty = Game::Reversi::Board->empties($game->board);
611 $self->_say(' final score: ' . $self->glyph(q{b}) . ' ' . $score->{b}
612 . ' ' . $self->glyph(q{w}) . ' ' . $score->{w}
613 6 ▶ . ($empty ? " (the $empty empty squares go to the winner)" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$empty ? : 1 5 6
614 }
615  
616 28 my $me = $self->colour;
617 28 ▶ $self->_say('', !defined $result->winner ? ' A tie.'
Branch: 2 branches
BranchTrueFalseTotal
!defined($result->winner) ? : 0 28 28
$result->winner eq $me ? : 0 28 28
618 : $result->winner eq $me ? ' You win.'
619 : ' You lose.');
620 28 ▶ $self->_say(' (' . $result->result . ')') unless $result->natural;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->natural 22 6 28
621 28 return;
622 }
623  
624 12 ▶sub settings {
Subroutine settings: called CC=1 SCAR=0.0
Pod documented
625 12 my ($self) = @_;
626 12 return { level => $self->level, variant => $self->variant, colour => $self->colour, seed => $self->seed };
627 }
628  
629 7 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=7.1
Pod documented
630 7 my ($self) = @_;
631 7 my $file = $self->history->file;
632 7 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('reversi');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 7 0 7
633 }
634  
635 12 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
636 12 my ($self, $file) = @_;
637 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
638 12 my $history = $self->history;
639 12 $history->settings($self->settings);
640 12 $history->version($VERSION);
641 12 my $path = eval { $history->saved($self->game)->write_file($file) };
642 12 ▶ return 'the game was not saved: ' . Game::Terminal::Kit::Save::why($@) . '.' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 12 12
643 12 $history->file($path);
644 12 return "the game is saved to $path.";
645 }
646  
647 9 ▶sub read_save {
Subroutine read_save: called CC=1 SCAR=0.0
Pod documented
648 9 my ($class, $file) = @_;
649 9 return Game::Terminal::Kit::Save->read_game($file, 'reversi');
650 }
651  
652 4 ▶sub replay_lines {
Subroutine replay_lines: called CC=3 SCAR=12.5
Pod documented
653 4 my ($self, $game) = @_;
654 4 my $count = $game->counts;
655 4 my $result = $game->result;
656 return ($self->board_text($game, undef),
657 ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b} . ' ' . $self->glyph(q{w}) . ' ' . $count->{w},
658 4 ▶ ($result ? ' the game is over' . (defined $result->winner ? ': ' . $NAME{ $result->winner } . ' won.' : ': a tie.') : ()));
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 0 4 4
defined $result->winner ? : 0 0 0
659 }
660  
661 9 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
662 9 return <<'USAGE';
663 reversi - play Reversi in a terminal
664  
665 reversi [--level 1..5] [--variant historic|othello]
666 [--colour b|w] [--seed STRING] [--ascii]
667  
668 --level how hard the opponent is, 1 to 5. Default 3.
669 --variant historic, the real Reversi opening where the players place the
670 first four discs themselves, or othello, where those four are
671 already down. Default historic.
672 --colour b to play dark and move first, w to play light. Default b.
673 --seed makes a game reproducible, and decides how the bot opens.
674 --ascii X and O on a plain grid, for a terminal without box characters.
675 --nopick type the squares out instead of pointing at them.
676 --save FILE where S, or "save", writes the game: FILE, else the file it
677 was resumed from, else reversi.save.
678 --resume FILE carry on a saved game, with its undo and redo.
679 --replay FILE watch a saved game a turn at a time; enter plays on from there.
680  
681 On a terminal the arrow keys walk the squares marked *, which are the only
682 ones that play, and the line under the board says what the move would turn
683 and what the score would become. Enter plays it, t goes back to typing,
684 u takes your last turn back, r plays it again, S saves the game and ? lists
685 the keys. Typed, the same are undo, redo and save. Needs Term::ReadKey; without it the squares are typed.
686  
687 The square the last move went on is bracketed and the discs it turned are
688 drawn ringed, so both are visible with --ascii and with colour off.
689  
690 Exit status is 0 if you won or tied, 1 if you lost, 2 for a bad option.
691 USAGE
692 }
693  
694 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
695 return [
696 2 { name => 'level', type => 'int', min => 1, max => 5, help => 'how hard the opponent is' },
697 { name => 'variant', type => 'enum', choices => [qw(historic othello)], help => 'place the first four discs yourselves, or find them down' },
698 { name => 'colour', type => 'enum', choices => [qw(b w)], help => 'b plays dark and moves first, w plays light' },
699 { name => 'seed', type => 'str', help => 'makes a game reproducible' },
700 ];
701 }
702  
703 26 ▶sub run {
Subroutine run: called CC=30 SCAR=37.9
Pod documented
704 26 my ($class, @argv) = @_;
705 26 my %opt;
706  
707 26 my $ok = eval {
708 26 ▶ local $SIG{__WARN__} = sub { print STDERR "reversi: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
709 26 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
710 'level=i', 'variant=s', 'colour=s', 'seed=s', 'ascii', 'pick!', 'help|h',
711 Game::Terminal::Kit::Save::flags());
712 };
713 26 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 25 26
714 25 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 8 17 25
715 0 ▶ if (@argv) { print STDERR "reversi: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 17 17
716 17 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) { print STDERR "reversi: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) 2 15 17
717  
718 15 ▶ my $from = defined $opt{resume} ? $opt{resume} : $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 6 9 15
719 15 my $save;
720 15 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 8 7 15
721 8 ($save, my $why) = $class->read_save($from);
722 8 ▶ unless ($save) { print STDERR "reversi: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 2 6 8
723 6 my $settings = $save->settings;
724 6 ▶ defined $opt{$_} or $opt{$_} = $settings->{$_} for grep { defined $settings->{$_} } qw(level variant colour seed);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $settings->{$_} 1 23 24
725 }
726 13 my %default = (level => 3, variant => 'historic', colour => 'b', seed => 'reversi');
727 13 ▶ defined $opt{$_} or $opt{$_} = $default{$_} for keys %default;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $default{$_} 45 7 52
728  
729 13 ▶ $opt{colour} = 'b' if lc $opt{colour} eq 'dark' || lc $opt{colour} eq 'black';
Branch: 1 branch
BranchTrueFalseTotal
if lc $opt{"colour"} eq "dark" or lc $opt{"colour"} eq "black" 0 13 13
Condition33%1/3
lc $opt{"colour"} eq "dark" or lc $opt{"colour"} eq "black"
l!l&&r!l&&!r
0013
MC/DC0%0/2
lc $opt{"colour"} eq "dark" or lc $opt{"colour"} eq "black"
lc $opt{"colour"} eq "dark" lc $opt{"colour"} eq "black"
Truth table33%1/3
A or B
A= lc $opt{"colour"} eq "dark"B= lc $opt{"colour"} eq "black"
ABresult
1X1
011
000
730 13 ▶ $opt{colour} = 'w' if lc $opt{colour} eq 'light' || lc $opt{colour} eq 'white';
Branch: 1 branch
BranchTrueFalseTotal
if lc $opt{"colour"} eq "light" or lc $opt{"colour"} eq "white" 0 13 13
Condition33%1/3
lc $opt{"colour"} eq "light" or lc $opt{"colour"} eq "white"
l!l&&r!l&&!r
0013
MC/DC0%0/2
lc $opt{"colour"} eq "light" or lc $opt{"colour"} eq "white"
lc $opt{"colour"} eq "light" lc $opt{"colour"} eq "white"
Truth table33%1/3
A or B
A= lc $opt{"colour"} eq "light"B= lc $opt{"colour"} eq "white"
ABresult
1X1
011
000
731  
732 13 my $terminal = eval {
733 $class->new(
734 level => $opt{level}, variant => $opt{variant},
735 colour => $opt{colour}, seed => $opt{seed},
736 ascii => $opt{ascii},
737 (defined $opt{save} ? (save_file => $opt{save}) : ()),
738 13 ▶ (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()));
Branch: 3 branches
BranchTrueFalseTotal
defined $opt{'save'} ? : 6 7 13
defined $opt{'pick'} ? : 0 13 13
$opt{'pick'} ? : 0 0 0
739 };
740 13 ▶ if (!$terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 0 13 13
741 0 ▶ my $why = $@ || "could not start\n";
Condition0%0/2
$@ || "could not start\n"
l!l
00
MC/DC0%0/1
$@ || "could not start\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
742 0 $why =~ s/ at \S+ line \d+\.?\n?\z//;
743 0 $why =~ s/\AGame::Reversi(?:::\w+)*: //;
744 0 chomp $why;
745 0 print STDERR "reversi: $why\n";
746 0 return 2;
747 }
748  
749 13 ▶ if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"replay"}) 1 12 13
750 my $replay = Game::Terminal::Kit::Replay->new(
751 save => $save,
752 3 ▶ load => sub { Game::Reversi->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
753 3 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
754 1 ▶ head => [ 'Reversi, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 1 0 1
755 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
756 screen => $terminal->screen,
757 );
758 my ($event, $step) = Game::Terminal::Kit::Run::guard(
759 1 ▶ sub { $replay->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
760 1 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 0 1
761 0 $save = $save->from_step($step);
762 }
763 12 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 5 7 12
764 5 my $game = eval { $terminal->history->resume($save) };
765 5 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 5 5
766 0 print STDERR "reversi: '" . $save->file . "' cannot be carried on: "
767 . Game::Terminal::Kit::Save::why($@) . ".\n";
768 0 return 2;
769 }
770 5 $terminal->game($game);
771 5 ▶ $terminal->history->file($terminal->save_file) if defined $terminal->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $terminal->save_file 1 4 5
772 }
773  
774 my $result = Game::Terminal::Kit::Run::guard(
775 12 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
776 12 ▶ return 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 12 12
777 12 ▶ return 0 if !defined $result->winner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result->winner 0 12 12
778 12 ▶ return 0 if $result->winner eq $opt{colour};
Branch: 1 branch
BranchTrueFalseTotal
if $result->winner eq $opt{"colour"} 0 12 12
779 12 return 1;
780 }
781  
782  
783 23 1;
784  
785 __END__
↑ Summary blib/lib/Game/Reversi.pm »