| 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
Condition100%3/3
$level >= 1 and $level <= 5
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 816 | ▶ | return undef unless Game::Go::Rules::is_colour($colour); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 815 | ▶ | return $self->mark($game, $colour) if $game->phase eq 'marking'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 800 | ▶ | return undef unless $colour == $game->turn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 799 | my $moves = $game->legal($colour); | |||||||||||||||||||||||||||
| 73 | 799 | ▶ | return undef unless @$moves; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 799 | ▶ | my @allowed = map { $_->kind eq 'pass' ? -1 : $_->point } @$moves; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 101 | 20 | ▶ | return undef unless $colour == $m->turn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 20 | ▶ | if ($m->proposed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 8 | ▶ | if (!$agree && !$self->_disputed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$agree or $self->_disputed
MC/DC0%0/2
$agree or $self->_disputed
$agree
$self->_disputed
Truth table33%1/3
A or B
A= $agreeB= $self->_disputed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 139 | 24 | my $id = $game->chain_id($pt); | |||||||||||||||||||||||||||
| 140 | 24 | ▶ | $ids{$id} = 1 if defined $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 20 | return [ sort { $a <=> $b } keys %ids ]; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 9 | 1; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||