blib/lib/Game/Shogi/Bot.pm

filter: Statementstmt 100% 34 / 34 Branchbran 80.0% 8 / 10 Conditioncond 48.1% 13 / 27 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 72.0% 72 / 100 CC 16 SCAR 17.2
CC
16
Cov
77%
declares
9.0
choose
8.8
tiebreak_seed
3.0
CRAP 5.6
« blib/lib/Game/Shogi/Result.pm ↑ Summary blib/lib/Game/Shogi/Error.pm »
1 package Game::Shogi::Bot;
2  
3 28 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Game::Shogi::Engine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 our $VERSION = '0.02';
11  
12 28 our @LADDER = (400, 6_000, 60_000, 300_000);
13  
14 28 our $LEVEL;
15  
16 221 ▶sub level_for {
Subroutine level_for: called CC=3 SCAR=11.0
Pod documented
17 221 my ($class, $seed) = @_;
18 221 ▶ return $LEVEL if defined $LEVEL;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Game::Shogi::Bot::LEVEL 19 202 221
19 202 ▶ my $n = unpack 'N', Digest::SHA::sha256('shogi-level:' . ($seed // ''));
Condition50%1/2
$seed // ""
l!l
2020
MC/DC0%0/1
$seed // ""
$seed
Truth table50%1/2
A
A= $seed
Aresult
11
00
20 202 return $LADDER[ $n % scalar @LADDER ];
21 }
22  
23 104 ▶sub tiebreak_seed {
Subroutine tiebreak_seed: called CC=3 SCAR=11.1
Pod documented
24 104 my ($class, $seed, $side) = @_;
25 104 ▶ return unpack 'N', Digest::SHA::sha256('shogi-bot:' . ($seed // '') . ':' . ($side // ''));
Condition75%3/4
$seed // ""
l!l
1004
$side // ""
l!l
1040
MC/DC100%1/1
$seed // ""
$seed
MC/DC0%0/1
$side // ""
$side
Truth table100%2/2
A
A= $seed
Aresult
11
00
Truth table50%1/2
A
A= $side
Aresult
11
00
26 }
27  
28 63 ▶sub choose {
Subroutine choose: called CC=7 SCAR=21.8
Pod documented
29 63 my ($class, $game, $side, %opt) = @_;
30 63 ▶ return undef unless defined $game && $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game and $game->status eq "active" 0 63 63
Condition33%1/3
defined $game and $game->status eq "active"
!ll&&!rl&&r
0063
MC/DC0%0/2
defined $game and $game->status eq "active"
defined $game $game->status eq "active"
Truth table33%1/3
A and B
A= defined $gameB= $game->status eq "active"
ABresult
0X0
100
111
31 63 ▶ return undef unless defined $side && defined $game->turn && $game->turn eq $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and defined $game->turn and $game->turn eq $side 1 62 63
Condition50%3/6
defined $side and defined $game->turn and $game->turn eq $side
!ll&&!rl&&r
0162
defined $side and defined $game->turn
!ll&&!rl&&r
0063
MC/DC33%1/3
defined $side and defined $game->turn and $game->turn eq $side
defined $side defined $game->turn $game->turn eq $side
Truth table50%2/4
A and B and C
A= defined $sideB= defined $game->turnC= $game->turn eq $side
ABCresult
0XX0
10X0
1100
1111
32 62 ▶ my $budget = $opt{nodes} // $class->level_for($opt{seed});
Condition66%2/3
$opt{"nodes"} // $class->level_for($opt{"seed"})
l!l&&r!l&&!r
44180
MC/DC0%0/2
$opt{"nodes"} // $class->level_for($opt{"seed"})
$opt{"nodes"} $class->level_for($opt{"seed"})
Truth table66%2/3
A // B
A= $opt{"nodes"}B= $class->level_for($opt{"seed"})
ABresult
1X1
011
000
33 return scalar $game->board->search(
34 nodes => $budget,
35 62 seed => $class->tiebreak_seed($opt{seed}, $side),
36 history => [ $game->history ],
37 );
38 }
39  
40 21 ▶sub declares {
Subroutine declares: called CC=6 SCAR=22.0
Pod documented
41 21 my ($class, $game, $side) = @_;
42 21 ▶ return 0 unless defined $game && defined $side && defined $game->turn && $game->turn eq $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game and defined $side and defined $game->turn and $game->turn eq $side 0 21 21
Condition33%3/9
defined $game and defined $side and defined $game->turn and $game->turn eq $side
!ll&&!rl&&r
0021
defined $game and defined $side and defined $game->turn
!ll&&!rl&&r
0021
defined $game and defined $side
!ll&&!rl&&r
0021
MC/DC0%0/4
defined $game and defined $side and defined $game->turn and $game->turn eq $side
defined $game defined $side defined $game->turn $game->turn eq $side
Truth table20%1/5
A and B and C and D
A= defined $gameB= defined $sideC= defined $game->turnD= $game->turn eq $side
ABCDresult
0XXX0
10XX0
110X0
11100
11111
43 21 ▶ return $game->can_declare ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$game->can_declare ? : 1 20 21
44 }
45  
46 3 ▶sub hint {
Subroutine hint: called CC=1 SCAR=0.0
Pod documented
47 3 my ($class, $game, $side, %opt) = @_;
48 3 return $class->choose($game, $side, %opt, nodes => $LADDER[-1]);
49 }
50  
51 28 1;
52  
53 __END__
« blib/lib/Game/Shogi/Result.pm ↑ Summary blib/lib/Game/Shogi/Error.pm »