| 1 | package Game::Snake; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use Game::Snake::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Game::Snake::Result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 15 | our @DIRECTIONS = qw/ up down left right /; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 15 | my %DELTA = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | up => [ 0, -1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | down => [ 0, 1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | left => [ -1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | right => [ 1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 15 | my %OPPOSITE = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | up => 'down', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | down => 'up', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | left => 'right', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | right => 'left', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 15 | my %IS_DIRECTION = map { $_ => 1 } @DIRECTIONS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | has cols => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | default => 40 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | has rows => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | default => 28 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | has seed => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | default => 'Game::Snake' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | has start_length => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | default => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | has food_score => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | default => 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | has start_speed => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | default => 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | has _body => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1285 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | has _queue => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1285 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 15 | has _heading => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 15 | has _food => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 15 | has _cause => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 15 | has _error => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | has _grow => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | has _score => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | has _ticks => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | has _status => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | default => 'ready', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | has _placed => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | has _last_rise => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | has _speed => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | private => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 3321 | ▶ | my $place_food = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3321 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 3321 | my @free = $self->free_cells; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 3321 | ▶ | return 0 unless @free; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3320 | my $k = $self->_placed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 3320 | my $hash = Digest::SHA::sha256($self->seed . "snake:food:$k"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 3320 | my $index = unpack('N', substr($hash, 0, 4)) % @free; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 3320 | $self->_food($free[$index]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 3320 | $self->_placed($k + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 3320 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1277 | ▶ | my $lay = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1277 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1277 | my $x = int($self->cols * 0.375); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 1277 | my $y = int($self->rows * 0.643); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1277 | $self->_body([ map { [ $x + $_, $y ] } 0 .. $self->start_length - 1 ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1277 | $self->_heading('left'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1277 | $self->_queue([]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1277 | $self->_food(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1277 | $self->_cause(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1277 | $self->_error(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1277 | $self->_grow(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1277 | $self->_score(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1277 | $self->_ticks(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1277 | $self->_status('ready'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1277 | $self->_placed(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1277 | $self->_last_rise($self->food_score); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1277 | $self->_speed($self->start_speed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 1277 | $self->$place_food; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1277 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 6 | ▶ | my $refuse = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | my ($self, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | $self->_error(Game::Snake::Error->of($code)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 98 | ▶ | my $finish = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 98 | my ($self, $cause) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 98 | $self->_status('finished'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 98 | $self->_cause($cause); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 98 | $self->_queue([]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 98 | return { moved => 0, ate => 0, grew => 0, died => $cause }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1285 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1285 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1285 | for my $name (qw/ cols rows start_length food_score start_speed /) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 6418 | my $value = $self->$name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 6418 | ▶ | Carp::croak("Game::Snake: $name must be a whole number, not " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $value and $value =~ /\A[0-9]+\z/u
MC/DC100%2/2
defined $value and $value =~ /\A[0-9]+\z/u
defined $value
$value =~ /\A[0-9]+\z/u
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\A[0-9]+\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | . (defined $value ? "'$value'" : 'undef')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | unless defined $value && $value =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 1283 | ▶ | Carp::croak('Game::Snake: a board is at least 6 by 6') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->cols < 6 or $self->rows < 6
MC/DC50%1/2
$self->cols < 6 or $self->rows < 6
$self->cols < 6
$self->rows < 6
Truth table66%2/3
A or B
A= $self->cols < 6B= $self->rows < 6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | if $self->cols < 6 || $self->rows < 6; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1279 | ▶ | Carp::croak('Game::Snake: a snake starts at least 2 long') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | if $self->start_length < 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1278 | ▶ | Carp::croak('Game::Snake: the speed starts at 1 or more') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | if $self->start_speed < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1277 | ▶ | Carp::croak('Game::Snake: food is worth 1 or more') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | if $self->food_score < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1276 | ▶ | Carp::croak('Game::Snake: a seed is a string with something in it') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->seed and CORE::length $self->seed
MC/DC50%1/2
defined $self->seed and CORE::length $self->seed
defined $self->seed
CORE::length $self->seed
Truth table66%2/3
A and B
A= defined $self->seedB= CORE::length $self->seed
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | unless defined $self->seed && CORE::length $self->seed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1275 | ▶ | Carp::croak('Game::Snake: a snake ' . $self->start_length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | . ' long does not fit a board ' . $self->cols . ' wide') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | if int($self->cols * 0.375) + $self->start_length > $self->cols; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1274 | $self->$lay; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 1274 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 5846 | ▶ | sub turn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 5846 | my ($self, $direction) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | Carp::croak("Game::Snake: '" . (defined $direction ? $direction : 'undef') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | . "' is not a direction") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 5846 | ▶ | unless defined $direction && $IS_DIRECTION{$direction}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $direction and $IS_DIRECTION{$direction}
MC/DC50%1/2
defined $direction and $IS_DIRECTION{$direction}
defined $direction
$IS_DIRECTION{$direction}
Truth table66%2/3
A and B
A= defined $directionB= $IS_DIRECTION{$direction}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 5845 | ▶ | return $self->$refuse('not_active') if $self->_status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 5844 | my $queue = $self->_queue; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 5844 | ▶ | my $against = @$queue ? $queue->[0] : $self->_heading; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 5844 | ▶ | return $self->$refuse('same_direction') if $direction eq $against; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 5842 | ▶ | return $self->$refuse('reverse') if $direction eq $OPPOSITE{$against}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 5839 | ▶ | if (@$queue >= 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | $queue->[1] = $direction; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 5838 | push @$queue, $direction; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 5839 | $self->_error(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 5839 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 27632 | ▶ | sub tick { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tick: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 27632 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 27632 | ▶ | return { moved => 0 } if $self->_status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 27630 | $self->_status('active'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 27630 | $self->_ticks($self->_ticks + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 27630 | my $queue = $self->_queue; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 27630 | ▶ | $self->_heading(shift @$queue) if @$queue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 27630 | my $body = $self->_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 27630 | my ($dx, $dy) = @{ $DELTA{ $self->_heading } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 27630 | my ($x, $y) = ($body->[0][0] + $dx, $body->[0][1] + $dy); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 27630 | ▶ | return $self->$finish('wall') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$x < 0 or $y < 0 or $x >= $self->cols or $y >= $self->rows
$x < 0 or $y < 0 or $x >= $self->cols
$x < 0 or $y < 0
MC/DC100%4/4
$x < 0 or $y < 0 or $x >= $self->cols or $y >= $self->rows
$x < 0
$y < 0
$x >= $self->cols
$y >= $self->rows
Truth table100%5/5
A or B or C or D
A= $x < 0B= $y < 0C= $x >= $self->colsD= $y >= $self->rows
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | if $x < 0 || $y < 0 || $x >= $self->cols || $y >= $self->rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 27597 | my $growing = $self->_grow > 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 27597 | ▶ | my $last = $growing ? $#$body : $#$body - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 27597 | for my $i (0 .. $last) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 730754 | ▶ | return $self->$finish('body') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$body->[$i][0] == $x and $body->[$i][1] == $y
MC/DC100%2/2
$body->[$i][0] == $x and $body->[$i][1] == $y
$body->[$i][0] == $x
$body->[$i][1] == $y
Truth table100%3/3
A and B
A= $body->[$i][0] == $xB= $body->[$i][1] == $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | if $body->[$i][0] == $x && $body->[$i][1] == $y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 27559 | unshift @$body, [ $x, $y ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 27559 | ▶ | if ($growing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 2024 | $self->_grow($self->_grow - 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 25535 | pop @$body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 27559 | ▶ | my $step = { moved => 1, ate => 0, grew => ($growing ? 1 : 0), died => undef }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 27559 | my $food = $self->_food; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 27559 | ▶ | if ($food && $food->[0] == $x && $food->[1] == $y) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$food and $food->[0] == $x and $food->[1] == $y
$food and $food->[0] == $x
MC/DC66%2/3
$food and $food->[0] == $x and $food->[1] == $y
$food
$food->[0] == $x
$food->[1] == $y
Truth table75%3/4
A and B and C
A= $foodB= $food->[0] == $xC= $food->[1] == $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 2044 | $step->{ate} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 2044 | $self->_grow($self->_grow + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 2044 | my $score = $self->_score + $self->food_score; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2044 | $self->_score($score); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2044 | ▶ | if ($score >= 2 * $self->_last_rise) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 278 | $self->_speed($self->_speed + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 278 | $self->_last_rise($score); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 2044 | $self->_food(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 2044 | ▶ | unless ($self->$place_food) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | $self->$finish('full'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | $step->{died} = 'full'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 27559 | return $step; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 27 | ▶ | sub quit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 27 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 27 | ▶ | return 0 if $self->_status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 26 | $self->$finish('quit'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 26 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | ▶ | sub reset { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | return $self->$lay; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1742071 | ▶ | sub _direction_from { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_direction_from: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 1742071 | my ($from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 1742071 | my ($dx, $dy) = ($to->[0] - $from->[0], $to->[1] - $from->[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 1742071 | for my $direction (@DIRECTIONS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 4577712 | my $delta = $DELTA{$direction}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 4577712 | ▶ | return $direction if $delta->[0] == $dx && $delta->[1] == $dy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$delta->[0] == $dx and $delta->[1] == $dy
MC/DC100%2/2
$delta->[0] == $dx and $delta->[1] == $dy
$delta->[0] == $dx
$delta->[1] == $dy
Truth table100%3/3
A and B
A= $delta->[0] == $dxB= $delta->[1] == $dy
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 64051 | ▶ | sub segments { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
segments: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 64051 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 64051 | my $body = $self->_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 64051 | my $n = $#$body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 64051 | my @dir; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 64051 | for my $i (0 .. $n - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1742071 | $dir[$i] = _direction_from($body->[ $i + 1 ], $body->[$i]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 64051 | $dir[0] = $self->_heading; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 64051 | $dir[$n] = $dir[ $n - 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 64051 | my @segments; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 64051 | for my $i (0 .. $n) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 1806122 | ▶ | my $bend = $i > 0 && $dir[ $i - 1 ] ne $dir[$i] ? $dir[ $i - 1 ] : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i > 0 and $dir[$i - 1] ne $dir[$i]
MC/DC100%2/2
$i > 0 and $dir[$i - 1] ne $dir[$i]
$i > 0
$dir[$i - 1] ne $dir[$i]
Truth table100%3/3
A and B
A= $i > 0B= $dir[$i - 1] ne $dir[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 1806122 | ▶ | push @segments, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | x => $body->[$i][0], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | y => $body->[$i][1], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | dir => $dir[$i], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | bend => $bend, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | last => ($i == $n ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 64051 | return \@segments; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 2263 | ▶ | sub at { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
at: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 2263 | my ($self, $x, $y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 2263 | my $body = $self->_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 2263 | for my $i (0 .. $#$body) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 30490 | ▶ | next unless $body->[$i][0] == $x && $body->[$i][1] == $y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$body->[$i][0] == $x and $body->[$i][1] == $y
MC/DC100%2/2
$body->[$i][0] == $x and $body->[$i][1] == $y
$body->[$i][0] == $x
$body->[$i][1] == $y
Truth table100%3/3
A and B
A= $body->[$i][0] == $xB= $body->[$i][1] == $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 313 | ▶ | return 'head' if $i == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 311 | ▶ | return 'tail' if $i == $#$body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 286 | return 'body'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 1950 | my $food = $self->_food; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 1950 | ▶ | return 'food' if $food && $food->[0] == $x && $food->[1] == $y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$food and $food->[0] == $x and $food->[1] == $y
$food and $food->[0] == $x
MC/DC66%2/3
$food and $food->[0] == $x and $food->[1] == $y
$food
$food->[0] == $x
$food->[1] == $y
Truth table75%3/4
A and B and C
A= $foodB= $food->[0] == $xC= $food->[1] == $y
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 1808 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 3325 | ▶ | sub free_cells { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
free_cells: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 3325 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 3325 | my %taken; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 3325 | $taken{ $_->[0] . ',' . $_->[1] }++ for @{ $self->_body }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 3325 | my @free; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 3325 | for my $y (0 .. $self->rows - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 34788 | for my $x (0 .. $self->cols - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 511873 | ▶ | push @free, [ $x, $y ] unless $taken{"$x,$y"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 3325 | return @free; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 29008 | ▶ | sub food { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
food: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 29008 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 29008 | my $food = $self->_food; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 29008 | ▶ | return $food ? [ @$food ] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 20392 | ▶ | sub length { return scalar @{ $_[0]->_body } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 30412 | ▶ | sub growing { return $_[0]->_grow } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
growing: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 59177 | ▶ | sub heading { return $_[0]->_heading } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
heading: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 58048 | ▶ | sub status { return $_[0]->_status } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 619 | ▶ | sub score { return $_[0]->_score } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 1170 | ▶ | sub speed { return $_[0]->_speed } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
speed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 26837 | ▶ | sub ticks { return $_[0]->_ticks } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ticks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 11 | ▶ | sub error { return $_[0]->_error } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 49 | ▶ | sub result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 49 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 49 | ▶ | return undef unless $self->_status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 47 | return Game::Snake::Result->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | cause => $self->_cause, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | score => $self->_score, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | length => $self->length, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | ticks => $self->_ticks, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | speed => $self->_speed, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||