| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | ▶ | return $high if $value > $high; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 114 | ▶ | Carp::croak("Game::Snake::Terminal: $name must be a whole number, not '$value'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | unless $value =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 38 | ▶ | $self->stopped('') unless defined $self->stopped; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 38 | ▶ | $self->refusal('') unless defined $self->refusal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 38 | ▶ | Carp::croak('Game::Snake::Terminal: the clock is a code reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | unless ref $self->clock eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 37 | ▶ | Carp::croak('Game::Snake::Terminal: the sleeper is a code reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | unless ref $self->sleeper eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 37 | ▶ | unless ($self->game) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 37 | my ($w, $h) = $self->_measure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 37 | ▶ | my $cols = defined $self->cols ? $self->cols | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $ENV{"NO_COLOR"} and length $ENV{"NO_COLOR"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 51655 | ▶ | return $self->_wants_ansi ? 1 : 0 if defined $self->_wants_ansi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | return -t $self->out ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 18 | ▶ | return -t $self->out ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 11 | ▶ | return 0 unless defined $self->in && -t $self->in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->in and -t $self->in
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | return eval { require Term::ReadKey; 1 } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 29 | ▶ | if ($self->keysource) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 20 | $self->raw(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 20 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 9 | ▶ | return undef unless $self->keys_available; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $self->_print("\e[?25h"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | $self->_hid_cursor(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 31 | ▶ | return $self unless $self->raw; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 20 | ▶ | eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 62 | ▶ | return () if $self->keysource || !$self->interactive; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->keysource or not $self->interactive
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | ▶ | return () unless eval { require Term::ReadKey; 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my ($w, $h) = eval { Term::ReadKey::GetTerminalSize($self->out) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | return $w ? ($w, $h) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
$w >= $need_w and !$h || $h >= $need_h
not $h or $h >= $need_h
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$h || 0
$h
Truth table50%1/2
A
A= $h
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 682 | ▶ | return $self->keysource->($wait) if $self->keysource; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 394 | ▶ | unless ($self->raw) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 394 | ▶ | $self->sleeper->($wait) if $wait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 394 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | my $mode = !defined $wait ? 0 : $wait > 0 ? $wait : -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 55 | ▶ | return $CONTROL{$char} if $CONTROL{$char}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 50 | ▶ | return $self->read_sequence if $char eq "\e"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 28 | ▶ | unless ($opener eq '[' || $opener eq 'O') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opener eq "[" or $opener eq "O"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 28 | my $char = $self->_read_char($SEQUENCE_WAIT); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 28 | ▶ | last unless defined $char; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 28 | $tail .= $char; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 28 | ▶ | last if $char =~ /[A-Za-z~]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 26 | ▶ | return $SEQUENCE{$tail} || 'escape'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$SEQUENCE{$tail} || 'escape'
MC/DC0%0/1
$SEQUENCE{$tail} || 'escape'
$SEQUENCE{$tail}
Truth table50%1/2
A
A= $SEQUENCE{$tail}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 561 | ▶ | sub _glyphs { return $GLYPH{ $_[0]->ascii ? 'ascii' : 'wide' } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_glyphs: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 202 | ▶ | return $text unless defined $key && defined $SGR{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and defined $SGR{$key}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $note and length $note
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 7 | ▶ | my $key = $note eq 'paused' ? 'paused' : 'over'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 561 | $self->frames($self->frames + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 561 | my @lines = $self->frame_lines($note); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 561 | ▶ | if ($self->interactive) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->interactive and not $self->quiet
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $direction and $direction ne $game->heading
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition77%7/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key or $key eq "q" or $key eq "eof"
not defined $key or $key eq "q"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 23 | ▶ | if (my $direction = $STEER{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 19 | ▶ | return 1 if $self->watch; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 18 | ▶ | $self->refused($self->refused + 1) unless $self->game->turn($direction); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 18 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 4 | ▶ | return $self->_pause if $key eq 'p' || $key eq ' '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "p" or $key eq " "
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 " "
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%6/9
not defined $key or $key eq "q" or $key eq "eof" or $key eq "interrupt"
not defined $key or $key eq "q" or $key eq "eof"
not defined $key or $key eq "q"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 2 | ▶ | last if $key eq 'p' || $key eq ' '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "p" or $key eq " "
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 " "
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 1 | ▶ | if ($STEER{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 1 | $self->refusal($why); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 1 | $self->stopped('narrow'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 29 | ▶ | if ($self->watch) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 17 | my $key = $self->read_key(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 17 | ▶ | unless ($self->_on_key($key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 596 | ▶ | last if $self->max_ticks && $ticker->ticks >= $self->max_ticks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->max_ticks and $ticker->ticks >= $self->max_ticks
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 591 | ▶ | unless ($ticker->due) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 591 | my $key = $self->read_key($ticker->remaining); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 591 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 21 | ▶ | unless ($self->_on_key($key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 9 | ▶ | $self->stopped('capped') unless $game->status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 1 | print STDERR usage(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 1 | return 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 10 | ▶ | if ($opt{help}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 1 | print usage(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 9 | ▶ | if ($opt{version}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 1 | print "snake $VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 8 | ▶ | if (@argv) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 7 | ▶ | $with{rows} = $opt{rows} if defined $opt{rows}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 7 | ▶ | $with{seed} = $opt{seed} if defined $opt{seed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 7 | ▶ | $with{level} = $opt{level} if defined $opt{level}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 7 | ▶ | $with{max_ticks} = $opt{ticks} if defined $opt{ticks}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 7 | ▶ | $with{watch} = 1 if $opt{watch}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 7 | ▶ | $with{quiet} = 1 if $opt{quiet}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 7 | ▶ | $with{ascii} = 1 if $opt{ascii}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 7 | ▶ | $with{ansi} = $opt{colour} ? 1 : 0 if defined $opt{colour}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 7 | ▶ | if ($opt{raylib}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | ▶ | unless (eval { require Game::Snake::Raylib; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | print STDERR 'snake: ' . $sentence->($@) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | return 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 0 | ▶ | return $window->start ? 0 : 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 7 | my $terminal = eval { $class->new(%with) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 7 | ▶ | unless ($terminal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | 5 | ▶ | return 0 if $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 2 | ▶ | print STDERR 'snake: ' . $terminal->refusal . "\n" if CORE::length $terminal->refusal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 2 | return 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||