blib/lib/Game/RoyalUr/Bot.pm

filter: Statementstmt 100% 72 / 72 Branchbran 90.9% 20 / 22 Conditioncond 69.5% 16 / 23 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 15 / 15 Podpod 83.3% 5 / 6 totaltotal 89.3% 134 / 150 CC 20 SCAR 14.7
CC
20
Cov
92%
rung
7.3
think
6.1
BUILD
6.0
CRAP 4.3
« blib/lib/Game/RoyalUr/Dice.pm ↑ Summary blib/lib/Game/RoyalUr/Variant.pm »
1 package Game::RoyalUr::Bot;
2  
3 52 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 52 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 52 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 52 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 52 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 52 ▶use Game::RoyalUr::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
11 52 ▶use Game::RoyalUr::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 52 our $VERSION = '0.02';
14  
15 52 my $E = 'Game::RoyalUr::Engine';
16  
17 52 my (%LADDER, %FINKEL, %MASTERS);
18 52 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 52 %FINKEL = (exposed => 16, rosette => 16, entry => 8);
20 52 %MASTERS = (exposed => 16);
21 52 %LADDER = (
22 short => [
23 { greedy => 1 },
24 { depth => 2, weights => { %FINKEL } },
25 { depth => 3, weights => { %FINKEL } },
26 { depth => 4, weights => { %FINKEL } },
27 ],
28 long => [
29 { greedy => 1 },
30 { depth => 2, weights => { %MASTERS } },
31 { depth => 3, weights => { %MASTERS } },
32 { depth => 4, weights => { %MASTERS } },
33 ],
34 );
35 }
36  
37 52 has level => (is => 'ro');
38  
39 52 has budget => (is => 'ro', default => 1_000_000);
40  
41 692 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
42 692 my ($self) = @_;
43 692 my $level = $self->level;
44 692 ▶ Carp::croak('Game::RoyalUr::Bot: a level is a whole number from 1 up')
Branch: 1 branch
BranchTrueFalseTotal
if defined $level and not $level =~ /\A[1-9]\d?\z/ 4 688 692
Condition100%3/3
defined $level and not $level =~ /\A[1-9]\d?\z/
!ll&&!rl&&r
106784
MC/DC100%2/2
defined $level and not $level =~ /\A[1-9]\d?\z/
defined $level not $level =~ /\A[1-9]\d?\z/
Truth table100%3/3
A and not(B)
A= defined $levelB= not $level =~ /\A[1-9]\d?\z/
ABresult
0X0
100
111
45 if defined $level && $level !~ /\A[1-9]\d?\z/;
46 688 my $budget = $self->budget;
47 688 ▶ Carp::croak('Game::RoyalUr::Bot: a budget is a whole number of positions from 1 to 2000000000')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget and $budget =~ /\A[1-9]\d{0,9}\z/ and $budget <= 2000000000 4 684 688
Condition83%5/6
defined $budget and $budget =~ /\A[1-9]\d{0,9}\z/ and $budget <= 2000000000
!ll&&!rl&&r
31684
defined $budget and $budget =~ /\A[1-9]\d{0,9}\z/
!ll&&!rl&&r
03685
MC/DC66%2/3
defined $budget and $budget =~ /\A[1-9]\d{0,9}\z/ and $budget <= 2000000000
defined $budget $budget =~ /\A[1-9]\d{0,9}\z/ $budget <= 2000000000
Truth table75%3/4
A and B and C
A= defined $budgetB= $budget =~ /\A[1-9]\d{0,9}\z/C= $budget <= 2000000000
ABCresult
0XX0
10X0
1100
1111
48 unless defined $budget && $budget =~ /\A[1-9]\d{0,9}\z/ && $budget <= 2_000_000_000;
49 684 return;
50 }
51  
52 11635 ▶my $rungs = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 11635 my ($rules) = @_;
54 11635 return $LADDER{ Game::RoyalUr::Variant->of($rules)->route };
55 52 };
56  
57 138 ▶sub levels {
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
58 138 my ($invocant, $rules) = @_;
59 138 my $top = scalar @{ $rungs->($rules) };
60 138 return 1 .. $top;
61 }
62  
63 16 ▶sub rung {
Subroutine rung: called CC=7 SCAR=19.9
Pod documented
64 16 my ($invocant, $level, $rules) = @_;
65 16 my $ladder = $rungs->($rules);
66 16 ▶ Carp::croak("Game::RoyalUr::Bot: there is no level '" . ($level // 'undef') . "'; the levels are 1 to " . @$ladder)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and $level =~ /\A\d+\z/ and $level >= 1 and $level <= @$ladder 4 12 16
Condition63%7/11
defined $level and $level =~ /\A\d+\z/ and $level >= 1 and $level <= @$ladder
!ll&&!rl&&r
2212
defined $level and $level =~ /\A\d+\z/ and $level >= 1
!ll&&!rl&&r
0214
defined $level and $level =~ /\A\d+\z/
!ll&&!rl&&r
0016
$level // "undef"
l!l
40
MC/DC50%2/4
defined $level and $level =~ /\A\d+\z/ and $level >= 1 and $level <= @$ladder
defined $level $level =~ /\A\d+\z/ $level >= 1 $level <= @$ladder
MC/DC0%0/1
$level // "undef"
$level
Truth table60%3/5
A and B and C and D
A= defined $levelB= $level =~ /\A\d+\z/C= $level >= 1D= $level <= @$ladder
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table50%1/2
A
A= $level
Aresult
11
00
67 unless defined $level && $level =~ /\A\d+\z/ && $level >= 1 && $level <= @$ladder;
68 12 my $rung = $ladder->[ $level - 1 ];
69 12 ▶ return { %$rung, ($rung->{weights} ? (weights => { %{ $rung->{weights} } }) : ()) };
Branch: 1 branch
BranchTrueFalseTotal
$rung->{'weights'} ? : 10 2 12
70 }
71  
72 5742 ▶sub level_for {
Subroutine level_for: called CC=3 SCAR=11.0
Pod documented
73 5742 my ($self, $rules) = @_;
74 5742 my $top = scalar @{ $rungs->($rules) };
75 5742 my $level = $self->level;
76 5742 ▶ return $top unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 2 5740 5742
77 5740 ▶ return $level > $top ? $top : $level;
Branch: 1 branch
BranchTrueFalseTotal
$level > $top ? : 1 5739 5740
78 }
79  
80 5741 ▶sub think {
Subroutine think: called CC=6 SCAR=18.1
Pod documented
81 5741 my ($self, $game) = @_;
82 5741 my @legal = $game->legal;
83 5741 ▶ return undef unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 2 5739 5741
84  
85 5739 my $variant = $game->variant;
86 5739 my $rules = $variant->as_hash;
87 5739 my $level = $self->level_for($variant);
88 5739 my $rung = $rungs->($variant)->[ $level - 1 ];
89  
90 5739 my ($board, $code) = $E->of_string($game->position);
91 5739 ▶ Carp::croak("Game::RoyalUr::Bot: the game's position was refused, code $code") unless $board;
Branch: 1 branch
BranchTrueFalseTotal
unless $board 0 5739 5739
92  
93 5739 my $found;
94 5739 ▶ if ($rung->{greedy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rung->{'greedy'}) { } 1763 3976 5739
95 1763 $found = { index => $board->greedy($game->roll, $rules), depth => 0, value => undef, nodes => '0', stopped => 0 };
96 }
97 else {
98 $found = $board->search($game->roll,
99 3976 rules => $rules, depth => $rung->{depth}, budget => $self->budget, weights => $rung->{weights});
100 }
101 Carp::croak('Game::RoyalUr::Bot: the search found no move where the game has one')
102 5739 ▶ unless $found->{index} >= 0 && $found->{index} < @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless $found->{"index"} >= 0 and $found->{"index"} < @legal 0 5739 5739
Condition33%1/3
$found->{"index"} >= 0 and $found->{"index"} < @legal
!ll&&!rl&&r
005739
MC/DC0%0/2
$found->{"index"} >= 0 and $found->{"index"} < @legal
$found->{"index"} >= 0 $found->{"index"} < @legal
Truth table33%1/3
A and B
A= $found->{"index"} >= 0B= $found->{"index"} < @legal
ABresult
0X0
100
111
103 5739 return { %$found, level => $level, move => $legal[ $found->{index} ] };
104 }
105  
106 5727 ▶sub choose {
Subroutine choose: called CC=2 SCAR=6.9
Pod documented
107 5727 my ($self, $game) = @_;
108 5727 my $thought = $self->think($game);
109 5727 ▶ return $thought ? $thought->{move} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$thought ? : 5726 1 5727
110 }
111  
112 52 1;
113  
114 __END__
« blib/lib/Game/RoyalUr/Dice.pm ↑ Summary blib/lib/Game/RoyalUr/Variant.pm »