Dominoes.pm

filter: Statementstmt 98.3% 305 / 310 Branchbran 80.6% 100 / 124 Conditioncond 62.1% 23 / 37 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 40 / 40 Podpod 92.8% 13 / 14 totaltotal 89.7% 490 / 546 CC 83 SCAR 21.2
CC
83
Cov
91%
play
17.2
BUILD
15.7
_move_of
10.5
CRAP 8.3
« Dominoes/Bot.pm ↑ Summary Dominoes/Layout.pm »
1 package Game::Dominoes;
2  
3 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Game::Dominoes::Tile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Game::Dominoes::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Game::Dominoes::Hand;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Game::Dominoes::Boneyard;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Game::Dominoes::Play;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Game::Dominoes::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Game::Dominoes::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Game::Dominoes::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Game::Dominoes::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Game::Dominoes::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Game::Dominoes::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 our $VERSION = '0.03';
23  
24 12 our %HAND_SIZE = (2 => 9, 3 => 7, 4 => 5);
25  
26 12 our %TARGET = (2 => 250, 3 => 200, 4 => 200);
27  
28 12 our @VARIANTS = qw/all_fives draw block/;
29  
30 12 has seed => (is => 'ro', isa => Str);
31 12 has players => (is => 'ro', isa => Int, default => 2);
32 12 has variant => (is => 'ro', isa => Str, default => 'all_fives');
33 12 has target => (is => 'rw', isa => Int);
34 12 has scale => (is => 'ro', isa => Int, default => 1);
35 12 has reserve => (is => 'ro', isa => Int, default => 0);
36  
37 12 has status => (is => 'rw', isa => Str, default => 'active');
38 12 has hand_number => (is => 'rw', isa => Int, default => 0);
39 12 has turn => (is => 'rw', isa => Int);
40 12 has hands => (is => 'rw', isa => HashRef, default => {});
41 12 has scores => (is => 'rw', isa => HashRef, default => {});
42 12 has boneyard => (is => 'rw', isa => Object);
43 12 has layout => (is => 'rw', isa => Object);
44 12 has result => (is => 'rw', isa => Object);
45 12 has history => (is => 'rw', isa => ArrayRef, default => []);
46  
47 12 has leader => (is => 'rw', isa => Int);
48 12 has forced_tile => (is => 'rw', isa => Object);
49  
50 12 has passes => (is => 'rw', isa => Int, default => 0);
51  
52 12 has deductions => (is => 'rw', isa => HashRef, default => {});
53  
54 1124 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=27.5
Pod undocumented
55 1124 my ($self) = @_;
56  
57 1124 ▶ die 'Game::Dominoes: seed must be 32 bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed and length $self->seed == 32 2 1122 1124
Condition100%3/3
defined $self->seed and length $self->seed == 32
!ll&&!rl&&r
111122
MC/DC100%2/2
defined $self->seed and length $self->seed == 32
defined $self->seed length $self->seed == 32
Truth table100%3/3
A and B
A= defined $self->seedB= length $self->seed == 32
ABresult
0X0
100
111
58 unless defined $self->seed && length $self->seed == 32;
59 die 'Game::Dominoes: players must be 2, 3 or 4'
60 1122 ▶ unless $HAND_SIZE{ $self->players };
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Dominoes::HAND_SIZE{$self->players} 2 1120 1122
61 die 'Game::Dominoes: unknown variant ' . $self->variant
62 1120 ▶ unless grep { $_ eq $self->variant } @VARIANTS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $self->variant;} @Game::Dominoes::VARIANTS 1 1119 1120
63 1119 ▶ die 'Game::Dominoes: scale must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->scale >= 1 0 1119 1119
64 unless $self->scale >= 1;
65  
66 1119 ▶ $self->target($TARGET{ $self->players } / $self->scale)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->target 1109 10 1119
67 unless defined $self->target;
68 1119 ▶ die 'Game::Dominoes: target must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->target > 0 1 1118 1119
69 unless $self->target > 0;
70  
71 3245 $self->scores({ map { $_ => 0 } $self->seats })
72 1118 ▶ unless keys %{ $self->scores };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self->scores;} 1118 0 1118
73 1118 ▶ $self->leader($self->_by_lot(1)) unless defined $self->leader;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->leader 1118 0 1118
74  
75 1118 ▶ if (keys %{ $self->hands }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->hands;}) { } 85 1033 1118
76 85 ▶ $self->layout(Game::Dominoes::Layout->new) unless $self->layout;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->layout 85 0 85
77 85 ▶ $self->boneyard(Game::Dominoes::Boneyard->new(reserve => $self->reserve))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->boneyard 85 0 85
78 unless $self->boneyard;
79 85 ▶ $self->hand_number(1) unless $self->hand_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hand_number 85 0 85
80 85 ▶ $self->turn($self->leader) unless defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->turn 85 0 85
81 }
82 else {
83 1033 $self->_deal;
84 }
85 1118 return $self;
86 }
87  
88 7756 ▶sub seats { return 1 .. $_[0]->players }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
89  
90 2063 ▶sub hand_size { return $HAND_SIZE{ $_[0]->players } }
Subroutine hand_size: called CC=1 SCAR=0.0
Pod documented
91  
92 1182 ▶sub _by_lot {
Subroutine _by_lot: called CC=2 SCAR=7.1
93 1182 my ($self, $n) = @_;
94 1182 my $word = unpack 'N', Digest::SHA::sha256($self->seed . "lot:$n");
95 1182 my $limit = int(4294967296 / $self->players) * $self->players;
96 1182 my $i = 0;
97 1182 ▶ while ($word >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word >= $limit) 0 1182 1182
98 0 $word = unpack 'N', Digest::SHA::sha256($self->seed . "lot:$n:" . ++$i);
99 }
100 1182 return ($word % $self->players) + 1;
101 }
102  
103 26108 ▶sub _note {
Subroutine _note: called CC=1 SCAR=0.0
104 26108 my ($self, $kind, %rest) = @_;
105 26108 push @{ $self->history }, { kind => $kind, hand => $self->hand_number, %rest };
106 26108 return;
107 }
108  
109 2060 ▶sub _deal {
Subroutine _deal: called CC=2 SCAR=6.9
110 2060 my ($self) = @_;
111  
112 2060 $self->hand_number($self->hand_number + 1);
113 2060 my $order = Game::Dominoes::Set::order_for($self->seed, $self->hand_number);
114 2060 my $size = $self->hand_size;
115  
116 2060 my %hands;
117 2060 my $at = 0;
118 2060 for my $seat ($self->seats) {
119 6068 my @ids = @{$order}[ $at .. $at + $size - 1 ];
120 6068 $at += $size;
121 $hands{$seat} = Game::Dominoes::Hand->new(
122 6068 tiles => [ map { Game::Dominoes::Set::tile_of($_) } @ids ]
123 );
124 }
125  
126 2060 $self->hands(\%hands);
127 $self->boneyard(Game::Dominoes::Boneyard->new(
128 reserve => $self->reserve,
129 2060 tiles => [ map { Game::Dominoes::Set::tile_of($_) } @{$order}[ $at .. $#$order ] ],
130 ));
131 2060 $self->layout(Game::Dominoes::Layout->new);
132 2060 $self->turn($self->leader);
133 2060 $self->passes(0);
134 2060 $self->forced_tile(undef);
135 2060 $self->deductions({});
136  
137 2060 $self->_note('deal', seat => $self->leader);
138 2060 return;
139 }
140  
141 2563 ▶sub hand { return $_[0]->hands->{ $_[1] } }
Subroutine hand: called CC=1 SCAR=0.0
Pod documented
142  
143 2107 ▶sub hand_count {
Subroutine hand_count: called CC=2 SCAR=7.1
Pod documented
144 2107 my ($self, $seat) = @_;
145 2107 ▶ my $hand = $self->hands->{$seat} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $hand = $self->hands->{$seat} or return 0 2107 0 2107
146 2107 return $hand->count;
147 }
148  
149 767 ▶sub boneyard_count { return $_[0]->boneyard->count }
Subroutine boneyard_count: called CC=1 SCAR=0.0
Pod documented
150  
151 762 ▶sub open_ends { return $_[0]->layout->open_ends }
Subroutine open_ends: called CC=1 SCAR=0.0
Pod documented
152  
153 21000 ▶sub count { return Game::Dominoes::Scoring::count($_[0]->layout) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
154  
155 20048 ▶sub legal {
Subroutine legal: called CC=7 SCAR=21.1
Pod documented
156 20048 my ($self, $seat) = @_;
157 20048 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 20048 20048
158 20048 ▶ return [] unless defined $seat && defined $self->turn && $seat == $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and defined $self->turn and $seat == $self->turn 0 20048 20048
Condition33%2/6
defined $seat and defined $self->turn and $seat == $self->turn
!ll&&!rl&&r
0020048
defined $seat and defined $self->turn
!ll&&!rl&&r
0020048
MC/DC0%0/3
defined $seat and defined $self->turn and $seat == $self->turn
defined $seat defined $self->turn $seat == $self->turn
Truth table25%1/4
A and B and C
A= defined $seatB= defined $self->turnC= $seat == $self->turn
ABCresult
0XX0
10X0
1100
1111
159  
160 20048 ▶ my $hand = $self->hands->{$seat} or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $hand = $self->hands->{$seat} or return [] 20048 0 20048
161 20048 my $moves = Game::Dominoes::Rules::candidates($self->layout, $hand);
162  
163 20048 ▶ if (my $forced = $self->forced_tile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $forced = $self->forced_tile) 2232 17816 20048
164 2232 $moves = [ grep { $_->{tile}->id == $forced->id } @$moves ];
165 }
166  
167 20048 return [ map { {
168 kind => 'play',
169 tile => $_->{tile},
170 arm => $_->{arm},
171 points => Game::Dominoes::Rules::score_of(
172 64694 $self->layout, $_->{tile}, $_->{arm}, $self->scale
173 ),
174 } } @$moves ];
175 }
176  
177 762 ▶sub view {
Subroutine view: called CC=7 SCAR=20.8
Pod documented
178 762 my ($self, $seat) = @_;
179 762 ▶ $seat = 'spectator' unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 0 762 762
180 762 ▶ my $own = ($seat ne 'spectator' && $self->hands->{$seat}) ? $self->hands->{$seat} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$seat ne 'spectator' && $self->hands->{$seat} ? : 762 0 762
Condition33%1/3
$seat ne "spectator" and $self->hands->{$seat}
!ll&&!rl&&r
00762
MC/DC0%0/2
$seat ne "spectator" and $self->hands->{$seat}
$seat ne "spectator" $self->hands->{$seat}
Truth table33%1/3
A and B
A= $seat ne "spectator"B= $self->hands->{$seat}
ABresult
0X0
100
111
181  
182 return {
183 seat => $seat,
184 players => $self->players,
185 variant => $self->variant,
186 target => $self->target,
187 scale => $self->scale,
188 status => $self->status,
189 turn => $self->turn,
190 hand_number => $self->hand_number,
191 762 scores => { %{ $self->scores } },
192 layout => $self->layout,
193 ends => [ $self->open_ends ],
194 count => $self->count,
195 2103 counts => { map { $_ => $self->hand_count($_) } $self->seats },
196 boneyard => $self->boneyard_count,
197 2103 ▶ deductions => { map { $_ => { %{ $self->deductions->{$_} || {} } } } $self->seats },
Branch: 1 branch
BranchTrueFalseTotal
$own ? : 762 0 762
Condition100%2/2
$self->deductions->{$_} || {}
l!l
112092
MC/DC100%1/1
$self->deductions->{$_} || {}
$self->deductions->{$_}
Truth table100%2/2
A
A= $self->deductions->{$_}
Aresult
11
00
198 forced => $self->forced_tile,
199 762 ▶ hand => $own ? [ @{ $own->tiles } ] : undef,
Branch: 1 branch
BranchTrueFalseTotal
$own ? : 762 0 762
200 count_own => $own ? $own->count : 0,
201 };
202 }
203  
204 17 ▶sub _error { return Game::Dominoes::Error->throw($_[1], @_[2 .. $#_]) }
Subroutine _error: called CC=1 SCAR=0.0
205  
206 20251 ▶sub _move_of {
Subroutine _move_of: called CC=10 SCAR=23.6
207 20251 my ($self, $move) = @_;
208 20251 ▶ return undef unless defined $move;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $move 1 20250 20251
209  
210 20250 ▶ if (!ref $move) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $move) 24 20226 20250
211 24 my $parsed = eval { Game::Dominoes::Notation::parse_move($move) };
212 24 ▶ return undef unless $parsed && ($parsed->{kind} // '') eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and ($parsed->{"kind"} // "") eq "play" 3 21 24
Condition80%4/5
$parsed and ($parsed->{"kind"} // "") eq "play"
!ll&&!rl&&r
1221
$parsed->{"kind"} // ""
l!l
230
MC/DC50%1/2
$parsed and ($parsed->{"kind"} // "") eq "play"
$parsed $parsed->{"kind"}
Truth table66%2/3
A and B
A= $parsedB= $parsed->{"kind"}
ABresult
0X0
100
111
213 21 return { tile => $parsed->{tile}, arm => $parsed->{arm} };
214 }
215 20226 ▶ if (ref $move eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "HASH") 20226 0 20226
216 20226 my $tile = $move->{tile};
217 20226 ▶ return undef unless defined $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tile 1 20225 20226
218 20225 ▶ $tile = eval { Game::Dominoes::Notation::parse_tile($tile) } unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 20225 20225
219 20225 ▶ return undef unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 20225 20225
220 20225 return { tile => $tile, arm => $move->{arm} };
221 }
222 0 return { tile => $move->tile, arm => $move->arm };
223 }
224  
225 20254 ▶sub play {
Subroutine play: called CC=17 SCAR=28.5
Pod documented
226 20254 my ($self, $seat, $move) = @_;
227  
228 20254 ▶ return $self->_error('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 20253 20254
229 20253 ▶ return $self->_error('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and defined $self->turn and $seat == $self->turn 2 20251 20253
Condition50%3/6
defined $seat and defined $self->turn and $seat == $self->turn
!ll&&!rl&&r
0220251
defined $seat and defined $self->turn
!ll&&!rl&&r
0020253
MC/DC33%1/3
defined $seat and defined $self->turn and $seat == $self->turn
defined $seat defined $self->turn $seat == $self->turn
Truth table50%2/4
A and B and C
A= defined $seatB= defined $self->turnC= $seat == $self->turn
ABCresult
0XX0
10X0
1100
1111
230 unless defined $seat && defined $self->turn && $seat == $self->turn;
231  
232 20251 my $want = $self->_move_of($move);
233 20251 ▶ return $self->_error('bad_move') unless $want && ref $want->{tile};
Branch: 1 branch
BranchTrueFalseTotal
unless $want and ref $want->{"tile"} 5 20246 20251
Condition66%2/3
$want and ref $want->{"tile"}
!ll&&!rl&&r
5020246
MC/DC50%1/2
$want and ref $want->{"tile"}
$want ref $want->{"tile"}
Truth table66%2/3
A and B
A= $wantB= ref $want->{"tile"}
ABresult
0X0
100
111
234  
235 20246 my $hand = $self->hands->{$seat};
236 20246 ▶ return $self->_error('tile_not_held') unless $hand->holds($want->{tile});
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->holds($want->{"tile"}) 3 20243 20246
237  
238 20243 ▶ if (my $forced = $self->forced_tile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $forced = $self->forced_tile) 2256 17987 20243
239 return $self->_error('tile_not_held')
240 2256 ▶ unless $want->{tile}->id == $forced->id;
Branch: 1 branch
BranchTrueFalseTotal
unless $want->{"tile"}->id == $forced->id 0 2256 2256
241 }
242  
243 20243 my $layout = $self->layout;
244 20243 my $arm = $want->{arm};
245 20243 ▶ $arm = ($layout->arms_open)[0] unless defined $arm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm 0 20243 20243
246 return $self->_error('no_such_arm')
247 20243 ▶ unless grep { $_ eq $arm } @Game::Dominoes::Layout::ARMS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} @Game::Dominoes::Layout::ARMS 2 20241 20243
248 return $self->_error('arm_closed')
249 20241 ▶ unless grep { $_ eq $arm } $layout->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} $layout->arms_open 2 20239 20241
250 return $self->_error('end_mismatch')
251 20239 ▶ unless $layout->can_place($want->{tile}, $arm);
Branch: 1 branch
BranchTrueFalseTotal
unless $layout->can_place($want->{"tile"}, $arm) 2 20237 20239
252  
253 20237 $hand->remove($want->{tile});
254 20237 my $play = $layout->place($want->{tile}, $arm);
255 20237 $self->forced_tile(undef);
256 20237 $self->passes(0);
257  
258 20237 my $points = Game::Dominoes::Scoring::score_for($self->count, $self->scale);
259 20237 $play->points($points);
260 20237 ▶ $self->_award($seat, $points) if $points;
Branch: 1 branch
BranchTrueFalseTotal
if $points 3895 16342 20237
261 20237 $self->_note('play', seat => $seat, play => $play, points => $points);
262  
263 20237 ▶ return $play if $self->_check_target;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_target 19 20218 20237
264  
265 20218 ▶ if ($hand->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand->is_empty) 967 19251 20218
266 967 $self->_end_hand('out', $seat);
267 967 return $play;
268 }
269  
270 19251 $self->_advance;
271 19251 return $play;
272 }
273  
274 4927 ▶sub _award {
Subroutine _award: called CC=1 SCAR=0.0
275 4927 my ($self, $seat, $points) = @_;
276 4927 $self->scores->{$seat} += $points;
277 4927 return;
278 }
279  
280 21268 ▶sub _check_target {
Subroutine _check_target: called CC=2 SCAR=6.9
281 21268 my ($self) = @_;
282 21268 my ($high) = sort { $b <=> $a } values %{ $self->scores };
283 21268 ▶ return 0 unless $high >= $self->target;
Branch: 1 branch
BranchTrueFalseTotal
unless $high >= $self->target 21245 23 21268
284 23 $self->_finish('target');
285 23 return 1;
286 }
287  
288 19251 ▶sub _advance {
Subroutine _advance: called CC=9 SCAR=22.0
289 19251 my ($self) = @_;
290  
291 19251 for (1 .. $self->players * 2) {
292 19616 $self->turn($self->turn % $self->players + 1);
293 19616 $self->forced_tile(undef);
294  
295 19616 my $hand = $self->hands->{ $self->turn };
296 19616 ▶ return if Game::Dominoes::Rules::can_play($self->layout, $hand);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Dominoes::Rules::can_play($self->layout, $hand) 16927 2689 19616
297  
298 2689 my $drawn = 0;
299 2689 ▶ while ($self->boneyard->can_draw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->boneyard->can_draw) 3756 429 4185
300 3756 my $tile = $self->boneyard->draw;
301 3756 $hand->add($tile);
302 3756 $drawn++;
303 3756 ▶ next unless Game::Dominoes::Rules::can_play($self->layout, $hand);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Dominoes::Rules::can_play($self->layout, $hand) 1496 2260 3756
304 2260 $self->forced_tile($tile);
305 2260 last;
306 }
307 2689 ▶ $self->_note('draw', seat => $self->turn, count => $drawn) if $drawn;
Branch: 1 branch
BranchTrueFalseTotal
if $drawn 2327 362 2689
308 2689 ▶ return if $self->forced_tile;
Branch: 1 branch
BranchTrueFalseTotal
if $self->forced_tile 2260 429 2689
309  
310 429 my @shut = $self->layout->open_ends;
311 429 $self->deductions->{ $self->turn }{$_} = 1 for @shut;
312  
313 429 $self->_note('pass', seat => $self->turn, cannot => [ sort { $a <=> $b } @shut ]);
314 429 $self->passes($self->passes + 1);
315 429 ▶ if ($self->passes >= $self->players) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->passes >= $self->players) 64 365 429
316 64 $self->_end_hand('blocked');
317 64 return;
318 }
319 }
320 0 return;
321 }
322  
323 1031 ▶sub _end_hand {
Subroutine _end_hand: called CC=8 SCAR=20.8
324 1031 my ($self, $reason, $out) = @_;
325  
326 1031 my %pips = map { $_ => $self->hands->{$_}->pips } $self->seats;
327  
328 1031 ▶ if ($reason eq 'out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason eq 'out') { } 967 64 1031
329 967 my $total = 0;
330 967 $total += $pips{$_} for grep { $_ != $out } $self->seats;
331 967 my $points = Game::Dominoes::Scoring::bonus($total, $self->scale);
332 967 $self->_award($out, $points);
333 967 $self->_note('hand_end', reason => 'out', seat => $out,
334 points => $points, pips => \%pips);
335 967 $self->leader($out);
336 }
337 else {
338 64 my ($low) = sort { $a <=> $b } values %pips;
339 64 my @light = sort { $a <=> $b } grep { $pips{$_} == $low } $self->seats;
340  
341 64 ▶ if (@light == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@light == 1) { } 61 3 64
elsif (@light == 2 and $self->players == 3) { } 2 1 3
342 61 my $total = 0;
343 61 $total += $pips{$_} for grep { $_ != $light[0] } $self->seats;
344 61 my $points = Game::Dominoes::Scoring::bonus($total, $self->scale);
345 61 $self->_award($light[0], $points);
346 61 $self->_note('hand_end', reason => 'blocked', seat => $light[0],
347 points => $points, pips => \%pips);
348 }
349 ▶ elsif (@light == 2 && $self->players == 3) {
Condition66%2/3
@light == 2 and $self->players == 3
!ll&&!rl&&r
012
MC/DC50%1/2
@light == 2 and $self->players == 3
@light == 2 $self->players == 3
Truth table66%2/3
A and B
A= @light == 2B= $self->players == 3
ABresult
0X0
100
111
350 2 my ($third) = grep { $pips{$_} != $low } $self->seats;
351 2 my $points = Game::Dominoes::Scoring::bonus($pips{$third}, $self->scale);
352 2 my $each = int($points / 2);
353 2 $self->_award($light[0], $points - $each);
354 2 $self->_award($light[1], $each);
355 2 $self->_note('hand_end', reason => 'blocked_split',
356 seats => \@light, points => $points, pips => \%pips);
357 }
358 else {
359 1 $self->_note('hand_end', reason => 'blocked_tied',
360 seats => \@light, points => 0, pips => \%pips);
361 }
362 64 $self->leader($self->_by_lot($self->hand_number + 1));
363 }
364  
365 1031 ▶ return if $self->_check_target;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_target 4 1027 1031
366 1027 $self->_deal;
367 1027 return;
368 }
369  
370 24 ▶sub _finish {
Subroutine _finish: called CC=1 SCAR=0.0
371 24 my ($self, $reason) = @_;
372 24 $self->status('finished');
373 24 $self->turn(undef);
374 $self->result(Game::Dominoes::Result->new(
375 places => $self->places,
376 24 scores => { %{ $self->scores } },
377 reason => $reason,
378 ));
379 24 $self->_note('game_end', reason => $reason);
380 24 return;
381 }
382  
383 27 ▶sub places {
Subroutine places: called CC=4 SCAR=13.9
Pod documented
384 27 my ($self) = @_;
385 27 my $scores = $self->scores;
386 27 my @order = sort { $scores->{$b} <=> $scores->{$a} } $self->seats;
387  
388 27 my (%place, $last, $rank);
389 27 my $seen = 0;
390 27 for my $seat (@order) {
391 73 $seen++;
392 73 ▶ if (!defined $last || $scores->{$seat} != $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last or $scores->{$seat} != $last) 72 1 73
Condition100%3/3
not defined $last or $scores->{$seat} != $last
l!l&&r!l&&!r
27451
MC/DC100%2/2
not defined $last or $scores->{$seat} != $last
not defined $last $scores->{$seat} != $last
Truth table100%3/3
not(A) or B
A= not defined $lastB= $scores->{$seat} != $last
ABresult
1X1
011
000
393 72 $rank = $seen;
394 72 $last = $scores->{$seat};
395 }
396 73 $place{$seat} = $rank;
397 }
398 27 return \%place;
399 }
400  
401 1 ▶sub resign {
Subroutine resign: called CC=5 SCAR=17.4
Pod documented
402 1 my ($self, $seat) = @_;
403 1 ▶ return $self->_error('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 1 1
404 return $self->_error('not_your_turn')
405 1 ▶ unless defined $seat && $self->hands->{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $self->hands->{$seat} 0 1 1
Condition33%1/3
defined $seat and $self->hands->{$seat}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $seat and $self->hands->{$seat}
defined $seat $self->hands->{$seat}
Truth table33%1/3
A and B
A= defined $seatB= $self->hands->{$seat}
ABresult
0X0
100
111
406  
407 1 my $scores = $self->scores;
408 1 my ($high) = sort { $b <=> $a } map { $scores->{$_} } grep { $_ != $seat } $self->seats;
409 1 ▶ $scores->{$seat} = -1 if $scores->{$seat} >= $high;
Branch: 1 branch
BranchTrueFalseTotal
if $scores->{$seat} >= $high 1 0 1
410 1 $self->_finish('resign');
411 1 return $self->result;
412 }
413  
414 2 ▶sub to_text {
Subroutine to_text: called CC=6 SCAR=17.9
Pod documented
415 2 my ($self) = @_;
416 2 my @moves;
417 2 for my $event (@{ $self->history }) {
418 124 ▶ push @moves, $event->{play} if $event->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "play" 92 32 124
419 push @moves, { kind => 'draw', count => $event->{count} }
420 124 ▶ if $event->{kind} eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "draw" 16 108 124
421 124 ▶ push @moves, { kind => 'pass' } if $event->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "pass" 4 120 124
422 124 ▶ push @moves, { kind => 'hand_end' } if $event->{kind} eq 'hand_end';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "hand_end" 4 120 124
423 }
424 2 return Game::Dominoes::Notation::to_text(\@moves);
425 }
426  
427 12 1;
428  
429 __END__
« Dominoes/Bot.pm ↑ Summary Dominoes/Layout.pm »