blib/lib/Game/RoyalUr/Bot.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Game/RoyalUr/Move.pm ↑ Summary blib/lib/Game/RoyalUr/Error.pm »
1 package Game::RoyalUr::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 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Game::RoyalUr::Engine ();
11 use Game::RoyalUr::Variant;
12  
13 our $VERSION = '0.03';
14  
15 my $E = 'Game::RoyalUr::Engine';
16  
17 my (%LADDER, %FINKEL, %MASTERS);
18 BEGIN {
19 %FINKEL = (exposed => 16, rosette => 16, entry => 8);
20 %MASTERS = (exposed => 16);
21 %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 has level => (is => 'ro');
38  
39 has budget => (is => 'ro', default => 1_000_000);
40  
41 sub BUILD {
42 my ($self) = @_;
43 my $level = $self->level;
44 Carp::croak('Game::RoyalUr::Bot: a level is a whole number from 1 up')
45 if defined $level && $level !~ /\A[1-9]\d?\z/;
46 my $budget = $self->budget;
47 Carp::croak('Game::RoyalUr::Bot: a budget is a whole number of positions from 1 to 2000000000')
48 unless defined $budget && $budget =~ /\A[1-9]\d{0,9}\z/ && $budget <= 2_000_000_000;
49 return;
50 }
51  
52 my $rungs = sub {
53 my ($rules) = @_;
54 return $LADDER{ Game::RoyalUr::Variant->of($rules)->route };
55 };
56  
57 sub levels {
58 my ($invocant, $rules) = @_;
59 my $top = scalar @{ $rungs->($rules) };
60 return 1 .. $top;
61 }
62  
63 sub rung {
64 my ($invocant, $level, $rules) = @_;
65 my $ladder = $rungs->($rules);
66 Carp::croak("Game::RoyalUr::Bot: there is no level '" . ($level // 'undef') . "'; the levels are 1 to " . @$ladder)
67 unless defined $level && $level =~ /\A\d+\z/ && $level >= 1 && $level <= @$ladder;
68 my $rung = $ladder->[ $level - 1 ];
69 return { %$rung, ($rung->{weights} ? (weights => { %{ $rung->{weights} } }) : ()) };
70 }
71  
72 sub level_for {
73 my ($self, $rules) = @_;
74 my $top = scalar @{ $rungs->($rules) };
75 my $level = $self->level;
76 return $top unless defined $level;
77 return $level > $top ? $top : $level;
78 }
79  
80 sub think {
81 my ($self, $game) = @_;
82 my @legal = $game->legal;
83 return undef unless @legal;
84  
85 my $variant = $game->variant;
86 my $rules = $variant->as_hash;
87 my $level = $self->level_for($variant);
88 my $rung = $rungs->($variant)->[ $level - 1 ];
89  
90 my ($board, $code) = $E->of_string($game->position);
91 Carp::croak("Game::RoyalUr::Bot: the game's position was refused, code $code") unless $board;
92  
93 my $found;
94 if ($rung->{greedy}) {
95 $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 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 unless $found->{index} >= 0 && $found->{index} < @legal;
103 return { %$found, level => $level, move => $legal[ $found->{index} ] };
104 }
105  
106 sub choose {
107 my ($self, $game) = @_;
108 my $thought = $self->think($game);
109 return $thought ? $thought->{move} : undef;
110 }
111  
112 1;
113  
114 __END__
« blib/lib/Game/RoyalUr/Move.pm ↑ Summary blib/lib/Game/RoyalUr/Error.pm »