Dominoes/Bot.pm

filter: Statementstmt 99.4% 178 / 179 Branchbran 92.3% 48 / 52 Conditioncond 70.9% 22 / 31 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 100% 20 / 20 Podpod 66.6% 2 / 3 totaltotal 91.0% 275 / 302 CC 50 SCAR 22.1
CC
50
Cov
95%
_rollout
15.0
choose
14.3
_sample
12.0
CRAP 9.1
« Dominoes/Terminal.pm ↑ Summary Dominoes.pm »
1 package Game::Dominoes::Bot;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Game::Dominoes::Hand;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Game::Dominoes::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Game::Dominoes::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Game::Dominoes::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $VERSION = '0.03';
15  
16 6 our %LEVEL = (
17 1 => { worlds => 0, depth => 0, shape => 0 },
18 2 => { worlds => 0, depth => 0, shape => 1 },
19 3 => { worlds => 24, depth => 8, shape => 1 },
20 4 => { worlds => 40, depth => 12, shape => 1 },
21 5 => { worlds => 64, depth => 16, shape => 1 },
22 );
23  
24 6 our $SAMPLE_TRIES = 40;
25  
26 6 has level => (is => 'ro', isa => Int, default => 3);
27 6 has seed => (is => 'ro', isa => Int, default => 0);
28 6 has last_search => (is => 'rw', isa => HashRef, default => {});
29  
30 205 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
31 205 my ($self) = @_;
32 205 ▶ die 'Game::Dominoes::Bot: level must be 1 to 5' unless $LEVEL{ $self->level };
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Dominoes::Bot::LEVEL{$self->level} 2 203 205
33 203 return $self;
34 }
35  
36 1 ▶sub levels { return { %LEVEL } }
Subroutine levels: called CC=1 SCAR=0.0
Pod documented
37  
38 204 ▶sub _stream {
Subroutine _stream: called CC=1 SCAR=0.0
39 204 my ($self, $key) = @_;
40 204 my ($counter, @words) = (0);
41 111677 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 111677 ▶ unless (@words) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@words) 13960 97717 111677
43 13960 @words = unpack 'N8',
44 Digest::SHA::sha256($self->seed . '|' . $key . '|' . $counter++);
45 }
46 111677 return shift @words;
47 204 };
48 }
49  
50 700 ▶sub _public_key {
Subroutine _public_key: called CC=3 SCAR=11.0
51 700 my ($self, $view) = @_;
52 my @parts = (
53 $view->{seat}, $view->{players}, $view->{hand_number},
54 $view->{turn} // '-', $view->{boneyard},
55 1962 join(',', map { $_ . ':' . $view->{counts}{$_} } sort keys %{ $view->{counts} }),
56 1962 join(',', map { $_ . ':' . $view->{scores}{$_} } sort keys %{ $view->{scores} }),
57 4587 join(',', map { $_->stringify } @{ $view->{layout}->tiles }),
58 700 join(',', @{ $view->{ends} }),
59 700 ▶ join(',', map { $_->stringify } @{ $view->{hand} || [] }),
Condition50%2/4
$view->{"turn"} // "-"
l!l
7000
$view->{'hand'} || []
l!l
7000
MC/DC0%0/1
$view->{"turn"} // "-"
$view->{"turn"}
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{"turn"}
Aresult
11
00
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
60 );
61 700 return join '|', @parts;
62 }
63  
64 6021 ▶sub _shuffle {
Subroutine _shuffle: called CC=3 SCAR=11.0
65 6021 my ($list, $next) = @_;
66 6021 ▶ for (my $i = $#$list; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 111677 6021 117698
67 111677 my $n = $i + 1;
68 111677 my $limit = int(4294967296 / $n) * $n;
69 111677 my $word;
70 111677 ▶ do { $word = $next->() } while $word >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
$word >= $limit and do { do { $word = &$next() } } 0 111677 111677
71 111677 my $j = $word % $n;
72 111677 @{$list}[ $i, $j ] = @{$list}[ $j, $i ];
73 }
74 6021 return $list;
75 }
76  
77 5088 ▶sub _unseen {
Subroutine _unseen: called CC=4 SCAR=13.9
78 5088 my ($self, $view) = @_;
79 5088 my %seen;
80 5088 $seen{ $_->id } = 1 for @{ $view->{layout}->tiles };
81 5088 ▶ $seen{ $_->id } = 1 for @{ $view->{hand} || [] };
Condition50%1/2
$view->{'hand'} || []
l!l
50880
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
82 5088 return [ grep { !$seen{ $_->id } } @{ Game::Dominoes::Set::tiles() } ];
83 }
84  
85 5088 ▶sub _sample {
Subroutine _sample: called CC=12 SCAR=24.9
86 5088 my ($self, $view, $next) = @_;
87  
88 5088 my $unseen = $self->_unseen($view);
89 5088 my @others = grep { $_ ne $view->{seat} } sort keys %{ $view->{counts} };
90 5088 ▶ my $deductions = $view->{deductions} || {};
Condition50%1/2
$view->{'deductions'} || {}
l!l
50880
MC/DC0%0/1
$view->{'deductions'} || {}
$view->{'deductions'}
Truth table50%1/2
A
A= $view->{'deductions'}
Aresult
11
00
91  
92 5088 my $best;
93 5088 for my $try (1 .. $SAMPLE_TRIES) {
94 6021 my @pool = @$unseen;
95 6021 _shuffle(\@pool, $next);
96  
97 6021 my (%hands, $bad);
98 6021 for my $seat (@others) {
99 8229 my $want = $view->{counts}{$seat};
100 8229 my @take = splice @pool, 0, $want;
101 8229 $hands{$seat} = \@take;
102 8229 ▶ next unless $deductions->{$seat} && keys %{ $deductions->{$seat} };
Branch: 1 branch
BranchTrueFalseTotal
unless $deductions->{$seat} and keys %{$$deductions{$seat};} 7096 1133 8229
Condition66%2/3
$deductions->{$seat} and keys %{$$deductions{$seat};}
!ll&&!rl&&r
070961133
MC/DC50%1/2
$deductions->{$seat} and keys %{$$deductions{$seat};}
$deductions->{$seat} keys %{$$deductions{$seat};}
Truth table66%2/3
A and B
A= $deductions->{$seat}B= keys %{$$deductions{$seat};}
ABresult
0X0
100
111
103 1133 for my $tile (@take) {
104 next unless $deductions->{$seat}{ $tile->high }
105 2164 ▶ || $deductions->{$seat}{ $tile->low };
Branch: 1 branch
BranchTrueFalseTotal
unless $deductions->{$seat}{$tile->high} or $deductions->{$seat}{$tile->low} 1231 933 2164
Condition100%3/3
$deductions->{$seat}{$tile->high} or $deductions->{$seat}{$tile->low}
l!l&&r!l&&!r
838951231
MC/DC100%2/2
$deductions->{$seat}{$tile->high} or $deductions->{$seat}{$tile->low}
$deductions->{$seat}{$tile->high} $deductions->{$seat}{$tile->low}
Truth table100%3/3
A or B
A= $deductions->{$seat}{$tile->high}B= $deductions->{$seat}{$tile->low}
ABresult
1X1
011
000
106 933 $bad = 1;
107 933 last;
108 }
109 1133 ▶ last if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 933 200 1133
110 }
111  
112 6021 my $world = { hands => \%hands, boneyard => [@pool] };
113 6021 ▶ $best = $world unless $best;
Branch: 1 branch
BranchTrueFalseTotal
unless $best 5088 933 6021
114 6021 ▶ return $world unless $bad;
Branch: 1 branch
BranchTrueFalseTotal
unless $bad 5088 933 6021
115 }
116  
117 0 return $best;
118 }
119  
120 23784 ▶sub _rollout {
Subroutine _rollout: called CC=15 SCAR=27.1
121 23784 my ($self, $view, $world, $first, $depth) = @_;
122  
123 23784 my $seat = $view->{seat};
124 23784 my $layout = $view->{layout}->clone;
125 my %hand = (
126 23784 ▶ $seat => Game::Dominoes::Hand->new(tiles => [ @{ $view->{hand} || [] } ]),
Condition50%1/2
$view->{'hand'} || []
l!l
237840
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
127 38184 map { $_ => Game::Dominoes::Hand->new(tiles => [ @{ $world->{hands}{$_} } ]) }
128 23784 keys %{ $world->{hands} },
129 );
130 23784 my @yard = @{ $world->{boneyard} };
131  
132 23784 $hand{$seat}->remove($first->{tile});
133 23784 $layout->place($first->{tile}, $first->{arm});
134 my $score = Game::Dominoes::Scoring::score_for(
135 Game::Dominoes::Scoring::count($layout), $view->{scale}
136 23784 );
137 23784 ▶ return $score + _out_bonus($view, \%hand, $seat) if $hand{$seat}->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $hand{$seat}->is_empty 48 23736 23784
138  
139 23736 my @seats = sort { $a <=> $b } keys %hand;
140 23736 my $at = 0;
141 23736 ▶ $at++ while $at < $#seats && $seats[$at] != $seat;
Branch: 1 branch
BranchTrueFalseTotal
$at < $#seats and $seats[$at] != $seat and do { ++$at } 17880 23736 41616
Condition100%3/3
$at < $#seats and $seats[$at] != $seat
!ll&&!rl&&r
78001593617880
MC/DC100%2/2
$at < $#seats and $seats[$at] != $seat
$at < $#seats $seats[$at] != $seat
Truth table100%3/3
A and B
A= $at < $#seatsB= $seats[$at] != $seat
ABresult
0X0
100
111
142  
143 23736 for my $ply (1 .. $depth) {
144 185523 $at = ($at + 1) % @seats;
145 185523 my $who = $seats[$at];
146 185523 my $moves = Game::Dominoes::Rules::candidates($layout, $hand{$who});
147  
148 185523 ▶ unless (@$moves) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$moves) 15378 170145 185523
149 15378 ▶ while (@yard) {
Branch: 1 branch
BranchTrueFalseTotal
if (@yard) 25983 2689 28672
150 25983 $hand{$who}->add(shift @yard);
151 25983 $moves = Game::Dominoes::Rules::candidates($layout, $hand{$who});
152 25983 ▶ last if @$moves;
Branch: 1 branch
BranchTrueFalseTotal
if @$moves 12689 13294 25983
153 }
154 15378 ▶ next unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 2689 12689 15378
155 }
156  
157 182834 my $best = $moves->[0];
158 182834 my $best_points = -1;
159 182834 for my $move (@$moves) {
160 my $points = Game::Dominoes::Rules::score_of(
161 $layout, $move->{tile}, $move->{arm}, $view->{scale}
162 606585 );
163 606585 ▶ next unless $points > $best_points;
Branch: 1 branch
BranchTrueFalseTotal
unless $points > $best_points 374528 232057 606585
164 232057 ($best, $best_points) = ($move, $points);
165 }
166  
167 182834 $hand{$who}->remove($best->{tile});
168 182834 $layout->place($best->{tile}, $best->{arm});
169 my $points = Game::Dominoes::Scoring::score_for(
170 Game::Dominoes::Scoring::count($layout), $view->{scale}
171 182834 );
172 182834 ▶ $score += $who eq $seat ? $points : -$points;
Branch: 1 branch
BranchTrueFalseTotal
$who eq $seat ? : 62035 120799 182834
173 return $score + ($who eq $seat ? 1 : -1) * _out_bonus($view, \%hand, $who)
174 182834 ▶ if $hand{$who}->is_empty;
Branch: 2 branches
BranchTrueFalseTotal
if $hand{$who}->is_empty 2190 180644 182834
$who eq $seat ? : 937 1253 2190
175 }
176  
177 21546 return $score - $hand{$seat}->pips / 5;
178 }
179  
180 2238 ▶sub _out_bonus {
Subroutine _out_bonus: called CC=2 SCAR=6.9
181 2238 my ($view, $hands, $who) = @_;
182 2238 my $pips = 0;
183 2238 $pips += $hands->{$_}->pips for grep { $_ ne $who } keys %$hands;
184 2238 return Game::Dominoes::Scoring::bonus($pips, $view->{scale});
185 }
186  
187 2332 ▶sub _shape {
Subroutine _shape: called CC=2 SCAR=6.9
188 2332 my ($self, $view, $move) = @_;
189 2332 my $tile = $move->{tile};
190 2332 ▶ return $tile->pips / 100 + ($tile->is_double ? 0.05 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tile->is_double ? : 455 1877 2332
191 }
192  
193 700 ▶sub choose {
Subroutine choose: called CC=14 SCAR=26.6
Pod documented
194 700 my ($self, $game, $seat) = @_;
195  
196 700 ▶ my $view = (ref $game eq 'HASH') ? $game : $game->view($seat);
Branch: 1 branch
BranchTrueFalseTotal
ref $game eq 'HASH' ? : 0 700 700
197 700 ▶ return undef unless $view->{hand} && @{ $view->{hand} };
Branch: 1 branch
BranchTrueFalseTotal
unless $view->{"hand"} and @{$view->{"hand"};} 0 700 700
Condition33%1/3
$view->{"hand"} and @{$view->{"hand"};}
!ll&&!rl&&r
00700
MC/DC0%0/2
$view->{"hand"} and @{$view->{"hand"};}
$view->{"hand"} @{$view->{"hand"};}
Truth table33%1/3
A and B
A= $view->{"hand"}B= @{$view->{"hand"};}
ABresult
0X0
100
111
198  
199 700 my $hand = Game::Dominoes::Hand->new(tiles => [ @{ $view->{hand} } ]);
200 700 my $moves = Game::Dominoes::Rules::candidates($view->{layout}, $hand);
201 700 ▶ if (my $forced = $view->{forced}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $forced = $view->{"forced"}) 53 647 700
202 53 $moves = [ grep { $_->{tile}->id == $forced->id } @$moves ];
203 }
204 700 ▶ return undef unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 700 700
205  
206 700 my $level = $LEVEL{ $self->level };
207 700 my $key = $self->_public_key($view);
208  
209 700 my @scored;
210 700 for my $move (@$moves) {
211 my $points = Game::Dominoes::Rules::score_of(
212 $view->{layout}, $move->{tile}, $move->{arm}, $view->{scale}
213 2692 );
214 push @scored, {
215 move => $move,
216 2692 ▶ score => $points + ($level->{shape} ? $self->_shape($view, $move) : 0),
Branch: 1 branch
BranchTrueFalseTotal
$level->{'shape'} ? : 2332 360 2692
217 points => $points,
218 };
219 }
220  
221 700 my $worlds = 0;
222 700 ▶ if ($level->{worlds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level->{"worlds"}) 203 497 700
223 203 my $next = $self->_stream($key);
224 203 my @sampled = map { $self->_sample($view, $next) } 1 .. $level->{worlds};
225 203 $worlds = scalar @sampled;
226  
227 203 for my $candidate (@scored) {
228 985 my $total = 0;
229 $total += $self->_rollout($view, $_, $candidate->{move}, $level->{depth})
230 985 for @sampled;
231 985 $candidate->{score} = $total / $worlds;
232 }
233 }
234  
235 my @order = sort {
236 700 $b->{score} <=> $a->{score}
237 || $b->{move}{tile}->pips <=> $a->{move}{tile}->pips
238 || $a->{move}{tile}->id <=> $b->{move}{tile}->id
239 || $a->{move}{arm} cmp $b->{move}{arm}
240 3392 ▶ } @scored;
Condition88%8/9
$Game::Dominoes::Bot::b->{'score'} <=> $Game::Dominoes::Bot::a->{'score'} || $Game::Dominoes::Bot::b->{'move'}{'tile'}->pips <=> $Game::Dominoes::Bot::a->{'move'}{'tile'}->pips || $Game::Dominoes::Bot::a->{'move'}{'tile'}->id <=> $Game::Dominoes::Bot::b->{'move'}{'tile'}->id || $Game::Dominoes::Bot::a->{'move'}{'arm'} cmp $Game::Dominoes::Bot::b->{'move'}{'arm'}
l!l&&r!l&&!r
30992930
$Game::Dominoes::Bot::b->{"score"} <=> $Game::Dominoes::Bot::a->{"score"} or $Game::Dominoes::Bot::b->{"move"}{"tile"}->pips <=> $Game::Dominoes::Bot::a->{"move"}{"tile"}->pips or $Game::Dominoes::Bot::a->{"move"}{"tile"}->id <=> $Game::Dominoes::Bot::b->{"move"}{"tile"}->id
l!l&&r!l&&!r
302277293
$Game::Dominoes::Bot::b->{"score"} <=> $Game::Dominoes::Bot::a->{"score"} or $Game::Dominoes::Bot::b->{"move"}{"tile"}->pips <=> $Game::Dominoes::Bot::a->{"move"}{"tile"}->pips
l!l&&r!l&&!r
2808214370
MC/DC0%0/4
$Game::Dominoes::Bot::b->{'score'} <=> $Game::Dominoes::Bot::a->{'score'} || $Game::Dominoes::Bot::b->{'move'}{'tile'}->pips <=> $Game::Dominoes::Bot::a->{'move'}{'tile'}->pips || $Game::Dominoes::Bot::a->{'move'}{'tile'}->id <=> $Game::Dominoes::Bot::b->{'move'}{'tile'}->id || $Game::Dominoes::Bot::a->{'move'}{'arm'} cmp $Game::Dominoes::Bot::b->{'move'}{'arm'}
$Game::Dominoes::Bot::b->{"score"} <=> $Game::Dominoes::Bot::a->{"score"} $Game::Dominoes::Bot::b->{"move"}{"tile"}->pips <=> $Game::Dominoes::Bot::a->{"move"}{"tile"}->pips $Game::Dominoes::Bot::a->{"move"}{"tile"}->id <=> $Game::Dominoes::Bot::b->{"move"}{"tile"}->id $Game::Dominoes::Bot::a->{'move'}{'arm'} cmp $Game::Dominoes::Bot::b->{'move'}{'arm'}
Truth table80%4/5
A or B or C || D
A= $Game::Dominoes::Bot::b->{"score"} <=> $Game::Dominoes::Bot::a->{"score"}B= $Game::Dominoes::Bot::b->{"move"}{"tile"}->pips <=> $Game::Dominoes::Bot::a->{"move"}{"tile"}->pipsC= $Game::Dominoes::Bot::a->{"move"}{"tile"}->id <=> $Game::Dominoes::Bot::b->{"move"}{"tile"}->idD= $Game::Dominoes::Bot::a->{'move'}{'arm'} cmp $Game::Dominoes::Bot::b->{'move'}{'arm'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
241  
242 $self->last_search({
243 level => $self->level,
244 worlds => $worlds,
245 depth => $level->{depth},
246 considered => scalar @scored,
247 score => $order[0]{score},
248 points => $order[0]{points},
249 play => $order[0]{move},
250 700 });
251  
252 700 return $order[0]{move};
253 }
254  
255 6 1;
256  
257 __END__
« Dominoes/Terminal.pm ↑ Summary Dominoes.pm »