blib/lib/Game/Fanorona/Bot.pm

filter: Statementstmt 98.8% 85 / 86 Branchbran 86.3% 38 / 44 Conditioncond 80.0% 28 / 35 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 89.6% 182 / 203 CC 36 SCAR 17.3
CC
36
Cov
92%
__ANON__
8.3
play
8.1
answer_draw
8.0
CRAP 5.6
« blib/lib/Game/Fanorona/Variant.pm ↑ Summary blib/lib/Game/Fanorona/Result.pm »
1 package Game::Fanorona::Bot;
2  
3 18 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 our $VERSION = '0.02';
11  
12 18 our @LADDER = (0, 100, 1_000, 10_000, 30_000);
13  
14 344 ▶my $number = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
15 344 my (@parts) = @_;
16 344 ▶ my $bytes = join '|', map { defined $_ ? $_ : '' } @parts;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1514 206 1720
17 344 ▶ Carp::croak('Game::Fanorona::Bot: a seed is bytes, and that one holds a character above 255')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 2 342 344
18 unless utf8::downgrade($bytes, 1);
19 342 return unpack 'N', Digest::SHA::sha256($bytes);
20 18 };
21  
22 33 ▶sub levels { @LADDER }
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
23  
24 324 ▶my $level_of = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
25 324 my (%with) = @_;
26 324 ▶ my $level = defined $with{level} ? $with{level} : $LADDER[-1];
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'level'} ? : 323 1 324
27 324 ▶ Carp::croak('Game::Fanorona::Bot: a level is a whole number of positions to look at, 0 for the player that only counts')
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $level and $level =~ /\A[0-9]{1,9}\z/u 3 321 324
Condition66%2/3
not ref $level and $level =~ /\A[0-9]{1,9}\z/u
!ll&&!rl&&r
03321
MC/DC50%1/2
not ref $level and $level =~ /\A[0-9]{1,9}\z/u
not ref $level $level =~ /\A[0-9]{1,9}\z/u
Truth table66%2/3
not(A) and B
A= not ref $levelB= $level =~ /\A[0-9]{1,9}\z/u
ABresult
0X0
100
111
28 unless !ref $level && $level =~ /\A[0-9]{1,9}\z/;
29 321 return $level;
30 18 };
31  
32 338 ▶my $check = sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
33 338 my ($game) = @_;
34 338 ▶ Carp::croak('Game::Fanorona::Bot: this takes a Game::Fanorona')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $game and $game->can("search") and $game->can("steps") and $game->can("step") 2 336 338
Condition66%6/9
ref $game and $game->can("search") and $game->can("steps") and $game->can("step")
!ll&&!rl&&r
20336
ref $game and $game->can("search") and $game->can("steps")
!ll&&!rl&&r
20336
ref $game and $game->can("search")
!ll&&!rl&&r
20336
MC/DC25%1/4
ref $game and $game->can("search") and $game->can("steps") and $game->can("step")
ref $game $game->can("search") $game->can("steps") $game->can("step")
Truth table40%2/5
A and B and C and D
A= ref $gameB= $game->can("search")C= $game->can("steps")D= $game->can("step")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
35 unless ref $game && $game->can('search') && $game->can('steps') && $game->can('step');
36 336 return;
37 18 };
38  
39 153 ▶my $greedy = sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
40 153 my ($game, $seed) = @_;
41 153 my $copy = Game::Fanorona->new(variant => $game->variant, position => $game->position);
42 153 my (@made, $n);
43 153 ▶ if ($game->in_chain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->in_chain) 0 153 153
44 0 return { move => '', steps => [], level => 0, nodes => '0', depth => 0, score => undef };
45 }
46 153 while (1) {
47 176 ▶ my @steps = $copy->steps or last;
Branch: 1 branch
BranchTrueFalseTotal
my(@steps) = $copy->steps or last 176 0 176
48 176 my ($most) = sort { $b <=> $a } map { scalar @{ $_->{takes} } } @steps;
49 176 my @best = grep { @{ $_->{takes} } == $most } @steps;
50 176 my $pick = $best[ $number->('greedy', $seed, $game->signature, $game->ply, $n++) % @best ];
51 175 my $turn = $copy->turn;
52 175 ▶ $copy->step($pick->{string}) or last;
Branch: 1 branch
BranchTrueFalseTotal
$copy->step($pick->{"string"}) or last 175 0 175
53 175 push @made, $pick;
54 175 ▶ last if $copy->over || !$copy->in_chain || $copy->turn ne $turn;
Branch: 1 branch
BranchTrueFalseTotal
if $copy->over or not $copy->in_chain or $copy->turn ne $turn 152 23 175
Condition83%5/6
$copy->over or not $copy->in_chain or $copy->turn ne $turn
l!l&&r!l&&!r
152023
$copy->over or not $copy->in_chain
l!l&&r!l&&!r
115123
MC/DC66%2/3
$copy->over or not $copy->in_chain or $copy->turn ne $turn
$copy->over not $copy->in_chain $copy->turn ne $turn
Truth table75%3/4
A or not(B) or C
A= $copy->overB= not $copy->in_chainC= $copy->turn ne $turn
ABCresult
1XX1
01X1
0011
0000
55 }
56 152 ▶ my $move = @made ? $made[0]{from} . join('', map { substr($_->{string}, 2) } @made) : '';
Branch: 1 branch
BranchTrueFalseTotal
@made ? : 152 0 152
57 152 return { move => $move, steps => \@made, level => 0, nodes => '0', depth => 0, score => undef };
58 18 };
59  
60 329 ▶sub think {
Subroutine think: called CC=5 SCAR=16.1
Pod documented
61 329 my ($class, $game, %with) = @_;
62 329 $check->($game);
63 327 ▶ return undef if $game->over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->over 3 324 327
64 324 my $level = $level_of->(%with);
65 321 my $seed = $with{seed};
66 321 ▶ return $greedy->($game, $seed) if $level == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 0 153 168 321
67 my $found = $game->search(
68 budget => $level,
69 seed => 1 + $number->('search', $seed, $game->signature, $game->ply, $game->in_progress) % 4_294_967_295,
70 168 ▶ (defined $with{depth} ? (depth => $with{depth}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
my $found = $game->search("budget", $level, "seed", 1 + &$number("search", $seed, $game->signature, $game->ply, $game->in_progress) % 4294967295, defined $with{"depth"} ? ("depth", $with{"depth"}) : ()) or return undef 167 0 167
defined $with{'depth'} ? : 2 165 167
71 ) or return undef;
72 167 $found->{level} = $level;
73 167 return $found;
74 }
75  
76 99 ▶sub choose {
Subroutine choose: called CC=2 SCAR=6.9
Pod documented
77 99 my ($class, $game, %with) = @_;
78 99 ▶ my $thought = $class->think($game, %with) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $thought = $class->think($game, %with) or return undef 96 1 97
79 96 return $thought->{move};
80 }
81  
82 212 ▶sub play {
Subroutine play: called CC=8 SCAR=21.0
Pod documented
83 212 my ($class, $game, %with) = @_;
84 212 ▶ my $thought = $class->think($game, %with) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $thought = $class->think($game, %with) or return undef 211 1 212
85 211 my $turn = $game->turn;
86 211 for my $step (@{ $thought->{steps} }) {
87 $game->step($step->{string})
88 319 ▶ or Carp::croak('Game::Fanorona::Bot: the game refused ' . $step->{string} . ': ' . $game->error->message);
Branch: 1 branch
BranchTrueFalseTotal
$game->step($step->{"string"}) or Carp::croak('Game::Fanorona::Bot: the game refused ' . $step->{"string"} . ': ' . $game->error->message) 319 0 319
89 }
90 211 ▶ $game->stop if !$game->over && $game->in_chain && defined $game->turn && $game->turn eq $turn;
Branch: 1 branch
BranchTrueFalseTotal
if not $game->over and $game->in_chain and defined $game->turn and $game->turn eq $turn 17 194 211
Condition77%7/9
not $game->over and $game->in_chain and defined $game->turn and $game->turn eq $turn
!ll&&!rl&&r
194017
not $game->over and $game->in_chain and defined $game->turn
!ll&&!rl&&r
194017
not $game->over and $game->in_chain
!ll&&!rl&&r
1218217
MC/DC50%2/4
not $game->over and $game->in_chain and defined $game->turn and $game->turn eq $turn
not $game->over $game->in_chain defined $game->turn $game->turn eq $turn
Truth table60%3/5
not(A) and B and C and D
A= not $game->overB= $game->in_chainC= defined $game->turnD= $game->turn eq $turn
ABCDresult
0XXX0
10XX0
110X0
11100
11111
91 211 return $thought;
92 }
93  
94 8 ▶sub hint {
Subroutine hint: called CC=1 SCAR=0.0
Pod documented
95 8 my ($class, $game, %with) = @_;
96 8 return $class->think($game, %with, level => $LADDER[-1]);
97 }
98  
99 9 ▶sub answer_draw {
Subroutine answer_draw: called CC=8 SCAR=20.8
Pod documented
100 9 my ($class, $game, %with) = @_;
101 9 $check->($game);
102 9 ▶ return undef if $game->over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->over 1 8 9
103 8 ▶ my $offered = $game->draw_offered_by // return undef;
Condition100%2/2
$game->draw_offered_by // (return undef)
l!l
71
MC/DC100%1/1
$game->draw_offered_by // (return undef)
$game->draw_offered_by
Truth table100%2/2
A
A= $game->draw_offered_by
Aresult
11
00
104 7 ▶ my $me = $offered eq 'white' ? 'black' : 'white';
Branch: 1 branch
BranchTrueFalseTotal
$offered eq 'white' ? : 5 2 7
105 7 ▶ my $mine = $game->evaluate * ($me eq 'white' ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$me eq 'white' ? : 2 5 7
106 7 my $stuck = $game->quiet * 2 >= $game->quiet_limit;
107 7 ▶ return ($mine < 0 || ($mine == 0 && $stuck)) ? 'accept' : 'decline';
Branch: 1 branch
BranchTrueFalseTotal
$mine < 0 || $mine == 0 && $stuck ? : 4 3 7
Condition100%6/6
$mine < 0 or $mine == 0 and $stuck
l!l&&r!l&&!r
313
$mine == 0 and $stuck
!ll&&!rl&&r
121
MC/DC100%3/3
$mine < 0 or $mine == 0 and $stuck
$mine < 0 $mine == 0 $stuck
Truth table100%4/4
A or B and C
A= $mine < 0B= $mine == 0C= $stuck
ABCresult
1XX1
0111
00X0
0100
108 }
109  
110 18 1;
111  
112 __END__
« blib/lib/Game/Fanorona/Variant.pm ↑ Summary blib/lib/Game/Fanorona/Result.pm »