| 1 | package Game::Senet::Bot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 55 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 55 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 55 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 55 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use Object::Proto::Sugar; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 55 | ▶ | use Game::Senet::Engine (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 55 | ▶ | use Game::Senet::Variant; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 55 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 55 | my %LADDER; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 55 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 55 | %LADDER = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | kendall => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | { name => 'greedy' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | { name => 'depth 1', depth => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | { name => 'depth 2', depth => 2 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | { name => 'depth 3', depth => 3 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | skyruk => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | { name => 'greedy' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | { name => 'depth 2', depth => 2, weights => { blockade => 64 } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | { name => 'depth 3', depth => 3, weights => { blockade => 64 } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 55 | has _level => (is => 'ro', init_arg => 'level', private => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 55 | has _budget => (is => 'ro', init_arg => 'budget', private => 1, default => 2_000_000); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1063 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1063 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1063 | my $level = $self->_level; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1063 | ▶ | Carp::croak('Game::Senet::Bot: a level is a whole number from 1') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $level and not ref $level and $level =~ /\A[1-9][0-9]*\z/
defined $level and not ref $level
MC/DC33%1/3
defined $level and not ref $level and $level =~ /\A[1-9][0-9]*\z/
defined $level
not ref $level
$level =~ /\A[1-9][0-9]*\z/
Truth table50%2/4
A and not(B) and C
A= defined $levelB= not ref $levelC= $level =~ /\A[1-9][0-9]*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | unless defined $level && !ref $level && $level =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1061 | my $budget = $self->_budget; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1061 | ▶ | Carp::croak('Game::Senet::Bot: a budget is a whole number of nodes from 1 to 2000000000') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $budget and $budget =~ /\A[1-9][0-9]{0,9}\z/ and $budget <= 2000000000
defined $budget and $budget =~ /\A[1-9][0-9]{0,9}\z/
MC/DC33%1/3
defined $budget and $budget =~ /\A[1-9][0-9]{0,9}\z/ and $budget <= 2000000000
defined $budget
$budget =~ /\A[1-9][0-9]{0,9}\z/
$budget <= 2000000000
Truth table50%2/4
A and B and C
A= defined $budgetB= $budget =~ /\A[1-9][0-9]{0,9}\z/C= $budget <= 2000000000
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | unless defined $budget && $budget =~ /\A[1-9][0-9]{0,9}\z/ && $budget <= 2_000_000_000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1060 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 82843 | ▶ | my $ladder_of = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 82843 | my ($variant) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 82843 | my $v = Game::Senet::Variant->of($variant); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 82843 | return $LADDER{ $v->exits }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 55 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 903 | ▶ | sub levels { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
levels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 903 | my ($class, $variant) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 903 | my $count = scalar @{ $ladder_of->($variant) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 903 | return 1 .. $count; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 81940 | ▶ | sub rung { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rung: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 81940 | my ($class, $level, $variant) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 81940 | my $ladder = $ladder_of->($variant); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 81940 | ▶ | Carp::croak('Game::Senet::Bot: there is no level ' . ($level // 'undef') . ' under these rules; the levels are 1 to ' | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%4/8
defined $level and $level =~ /\A[1-9][0-9]*\z/ and $level <= @$ladder
defined $level and $level =~ /\A[1-9][0-9]*\z/
$level // "undef"
MC/DC33%1/3
defined $level and $level =~ /\A[1-9][0-9]*\z/ and $level <= @$ladder
defined $level
$level =~ /\A[1-9][0-9]*\z/
$level <= @$ladder
MC/DC0%0/1
$level // "undef"
$level
Truth table50%2/4
A and B and C
A= defined $levelB= $level =~ /\A[1-9][0-9]*\z/C= $level <= @$ladder
Truth table50%1/2
A
A= $level
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | . scalar @$ladder) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | unless defined $level && $level =~ /\A[1-9][0-9]*\z/ && $level <= @$ladder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 81936 | return { %{ $ladder->[ $level - 1 ] } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub level { $_[0]->_level } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
level: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub budget { $_[0]->_budget } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
budget: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 81923 | ▶ | sub think { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
think: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 81923 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 81923 | ▶ | Carp::croak('Game::Senet::Bot: think is asked of a Game::Senet') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($game) and $game->isa("Game::Senet")
MC/DC0%0/2
builtin::blessed($game) and $game->isa("Game::Senet")
builtin::blessed($game)
$game->isa("Game::Senet")
Truth table33%1/3
A and B
A= builtin::blessed($game)B= $game->isa("Game::Senet")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | unless Scalar::Util::blessed($game) && $game->isa('Game::Senet'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 81923 | ▶ | return undef if $game->is_over; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 81923 | my @legal = $game->legal; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 81923 | ▶ | return undef unless @legal; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 81923 | my $variant = $game->variant; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 81923 | my $rung = $self->rung($self->_level, $variant); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 81921 | my $board = Game::Senet::Engine->new(position => $game->position); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 81921 | ▶ | my $only = $game->turn->{bound} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$game->turn->{"bound"} // 0
MC/DC100%1/1
$game->turn->{"bound"} // 0
$game->turn->{"bound"}
Truth table100%2/2
A
A= $game->turn->{"bound"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 81921 | my $rules = $variant->as_hash; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 81921 | ▶ | unless ($rung->{depth}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 30729 | my $index = $board->greedy($game->roll, $rules, $only); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 30729 | return { move => $legal[$index], depth => 0, value => undef, nodes => 0, stopped => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | my $found = $board->search($game->roll, rules => $rules, weights => $rung->{weights}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 51192 | budget => $self->_budget, depth => $rung->{depth}, only => $only); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | return { move => $legal[ $found->{index} ], depth => $found->{depth}, value => $found->{value}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 51192 | nodes => $found->{nodes}, stopped => $found->{stopped} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 81922 | ▶ | sub choose { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 81922 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 81922 | my $thought = $self->think($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 81920 | ▶ | return $thought ? $thought->{move} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 55 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||