| 1 | package Game::Snake::Bot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | our @LADDER = (1, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | my %DESCRIBE = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 => 'greedy: of the safe headings, the one that closes on the food', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 => 'flood-fill: greedy, but never into a space smaller than itself while a bigger one is open', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | my %DELTA = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | up => [ 0, -1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | down => [ 0, 1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | left => [ -1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | right => [ 1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | my %SIDES = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | up => [ 'left', 'right' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | down => [ 'left', 'right' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | left => [ 'up', 'down' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | right => [ 'up', 'down' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub levels { return @LADDER } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
levels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 52 | ▶ | sub describe { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
describe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 52 | my ($class, $level) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 52 | return $DESCRIBE{ $class->_level($level) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 26488 | ▶ | sub _level { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_level: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 26488 | my ($class, $level) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 26488 | ▶ | $level = $LADDER[-1] unless defined $level; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Carp::croak("Game::Snake::Bot: a level is one of @LADDER, not '$level'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 26488 | ▶ | unless !ref $level && $DESCRIBE{$level}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $level and $DESCRIBE{$level}
MC/DC50%1/2
not ref $level and $DESCRIBE{$level}
not ref $level
$DESCRIBE{$level}
Truth table66%2/3
not(A) and B
A= not ref $levelB= $DESCRIBE{$level}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 26483 | return $level; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 30371 | ▶ | my $scan = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 30371 | my ($game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 30371 | my $cols = $game->cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 30371 | my $segments = $game->segments; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 30371 | my $growing = $game->growing; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 30371 | my @blocked; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 30371 | for my $i (0 .. $#$segments) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 870493 | my $segment = $segments->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 870493 | ▶ | next if $i == $#$segments && $i > 0 && !$growing; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$i == $segments->$#* and $i > 0 and not $growing
$i == $segments->$#* and $i > 0
MC/DC66%2/3
$i == $segments->$#* and $i > 0 and not $growing
$i == $segments->$#*
$i > 0
not $growing
Truth table75%3/4
A and B and not(C)
A= $i == $segments->$#*B= $i > 0C= not $growing
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 842300 | $blocked[ $segment->{y} * $cols + $segment->{x} ] = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 30371 | return \@blocked; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 200481 | ▶ | my $ahead = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 200481 | my ($head, $direction) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 200481 | my $delta = $DELTA{$direction}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 200481 | return ($head->{x} + $delta->[0], $head->{y} + $delta->[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 91035 | ▶ | my $fatal = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 91035 | my ($game, $blocked, $x, $y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 91035 | ▶ | return 1 if $x < 0 || $y < 0 || $x >= $game->cols || $y >= $game->rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$x < 0 or $y < 0 or $x >= $game->cols or $y >= $game->rows
$x < 0 or $y < 0 or $x >= $game->cols
$x < 0 or $y < 0
MC/DC100%4/4
$x < 0 or $y < 0 or $x >= $game->cols or $y >= $game->rows
$x < 0
$y < 0
$x >= $game->cols
$y >= $game->rows
Truth table100%5/5
A or B or C or D
A= $x < 0B= $y < 0C= $x >= $game->colsD= $y >= $game->rows
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 84110 | ▶ | return $blocked->[ $y * $game->cols + $x ] ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 47301 | ▶ | my $region = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 47301 | my ($game, $blocked, $x0, $y0) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 47301 | my ($cols, $rows) = ($game->cols, $game->rows); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 47301 | ▶ | return (0, []) if $x0 < 0 || $y0 < 0 || $x0 >= $cols || $y0 >= $rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
$x0 < 0 or $y0 < 0 or $x0 >= $cols or $y0 >= $rows
$x0 < 0 or $y0 < 0 or $x0 >= $cols
$x0 < 0 or $y0 < 0
MC/DC50%2/4
$x0 < 0 or $y0 < 0 or $x0 >= $cols or $y0 >= $rows
$x0 < 0
$y0 < 0
$x0 >= $cols
$y0 >= $rows
Truth table60%3/5
A or B or C or D
A= $x0 < 0B= $y0 < 0C= $x0 >= $colsD= $y0 >= $rows
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 47299 | my $start = $y0 * $cols + $x0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 47299 | ▶ | return (0, []) if $blocked->[$start]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 47292 | my @seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 47292 | $seen[$start] = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 47292 | my @queue = ($start); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 47292 | my $at = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 47292 | ▶ | while ($at < @queue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6779720 | my $k = $queue[ $at++ ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 6779720 | my $x = $k % $cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 6779720 | my @next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 6779720 | ▶ | push @next, $k - 1 if $x > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 6779720 | ▶ | push @next, $k + 1 if $x < $cols - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 6779720 | ▶ | push @next, $k - $cols if $k >= $cols; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 6779720 | ▶ | push @next, $k + $cols if $k + $cols < $cols * $rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 6779720 | for my $n (@next) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 25043778 | ▶ | next if $seen[$n] || $blocked->[$n]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$seen[$n] or $blocked->[$n]
MC/DC100%2/2
$seen[$n] or $blocked->[$n]
$seen[$n]
$blocked->[$n]
Truth table100%3/3
A or B
A= $seen[$n]B= $blocked->[$n]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 6732428 | $seen[$n] = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6732428 | push @queue, $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 47292 | return (scalar @queue, \@seen); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 24292 | ▶ | my $closest = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 24292 | my ($head, $directions, $food) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 24292 | my (@best, $best); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 24292 | for my $direction (@$directions) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 62145 | my ($x, $y) = $ahead->($head, $direction); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 62145 | my $distance = abs($food->[0] - $x) + abs($food->[1] - $y); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 62145 | ▶ | if (!defined $best || $distance < $best) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not defined $best or $distance < $best
MC/DC100%2/2
not defined $best or $distance < $best
not defined $best
$distance < $best
Truth table100%3/3
not(A) or B
A= not defined $bestB= $distance < $best
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 27879 | $best = $distance; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 27879 | @best = ($direction); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | elsif ($distance == $best) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 11280 | push @best, $direction; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 24292 | return \@best; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 26378 | ▶ | my $tie = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 26378 | my ($game, $heading, $list) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 26378 | ▶ | return $heading unless @$list; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 26378 | ▶ | return $heading if grep { $_ eq $heading } @$list; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 5256 | ▶ | return $list->[0] if @$list == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 62 | my $hash = Digest::SHA::sha256($game->seed . 'snake:bot:' . $game->ticks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 62 | return $list->[ unpack('N', substr($hash, 0, 4)) % @$list ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 27 | ▶ | sub room { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
room: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 27 | my ($class, $game, $direction) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | Carp::croak("Game::Snake::Bot: '" . (defined $direction ? $direction : 'undef') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | . "' is not a direction") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 27 | ▶ | unless defined $direction && $DELTA{$direction}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $direction and $DELTA{$direction}
MC/DC50%1/2
defined $direction and $DELTA{$direction}
defined $direction
$DELTA{$direction}
Truth table66%2/3
A and B
A= defined $directionB= $DELTA{$direction}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 26 | my ($head) = @{ $game->segments }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 26 | my $blocked = $scan->($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 26 | my ($room) = $region->($game, $blocked, $ahead->($head, $direction)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 26 | return $room; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 3912 | ▶ | sub safe { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
safe: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3912 | my ($class, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 3912 | ▶ | return () if $game->status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 3911 | my ($head) = @{ $game->segments }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3911 | my $heading = $game->heading; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 3911 | my $blocked = $scan->($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 11733 | return grep { !$fatal->($game, $blocked, $ahead->($head, $_)) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 3911 | $heading, @{ $SIDES{$heading} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 26436 | ▶ | sub choose { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 26436 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 26436 | my $level = $class->_level($with{level}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 26435 | ▶ | return undef if $game->status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 26434 | my ($head) = @{ $game->segments }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 26434 | my $heading = $game->heading; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 26434 | my $blocked = $scan->($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 79302 | my @safe = grep { !$fatal->($game, $blocked, $ahead->($head, $_)) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 26434 | $heading, @{ $SIDES{$heading} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 26434 | ▶ | return $heading unless @safe; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 26378 | my $food = $game->food; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 26378 | ▶ | if ($level == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6611 | ▶ | return $tie->($game, $heading, \@safe) unless $food; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6611 | return $tie->($game, $heading, $closest->($head, \@safe, $food)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 19767 | my $length = $game->length; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 19767 | my (%room, $most); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 19767 | for my $direction (@safe) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 47275 | my ($count) = $region->($game, $blocked, $ahead->($head, $direction)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 47275 | $room{$direction} = $count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 47275 | ▶ | $most = $count if !defined $most || $count > $most; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $most or $count > $most
MC/DC100%2/2
not defined $most or $count > $most
not defined $most
$count > $most
Truth table100%3/3
not(A) or B
A= not defined $mostB= $count > $most
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 19767 | my @big = grep { $room{$_} >= $length } @safe; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 19767 | ▶ | @big = grep { $room{$_} == $most } @safe unless @big; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 19767 | ▶ | return $tie->($game, $heading, \@big) if @big == 1 || !$food; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@big == 1 or not $food
MC/DC50%1/2
@big == 1 or not $food
@big == 1
not $food
Truth table66%2/3
A or not(B)
A= @big == 1B= not $food
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 17681 | return $tie->($game, $heading, $closest->($head, \@big, $food)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||