blib/lib/Game/Snake/Terminal.pm

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