blib/lib/Game/Snake/Terminal.pm

filter: Statementstmt 93.9% 308 / 328 Branchbran 81.8% 149 / 182 Conditioncond 67.9% 36 / 53 MC/DCmcdc 43.3% 13 / 30 Subroutinesub 100% 52 / 52 Podpod 94.4% 17 / 18 totaltotal 86.7% 575 / 663 CC 113 SCAR 24.7
CC
113
Cov
88%
run
36.9
BUILD
19.7
start
15.0
CRAP 11.8
« blib/lib/Game/Snake/Raylib.pm ↑ Summary blib/lib/Game/Snake.pm »
1 package Game::Snake::Terminal;
2  
3 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Game::Snake;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Game::Snake::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Game::Terminal::Kit::Ticker;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 our $VERSION = '0.13';
20  
21 7 my %GLYPH = (
22 wide => {
23 head => { up => "\xE2\x96\x80\xE2\x96\x80", down => "\xE2\x96\x84\xE2\x96\x84", left => "\xE2\x96\x8C\xE2\x96\x88", right => "\xE2\x96\x88\xE2\x96\x90" },
24 body => "\xE2\x96\x88\xE2\x96\x88",
25 tail => "\xE2\x96\x92\xE2\x96\x92",
26 food => "\xE2\x97\x86 ",
27 ground => ' ',
28 h => "\xE2\x94\x80", v => "\xE2\x94\x82",
29 tl => "\xE2\x95\xAD", tr => "\xE2\x95\xAE", bl => "\xE2\x95\xB0", br => "\xE2\x95\xAF",
30 },
31 ascii => {
32 head => { up => '^^', down => 'vv', left => '<<', right => '>>' },
33 body => '##',
34 tail => '::',
35 food => '()',
36 ground => ' ',
37 h => '-', v => '|',
38 tl => '+', tr => '+', bl => '+', br => '+',
39 },
40 );
41  
42 7 my %SGR = (
43 head => '1;32',
44 body => '32',
45 tail => '2;32',
46 food => '1;31',
47 frame => '2',
48 status => '1',
49 paused => '1;33',
50 over => '1;31',
51 legend => '2',
52 );
53  
54 7 my %STEER = (
55 up => 'up', down => 'down', left => 'left', right => 'right',
56 w => 'up', s => 'down', a => 'left', d => 'right',
57 k => 'up', j => 'down', h => 'left', l => 'right',
58 );
59  
60  
61 7 my ($MIN_COLS, $MAX_COLS, $MIN_ROWS, $MAX_ROWS) = (10, 40, 8, 28);
62  
63 has in => (
64 is => 'rw',
65 isa => Any,
66 7 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
67 7 );
68  
69 has out => (
70 is => 'rw',
71 isa => Any,
72 7 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
73 7 );
74  
75 7 has cols => (is => 'ro');
76 7 has rows => (is => 'ro');
77  
78 has seed => (
79 is => 'ro',
80 isa => Str,
81 default => ''
82 7 );
83  
84 has level => (
85 is => 'ro',
86 isa => Int,
87 default => 2
88 7 );
89  
90 has watch => (
91 is => 'ro',
92 isa => Int,
93 default => 0
94 7 );
95  
96 has quiet => (
97 is => 'ro',
98 isa => Int,
99 default => 0
100 7 );
101  
102 has ascii => (
103 is => 'ro',
104 isa => Int,
105 default => 0
106 7 );
107  
108 has max_ticks => (
109 is => 'ro',
110 isa => Int,
111 default => 0
112 7 );
113  
114 has game => (
115 is => 'rw',
116 isa => Object
117 7 );
118  
119 has [qw/keyboard screen/] => (
120 is => 'rw'
121 7 );
122  
123 7 has keysource => (is => 'rw');
124  
125 has pending => (
126 is => 'rw',
127 isa => ArrayRef,
128 41 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
129 7 );
130  
131 has clock => (
132 is => 'ro',
133 isa => Any,
134 7 ▶ default => sub { \&Time::HiRes::time }
Subroutine __ANON__: called CC=1 SCAR=0.0
135 7 );
136  
137 has sleeper => (
138 is => 'ro',
139 isa => Any,
140 7 ▶ default => sub { sub { select undef, undef, undef, $_[0] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
141 7 );
142  
143 has ticker => (
144 is => 'rw',
145 isa => Object
146 7 );
147  
148 7 has width => (is => 'rw');
149 7 has height => (is => 'rw');
150  
151 has frames => (
152 is => 'rw',
153 isa => Int,
154 default => 0
155 7 );
156  
157 has refused => (
158 is => 'rw',
159 isa => Int,
160 default => 0
161 7 );
162  
163 has stopped => (
164 is => 'rw',
165 isa => Str,
166 default => ''
167 7 );
168  
169 has refusal => (
170 is => 'rw',
171 isa => Str,
172 default => ''
173 7 );
174  
175 has _wants_ansi => (
176 is => 'ro',
177 init_arg => 'ansi',
178 private => 1
179 7 );
180  
181 has _wants_interactive => (
182 is => 'ro',
183 init_arg => 'interactive',
184 private => 1
185 7 );
186  
187 6 ▶my $clamp = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
188 6 my ($value, $low, $high) = @_;
189 6 ▶ return $low if $value < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $low 2 4 6
190 4 ▶ return $high if $value > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $high 2 2 4
191 2 return $value;
192 7 };
193  
194 41 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=29.8
Pod undocumented
195 41 my ($self) = @_;
196  
197 41 Game::Snake::Bot->describe($self->level);
198  
199 39 for my $name (qw/ cols rows width height max_ticks /) {
200 191 my $value = $self->$name;
201 191 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 77 114 191
202 114 ▶ Carp::croak("Game::Snake::Terminal: $name must be a whole number, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/u 1 113 114
203 unless $value =~ /\A[0-9]+\z/;
204 }
205  
206 38 ▶ $self->stopped('') unless defined $self->stopped;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stopped 38 0 38
207 38 ▶ $self->refusal('') unless defined $self->refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->refusal 38 0 38
208  
209 38 ▶ Carp::croak('Game::Snake::Terminal: the clock is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->clock eq "CODE" 1 37 38
210 unless ref $self->clock eq 'CODE';
211 37 ▶ Carp::croak('Game::Snake::Terminal: the sleeper is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->sleeper eq "CODE" 0 37 37
212 unless ref $self->sleeper eq 'CODE';
213  
214 37 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 37 0 37
$self->keysource ? : 27 10 37
215 in => $self->in,
216 pending => $self->pending,
217 sleeper => $self->sleeper,
218 ($self->keysource ? (keysource => $self->keysource) : ()),
219 )) unless $self->keyboard;
220 37 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->screen 37 0 37
$self->ascii ? : 31 6 37
221 out => $self->out,
222 interactive => $self->interactive,
223 colour => $self->ansi,
224 ascii => $self->ascii ? 1 : 0,
225 )) unless $self->screen;
226  
227 37 ▶ unless ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->game) 37 0 37
228 37 my ($w, $h) = $self->_measure;
229 37 ▶ my $cols = defined $self->cols ? $self->cols
Branch: 2 branches
BranchTrueFalseTotal
defined $self->cols ? : 33 4 37
$w ? : 3 1 4
230 : $w ? $clamp->(int(($w - 2) / 2), $MIN_COLS, $MAX_COLS) : $MAX_COLS;
231 37 ▶ my $rows = defined $self->rows ? $self->rows
Branch: 2 branches
BranchTrueFalseTotal
defined $self->rows ? : 32 5 37
$h ? : 3 2 5
232 : $h ? $clamp->($h - 5, $MIN_ROWS, $MAX_ROWS) : $MAX_ROWS;
233 37 my $seed = $self->seed;
234 37 ▶ $seed = Digest::SHA::sha256_hex(join ':', $self->clock->(), $$)
Branch: 1 branch
BranchTrueFalseTotal
unless length $seed 3 34 37
235 unless CORE::length $seed;
236 37 $self->game(Game::Snake->new(cols => $cols, rows => $rows, seed => $seed));
237 }
238  
239 35 ▶ $self->ticker(Game::Terminal::Kit::Ticker->new(clock => $self->clock)) unless $self->ticker;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ticker 35 0 35
240  
241 35 return;
242 }
243  
244 11650 ▶sub ansi {
Subroutine ansi: called CC=6 SCAR=18.1
Pod documented
245 11650 my ($self) = @_;
246 11650 ▶ return 0 if defined $ENV{NO_COLOR} && CORE::length $ENV{NO_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"} 12 11638 11650
Condition66%2/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
!ll&&!rl&&r
11638012
MC/DC50%1/2
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
defined $ENV{"NO_COLOR"} length $ENV{"NO_COLOR"}
Truth table66%2/3
A and B
A= defined $ENV{"NO_COLOR"}B= length $ENV{"NO_COLOR"}
ABresult
0X0
100
111
247 11638 ▶ return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_ansi 11631 7 11638
$self->_wants_ansi ? : 22 11609 11631
248 7 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 7 7
249 }
250  
251 12256 ▶sub interactive {
Subroutine interactive: called CC=4 SCAR=14.3
Pod documented
252 12256 my ($self) = @_;
253 12256 ▶ return $self->_wants_interactive ? 1 : 0 if defined $self->_wants_interactive;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_wants_interactive 12189 67 12256
$self->_wants_interactive ? : 0 12189 12189
254 67 ▶ return -t $self->out ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $self->out ? : 0 67 67
255 }
256  
257 11613 ▶sub _synced {
Subroutine _synced: called CC=1 SCAR=0.0
258 11613 my ($self) = @_;
259 11613 my $screen = $self->screen;
260 11613 $screen->out($self->out);
261 11613 $screen->interactive($self->interactive);
262 11613 $screen->colour($self->ansi);
263 11613 return $screen;
264 }
265  
266 1 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
267 1 my ($self) = @_;
268 1 return $self->keyboard->raw;
269 }
270  
271 2 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
272 2 my ($self) = @_;
273 2 return $self->keyboard->keys_available;
274 }
275  
276 29 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=6.9
Pod documented
277 29 my ($self) = @_;
278 29 ▶ return $self->keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 20 9 29
279 }
280  
281 31 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
282 31 my ($self) = @_;
283 31 $self->screen->restore;
284 31 $self->keyboard->leave_raw;
285 31 return $self;
286 }
287  
288 628 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
289 628 my ($self, $wait) = @_;
290 628 return $self->keyboard->read_key($wait);
291 }
292  
293 4 ▶sub cannot_play {
Subroutine cannot_play: called CC=1 SCAR=0.0
Pod documented
294 4 return 'A real-time game needs a terminal and Term::ReadKey to read the keys.'
295 . ' Install Term::ReadKey, or run snake --watch to watch the bot play.';
296 }
297  
298 69 ▶sub _measure {
Subroutine _measure: called CC=5 SCAR=19.4
299 69 my ($self) = @_;
300 69 ▶ return ($self->width, $self->height) if defined $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->width 7 62 69
301 62 ▶ return () if $self->keysource || !$self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource or not $self->interactive 62 0 62
Condition66%2/3
$self->keysource or not $self->interactive
l!l&&r!l&&!r
43190
MC/DC0%0/2
$self->keysource or not $self->interactive
$self->keysource not $self->interactive
Truth table66%2/3
A or not(B)
A= $self->keysourceB= not $self->interactive
ABresult
1X1
011
000
302 0 my ($w, $h) = $self->_synced->measured;
303 0 ▶ return $w ? ($w, $h) : ();
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 0 0 0
304 }
305  
306 32 ▶sub too_narrow {
Subroutine too_narrow: called CC=6 SCAR=18.5
Pod documented
307 32 my ($self) = @_;
308 32 my ($w, $h) = $self->_measure;
309 32 ▶ return undef unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 29 3 32
310 3 my $game = $self->game;
311 3 my ($need_w, $need_h) = (2 * $game->cols + 2, $game->rows + 5);
312 3 ▶ return undef if $w >= $need_w && (!$h || $h >= $need_h);
Branch: 1 branch
BranchTrueFalseTotal
if $w >= $need_w and !$h || $h >= $need_h 1 2 3
Condition50%3/6
$w >= $need_w and !$h || $h >= $need_h
!ll&&!rl&&r
201
not $h or $h >= $need_h
l!l&&r!l&&!r
010
MC/DC33%1/3
$w >= $need_w and !$h || $h >= $need_h
$w >= $need_w not $h $h >= $need_h
Truth table50%2/4
A and not(B) or C
A= $w >= $need_wB= not $hC= $h >= $need_h
ABCresult
0XX0
1000
11X1
1011
313 2 ▶ return sprintf 'This window is %d columns by %d lines, and a %d by %d board needs %d by %d.'
Condition50%1/2
$h || 0
l!l
20
MC/DC0%0/1
$h || 0
$h
Truth table50%1/2
A
A= $h
Aresult
11
00
314 . ' Widen it, or ask for a smaller board with --cols and --rows.',
315 $w, ($h || 0), $game->cols, $game->rows, $need_w, $need_h;
316 }
317  
318 561 ▶sub _glyphs { return $GLYPH{ $_[0]->ascii ? 'ascii' : 'wide' } }
Subroutine _glyphs: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 560 1 561
319  
320 51756 ▶sub _paint {
Subroutine _paint: called CC=3 SCAR=11.0
321 51756 my ($self, $text, $key) = @_;
322 51756 ▶ return $text unless defined $key && defined $SGR{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $SGR{$key} 40730 11026 51756
Condition66%2/3
defined $key and defined $SGR{$key}
!ll&&!rl&&r
40730011026
MC/DC50%1/2
defined $key and defined $SGR{$key}
defined $key defined $SGR{$key}
Truth table66%2/3
A and B
A= defined $keyB= defined $SGR{$key}
ABresult
0X0
100
111
323 11026 return $self->_synced->paint($text, $SGR{$key});
324 }
325  
326 26 ▶sub _say {
Subroutine _say: called CC=2 SCAR=7.1
327 26 my ($self, @lines) = @_;
328 26 ▶ $self->_synced->say(@lines) if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 26 0 26
329 26 return;
330 }
331  
332 561 ▶sub status_line {
Subroutine status_line: called CC=4 SCAR=13.9
Pod documented
333 561 my ($self, $note) = @_;
334 561 my $game = $self->game;
335 561 my $line = sprintf ' Snake score %d length %d speed %d/s',
336 $game->score, $game->length, $game->speed;
337 561 $line = $self->_paint($line, 'status');
338 561 ▶ if (defined $note && CORE::length $note) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $note and length $note) 7 554 561
Condition66%2/3
defined $note and length $note
!ll&&!rl&&r
55407
MC/DC50%1/2
defined $note and length $note
defined $note length $note
Truth table66%2/3
A and B
A= defined $noteB= length $note
ABresult
0X0
100
111
339 7 ▶ my $key = $note eq 'paused' ? 'paused' : 'over';
Branch: 1 branch
BranchTrueFalseTotal
$note eq 'paused' ? : 3 4 7
340 7 $line .= ' ' . $self->_paint($note, $key);
341 }
342 561 return $line;
343 }
344  
345 561 ▶sub legend {
Subroutine legend: called CC=2 SCAR=6.9
Pod documented
346 561 my ($self) = @_;
347 561 ▶ my $text = $self->watch
Branch: 1 branch
BranchTrueFalseTotal
$self->watch ? : 513 48 561
348 ? ' watching level ' . $self->level . ': q quits, p pauses'
349 : ' arrows, wasd or hjkl steer; p pauses; q quits';
350 561 return $self->_paint($text, 'legend');
351 }
352  
353 561 ▶sub board_lines {
Subroutine board_lines: called CC=6 SCAR=17.9
Pod documented
354 561 my ($self) = @_;
355 561 my $game = $self->game;
356 561 my $g = $self->_glyphs;
357 561 my ($cols, $rows) = ($game->cols, $game->rows);
358  
359 561 my @cell = map { [ ($g->{ground}) x $cols ] } 1 .. $rows;
360 561 my @key = map { [ (undef) x $cols ] } 1 .. $rows;
361  
362 561 ▶ if (my $food = $game->food) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $food = $game->food) 561 0 561
363 561 $cell[ $food->[1] ][ $food->[0] ] = $g->{food};
364 561 $key[ $food->[1] ][ $food->[0] ] = 'food';
365 }
366  
367 561 my $segments = $game->segments;
368 561 for my $i (0 .. $#$segments) {
369 7653 my $segment = $segments->[$i];
370 my ($glyph, $key) = $i == 0 ? ($g->{head}{ $segment->{dir} }, 'head')
371 : $segment->{last} ? ($g->{tail}, 'tail')
372 7653 ▶ : ($g->{body}, 'body');
Branch: 2 branches
BranchTrueFalseTotal
$i == 0 ? : 561 7092 7653
$segment->{'last'} ? : 561 6531 7092
373 7653 $cell[ $segment->{y} ][ $segment->{x} ] = $glyph;
374 7653 $key[ $segment->{y} ][ $segment->{x} ] = $key;
375 }
376  
377 561 my $v = $self->_paint($g->{v}, 'frame');
378 561 my @lines = ($self->_paint($g->{tl} . ($g->{h} x (2 * $cols)) . $g->{tr}, 'frame'));
379 561 for my $y (0 .. $rows - 1) {
380 4488 push @lines, $v . join('', map { $self->_paint($cell[$y][$_], $key[$y][$_]) } 0 .. $cols - 1) . $v;
381 }
382 561 push @lines, $self->_paint($g->{bl} . ($g->{h} x (2 * $cols)) . $g->{br}, 'frame');
383 561 return @lines;
384 }
385  
386 561 ▶sub frame_lines {
Subroutine frame_lines: called CC=1 SCAR=0.0
Pod documented
387 561 my ($self, $note) = @_;
388 561 return ($self->status_line($note), $self->board_lines, $self->legend);
389 }
390  
391 599 ▶sub _frame {
Subroutine _frame: called CC=3 SCAR=11.0
392 599 my ($self, $note) = @_;
393 599 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 38 561 599
394 561 $self->frames($self->frames + 1);
395 561 my @lines = $self->frame_lines($note);
396 561 my $screen = $self->_synced->flush_on;
397 561 $screen->show(\@lines);
398 561 ▶ $screen->say('') unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 561 0 561
399 561 return;
400 }
401  
402 26 ▶sub _intro {
Subroutine _intro: called CC=3 SCAR=15.1
403 26 my ($self) = @_;
404 26 ▶ return unless $self->interactive && !$self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive and not $self->quiet 26 0 26
Condition33%1/3
$self->interactive and not $self->quiet
!ll&&!rl&&r
2600
MC/DC0%0/2
$self->interactive and not $self->quiet
$self->interactive not $self->quiet
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not $self->quiet
ABresult
0X0
100
111
405 0 $self->_synced->flush_on->clear->hide_cursor;
406 0 return;
407 }
408  
409 538 ▶sub _bot_turn {
Subroutine _bot_turn: called CC=3 SCAR=11.0
410 538 my ($self) = @_;
411 538 my $game = $self->game;
412 538 my $direction = Game::Snake::Bot->choose($game, level => $self->level);
413 538 ▶ $game->turn($direction) if defined $direction && $direction ne $game->heading;
Branch: 1 branch
BranchTrueFalseTotal
if defined $direction and $direction ne $game->heading 137 401 538
Condition66%2/3
defined $direction and $direction ne $game->heading
!ll&&!rl&&r
0401137
MC/DC50%1/2
defined $direction and $direction ne $game->heading
defined $direction $direction ne $game->heading
Truth table66%2/3
A and B
A= defined $directionB= $direction ne $game->heading
ABresult
0X0
100
111
414 538 return;
415 }
416  
417 39 ▶sub _on_key {
Subroutine _on_key: called CC=10 SCAR=23.1
418 39 my ($self, $key) = @_;
419 39 ▶ return 0 if !defined $key || $key eq 'q' || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt" 16 23 39
Condition77%7/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
16023
not defined $key or $key eq "q" or $key eq "eof"
l!l&&r!l&&!r
16023
not defined $key or $key eq "q"
l!l&&r!l&&!r
11523
MC/DC50%2/4
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "q" $key eq "eof" $key eq "interrupt"
Truth table60%3/5
not(A) or B or C or D
A= not defined $keyB= $key eq "q"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
420 23 ▶ if (my $direction = $STEER{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $direction = $STEER{$key}) 19 4 23
421 19 ▶ return 1 if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 1 18 19
422 18 ▶ $self->refused($self->refused + 1) unless $self->game->turn($direction);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game->turn($direction) 1 17 18
423 18 return 1;
424 }
425 4 ▶ return $self->_pause if $key eq 'p' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" or $key eq " " 3 1 4
Condition100%3/3
$key eq "p" or $key eq " "
l!l&&r!l&&!r
211
MC/DC100%2/2
$key eq "p" or $key eq " "
$key eq "p" $key eq " "
Truth table100%3/3
A or B
A= $key eq "p"B= $key eq " "
ABresult
1X1
011
000
426 1 return 1;
427 }
428  
429 3 ▶sub _pause {
Subroutine _pause: called CC=8 SCAR=21.2
430 3 my ($self) = @_;
431 3 $self->_frame('paused');
432 3 while (1) {
433 3 my $key = $self->read_key(undef);
434 3 ▶ return 0 if !defined $key || $key eq 'q' || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt" 1 2 3
Condition66%6/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
102
not defined $key or $key eq "q" or $key eq "eof"
l!l&&r!l&&!r
102
not defined $key or $key eq "q"
l!l&&r!l&&!r
102
MC/DC25%1/4
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "q" $key eq "eof" $key eq "interrupt"
Truth table40%2/5
not(A) or B or C or D
A= not defined $keyB= $key eq "q"C= $key eq "eof"D= $key eq "interrupt"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
435 2 ▶ last if $key eq 'p' || $key eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "p" or $key eq " " 1 1 2
Condition66%2/3
$key eq "p" or $key eq " "
l!l&&r!l&&!r
101
MC/DC50%1/2
$key eq "p" or $key eq " "
$key eq "p" $key eq " "
Truth table66%2/3
A or B
A= $key eq "p"B= $key eq " "
ABresult
1X1
011
000
436 1 ▶ if ($STEER{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STEER{$key}) 1 0 1
437 1 $self->_on_key($key);
438 1 last;
439 }
440 }
441 2 $self->ticker->start(1 / $self->game->speed);
442 2 return 1;
443 }
444  
445 26 ▶my $finish = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
446 26 my ($self) = @_;
447 26 my $game = $self->game;
448 26 ▶ $game->quit unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 22 4 26
449 26 $self->leave_raw;
450 26 my $result = $game->result;
451 26 $self->_say($result->stringify);
452 26 return $result;
453 7 };
454  
455 30 ▶sub start {
Subroutine start: called CC=15 SCAR=27.1
Pod documented
456 30 my ($self) = @_;
457 30 my $game = $self->game;
458  
459 30 ▶ if (my $why = $self->too_narrow) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $self->too_narrow) 1 29 30
460 1 $self->refusal($why);
461 1 $self->stopped('narrow');
462 1 return undef;
463 }
464  
465 29 ▶ if ($self->watch) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->watch) { } 9 20 29
elsif (not $self->enter_raw) { } 3 17 20
466 9 $self->enter_raw;
467 }
468 elsif (!$self->enter_raw) {
469 3 $self->refusal($self->cannot_play);
470 3 $self->stopped('keys');
471 3 return undef;
472 }
473  
474 26 $self->_intro;
475  
476 26 my $ticker = $self->ticker;
477 26 $self->_frame;
478  
479 26 ▶ unless ($self->watch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->watch) 17 9 26
480 17 my $key = $self->read_key(undef);
481 17 ▶ unless ($self->_on_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_on_key($key)) 1 16 17
482 1 $self->stopped('quit');
483 1 return $self->$finish;
484 }
485 }
486  
487 25 $ticker->start(1 / $game->speed);
488  
489 25 ▶ while ($game->status ne 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status ne "finished") 596 4 600
490 596 ▶ last if $self->max_ticks && $ticker->ticks >= $self->max_ticks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_ticks and $ticker->ticks >= $self->max_ticks 5 591 596
Condition100%3/3
$self->max_ticks and $ticker->ticks >= $self->max_ticks
!ll&&!rl&&r
3022895
MC/DC100%2/2
$self->max_ticks and $ticker->ticks >= $self->max_ticks
$self->max_ticks $ticker->ticks >= $self->max_ticks
Truth table100%3/3
A and B
A= $self->max_ticksB= $ticker->ticks >= $self->max_ticks
ABresult
0X0
100
111
491  
492 591 ▶ unless ($ticker->due) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ticker->due) 591 0 591
493 591 my $key = $self->read_key($ticker->remaining);
494 591 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 21 570 591
495 21 ▶ unless ($self->_on_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_on_key($key)) 16 5 21
496 16 $self->stopped('quit');
497 16 return $self->$finish;
498 }
499 5 next;
500 }
501 }
502  
503 570 ▶ $self->_bot_turn if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 538 32 570
504 570 my $step = $game->tick;
505 570 $ticker->tick(1 / $game->speed);
506 570 ▶ $self->_frame($step->{died} ? $game->result->stringify : undef);
Branch: 1 branch
BranchTrueFalseTotal
$step->{'died'} ? : 4 566 570
507 }
508  
509 9 ▶ $self->stopped('capped') unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 5 4 9
510 9 return $self->$finish;
511 }
512  
513 13 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
514 13 return <<'USAGE';
515 usage: snake [options]
516  
517 --cols N, --rows N the board; the default fits the window, at most 40 by 28
518 --seed TEXT the game to play: the same seed and the same moves are the same game
519 --watch watch the bot play
520 --level N the bot's level for --watch, 1 or 2 (default 2)
521 --ticks N stop after N ticks (0, the default, plays to the end)
522 --quiet no frames, only how it ended
523 --ascii plain characters instead of block glyphs
524 --colour, --no-colour force colour on or off; NO_COLOR in the environment wins
525 --raylib open a window instead of playing in the terminal
526 (needs Raylib::FFI and perl 5.38 or later)
527 --cell N the window's cell size in pixels (default 20)
528 --version, --help
529  
530 keys: arrows, wasd or hjkl steer; p or space pauses; q quits.
531  
532 exit status: 0 the game ended or you quit; 2 a bad option, or a game that could not start.
533 USAGE
534 }
535  
536 2 ▶my $sentence = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
537 2 my ($why) = @_;
538 2 $why =~ s/ at \S+ line \d+\.?\s*\z//s;
539 2 $why =~ s/\A(?:Game::Snake(?:::\w+)*|snake): //;
540 2 $why =~ s/\s+\z//;
541 2 ▶ $why .= '.' unless $why =~ /[.!?]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $why =~ /[.!?]\z/u 2 0 2
542 2 return $why;
543 7 };
544  
545 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
546 return [
547 2 { name => 'level', type => 'int', min => 1, max => 2, help => "the bot's level, for watch" },
548 { name => 'watch', type => 'flag', help => 'watch the bot play' },
549 { name => 'rows', type => 'int', min => 8, max => 28, help => 'the board; the default fits the window' },
550 { name => 'cols', type => 'int', min => 8, max => 40, help => 'the board; the default fits the window' },
551 { name => 'seed', type => 'str', help => 'the same seed and the same moves are the same game' },
552 ];
553 }
554  
555 21 ▶sub run {
Subroutine run: called CC=24 SCAR=36.1
Pod documented
556 21 my ($class, @argv) = @_;
557  
558 21 my %opt;
559 21 my $ok = eval {
560 21 ▶ local $SIG{__WARN__} = sub { my $w = $_[0]; $w =~ s/\s+\z//; print STDERR "snake: $w\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
561 21 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
562 'cols|c=i', 'rows|r=i', 'seed=s', 'level|l=i', 'watch|w', 'ticks=i',
563 'quiet|q', 'ascii|a', 'colour|color!', 'raylib', 'cell=i', 'version|v', 'help|h');
564 };
565 21 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 19 21
566 2 print STDERR usage();
567 2 return 2;
568 }
569 19 ▶ if ($opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 10 9 19
570 10 print usage();
571 10 return 0;
572 }
573 9 ▶ if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 1 8 9
574 1 print "snake $VERSION\n";
575 1 return 0;
576 }
577 8 ▶ if (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 7 8
578 1 print STDERR "snake: I do not know '$argv[0]'.\n", usage();
579 1 return 2;
580 }
581  
582 7 my %with;
583 7 ▶ $with{cols} = $opt{cols} if defined $opt{cols};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"cols"} 6 1 7
584 7 ▶ $with{rows} = $opt{rows} if defined $opt{rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"rows"} 5 2 7
585 7 ▶ $with{seed} = $opt{seed} if defined $opt{seed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"seed"} 3 4 7
586 7 ▶ $with{level} = $opt{level} if defined $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"level"} 1 6 7
587 7 ▶ $with{max_ticks} = $opt{ticks} if defined $opt{ticks};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"ticks"} 5 2 7
588 7 ▶ $with{watch} = 1 if $opt{watch};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"watch"} 5 2 7
589 7 ▶ $with{quiet} = 1 if $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"quiet"} 2 5 7
590 7 ▶ $with{ascii} = 1 if $opt{ascii};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"ascii"} 1 6 7
591 7 ▶ $with{ansi} = $opt{colour} ? 1 : 0 if defined $opt{colour};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opt{"colour"} 1 6 7
$opt{'colour'} ? : 0 1 1
592  
593 7 ▶ if ($opt{raylib}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"raylib"}) 0 7 7
594 0 ▶ unless (eval { require Game::Snake::Raylib; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Game::Snake::Raylib; 1 } }) 0 0 0
595 0 print STDERR "snake: the window is not part of this build; play in the terminal.\n";
596 0 return 2;
597 }
598 0 ▶ unless (Game::Snake::Raylib->available) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Game::Snake::Raylib"->available) 0 0 0
599 0 print STDERR 'snake: ' . Game::Snake::Raylib->why_not . "\n";
600 0 return 2;
601 }
602 0 ▶ $with{cell} = $opt{cell} if defined $opt{cell};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"cell"} 0 0 0
603 0 delete @with{qw/ quiet ascii ansi /};
604 0 my $window = eval { Game::Snake::Raylib->new(%with) };
605 0 ▶ unless ($window) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($window) 0 0 0
606 0 print STDERR 'snake: ' . $sentence->($@) . "\n";
607 0 return 2;
608 }
609 0 ▶ return $window->start ? 0 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$window->start ? : 0 0 0
610 }
611  
612 7 my $terminal = eval { $class->new(%with) };
613 7 ▶ unless ($terminal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terminal) 2 5 7
614 2 print STDERR 'snake: ' . $sentence->($@) . "\n";
615 2 return 2;
616 }
617  
618 5 ▶ my $result = Game::Terminal::Kit::Run::guard(sub { $terminal->start }, $terminal);
Subroutine __ANON__: called CC=1 SCAR=0.0
619  
620 5 ▶ return 0 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 2 5
621 2 ▶ print STDERR 'snake: ' . $terminal->refusal . "\n" if CORE::length $terminal->refusal;
Branch: 1 branch
BranchTrueFalseTotal
if length $terminal->refusal 2 0 2
622 2 return 2;
623 }
624  
625 7 1;
626  
627 __END__
« blib/lib/Game/Snake/Raylib.pm ↑ Summary blib/lib/Game/Snake.pm »