blib/lib/Game/Snake/Bot.pm

filter: Statementstmt 100% 131 / 131 Branchbran 94.6% 53 / 56 Conditioncond 85.7% 36 / 42 MC/DCmcdc 73.9% 17 / 23 Subroutinesub 100% 17 / 17 Podpod 100% 5 / 5 totaltotal 94.5% 259 / 274 CC 47 SCAR 20.0
CC
47
Cov
96%
__ANON__
14.0
choose
11.0
__ANON__
6.0
CRAP 7.4
« blib/lib/Game/Snake.pm ↑ Summary blib/lib/Game/Snake/Result.pm »
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
BranchTrueFalseTotal
unless defined $level 2 26486 26488
43 Carp::croak("Game::Snake::Bot: a level is one of @LADDER, not '$level'")
44 26488 ▶ unless !ref $level && $DESCRIBE{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $level and $DESCRIBE{$level} 5 26483 26488
Condition66%2/3
not ref $level and $DESCRIBE{$level}
!ll&&!rl&&r
0526483
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}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $i == $segments->$#* and $i > 0 and not $growing 28193 842300 870493
Condition83%5/6
$i == $segments->$#* and $i > 0 and not $growing
!ll&&!rl&&r
840122217828193
$i == $segments->$#* and $i > 0
!ll&&!rl&&r
840122030371
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $x < 0 or $y < 0 or $x >= $game->cols or $y >= $game->rows 6925 84110 91035
Condition100%9/9
$x < 0 or $y < 0 or $x >= $game->cols or $y >= $game->rows
l!l&&r!l&&!r
4737218884110
$x < 0 or $y < 0 or $x >= $game->cols
l!l&&r!l&&!r
3328140986298
$x < 0 or $y < 0
l!l&&r!l&&!r
1239208987707
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
ABCDresult
1XXX1
01XX1
001X1
00011
00000
71 84110 ▶ return $blocked->[ $y * $game->cols + $x ] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$blocked[$y * $game->cols + $x] ? : 10110 74000 84110
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
BranchTrueFalseTotal
if $x0 < 0 or $y0 < 0 or $x0 >= $cols or $y0 >= $rows 2 47299 47301
Condition77%7/9
$x0 < 0 or $y0 < 0 or $x0 >= $cols or $y0 >= $rows
l!l&&r!l&&!r
1147299
$x0 < 0 or $y0 < 0 or $x0 >= $cols
l!l&&r!l&&!r
1047300
$x0 < 0 or $y0 < 0
l!l&&r!l&&!r
0147300
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
ABCDresult
1XXX1
01XX1
001X1
00011
00000
78 47299 my $start = $y0 * $cols + $x0;
79 47299 ▶ return (0, []) if $blocked->[$start];
Branch: 1 branch
BranchTrueFalseTotal
if $blocked->[$start] 7 47292 47299
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
BranchTrueFalseTotal
if ($at < @queue) 6779720 47292 6827012
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
BranchTrueFalseTotal
if $x > 0 6328853 450867 6779720
89 6779720 ▶ push @next, $k + 1 if $x < $cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x < $cols - 1 6330618 449102 6779720
90 6779720 ▶ push @next, $k - $cols if $k >= $cols;
Branch: 1 branch
BranchTrueFalseTotal
if $k >= $cols 6190030 589690 6779720
91 6779720 ▶ push @next, $k + $cols if $k + $cols < $cols * $rows;
Branch: 1 branch
BranchTrueFalseTotal
if $k + $cols < $cols * $rows 6194277 585443 6779720
92 6779720 for my $n (@next) {
93 25043778 ▶ next if $seen[$n] || $blocked->[$n];
Branch: 1 branch
BranchTrueFalseTotal
if $seen[$n] or $blocked->[$n] 18311350 6732428 25043778
Condition100%3/3
$seen[$n] or $blocked->[$n]
l!l&&r!l&&!r
1671403815973126732428
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]
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (not defined $best or $distance < $best) { } 27879 34266 62145
elsif ($distance == $best) { } 11280 22986 34266
Condition100%3/3
not defined $best or $distance < $best
l!l&&r!l&&!r
24292358734266
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless @$list 0 26378 26378
121 26378 ▶ return $heading if grep { $_ eq $heading } @$list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $heading;} @$list 21122 5256 26378
122 5256 ▶ return $list->[0] if @$list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 1 5194 62 5256
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
BranchTrueFalseTotal
unless defined $direction and $DELTA{$direction} 1 26 27
defined $direction ? : 1 0 1
Condition66%2/3
defined $direction and $DELTA{$direction}
!ll&&!rl&&r
0126
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}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $game->status eq "finished" 1 3911 3912
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
BranchTrueFalseTotal
if $game->status eq "finished" 1 26434 26435
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
BranchTrueFalseTotal
unless @safe 56 26378 26434
161  
162 26378 my $food = $game->food;
163  
164 26378 ▶ if ($level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 6611 19767 26378
165 6611 ▶ return $tie->($game, $heading, \@safe) unless $food;
Branch: 1 branch
BranchTrueFalseTotal
unless $food 0 6611 6611
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
BranchTrueFalseTotal
if not defined $most or $count > $most 20038 27237 47275
Condition100%3/3
not defined $most or $count > $most
l!l&&r!l&&!r
1976727127237
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
ABresult
1X1
011
000
175 }
176 19767 my @big = grep { $room{$_} >= $length } @safe;
177 19767 ▶ @big = grep { $room{$_} == $most } @safe unless @big;
Branch: 1 branch
BranchTrueFalseTotal
unless @big 1601 18166 19767
178 19767 ▶ return $tie->($game, $heading, \@big) if @big == 1 || !$food;
Branch: 1 branch
BranchTrueFalseTotal
if @big == 1 or not $food 2086 17681 19767
Condition66%2/3
@big == 1 or not $food
l!l&&r!l&&!r
2086017681
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
ABresult
1X1
011
000
179 17681 return $tie->($game, $heading, $closest->($head, \@big, $food));
180 }
181  
182 8 1;
183  
184 __END__
« blib/lib/Game/Snake.pm ↑ Summary blib/lib/Game/Snake/Result.pm »