Chess/Bot.pm

filter: Statementstmt 100% 53 / 53 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 95.4% 83 / 87 CC 8 SCAR 11.7
CC
8
Cov
96%
choose
7.0
level_for
2.0
BEGIN
1.0
CRAP 3.2
« Chess/Game.pm ↑ Summary Chess/Watcher.pm »
1 package Term::Chess::Bot;
2  
3 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Chess::Plisco::Engine::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Chess::Plisco::Engine::TranspositionTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Term::Chess::Watcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = '0.01';
14  
15 6 our @LADDER = (2_000, 10_000, 10_000, 40_000);
16  
17 6 our $LEVEL;
18  
19 6 my $TT_MB = 1;
20  
21 39 ▶sub ladder { @LADDER }
Subroutine ladder: called CC=1 SCAR=0.0
Pod documented
22  
23 226 ▶sub level_for {
Subroutine level_for: called CC=2 SCAR=6.9
Pod documented
24 226 my ($class, $seed) = @_;
25 226 ▶ return $LEVEL if defined $LEVEL;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Term::Chess::Bot::LEVEL 87 139 226
26 139 my $n = unpack 'N', Digest::SHA::sha256("chess-level:$seed");
27 139 return $LADDER[ $n % scalar @LADDER ];
28 }
29  
30 91 ▶sub choose {
Subroutine choose: called CC=7 SCAR=19.5
Pod documented
31 91 my ($class, $game, $seat) = @_;
32 91 ▶ return undef unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 1 90 91
33 90 ▶ return undef unless defined $game->turn && $game->turn eq $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game->turn and $game->turn eq $seat 3 87 90
Condition66%2/3
defined $game->turn and $game->turn eq $seat
!ll&&!rl&&r
0387
MC/DC50%1/2
defined $game->turn and $game->turn eq $seat
defined $game->turn $game->turn eq $seat
Truth table66%2/3
A and B
A= defined $game->turnB= $game->turn eq $seat
ABresult
0X0
100
111
34  
35 87 my $budget = $class->level_for($game->_held_seed);
36 87 my $position = $game->position->copy;
37 87 my @info;
38 my $tree = Chess::Plisco::Engine::Tree->new(
39 position => $position,
40 signatures => $game->signatures,
41 tt => Chess::Plisco::Engine::TranspositionTable->new($TT_MB),
42 watcher => Term::Chess::Watcher->new($budget),
43 1140 ▶ info => sub { push @info, "@_" },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 87 );
45 87 $tree->{max_nodes} = $budget;
46 87 $tree->{nodes_to_tc} = 1000;
47 87 $tree->{start_time} = [ Time::HiRes::gettimeofday() ];
48  
49 87 my ($move) = $tree->think;
50 87 ▶ return undef unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 87 87
51 87 ▶ die "Term::Chess::Bot: the search failed: $info[-1]\n" if grep { /Error/ } @info;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/Error/u;} @info 0 87 87
52 87 my $uci = $game->position->moveCoordinateNotation($move);
53 87 ▶ return wantarray ? ($uci, $tree->{nodes}, $budget) : $uci;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 80 87
54 }
55  
56 5 ▶sub hint {
Subroutine hint: called CC=1 SCAR=0.0
Pod documented
57 5 my ($class, $game, $seat) = @_;
58 5 local $LEVEL = $LADDER[-1];
59 5 return $class->choose($game, $seat);
60 }
61  
62 6 1;
63  
64 __END__
« Chess/Game.pm ↑ Summary Chess/Watcher.pm »