blib/lib/Game/Go/Bot.pm

filter: Statementstmt 100% 97 / 97 Branchbran 82.3% 28 / 34 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 17 / 17 Podpod 80.0% 4 / 5 totaltotal 94.6% 159 / 168 CC 23 SCAR 16.6
CC
23
Cov
95%
mark
10.0
choose
8.0
_dead_ids
4.1
CRAP 5.3
« blib/lib/Game/Go/Move.pm ↑ Summary blib/lib/Game/Go/Result.pm »
1 package Game::Go::Bot;
2  
3 31 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 31 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 31 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 31 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 31 ▶use Game::Go::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 31 ▶use Game::Go::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 31 our $VERSION = '0.05';
14  
15 31 my $B = Game::Go::Rules::BLACK;
16 31 my $W = Game::Go::Rules::WHITE;
17  
18 31 our %LEVEL;
19 31 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 31 %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 31 ▶use constant EXPLORE => 700;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 31 ▶use constant DEAD_PLAYOUTS => 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 31 has level => (is => 'ro', isa => Int, default => 3);
32 31 has seed => (is => 'ro', default => 'go');
33  
34 31 has last_search => (is => 'rw');
35  
36 31 has _disputed => (is => 'rw', private => 1, default => 0);
37  
38 206 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
39 206 my ($self) = @_;
40 206 my $level = $self->level;
41 206 ▶ die "Game::Go::Bot: level must be 1 to 5, not '$level'"
Branch: 1 branch
BranchTrueFalseTotal
unless $level >= 1 and $level <= 5 3 203 206
Condition100%3/3
$level >= 1 and $level <= 5
!ll&&!rl&&r
21203
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 203 return;
44 }
45  
46 1 ▶sub levels { (1 .. 5) }
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
47  
48 1216 ▶sub budget {
Subroutine budget: called CC=2 SCAR=7.1
Pod documented
49 1216 my ($self, $size) = @_;
50 1216 ▶ 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} 1216 0 1216
51 1216 return $table->{ $self->level };
52 }
53  
54 1230 ▶sub _rng_seed {
Subroutine _rng_seed: called CC=1 SCAR=0.0
55 1230 my ($self, $game, $colour) = @_;
56 my $label = join ':', 'go-bot', $self->seed,
57 1230 Game::Go::Rules::letter($colour), scalar @{ $game->log };
58 1230 my $digest = Digest::SHA::sha256($label);
59 1230 return unpack 'N', substr($digest, 0, 4);
60 }
61  
62 1229 ▶sub choose {
Subroutine choose: called CC=8 SCAR=20.8
Pod documented
63 1229 my ($self, $game, $colour) = @_;
64  
65 1229 ▶ return undef unless $game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 2 1227 1229
66 1227 ▶ return undef unless Game::Go::Rules::is_colour($colour);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 1 1226 1227
67  
68 1226 ▶ return $self->mark($game, $colour) if $game->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $game->phase eq "marking" 42 1184 1226
69  
70 1184 ▶ return undef unless $colour == $game->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $game->turn 1 1183 1184
71  
72 1183 my $moves = $game->legal($colour);
73 1183 ▶ return undef unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 1183 1183
74  
75 1183 ▶ my @allowed = map { $_->kind eq 'pass' ? -1 : $_->point } @$moves;
Branch: 1 branch
BranchTrueFalseTotal
$_->kind eq 'pass' ? : 1183 46756 47939
76  
77 1183 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 1183 $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 1183 ▶ if $result->{point} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"point"} < 0 70 1113 1183
94  
95 1113 return Game::Go::Move->new(kind => 'play', colour => $colour, point => $result->{point});
96 }
97  
98 47 ▶sub mark {
Subroutine mark: called CC=10 SCAR=23.1
Pod documented
99 47 my ($self, $game, $colour) = @_;
100 47 ▶ my $m = $game->marking or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = $game->marking or return undef 47 0 47
101 47 ▶ return undef unless $colour == $m->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $m->turn 0 47 47
102  
103 47 ▶ if ($m->proposed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->proposed) 19 28 47
104 19 my %theirs = map { $_ => 1 } @{ $m->dead_points };
105 19 my %mine = map { $_ => 1 } @{ $self->_dead_ids($game) };
106  
107 my $agree = (keys %theirs) == (keys %mine)
108 19 ▶ && !grep { !$theirs{$_} } keys %mine;
Condition66%2/3
keys %theirs == keys %mine && !grep({not $theirs{$_};} keys %mine)
!ll&&!rl&&r
2017
MC/DC50%1/2
keys %theirs == keys %mine && !grep({not $theirs{$_};} keys %mine)
keys %theirs == keys %mine !grep({not $theirs{$_};} keys %mine)
Truth table66%2/3
A && not(B)
A= keys %theirs == keys %mineB= !grep({not $theirs{$_};} keys %mine)
ABresult
0X0
100
111
109  
110 19 ▶ if (!$agree && !$self->_disputed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agree or $self->_disputed) 1 18 19
Condition100%3/3
$agree or $self->_disputed
l!l&&r!l&&!r
1711
MC/DC100%2/2
$agree or $self->_disputed
$agree $self->_disputed
Truth table100%3/3
A or B
A= $agreeB= $self->_disputed
ABresult
1X1
011
000
111 1 $self->_disputed(1);
112 1 return Game::Go::Move->new(kind => 'dispute', colour => $colour);
113 }
114 18 return Game::Go::Move->new(kind => 'accept', colour => $colour);
115 }
116  
117 28 my @todo;
118 28 my %already = map { $_ => 1 } @{ $m->dead_points };
119 28 for my $id (@{ $self->_dead_ids($game) }) {
120 31 ▶ next if $already{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $already{$id} 14 17 31
121 17 push @todo, Game::Go::Move->new(kind => 'mark', colour => $colour, point => $id);
122 }
123 28 ▶ return $todo[0] if @todo;
Branch: 1 branch
BranchTrueFalseTotal
if @todo 12 16 28
124  
125 16 return Game::Go::Move->new(kind => 'done', colour => $colour);
126 }
127  
128 47 ▶sub _dead_ids {
Subroutine _dead_ids: called CC=4 SCAR=14.0
129 47 my ($self, $game) = @_;
130  
131 47 my $guess = $game->dead_guess_from(
132 seed => $self->_rng_seed($game, Game::Go::Rules::BLACK),
133 playouts => DEAD_PLAYOUTS,
134 );
135  
136 47 my %ids;
137 47 for my $pt (@$guess) {
138 120 ▶ next if $game->is_alive($pt);
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_alive($pt) 0 120 120
139 120 my $id = $game->chain_id($pt);
140 120 ▶ $ids{$id} = 1 if defined $id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 120 0 120
141 }
142 47 return [ sort { $a <=> $b } keys %ids ];
143 }
144  
145 31 1;
146  
147 __END__
« blib/lib/Game/Go/Move.pm ↑ Summary blib/lib/Game/Go/Result.pm »