blib/lib/Game/Go/Bot.pm

filter: Statementstmt 96.9% 94 / 97 Branchbran 79.4% 27 / 34 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 17 / 17 Podpod 80.0% 4 / 5 totaltotal 88.6% 149 / 168 CC 23 SCAR 17.0
CC
23
Cov
90%
mark
10.9
choose
8.0
_dead_ids
4.2
CRAP 5.5
« blib/lib/Game/Go/Move.pm ↑ Summary blib/lib/Game/Go/Result.pm »
1 package Game::Go::Bot;
2  
3 9 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 9 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 9 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 9 ▶use Game::Go::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 9 ▶use Game::Go::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 9 our $VERSION = '0.03';
14  
15 9 my $B = Game::Go::Rules::BLACK;
16 9 my $W = Game::Go::Rules::WHITE;
17  
18 9 our %LEVEL;
19 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 %LEVEL = (
21 9 => { 1 => 50, 2 => 200, 3 => 600, 4 => 1500, 5 => 3000 },
22 13 => { 1 => 50, 2 => 150, 3 => 350, 4 => 700, 5 => 1200 },
23 19 => { 1 => 50, 2 => 100, 3 => 160, 4 => 240, 5 => 320 },
24 );
25 }
26  
27 9 ▶use constant EXPLORE => 700;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 9 ▶use constant DEAD_PLAYOUTS => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 9 has level => (is => 'ro', isa => Int, default => 3);
32 9 has seed => (is => 'ro', default => 'go');
33  
34 9 has last_search => (is => 'rw');
35  
36 9 has _disputed => (is => 'rw', private => 1, default => 0);
37  
38 133 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
39 133 my ($self) = @_;
40 133 my $level = $self->level;
41 133 ▶ die "Game::Go::Bot: level must be 1 to 5, not '$level'"
Branch: 1 branch
BranchTrueFalseTotal
unless $level >= 1 and $level <= 5 3 130 133
Condition100%3/3
$level >= 1 and $level <= 5
!ll&&!rl&&r
21130
MC/DC100%2/2
$level >= 1 and $level <= 5
$level >= 1 $level <= 5
Truth table100%3/3
A and B
A= $level >= 1B= $level <= 5
ABresult
0X0
100
111
42 unless $level >= 1 && $level <= 5;
43 130 return;
44 }
45  
46 1 ▶sub levels { (1 .. 5) }
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
47  
48 832 ▶sub budget {
Subroutine budget: called CC=2 SCAR=7.1
Pod documented
49 832 my ($self, $size) = @_;
50 832 ▶ my $table = $LEVEL{$size} or return $LEVEL{19}{ $self->level };
Branch: 1 branch
BranchTrueFalseTotal
my $table = $Game::Go::Bot::LEVEL{$size} or return $Game::Go::Bot::LEVEL{19}{$self->level} 832 0 832
51 832 return $table->{ $self->level };
52 }
53  
54 819 ▶sub _rng_seed {
Subroutine _rng_seed: called CC=1 SCAR=0.0
55 819 my ($self, $game, $colour) = @_;
56 my $label = join ':', 'go-bot', $self->seed,
57 819 Game::Go::Rules::letter($colour), scalar @{ $game->log };
58 819 my $digest = Digest::SHA::sha256($label);
59 819 return unpack 'N', substr($digest, 0, 4);
60 }
61  
62 818 ▶sub choose {
Subroutine choose: called CC=8 SCAR=20.8
Pod documented
63 818 my ($self, $game, $colour) = @_;
64  
65 818 ▶ return undef unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 2 816 818
66 816 ▶ return undef unless Game::Go::Rules::is_colour($colour);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 1 815 816
67  
68 815 ▶ return $self->mark($game, $colour) if $game->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $game->phase eq "marking" 15 800 815
69  
70 800 ▶ return undef unless $colour == $game->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $game->turn 1 799 800
71  
72 799 my $moves = $game->legal($colour);
73 799 ▶ return undef unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 799 799
74  
75 799 ▶ my @allowed = map { $_->kind eq 'pass' ? -1 : $_->point } @$moves;
Branch: 1 branch
BranchTrueFalseTotal
$_->kind eq 'pass' ? : 799 27452 28251
76  
77 799 my $result = $game->search_from(
78 colour => $colour,
79 allowed => \@allowed,
80 seed => $self->_rng_seed($game, $colour),
81 playouts => $self->budget($game->size),
82 explore => EXPLORE,
83 );
84  
85 799 $self->last_search({
86 %$result,
87 level => $self->level,
88 size => $game->size,
89 roots => scalar @allowed,
90 });
91  
92 return Game::Go::Move->new(kind => 'pass', colour => $colour)
93 799 ▶ if $result->{point} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"point"} < 0 46 753 799
94  
95 753 return Game::Go::Move->new(kind => 'play', colour => $colour, point => $result->{point});
96 }
97  
98 20 ▶sub mark {
Subroutine mark: called CC=10 SCAR=23.8
Pod documented
99 20 my ($self, $game, $colour) = @_;
100 20 ▶ my $m = $game->marking or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = $game->marking or return undef 20 0 20
101 20 ▶ return undef unless $colour == $m->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $m->turn 0 20 20
102  
103 20 ▶ if ($m->proposed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->proposed) 8 12 20
104 8 my %theirs = map { $_ => 1 } @{ $m->dead_points };
105 8 my %mine = map { $_ => 1 } @{ $self->_dead_ids($game) };
106  
107 my $agree = (keys %theirs) == (keys %mine)
108 8 ▶ && !grep { !$theirs{$_} } keys %mine;
Condition33%1/3
keys %theirs == keys %mine && !grep({not $theirs{$_};} keys %mine)
!ll&&!rl&&r
008
MC/DC0%0/2
keys %theirs == keys %mine && !grep({not $theirs{$_};} keys %mine)
keys %theirs == keys %mine !grep({not $theirs{$_};} keys %mine)
Truth table33%1/3
A && not(B)
A= keys %theirs == keys %mineB= !grep({not $theirs{$_};} keys %mine)
ABresult
0X0
100
111
109  
110 8 ▶ if (!$agree && !$self->_disputed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agree or $self->_disputed) 0 8 8
Condition33%1/3
$agree or $self->_disputed
l!l&&r!l&&!r
800
MC/DC0%0/2
$agree or $self->_disputed
$agree $self->_disputed
Truth table33%1/3
A or B
A= $agreeB= $self->_disputed
ABresult
1X1
011
000
111 0 $self->_disputed(1);
112 0 return Game::Go::Move->new(kind => 'dispute', colour => $colour);
113 }
114 8 return Game::Go::Move->new(kind => 'accept', colour => $colour);
115 }
116  
117 12 my @todo;
118 12 my %already = map { $_ => 1 } @{ $m->dead_points };
119 12 for my $id (@{ $self->_dead_ids($game) }) {
120 8 ▶ next if $already{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $already{$id} 4 4 8
121 4 push @todo, Game::Go::Move->new(kind => 'mark', colour => $colour, point => $id);
122 }
123 12 ▶ return $todo[0] if @todo;
Branch: 1 branch
BranchTrueFalseTotal
if @todo 4 8 12
124  
125 8 return Game::Go::Move->new(kind => 'done', colour => $colour);
126 }
127  
128 20 ▶sub _dead_ids {
Subroutine _dead_ids: called CC=4 SCAR=14.3
129 20 my ($self, $game) = @_;
130  
131 20 my $guess = $game->dead_guess_from(
132 seed => $self->_rng_seed($game, Game::Go::Rules::BLACK),
133 playouts => DEAD_PLAYOUTS,
134 );
135  
136 20 my %ids;
137 20 for my $pt (@$guess) {
138 24 ▶ next if $game->is_alive($pt);
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_alive($pt) 0 24 24
139 24 my $id = $game->chain_id($pt);
140 24 ▶ $ids{$id} = 1 if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 24 0 24
141 }
142 20 return [ sort { $a <=> $b } keys %ids ];
143 }
144  
145 9 1;
146  
147 __END__
« blib/lib/Game/Go/Move.pm ↑ Summary blib/lib/Game/Go/Result.pm »