Reversi/Terminal.pm

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