| 1 | package Game::Fanorona::Bot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 18 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 18 | our @LADDER = (0, 100, 1_000, 10_000, 30_000); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 344 | ▶ | my $number = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 344 | my (@parts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 344 | ▶ | my $bytes = join '|', map { defined $_ ? $_ : '' } @parts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 344 | ▶ | Carp::croak('Game::Fanorona::Bot: a seed is bytes, and that one holds a character above 255') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | unless utf8::downgrade($bytes, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 342 | return unpack 'N', Digest::SHA::sha256($bytes); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 33 | ▶ | sub levels { @LADDER } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
levels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 324 | ▶ | my $level_of = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 324 | my (%with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 324 | ▶ | my $level = defined $with{level} ? $with{level} : $LADDER[-1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 324 | ▶ | Carp::croak('Game::Fanorona::Bot: a level is a whole number of positions to look at, 0 for the player that only counts') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $level and $level =~ /\A[0-9]{1,9}\z/u
MC/DC50%1/2
not ref $level and $level =~ /\A[0-9]{1,9}\z/u
not ref $level
$level =~ /\A[0-9]{1,9}\z/u
Truth table66%2/3
not(A) and B
A= not ref $levelB= $level =~ /\A[0-9]{1,9}\z/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | unless !ref $level && $level =~ /\A[0-9]{1,9}\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 321 | return $level; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 338 | ▶ | my $check = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 338 | my ($game) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 338 | ▶ | Carp::croak('Game::Fanorona::Bot: this takes a Game::Fanorona') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
ref $game and $game->can("search") and $game->can("steps") and $game->can("step")
ref $game and $game->can("search") and $game->can("steps")
ref $game and $game->can("search")
MC/DC25%1/4
ref $game and $game->can("search") and $game->can("steps") and $game->can("step")
ref $game
$game->can("search")
$game->can("steps")
$game->can("step")
Truth table40%2/5
A and B and C and D
A= ref $gameB= $game->can("search")C= $game->can("steps")D= $game->can("step")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | unless ref $game && $game->can('search') && $game->can('steps') && $game->can('step'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 336 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 153 | ▶ | my $greedy = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 153 | my ($game, $seed) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 153 | my $copy = Game::Fanorona->new(variant => $game->variant, position => $game->position); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 153 | my (@made, $n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 153 | ▶ | if ($game->in_chain) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return { move => '', steps => [], level => 0, nodes => '0', depth => 0, score => undef }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 153 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 176 | ▶ | my @steps = $copy->steps or last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 176 | my ($most) = sort { $b <=> $a } map { scalar @{ $_->{takes} } } @steps; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 176 | my @best = grep { @{ $_->{takes} } == $most } @steps; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 176 | my $pick = $best[ $number->('greedy', $seed, $game->signature, $game->ply, $n++) % @best ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 175 | my $turn = $copy->turn; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 175 | ▶ | $copy->step($pick->{string}) or last; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 175 | push @made, $pick; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 175 | ▶ | last if $copy->over || !$copy->in_chain || $copy->turn ne $turn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$copy->over or not $copy->in_chain or $copy->turn ne $turn
$copy->over or not $copy->in_chain
MC/DC66%2/3
$copy->over or not $copy->in_chain or $copy->turn ne $turn
$copy->over
not $copy->in_chain
$copy->turn ne $turn
Truth table75%3/4
A or not(B) or C
A= $copy->overB= not $copy->in_chainC= $copy->turn ne $turn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 152 | ▶ | my $move = @made ? $made[0]{from} . join('', map { substr($_->{string}, 2) } @made) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 152 | return { move => $move, steps => \@made, level => 0, nodes => '0', depth => 0, score => undef }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 329 | ▶ | sub think { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
think: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 329 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 329 | $check->($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 327 | ▶ | return undef if $game->over; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 324 | my $level = $level_of->(%with); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 321 | my $seed = $with{seed}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 321 | ▶ | return $greedy->($game, $seed) if $level == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | my $found = $game->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | budget => $level, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | seed => 1 + $number->('search', $seed, $game->signature, $game->ply, $game->in_progress) % 4_294_967_295, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 168 | ▶ | (defined $with{depth} ? (depth => $with{depth}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 167 | $found->{level} = $level; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 167 | return $found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 99 | ▶ | sub choose { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 99 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 99 | ▶ | my $thought = $class->think($game, %with) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 96 | return $thought->{move}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 212 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 212 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 212 | ▶ | my $thought = $class->think($game, %with) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 211 | my $turn = $game->turn; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 211 | for my $step (@{ $thought->{steps} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | $game->step($step->{string}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 319 | ▶ | or Carp::croak('Game::Fanorona::Bot: the game refused ' . $step->{string} . ': ' . $game->error->message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 211 | ▶ | $game->stop if !$game->over && $game->in_chain && defined $game->turn && $game->turn eq $turn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
not $game->over and $game->in_chain and defined $game->turn and $game->turn eq $turn
not $game->over and $game->in_chain and defined $game->turn
not $game->over and $game->in_chain
MC/DC50%2/4
not $game->over and $game->in_chain and defined $game->turn and $game->turn eq $turn
not $game->over
$game->in_chain
defined $game->turn
$game->turn eq $turn
Truth table60%3/5
not(A) and B and C and D
A= not $game->overB= $game->in_chainC= defined $game->turnD= $game->turn eq $turn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 211 | return $thought; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | sub hint { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 8 | return $class->think($game, %with, level => $LADDER[-1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 9 | ▶ | sub answer_draw { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
answer_draw: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | $check->($game); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 9 | ▶ | return undef if $game->over; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | my $offered = $game->draw_offered_by // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$game->draw_offered_by // (return undef)
MC/DC100%1/1
$game->draw_offered_by // (return undef)
$game->draw_offered_by
Truth table100%2/2
A
A= $game->draw_offered_by
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | ▶ | my $me = $offered eq 'white' ? 'black' : 'white'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | ▶ | my $mine = $game->evaluate * ($me eq 'white' ? 1 : -1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | my $stuck = $game->quiet * 2 >= $game->quiet_limit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | ▶ | return ($mine < 0 || ($mine == 0 && $stuck)) ? 'accept' : 'decline'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$mine < 0 or $mine == 0 and $stuck
$mine == 0 and $stuck
MC/DC100%3/3
$mine < 0 or $mine == 0 and $stuck
$mine < 0
$mine == 0
$stuck
Truth table100%4/4
A or B and C
A= $mine < 0B= $mine == 0C= $stuck
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||