| 1 | package Game::Sternhalma::Bot; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 26 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 26 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # THE RUNGS, weakest first. 0 is a random choice among moves that go forward; | ||||||||||||||||||||||||||||||||
| 12 | # 1 looks one ply ahead; 2 and 3 search, by a budget of positions looked at. | ||||||||||||||||||||||||||||||||
| 13 | # phase 07's ladder measured them (plan_game_sternhalma/07, Built). | ||||||||||||||||||||||||||||||||
| 14 | 26 | our %RUNG = ( | |||||||||||||||||||||||||||||||
| 15 | 0 => { random => 1 }, | ||||||||||||||||||||||||||||||||
| 16 | 1 => { budget => 1_000_000, depth => 1 }, | ||||||||||||||||||||||||||||||||
| 17 | 2 => { budget => 10_000 }, | ||||||||||||||||||||||||||||||||
| 18 | 3 => { budget => 100_000 }, | ||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub rungs { sort { $a <=> $b } keys %RUNG } | ||||||||||||||||||||||||||||||
|
Subroutine
rungs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 4171 | ▶ | sub choose { | ||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 24 | 4171 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||
| 25 | 4171 | ▶ | my $info = $class->think($game, %with) or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 4168 | ▶ | return $info->{pass} ? { kind => 'pass' } : { kind => 'move', from => $info->{from}, to => $info->{to} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 4213 | ▶ | sub think { | ||||||||||||||||||||||||||||||
|
Subroutine
think: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 30 | 4213 | my ($class, $game, %with) = @_; | |||||||||||||||||||||||||||||||
| 31 | 4213 | my ($board, @history); | |||||||||||||||||||||||||||||||
| 32 | 4213 | ▶ | if (ref $game && $game->can('board')) { $board = $game->board; @history = $game->history($game->to_move) } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $game and $game->can("board")
MC/DC100%2/2
ref $game and $game->can("board")
ref $game
$game->can("board")
Truth table100%3/3
A and B
A= ref $gameB= $game->can("board")
| |||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | elsif (ref $game && $game->can('search')) { $board = $game } | ||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $game and $game->can("search")
MC/DC50%1/2
ref $game and $game->can("search")
ref $game
$game->can("search")
Truth table66%2/3
A and B
A= ref $gameB= $game->can("search")
| |||||||||||||||||||||||||||||||||
| 34 | 1 | else { Carp::croak('Game::Sternhalma::Bot: this takes a Game::Sternhalma or a Game::Sternhalma::Engine') } | |||||||||||||||||||||||||||||||
| 35 | 4212 | ▶ | @history = @{ $with{history} } if $with{history}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | 4212 | ▶ | return undef if $board->over; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 4211 | ▶ | my $rung = defined $with{rung} ? $with{rung} : (rungs())[-1]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 4211 | ▶ | my $r = $RUNG{$rung} or Carp::croak("Game::Sternhalma::Bot: there is no rung $rung; there are " . join(', ', rungs())); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 4210 | ▶ | my $seed = defined $with{seed} ? $with{seed} : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 4210 | ▶ | Carp::croak('Game::Sternhalma::Bot: a seed is a whole number') unless $seed =~ /\A[0-9]{1,15}\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 4210 | $seed = ($seed * 1_000_003 + $board->ply) % 1_000_000_000_000; | |||||||||||||||||||||||||||||||
| 42 | 4210 | my $out; | |||||||||||||||||||||||||||||||
| 43 | 4210 | ▶ | if ($r->{random}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 1664 | $out = $board->random_forward($seed + 1); | |||||||||||||||||||||||||||||||
| 45 | 1664 | $out->{nodes} = '0'; | |||||||||||||||||||||||||||||||
| 46 | 1664 | $out->{depth} = 0; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||||||
| 49 | $out = $board->search(budget => $r->{budget}, depth => $r->{depth} // 0, seed => $seed + 1, | ||||||||||||||||||||||||||||||||
| 50 | 2546 | ▶ | avoid => \@history, ($with{weights} ? (weights => $with{weights}) : ())); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$r->{"depth"} // 0
MC/DC100%1/1
$r->{"depth"} // 0
$r->{"depth"}
Truth table100%2/2
A
A= $r->{"depth"}
| |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | 4210 | $out->{rung} = $rung; | |||||||||||||||||||||||||||||||
| 53 | 4210 | return $out; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 26 | 1; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||||||