blib/lib/Game/Sternhalma/Bot.pm

filter: Statementstmt 100% 42 / 42 Branchbran 79.1% 19 / 24 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 92.1% 82 / 89 CC 16 SCAR 22.7
CC
16
Cov
92%
think
14.2
choose
3.0
BEGIN
1.0
CRAP 9.7
« blib/lib/Game/Sternhalma/Notation.pm ↑ Summary blib/lib/Game/Sternhalma.pm »
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
BranchTrueFalseTotal
my $info = $class->think($game, %with) or return undef 4168 1 4169
26 4168 ▶ return $info->{pass} ? { kind => 'pass' } : { kind => 'move', from => $info->{from}, to => $info->{to} };
Branch: 1 branch
BranchTrueFalseTotal
$info->{'pass'} ? : 0 4168 4168
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
BranchTrueFalseTotal
if (ref $game and $game->can('board')) { } 4208 5 4213
elsif (ref $game and $game->can('search')) { } 4 1 5
Condition100%3/3
ref $game and $game->can("board")
!ll&&!rl&&r
144208
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")
ABresult
0X0
100
111
33 4 ▶ elsif (ref $game && $game->can('search')) { $board = $game }
Condition66%2/3
ref $game and $game->can("search")
!ll&&!rl&&r
104
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $with{"history"} 4 4208 4212
36 4212 ▶ return undef if $board->over;
Branch: 1 branch
BranchTrueFalseTotal
if $board->over 1 4211 4212
37 4211 ▶ my $rung = defined $with{rung} ? $with{rung} : (rungs())[-1];
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'rung'} ? : 4211 0 4211
38 4211 ▶ my $r = $RUNG{$rung} or Carp::croak("Game::Sternhalma::Bot: there is no rung $rung; there are " . join(', ', rungs()));
Branch: 1 branch
BranchTrueFalseTotal
my $r = $Game::Sternhalma::Bot::RUNG{$rung} or Carp::croak("Game::Sternhalma::Bot: there is no rung $rung; there are " . join(", ", rungs())) 4210 1 4211
39 4210 ▶ my $seed = defined $with{seed} ? $with{seed} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'seed'} ? : 4210 0 4210
40 4210 ▶ Carp::croak('Game::Sternhalma::Bot: a seed is a whole number') unless $seed =~ /\A[0-9]{1,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $seed =~ /\A[0-9]{1,15}\z/u 0 4210 4210
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
BranchTrueFalseTotal
if ($r->{'random'}) { } 1664 2546 4210
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
BranchTrueFalseTotal
$with{'weights'} ? : 0 2546 2546
Condition100%2/2
$r->{"depth"} // 0
l!l
1586960
MC/DC100%1/1
$r->{"depth"} // 0
$r->{"depth"}
Truth table100%2/2
A
A= $r->{"depth"}
Aresult
11
00
51 }
52 4210 $out->{rung} = $rung;
53 4210 return $out;
54 }
55  
56 26 1;
57  
58 __END__
« blib/lib/Game/Sternhalma/Notation.pm ↑ Summary blib/lib/Game/Sternhalma.pm »