blib/lib/Game/Brandubh/Bot.pm

filter: Statementstmt 34.5% 19 / 55 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 38.4% 5 / 13 Podpod 100% 6 / 6 totaltotal 23.0% 30 / 130 CC 21 SCAR 39.9
CC
21
Cov
19%
think
132.0
_level
42.0
slip_for
12.0
CRAP 54.1
« blib/lib/Game/Brandubh/Notation.pm ↑ Summary blib/arch/Game/Brandubh/Install/Files.pm »
1 package Game::Brandubh::Bot;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.03';
11  
12 1 our @LADDER = (1_000, 10_000, 10_000, 100_000);
13  
14 1 our %SLIP = (
15 1_000 => 20,
16 10_000 => 0,
17 100_000 => 0,
18 );
19  
20 1 our $LEVEL;
21  
22 0 ▶sub levels {
Subroutine levels: not called CC=1 SCAR=6.9
Pod documented
23 0 my %seen;
24 0 return grep { !$seen{$_}++ } sort { $a <=> $b } @LADDER;
25 }
26  
27 0 ▶sub _number {
Subroutine _number: not called CC=2 SCAR=17.9
28 0 my (@parts) = @_;
29 0 ▶ return unpack 'N', Digest::SHA::sha256(join '|', map { defined $_ ? $_ : '' } @parts);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
30 }
31  
32 0 ▶sub level_for {
Subroutine level_for: not called CC=2 SCAR=17.9
Pod documented
33 0 my ($class, $seed) = @_;
34 0 ▶ return $LADDER[ int(@LADDER / 2) ] unless defined $seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed 0 0 0
35 0 return $LADDER[ _number('level', $seed) % @LADDER ];
36 }
37  
38 0 ▶sub slip_for {
Subroutine slip_for: not called CC=3 SCAR=24.8
Pod documented
39 0 my ($class, $level) = @_;
40 0 ▶ return 0 unless defined $level && exists $SLIP{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and exists $Game::Brandubh::Bot::SLIP{$level} 0 0 0
Condition0%0/3
defined $level and exists $Game::Brandubh::Bot::SLIP{$level}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $level and exists $Game::Brandubh::Bot::SLIP{$level}
defined $level exists $Game::Brandubh::Bot::SLIP{$level}
Truth table0%0/3
A and B
A= defined $levelB= exists $Game::Brandubh::Bot::SLIP{$level}
ABresult
0X0
100
111
41 0 return $SLIP{$level};
42 }
43  
44 0 ▶sub _level {
Subroutine _level: not called CC=6 SCAR=37.4
45 0 my ($class, %with) = @_;
46 my $level = defined $with{level} ? $with{level}
47 : defined $LEVEL ? $LEVEL
48 0 ▶ : $class->level_for($with{seed});
Branch: 2 branches
BranchTrueFalseTotal
defined $with{'level'} ? : 0 0 0
defined $Game::Brandubh::Bot::LEVEL ? : 0 0 0
49 0 ▶ 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 0 0 0
Condition0%0/6
not ref $level and $level =~ /\A[0-9]{1,9}\z/ and $level >= 1
!ll&&!rl&&r
000
not ref $level and $level =~ /\A[0-9]{1,9}\z/
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 return $level;
52 }
53  
54 0 ▶sub choose {
Subroutine choose: not called CC=2 SCAR=17.9
Pod documented
55 0 my ($class, $game, %with) = @_;
56 0 my $thought = $class->think($game, %with);
57 0 ▶ return $thought ? $thought->{move} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$thought ? : 0 0 0
58 }
59  
60 0 ▶sub think {
Subroutine think: not called CC=11 SCAR=48.8
Pod documented
61 0 my ($class, $game, %with) = @_;
62 0 ▶ 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") 0 0 0
Condition0%0/6
ref $game and $game->can("search") and $game->can("legal")
!ll&&!rl&&r
000
ref $game and $game->can("search")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $game and $game->can("search") and $game->can("legal")
ref $game $game->can("search") $game->can("legal")
Truth table0%0/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 0 ▶ return undef unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 0 0 0
65  
66 0 my $level = $class->_level(%with);
67 0 my $seat = $game->turn;
68 0 my $seed = $with{seed};
69  
70 0 ▶ my $slip = defined $with{slip} ? $with{slip} : $class->slip_for($level);
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'slip'} ? : 0 0 0
71 0 ▶ 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) 0 0 0
Condition0%0/3
$slip and _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/3
A and B
A= $slipB= _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
ABresult
0X0
100
111
72 0 my $legal = $game->legal;
73 0 my $move = $legal->[ _number('which', $seed, $seat, $game->ply, $game->signature) % @$legal ]{move};
74 0 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 0 ▶ (defined $with{weights} ? (weights => $with{weights}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $seed ? : 0 0 0
defined $with{'weights'} ? : 0 0 0
81 );
82 0 ▶ return undef unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
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 0 };
91 }
92  
93 0 ▶sub hint {
Subroutine hint: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($class, $game, %with) = @_;
95 0 return $class->choose($game, %with, level => $LADDER[-1], slip => 0);
96 }
97  
98 1 1;
99  
100 __END__
« blib/lib/Game/Brandubh/Notation.pm ↑ Summary blib/arch/Game/Brandubh/Install/Files.pm »