Reversi/Terminal.pm

filter: Statementstmt 88.5% 301 / 340 Branchbran 74.5% 149 / 200 Conditioncond 62.3% 43 / 69 MC/DCmcdc 44.1% 19 / 43 Subroutinesub 95.1% 39 / 41 Podpod 94.7% 18 / 19 totaltotal 79.9% 569 / 712 CC 130 SCAR 26.5
CC
130
Cov
81%
pick_square
55.3
_typed_turn
15.4
read_char
15.1
CRAP 14.1
↑ Summary Reversi/Move.pm »
1 package Game::Reversi::Terminal;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Game::Reversi;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Game::Reversi::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Game::Reversi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Game::Reversi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Game::Reversi::Opening;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.02';
15  
16 2 my %GLYPH = (
17 b => { wide => "\x{25CF}", ascii => 'X' },
18 w => { wide => "\x{25CB}", ascii => 'O' },
19 );
20  
21 2 my %TURNED = (
22 b => { wide => "\x{25C9}", ascii => 'x' },
23 w => { wide => "\x{25CE}", ascii => 'o' },
24 );
25 2 my %NAME = (b => 'dark', w => 'light');
26  
27 2 my %LINE = (
28 wide => { h => "\x{2500}", v => "\x{2502}", tl => "\x{250C}", tr => "\x{2510}",
29 bl => "\x{2514}", br => "\x{2518}", t => "\x{252C}", b => "\x{2534}",
30 l => "\x{251C}", r => "\x{2524}", x => "\x{253C}" },
31 ascii => { h => '-', v => '|', tl => '+', tr => '+', bl => '+', br => '+',
32 t => '+', b => '+', l => '+', r => '+', x => '+' },
33 );
34  
35 2 my %INK = (b => "\e[1m", w => "\e[2m", '*' => "\e[33m", grid => "\e[2m",
36 turned => "\e[1;32m", placed => "\e[1;36m", cursor => "\e[1;7m");
37 2 my $OFF = "\e[0m";
38  
39 has in => (
40 is => 'rw',
41 isa => Any,
42 16 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
43 2 );
44  
45 has out => (
46 is => 'rw',
47 isa => Any,
48 7 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
49 2 );
50  
51 has level => (
52 is => 'ro',
53 isa => Int,
54 default => 3
55 2 );
56  
57 has variant => (
58 is => 'ro',
59 isa => Str,
60 default => 'historic'
61 2 );
62  
63 has colour => (
64 is => 'ro',
65 isa => Str,
66 default => 'b'
67 2 );
68  
69 has seed => (
70 is => 'ro',
71 default => 'terminal'
72 2 );
73  
74 has [qw/quiet ascii/] => (
75 is => 'rw',
76 isa => Bool,
77 default => 0
78 2 );
79  
80 has game => (
81 is => 'rw',
82 isa => Any
83 2 );
84  
85 2 has last_move => (is => 'rw');
86  
87 2 has raw => (is => 'rw', default => 0);
88  
89 2 has keysource => (is => 'rw');
90  
91 27 ▶has pending => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
92  
93 2 has _picking => (is => 'rw', init_arg => 'picking', private => 1);
94  
95 2 our %SEQUENCE = (
96 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
97 'H' => 'home', 'F' => 'end',
98 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
99 '7~' => 'home', '8~' => 'end',
100 );
101  
102 2 our %CONTROL = (
103 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
104 "\x7f" => 'backspace', "\x08" => 'backspace',
105 "\x03" => 'interrupt', "\x04" => 'eof',
106 );
107  
108 has _wants_ansi => (
109 is => 'rw',
110 init_arg => 'ansi',
111 private => 1
112 2 );
113  
114 27 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
115 27 my ($self) = @_;
116  
117 27 ▶ binmode $self->out, ':encoding(UTF-8)' unless $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ascii 14 13 27
118 27 my $previous = select $self->out;
119 27 $| = 1;
120 27 select $previous;
121  
122 27 ▶ 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 26 27
Condition100%3/3
$self->colour eq "b" or $self->colour eq "w"
l!l&&r!l&&!r
2421
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
123 unless $self->colour eq 'b' || $self->colour eq 'w';
124 26 ▶ die "Game::Reversi::Terminal: there is no variant '" . $self->variant . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless "Game::Reversi::Opening"->describes($self->variant) 1 25 26
125 unless Game::Reversi::Opening->describes($self->variant);
126  
127 die 'Game::Reversi::Terminal: there is no level ' . $self->level
128 25 ▶ unless grep { $_ == $self->level } Game::Reversi::Bot->levels;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $self->level;} "Game::Reversi::Bot"->levels 1 24 25
129  
130 24 return $self;
131 }
132  
133 10006 ▶sub ansi {
Subroutine ansi: called CC=6 SCAR=17.9
Pod documented
134 10006 my ($self) = @_;
135 10006 ▶ return 0 if defined $ENV{NO_COLOR} && length $ENV{NO_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"} 2 10004 10006
Condition100%3/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
1000312
MC/DC100%2/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table100%3/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
136 10004 ▶ return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_ansi 353 9651 10004
$self->_wants_ansi ? : 27 326 353
137 9651 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 9651 9651
138 }
139  
140 10001 ▶sub _paint {
Subroutine _paint: called CC=3 SCAR=11.1
141 10001 my ($self, $glyph, $key) = @_;
142 10001 ▶ return $glyph unless $self->ansi;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ansi 9976 25 10001
143 25 ▶ return ($INK{$key} || '') . $glyph . $OFF;
Condition50%1/2
$INK{$key} || ''
l!l
250
MC/DC0%0/1
$INK{$key} || ''
$INK{$key}
Truth table50%1/2
A
A= $INK{$key}
Aresult
11
00
144 }
145  
146 733 ▶sub _say {
Subroutine _say: called CC=2 SCAR=6.9
147 733 my ($self, @lines) = @_;
148 733 my $fh = $self->out;
149 733 print {$fh} $_, "\n" for @lines;
150 733 return;
151 }
152  
153 168 ▶sub _ask {
Subroutine _ask: called CC=2 SCAR=6.9
154 168 my ($self, $prompt) = @_;
155 168 my $fh = $self->out;
156 168 print {$fh} $prompt;
157 168 my $in = $self->in;
158 168 my $line = <$in>;
159 168 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 167 168
160  
161 167 print {$fh} "\n";
162  
163 167 $line =~ s/\A\s+|\s+\z//g;
164 167 return lc $line;
165 }
166  
167 5539 ▶sub glyph {
Subroutine glyph: called CC=2 SCAR=6.9
Pod documented
168 5539 my ($self, $colour) = @_;
169 5539 ▶ return $GLYPH{$colour}{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1193 4346 5539
170 }
171  
172 190 ▶sub board_text {
Subroutine board_text: called CC=8 SCAR=20.8
Pod documented
173 190 my ($self, $game, $colour, %o) = @_;
174 1227 my %legal = map { $_->square => 1 }
175 190 ▶ @{ defined $colour ? $game->legal($colour) : [] };
Branch: 1 branch
BranchTrueFalseTotal
defined $colour ? : 180 10 190
176 190 ▶ my %flipped = map { $_ => 1 } @{ $o{flips} || [] };
Condition100%2/2
$o{'flips'} || []
l!l
15733
MC/DC100%1/1
$o{'flips'} || []
$o{'flips'}
Truth table100%2/2
A
A= $o{'flips'}
Aresult
11
00
177 190 ▶ my $line = $LINE{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 44 146 190
178 190 ▶ my $kind = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 44 146 190
179  
180 190 my $files = ' ' . join ' ', 'a' .. 'h';
181 1710 ▶ my $rule = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
182 1710 my ($left, $join, $right) = @_;
183 return ' ' . $self->_paint(
184 1710 $left . join($join, ($line->{h} x 3) x 8) . $right, 'grid');
185 190 };
186  
187 190 my @lines = ($files, $rule->(@{$line}{qw/tl t tr/}));
188 190 for my $row (0 .. 7) {
189 1520 my $rank = 8 - $row;
190 1520 my @cells;
191 1520 for my $col (0 .. 7) {
192 12160 my $square = $row * 8 + $col;
193 12160 my $cell = $game->board->[$square];
194 12160 push @cells, $self->_cell($square, $cell, $kind, \%legal,
195 \%flipped, %o);
196 }
197 1520 my $bar = $self->_paint($line->{v}, 'grid');
198 1520 push @lines, " $rank " . $bar . join($bar, @cells) . $bar . " $rank";
199 1520 ▶ push @lines, $rule->(@{$line}{qw/l x r/}) unless $row == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless $row == 7 1330 190 1520
200 }
201 190 push @lines, $rule->(@{$line}{qw/bl b br/}), $files;
202 190 return @lines;
203 }
204  
205 176 ▶sub since {
Subroutine since: called CC=2 SCAR=6.9
Pod documented
206 176 my ($self) = @_;
207 176 ▶ my $move = $self->last_move or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $move = $self->last_move or return () 157 19 176
208 157 return (last => $move->square, flips => $move->flips);
209 }
210  
211 318 ▶sub remember {
Subroutine remember: called CC=3 SCAR=12.5
Pod documented
212 318 my ($self, $move) = @_;
213 318 ▶ $self->last_move(ref $move && $move->can('square') ? $move : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $move && $move->can('square') ? : 318 0 318
Condition33%1/3
ref $move and $move->can("square")
!ll&&!rl&&r
00318
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
214 318 return $self;
215 }
216  
217 12160 ▶sub _cell {
Subroutine _cell: called CC=12 SCAR=24.9
218 12160 my ($self, $square, $cell, $kind, $legal, $flipped, %o) = @_;
219  
220 12160 ▶ my $cursor = defined $o{cursor} && $o{cursor} == $square;
Condition100%3/3
defined $o{'cursor'} && $o{'cursor'} == $square
!ll&&!rl&&r
117124417
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
221 12160 ▶ my $placed = defined $o{last} && $o{last} == $square;
Condition100%3/3
defined $o{'last'} && $o{'last'} == $square
!ll&&!rl&&r
21129891157
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
222  
223 12160 ▶ if ($cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor) 7 12153 12160
224 my $face = defined $cell ? $self->glyph($cell)
225 7 ▶ : $o{to_play} ? $GLYPH{ $o{to_play} }{$kind} : '*';
Branch: 2 branches
BranchTrueFalseTotal
defined $cell ? : 0 7 7
$o{'to_play'} ? : 7 0 7
226 7 return $self->_paint('>' . $face . '<', 'cursor');
227 }
228  
229 12153 ▶ return $self->_paint('[' . $self->glyph($cell) . ']', 'placed')
Branch: 1 branch
BranchTrueFalseTotal
if $placed and defined $cell 157 11996 12153
Condition66%2/3
$placed and defined $cell
!ll&&!rl&&r
119960157
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
230 if $placed && defined $cell;
231  
232 return ' ' . $self->_paint($TURNED{$cell}{$kind}, 'turned') . ' '
233 11996 ▶ if defined $cell && $flipped->{$square};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell and $flipped->{$square} 384 11612 11996
Condition100%3/3
defined $cell and $flipped->{$square}
!ll&&!rl&&r
66164996384
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
234  
235 return ' ' . (defined $cell ? $self->_paint($self->glyph($cell), $cell)
236 11612 ▶ : $legal->{$square} ? $self->_paint('*', '*')
Branch: 2 branches
BranchTrueFalseTotal
defined $cell ? : 4996 6616 11612
$legal->{$square} ? : 1220 5396 6616
237 : ' ') . ' ';
238 }
239  
240 8 ▶sub start {
Subroutine start: called CC=8 SCAR=20.8
Pod documented
241 8 my ($self) = @_;
242  
243 8 my $game = Game::Reversi->new(
244 variant => $self->variant, seed => $self->seed);
245 8 $self->game($game);
246 8 my $bot = Game::Reversi::Bot->new(
247 level => $self->level, seed => $self->seed . ':bot');
248 8 my $me = $self->colour;
249  
250 8 $self->_intro;
251 8 my $seen = scalar @{ $game->events };
252  
253 8 ▶ while ($game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "active") 319 5 324
254 319 my $turn = $game->turn;
255  
256 319 ▶ if ($turn eq $me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn eq $me) { } 163 156 319
257 163 my $quit = $self->_human_turn($game, $me);
258 163 ▶ if ($quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quit) 3 160 163
259 3 $game->resign($me);
260 3 last;
261 }
262 }
263 else {
264 156 my $move = $bot->choose($game, $turn);
265 156 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 156 156
266 156 my $done = $game->play($turn, $move->square);
267 156 $self->remember($done);
268 156 ▶ $self->_say('', $NAME{$turn} . ' plays ' . $move->name
Branch: 2 branches
BranchTrueFalseTotal
if $move->phase eq "play" 148 8 156
$move->turned == 1 ? : 54 94 148
269 . ', turning ' . $move->turned
270 . ($move->turned == 1 ? ' disc' : ' discs'))
271 if $move->phase eq 'play';
272 156 ▶ $self->_say('', $NAME{$turn} . ' places on ' . $move->name)
Branch: 1 branch
BranchTrueFalseTotal
if $move->phase eq "place" 8 148 156
273 if $move->phase eq 'place';
274 }
275  
276 316 $seen = $self->_announce_passes($game, $seen);
277 }
278  
279 8 $self->_finish($game);
280 8 return $game->result;
281 }
282  
283 8 ▶sub _intro {
Subroutine _intro: called CC=3 SCAR=11.0
284 8 my ($self) = @_;
285 8 my $me = $self->colour;
286 $self->_say(
287 8 ▶ 'Reversi. You are ' . $self->glyph($me) . ', ' . $NAME{$me} . '.',
Branch: 2 branches
BranchTrueFalseTotal
unless $self->quiet 6 2 8
$self->variant eq 'historic' ? : 4 2 6
288 $self->variant eq 'historic'
289 ? 'The historic opening: the first four discs are placed by the '
290 . 'players, on the centre four, and turn nothing.'
291 : 'The Othello opening: four discs are already on the board.',
292 'Enter a square such as d3. "help" for help, "quit" to give up.',
293 ) unless $self->quiet;
294 8 return;
295 }
296  
297 162 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
298 162 my ($self) = @_;
299 162 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 162 162
300 162 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 162 0 162
301 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
302 }
303  
304 165 ▶sub picking {
Subroutine picking: called CC=6 SCAR=17.9
Pod documented
305 165 my ($self, @set) = @_;
306 165 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 1 164 165
$set[0] ? : 0 1 1
307 165 ▶ return 0 if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 2 163 165
308 163 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 3 160 163
$self->_picking ? : 1 2 3
309 160 return $self->keys_available;
310 }
311  
312 5 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=20.4
Pod documented
313 5 my ($self) = @_;
314 5 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 5 5
315 5 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 4 1 5
316 1 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 1 0 1
317 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
318 0 $self->raw(1);
319 0 return $self;
320 }
321  
322 0 ▶sub leave_raw {
Subroutine leave_raw: not called CC=3 SCAR=24.8
Pod documented
323 0 my ($self) = @_;
324 0 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 0 0
325 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
326 0 $self->raw(0);
327 0 return $self;
328 }
329  
330 48 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
331 48 my ($self, $wait) = @_;
332 48 my $pending = $self->pending;
333 48 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 46 48
334 46 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 46 0 46
335 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
336 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
337 0 select undef, undef, undef, 0.05;
338 0 return Term::ReadKey::ReadKey(-1, $self->in);
339 }
340  
341 26 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
342 26 my ($self) = @_;
343 26 my $char = $self->read_char(1);
344 26 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 25 26
345 25 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Reversi::Terminal::CONTROL{$char} 6 19 25
346 19 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 11 8 19
347 8 return $char;
348 }
349  
350 11 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
351 11 my ($self) = @_;
352 11 my $opener = $self->read_char(0);
353 11 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 11 11
354 11 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 9 11
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
812
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
355 2 unshift @{ $self->pending }, $opener;
356 2 return 'escape';
357 }
358 9 my $tail = '';
359 9 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 11 0 11
360 11 my $char = $self->read_char(0);
361 11 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 11 11
362 11 $tail .= $char;
363 11 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 9 2 11
364 }
365 9 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Reversi::Terminal::SEQUENCE{$tail} || 'escape'
l!l
90
MC/DC0%0/1
$Game::Reversi::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Reversi::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Reversi::Terminal::SEQUENCE{$tail}
Aresult
11
00
366 }
367  
368 7 ▶sub interactive {
Subroutine interactive: called CC=3 SCAR=11.4
Pod documented
369 7 my ($self) = @_;
370 7 ▶ my $out = $self->out or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $out = $self->out or return 0 7 0 7
371 7 ▶ return (eval { -t $out }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $out } } ? : 0 7 7
372 }
373  
374 7 ▶sub clear {
Subroutine clear: called CC=2 SCAR=10.9
Pod documented
375 7 my ($self) = @_;
376 7 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 7 0 7
377 0 my $fh = $self->out;
378 0 print {$fh} "\e[2J\e[3J\e[H";
379 0 return $self;
380 }
381  
382 8 ▶sub square_line {
Subroutine square_line: called CC=6 SCAR=19.6
Pod documented
383 8 my ($self, $game, $me, $square) = @_;
384  
385 8 my $name = Game::Reversi::Board->name_of($square);
386 8 my $trial = $game->clone;
387 8 my $move = $trial->play($me, $square);
388  
389 8 ▶ return "$name: " . $move->message
Branch: 1 branch
BranchTrueFalseTotal
if ref $move and $move->isa("Game::Reversi::Error") 0 8 8
Condition33%1/3
ref $move and $move->isa("Game::Reversi::Error")
!ll&&!rl&&r
080
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
390 if ref $move && $move->isa('Game::Reversi::Error');
391  
392 8 ▶ return "$name: placed, turning nothing yet" if $move->phase eq 'place';
Branch: 1 branch
BranchTrueFalseTotal
if $move->phase eq "place" 0 8 8
393  
394 8 my $count = $trial->counts;
395 return sprintf '%s: turns %d %s, and you would be %d to %d',
396 $name, $move->turned, ($move->turned == 1 ? 'disc' : 'discs'),
397 8 ▶ $count->{$me}, $count->{ $me eq 'b' ? 'w' : 'b' };
Branch: 2 branches
BranchTrueFalseTotal
$move->turned == 1 ? : 8 0 8
$me eq 'b' ? : 8 0 8
398 }
399  
400 0 ▶sub pick_help {
Subroutine pick_help: not called CC=1 SCAR=6.9
Pod documented
401 return (
402 0 'arrows walk the squares marked *, which are the only ones that play.',
403 'the line under the board says what the move would turn.',
404 'enter plays it, t goes back to typing, q gives up.',
405 );
406 }
407  
408 4 ▶sub pick_square {
Subroutine pick_square: called CC=24 SCAR=40.1
Pod documented
409 4 my ($self, $game, $me) = @_;
410  
411 4 my @legal = sort { $a <=> $b } map { $_->square } @{ $game->legal($me) };
412 4 ▶ return $self->_typed_turn($game, $me) unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 0 4 4
413  
414 4 my $at = 0;
415 4 ▶ if (my $last = $self->last_move) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->last_move) 0 4 4
416 0 my ($near) = sort { abs($a - $last->square) <=> abs($b - $last->square) }
417 0 .. $#legal;
418 0 ▶ $at = $near if defined $near;
Branch: 1 branch
BranchTrueFalseTotal
if defined $near 0 0 0
419 }
420  
421 4 my @notice;
422  
423 4 while (1) {
424 7 my $count = $game->counts;
425 7 $self->clear;
426 7 $self->_say('', $self->board_text($game, $me, $self->since,
427 cursor => $legal[$at], to_play => $me));
428 $self->_say(' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b}
429 7 . ' ' . $self->glyph(q{w}) . ' ' . $count->{w});
430 7 $self->_say(' ' . $self->square_line($game, $me, $legal[$at]));
431 7 $self->_say(' ' . $_) for @notice;
432 7 @notice = ();
433 7 $self->_say(' ' . $self->_paint(
434 'arrows to choose, enter to play, ? for the keys', 'grid'));
435  
436 7 my $key = $self->read_key;
437 7 ▶ 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 7 7
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
007
not defined $key or $key eq "eof"
l!l&&r!l&&!r
007
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 7 ▶ if ($key eq 'left' || $key eq 'up') { $at = ($at - 1) % @legal; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "left" or $key eq "up") 0 7 7
Condition33%1/3
$key eq "left" or $key eq "up"
l!l&&r!l&&!r
007
MC/DC0%0/2
$key eq "left" or $key eq "up"
$key eq "left" $key eq "up"
Truth table33%1/3
A or B
A= $key eq "left"B= $key eq "up"
ABresult
1X1
011
000
440 7 ▶ if ($key eq 'right' || $key eq 'down' || $key eq 'tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "right" or $key eq "down" or $key eq "tab") 2 5 7
Condition66%4/6
$key eq "right" or $key eq "down" or $key eq "tab"
l!l&&r!l&&!r
205
$key eq "right" or $key eq "down"
l!l&&r!l&&!r
205
MC/DC33%1/3
$key eq "right" or $key eq "down" or $key eq "tab"
$key eq "right" $key eq "down" $key eq "tab"
Truth table50%2/4
A or B or C
A= $key eq "right"B= $key eq "down"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
441 2 $at = ($at + 1) % @legal;
442 2 next;
443 }
444 5 ▶ if ($key eq 'home') { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home") 0 5 5
445 5 ▶ if ($key eq 'end') { $at = $#legal; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end") 0 5 5
446  
447 5 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 1 4 5
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
104
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
448 1 my $played = $game->play($me, $legal[$at]);
449 1 ▶ if (ref $played && $played->isa('Game::Reversi::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $played and $played->isa("Game::Reversi::Error")) 0 1 1
Condition33%1/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
010
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
450 0 @notice = ($played->message . '.');
451 0 next;
452 }
453 1 $self->remember($played);
454 1 ▶ $self->_say(' you play ' . $played->name
Branch: 2 branches
BranchTrueFalseTotal
$played->phase eq 'play' ? : 1 0 1
$played->turned == 1 ? : 1 0 1
455 . ($played->phase eq 'play'
456 ? ', turning ' . $played->turned
457 . ($played->turned == 1 ? ' disc' : ' discs')
458 : ''));
459 1 return 0;
460 }
461  
462 4 ▶ if ($key eq 'q') { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 3 1 4
463 1 ▶ if ($key eq 't') { $self->picking(0); return $self->_typed_turn($game, $me) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t") 0 1 1
464 1 ▶ if ($key eq '?') { @notice = $self->pick_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 1 1
465  
466 1 @notice = ('that key does nothing here. ? for the ones that do');
467 }
468 }
469  
470 163 ▶sub _human_turn {
Subroutine _human_turn: called CC=3 SCAR=11.0
471 163 my ($self, $game, $me) = @_;
472  
473 163 ▶ if ($self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking) 1 162 163
474 1 ▶ return $self->pick_square($game, $me) if $self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enter_raw 0 1 1
475 1 $self->picking(0);
476 }
477  
478 163 return $self->_typed_turn($game, $me);
479 }
480  
481 163 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=15 SCAR=27.4
482 163 my ($self, $game, $me) = @_;
483  
484 163 while (1) {
485 168 my $count = $game->counts;
486 168 $self->_say("", $self->board_text($game, $me, $self->since));
487 $self->_say(
488 ' discs: ' . $self->glyph(q{b}) . ' ' . $count->{b} . ' '
489 . $self->glyph(q{w}) . ' ' . $count->{w},
490 168 ▶ ' ' . ($game->phase eq 'opening'
Branch: 1 branch
BranchTrueFalseTotal
$game->phase eq 'opening' ? : 15 153 168
491 ? 'Place a disc on one of the centre four.'
492 : 'Your move. The squares marked * are the ones that outflank something.'),
493 );
494  
495 168 my $answer = $self->_ask(' > ');
496  
497 168 ▶ return 1 unless defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 1 167 168
498 167 ▶ return 1 if $answer eq 'quit' || $answer eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $answer eq "quit" or $answer eq "resign" 2 165 167
Condition66%2/3
$answer eq "quit" or $answer eq "resign"
l!l&&r!l&&!r
20165
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
499  
500 165 ▶ if ($answer eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer eq "help") 1 164 165
501 1 $self->_help($game, $me);
502 1 next;
503 }
504 164 ▶ if ($answer eq 'board' || $answer eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($answer eq "board" or $answer eq "") 0 164 164
Condition33%1/3
$answer eq "board" or $answer eq ""
l!l&&r!l&&!r
00164
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
505 0 next;
506 }
507  
508 164 my $square = Game::Reversi::Notation->text_to_square($answer);
509 164 ▶ if (!defined $square) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $square) 3 161 164
510 3 $self->_say(' "' . $answer . '" is not a square. Try d3, or "help".');
511 3 next;
512 }
513  
514 161 my $played = $game->play($me, $square);
515 161 ▶ $self->remember($played) unless ref $played && $played->isa("Game::Reversi::Error");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $played and $played->isa("Game::Reversi::Error") 160 1 161
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
01601
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
516 161 ▶ if (ref $played && $played->isa('Game::Reversi::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $played and $played->isa("Game::Reversi::Error")) 1 160 161
Condition66%2/3
ref $played and $played->isa("Game::Reversi::Error")
!ll&&!rl&&r
01601
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
517 1 $self->_say(' ' . $played->message . '.');
518 1 next;
519 }
520  
521 160 ▶ $self->_say(' you play ' . $played->name
Branch: 2 branches
BranchTrueFalseTotal
$played->phase eq 'play' ? : 152 8 160
$played->turned == 1 ? : 65 87 152
522 . ($played->phase eq 'play'
523 ? ', turning ' . $played->turned
524 . ($played->turned == 1 ? ' disc' : ' discs')
525 : ''));
526 160 return 0;
527 }
528 }
529  
530 1 ▶sub _help {
Subroutine _help: called CC=1 SCAR=0.0
531 1 my ($self, $game, $me) = @_;
532 1 $self->_say(
533 '',
534 ' Enter a square as a file and a rank, such as d3 or f5.',
535 ' A move must outflank at least one of the other side\'s discs;',
536 ' every disc it outflanks is turned, and you do not get to choose.',
537 ' The squares marked * are the ones you can play.',
538 ' A turn with no legal move is forfeited for you automatically.',
539 ' The game ends when neither side can move, which can happen with',
540 ' the board not full. "quit" gives up.',
541 '',
542 );
543 1 return;
544 }
545  
546 316 ▶sub _announce_passes {
Subroutine _announce_passes: called CC=3 SCAR=11.0
547 316 my ($self, $game, $seen) = @_;
548 316 my $events = $game->events;
549 316 for my $i ($seen .. $#$events) {
550 325 my $event = $events->[$i];
551 325 ▶ next unless $event->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"kind"} eq "pass" 321 4 325
552 4 my $colour = $event->{payload}{colour};
553 4 $self->_say(' ' . $NAME{$colour} . ' has no legal move, so that turn is '
554 . 'forfeited and the other side plays again.');
555 }
556 316 return scalar @$events;
557 }
558  
559 9 ▶sub _finish {
Subroutine _finish: called CC=7 SCAR=19.6
560 9 my ($self, $game) = @_;
561 9 my $result = $game->result;
562 9 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 9 9
563  
564 9 $self->_say('', $self->board_text($game, undef), '');
565  
566 9 my $count = $result->counts;
567 $self->_say(' discs on the board: ' . $self->glyph(q{b}) . ' ' . $count->{b}
568 9 . ' ' . $self->glyph(q{w}) . ' ' . $count->{w});
569  
570 9 my $score = $result->score;
571 9 ▶ if ($score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score) 6 3 9
572 6 my $empty = Game::Reversi::Board->empties($game->board);
573 $self->_say(' final score: ' . $self->glyph(q{b}) . ' ' . $score->{b}
574 . ' ' . $self->glyph(q{w}) . ' ' . $score->{w}
575 6 ▶ . ($empty ? " (the $empty empty squares go to the winner)" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$empty ? : 1 5 6
576 }
577  
578 9 my $me = $self->colour;
579 9 ▶ $self->_say('', !defined $result->winner ? ' A tie.'
Branch: 2 branches
BranchTrueFalseTotal
!defined($result->winner) ? : 0 9 9
$result->winner eq $me ? : 0 9 9
580 : $result->winner eq $me ? ' You win.'
581 : ' You lose.');
582 9 ▶ $self->_say(' (' . $result->result . ')') unless $result->natural;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->natural 3 6 9
583 9 return;
584 }
585  
586 2 1;
587  
588 __END__
↑ Summary Reversi/Move.pm »