blib/lib/Game/Brandubh/Bot.pm

filter: Statementstmt 100% 55 / 55 Branchbran 85.7% 24 / 28 Conditioncond 83.3% 15 / 18 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 92.3% 120 / 130 CC 21 SCAR 17.4
CC
21
Cov
93%
think
11.3
_level
6.0
slip_for
3.0
CRAP 5.7
« blib/lib/Game/Brandubh/Result.pm ↑ Summary blib/lib/Game/Brandubh/Rules.pm »
1 package Game::Brandubh::Bot;
2  
3 39 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 39 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 our $VERSION = '0.02';
11  
12 39 our @LADDER = (1_000, 10_000, 10_000, 100_000);
13  
14 39 our %SLIP = (
15 1_000 => 20,
16 10_000 => 0,
17 100_000 => 0,
18 );
19  
20 39 our $LEVEL;
21  
22 171 ▶sub levels {
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
23 171 my %seen;
24 171 return grep { !$seen{$_}++ } sort { $a <=> $b } @LADDER;
25 }
26  
27 2190 ▶sub _number {
Subroutine _number: called CC=2 SCAR=7.1
28 2190 my (@parts) = @_;
29 2190 ▶ return unpack 'N', Digest::SHA::sha256(join '|', map { defined $_ ? $_ : '' } @parts);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 7608 0 7608
30 }
31  
32 1115 ▶sub level_for {
Subroutine level_for: called CC=2 SCAR=6.9
Pod documented
33 1115 my ($class, $seed) = @_;
34 1115 ▶ return $LADDER[ int(@LADDER / 2) ] unless defined $seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed 1 1114 1115
35 1114 return $LADDER[ _number('level', $seed) % @LADDER ];
36 }
37  
38 1011 ▶sub slip_for {
Subroutine slip_for: called CC=3 SCAR=11.0
Pod documented
39 1011 my ($class, $level) = @_;
40 1011 ▶ return 0 unless defined $level && exists $SLIP{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and exists $Game::Brandubh::Bot::SLIP{$level} 2 1009 1011
Condition100%3/3
defined $level and exists $Game::Brandubh::Bot::SLIP{$level}
!ll&&!rl&&r
111009
MC/DC100%2/2
defined $level and exists $Game::Brandubh::Bot::SLIP{$level}
defined $level exists $Game::Brandubh::Bot::SLIP{$level}
Truth table100%3/3
A and B
A= defined $levelB= exists $Game::Brandubh::Bot::SLIP{$level}
ABresult
0X0
100
111
41 1009 return $SLIP{$level};
42 }
43  
44 1152 ▶sub _level {
Subroutine _level: called CC=6 SCAR=17.9
45 1152 my ($class, %with) = @_;
46 my $level = defined $with{level} ? $with{level}
47 : defined $LEVEL ? $LEVEL
48 1152 ▶ : $class->level_for($with{seed});
Branch: 2 branches
BranchTrueFalseTotal
defined $with{'level'} ? : 1041 111 1152
defined $Game::Brandubh::Bot::LEVEL ? : 1 110 111
49 1152 ▶ Carp::croak('Game::Brandubh::Bot: a level is a whole number of positions to search, from 1 up')
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $level and $level =~ /\A[0-9]{1,9}\z/ and $level >= 1 2 1150 1152
Condition83%5/6
not ref $level and $level =~ /\A[0-9]{1,9}\z/ and $level >= 1
!ll&&!rl&&r
111150
not ref $level and $level =~ /\A[0-9]{1,9}\z/
!ll&&!rl&&r
011151
MC/DC66%2/3
not ref $level and $level =~ /\A[0-9]{1,9}\z/ and $level >= 1
not ref $level $level =~ /\A[0-9]{1,9}\z/ $level >= 1
Truth table75%3/4
not(A) and B and C
A= not ref $levelB= $level =~ /\A[0-9]{1,9}\z/C= $level >= 1
ABCresult
0XX0
10X0
1100
1111
50 unless !ref $level && $level =~ /\A[0-9]{1,9}\z/ && $level >= 1;
51 1150 return $level;
52 }
53  
54 460 ▶sub choose {
Subroutine choose: called CC=2 SCAR=6.9
Pod documented
55 460 my ($class, $game, %with) = @_;
56 460 my $thought = $class->think($game, %with);
57 456 ▶ return $thought ? $thought->{move} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$thought ? : 454 2 456
58 }
59  
60 1157 ▶sub think {
Subroutine think: called CC=11 SCAR=24.2
Pod documented
61 1157 my ($class, $game, %with) = @_;
62 1157 ▶ Carp::croak('Game::Brandubh::Bot: choose takes a Game::Brandubh')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $game and $game->can("search") and $game->can("legal") 2 1155 1157
Condition66%4/6
ref $game and $game->can("search") and $game->can("legal")
!ll&&!rl&&r
201155
ref $game and $game->can("search")
!ll&&!rl&&r
201155
MC/DC33%1/3
ref $game and $game->can("search") and $game->can("legal")
ref $game $game->can("search") $game->can("legal")
Truth table50%2/4
A and B and C
A= ref $gameB= $game->can("search")C= $game->can("legal")
ABCresult
0XX0
10X0
1100
1111
63 unless ref $game && $game->can('search') && $game->can('legal');
64 1155 ▶ return undef unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 3 1152 1155
65  
66 1152 my $level = $class->_level(%with);
67 1150 my $seat = $game->turn;
68 1150 my $seed = $with{seed};
69  
70 1150 ▶ my $slip = defined $with{slip} ? $with{slip} : $class->slip_for($level);
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'slip'} ? : 144 1006 1150
71 1150 ▶ if ($slip && _number('slip', $seed, $seat, $game->ply, $game->signature) % 100 < $slip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slip and _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip) 222 928 1150
Condition100%3/3
$slip and _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
!ll&&!rl&&r
296632222
MC/DC100%2/2
$slip and _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
$slip _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
Truth table100%3/3
A and B
A= $slipB= _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
ABresult
0X0
100
111
72 222 my $legal = $game->legal;
73 222 my $move = $legal->[ _number('which', $seed, $seat, $game->ply, $game->signature) % @$legal ]{move};
74 222 return { move => $move, level => $level, slipped => 1, depth => 0, nodes => 0, score => undef };
75 }
76  
77 my $found = $game->search(
78 budget => $level,
79 salt => join('|', 'bot', (defined $seed ? $seed : ''), $seat),
80 928 ▶ (defined $with{weights} ? (weights => $with{weights}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $seed ? : 928 0 928
defined $with{'weights'} ? : 0 928 928
81 );
82 928 ▶ return undef unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 928 928
83 return {
84 move => $found->{move},
85 level => $level,
86 slipped => 0,
87 depth => $found->{depth},
88 nodes => $found->{nodes},
89 score => $found->{score},
90 928 };
91 }
92  
93 13 ▶sub hint {
Subroutine hint: called CC=1 SCAR=0.0
Pod documented
94 13 my ($class, $game, %with) = @_;
95 13 return $class->choose($game, %with, level => $LADDER[-1], slip => 0);
96 }
97  
98 39 1;
99  
100 __END__
« blib/lib/Game/Brandubh/Result.pm ↑ Summary blib/lib/Game/Brandubh/Rules.pm »