| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $level and exists $Game::Brandubh::Bot::SLIP{$level}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1152 | ▶ | Carp::croak('Game::Brandubh::Bot: a level is a whole number of positions to search, from 1 up') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not ref $level and $level =~ /\A[0-9]{1,9}\z/ and $level >= 1
not ref $level and $level =~ /\A[0-9]{1,9}\z/
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
ref $game and $game->can("search") and $game->can("legal")
ref $game and $game->can("search")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | unless ref $game && $game->can('search') && $game->can('legal'); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 1155 | ▶ | return undef unless $game->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1150 | ▶ | if ($slip && _number('slip', $seed, $seat, $game->ply, $game->signature) % 100 < $slip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$slip and _number("slip", $seed, $seat, $game->ply, $game->signature) % 100 < $slip
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 928 | ▶ | return undef unless $found; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||