blib/lib/Game/Dominoes.pm

filter: Statementstmt 98.6% 358 / 363 Branchbran 85.1% 143 / 168 Conditioncond 63.4% 33 / 52 MC/DCmcdc 45.1% 14 / 31 Subroutinesub 100% 41 / 41 Podpod 93.3% 14 / 15 totaltotal 90.0% 603 / 670 CC 113 SCAR 24.4
CC
113
Cov
92%
from_text
26.3
play
17.2
BUILD
15.7
CRAP 11.5
« blib/lib/Game/Dominoes/Terminal.pm ↑ Summary blib/lib/Game/Dominoes/Bot.pm »
1 package Game::Dominoes;
2  
3 38 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 38 ▶use Game::Dominoes::Tile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 38 ▶use Game::Dominoes::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use Game::Dominoes::Hand;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use Game::Dominoes::Boneyard;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 38 ▶use Game::Dominoes::Play;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 38 ▶use Game::Dominoes::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 38 ▶use Game::Dominoes::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 38 ▶use Game::Dominoes::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 38 ▶use Game::Dominoes::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 38 ▶use Game::Dominoes::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 38 ▶use Game::Dominoes::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 38 our $VERSION = '0.05';
23  
24 38 our %HAND_SIZE = (2 => 9, 3 => 7, 4 => 5);
25  
26 38 our %TARGET = (2 => 250, 3 => 200, 4 => 200);
27  
28 38 our @VARIANTS = qw/all_fives draw block/;
29  
30 38 has seed => (is => 'ro', isa => Str);
31 38 has players => (is => 'ro', isa => Int, default => 2);
32 38 has variant => (is => 'ro', isa => Str, default => 'all_fives');
33 38 has target => (is => 'rw', isa => Int);
34 38 has scale => (is => 'ro', isa => Int, default => 1);
35 38 has reserve => (is => 'ro', isa => Int, default => 0);
36  
37 38 has status => (is => 'rw', isa => Str, default => 'active');
38 38 has hand_number => (is => 'rw', isa => Int, default => 0);
39 38 has turn => (is => 'rw', isa => Int);
40 38 has hands => (is => 'rw', isa => HashRef, default => {});
41 38 has scores => (is => 'rw', isa => HashRef, default => {});
42 38 has boneyard => (is => 'rw', isa => Object);
43 38 has layout => (is => 'rw', isa => Object);
44 38 has result => (is => 'rw', isa => Object);
45 38 has history => (is => 'rw', isa => ArrayRef, default => []);
46  
47 38 has leader => (is => 'rw', isa => Int);
48 38 has forced_tile => (is => 'rw', isa => Object);
49  
50 38 has passes => (is => 'rw', isa => Int, default => 0);
51  
52 38 has deductions => (is => 'rw', isa => HashRef, default => {});
53  
54 1201 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=27.5
Pod undocumented
55 1201 my ($self) = @_;
56  
57 1201 ▶ die 'Game::Dominoes: seed must be 32 bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->seed and length $self->seed == 32 2 1199 1201
Condition100%3/3
defined $self->seed and length $self->seed == 32
!ll&&!rl&&r
111199
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 1199 ▶ unless $HAND_SIZE{ $self->players };
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Dominoes::HAND_SIZE{$self->players} 2 1197 1199
61 die 'Game::Dominoes: unknown variant ' . $self->variant
62 1197 ▶ unless grep { $_ eq $self->variant } @VARIANTS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $self->variant;} @Game::Dominoes::VARIANTS 1 1196 1197
63 1196 ▶ die 'Game::Dominoes: scale must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->scale >= 1 0 1196 1196
64 unless $self->scale >= 1;
65  
66 1196 ▶ $self->target($TARGET{ $self->players } / $self->scale)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->target 1142 54 1196
67 unless defined $self->target;
68 1196 ▶ die 'Game::Dominoes: target must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->target > 0 1 1195 1196
69 unless $self->target > 0;
70  
71 3423 $self->scores({ map { $_ => 0 } $self->seats })
72 1195 ▶ unless keys %{ $self->scores };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self->scores;} 1195 0 1195
73 1195 ▶ $self->leader($self->_by_lot(1)) unless defined $self->leader;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->leader 1195 0 1195
74  
75 1195 ▶ if (keys %{ $self->hands }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->hands;}) { } 85 1110 1195
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 1110 $self->_deal;
84 }
85 1195 return $self;
86 }
87  
88 8881 ▶sub seats { return 1 .. $_[0]->players }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
89  
90 2175 ▶sub hand_size { return $HAND_SIZE{ $_[0]->players } }
Subroutine hand_size: called CC=1 SCAR=0.0
Pod documented
91  
92 1259 ▶sub _by_lot {
Subroutine _by_lot: called CC=2 SCAR=7.1
93 1259 my ($self, $n) = @_;
94 1259 my $word = unpack 'N', Digest::SHA::sha256($self->seed . "lot:$n");
95 1259 my $limit = int(4294967296 / $self->players) * $self->players;
96 1259 my $i = 0;
97 1259 ▶ while ($word >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word >= $limit) 0 1259 1259
98 0 $word = unpack 'N', Digest::SHA::sha256($self->seed . "lot:$n:" . ++$i);
99 }
100 1259 return ($word % $self->players) + 1;
101 }
102  
103 27447 ▶sub _note {
Subroutine _note: called CC=1 SCAR=0.0
104 27447 my ($self, $kind, %rest) = @_;
105 27447 push @{ $self->history }, { kind => $kind, hand => $self->hand_number, %rest };
106 27447 return;
107 }
108  
109 2172 ▶sub _deal {
Subroutine _deal: called CC=2 SCAR=6.9
110 2172 my ($self) = @_;
111  
112 2172 $self->hand_number($self->hand_number + 1);
113 2172 my $order = Game::Dominoes::Set::order_for($self->seed, $self->hand_number);
114 2172 my $size = $self->hand_size;
115  
116 2172 my %hands;
117 2172 my $at = 0;
118 2172 for my $seat ($self->seats) {
119 6372 my @ids = @{$order}[ $at .. $at + $size - 1 ];
120 6372 $at += $size;
121 $hands{$seat} = Game::Dominoes::Hand->new(
122 6372 tiles => [ map { Game::Dominoes::Set::tile_of($_) } @ids ]
123 );
124 }
125  
126 2172 $self->hands(\%hands);
127 $self->boneyard(Game::Dominoes::Boneyard->new(
128 reserve => $self->reserve,
129 2172 tiles => [ map { Game::Dominoes::Set::tile_of($_) } @{$order}[ $at .. $#$order ] ],
130 ));
131 2172 $self->layout(Game::Dominoes::Layout->new);
132 2172 $self->turn($self->leader);
133 2172 $self->passes(0);
134 2172 $self->forced_tile(undef);
135 2172 $self->deductions({});
136  
137 2172 $self->_note('deal', seat => $self->leader);
138 2172 return;
139 }
140  
141 2593 ▶sub hand { return $_[0]->hands->{ $_[1] } }
Subroutine hand: called CC=1 SCAR=0.0
Pod documented
142  
143 3075 ▶sub hand_count {
Subroutine hand_count: called CC=2 SCAR=7.1
Pod documented
144 3075 my ($self, $seat) = @_;
145 3075 ▶ my $hand = $self->hands->{$seat} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $hand = $self->hands->{$seat} or return 0 3075 0 3075
146 3075 return $hand->count;
147 }
148  
149 1151 ▶sub boneyard_count { return $_[0]->boneyard->count }
Subroutine boneyard_count: called CC=1 SCAR=0.0
Pod documented
150  
151 1146 ▶sub open_ends { return $_[0]->layout->open_ends }
Subroutine open_ends: called CC=1 SCAR=0.0
Pod documented
152  
153 22369 ▶sub count { return Game::Dominoes::Scoring::count($_[0]->layout) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
154  
155 20197 ▶sub legal {
Subroutine legal: called CC=7 SCAR=21.1
Pod documented
156 20197 my ($self, $seat) = @_;
157 20197 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 20197 20197
158 20197 ▶ 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 20197 20197
Condition33%2/6
defined $seat and defined $self->turn and $seat == $self->turn
!ll&&!rl&&r
0020197
defined $seat and defined $self->turn
!ll&&!rl&&r
0020197
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 20197 ▶ my $hand = $self->hands->{$seat} or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $hand = $self->hands->{$seat} or return [] 20197 0 20197
161 20197 my $moves = Game::Dominoes::Rules::candidates($self->layout, $hand);
162  
163 20197 ▶ if (my $forced = $self->forced_tile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $forced = $self->forced_tile) 2242 17955 20197
164 2242 $moves = [ grep { $_->{tile}->id == $forced->id } @$moves ];
165 }
166  
167 20197 return [ map { {
168 kind => 'play',
169 tile => $_->{tile},
170 arm => $_->{arm},
171 points => Game::Dominoes::Rules::score_of(
172 65359 $self->layout, $_->{tile}, $_->{arm}, $self->scale
173 ),
174 } } @$moves ];
175 }
176  
177 1146 ▶sub view {
Subroutine view: called CC=7 SCAR=20.8
Pod documented
178 1146 my ($self, $seat) = @_;
179 1146 ▶ $seat = 'spectator' unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 0 1146 1146
180 1146 ▶ my $own = ($seat ne 'spectator' && $self->hands->{$seat}) ? $self->hands->{$seat} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$seat ne 'spectator' && $self->hands->{$seat} ? : 1146 0 1146
Condition33%1/3
$seat ne "spectator" and $self->hands->{$seat}
!ll&&!rl&&r
001146
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 1146 scores => { %{ $self->scores } },
192 layout => $self->layout,
193 ends => [ $self->open_ends ],
194 count => $self->count,
195 3071 counts => { map { $_ => $self->hand_count($_) } $self->seats },
196 boneyard => $self->boneyard_count,
197 3071 ▶ deductions => { map { $_ => { %{ $self->deductions->{$_} || {} } } } $self->seats },
Branch: 1 branch
BranchTrueFalseTotal
$own ? : 1146 0 1146
Condition100%2/2
$self->deductions->{$_} || {}
l!l
683003
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 1146 ▶ hand => $own ? [ @{ $own->tiles } ] : undef,
Branch: 1 branch
BranchTrueFalseTotal
$own ? : 1146 0 1146
200 count_own => $own ? $own->count : 0,
201 };
202 }
203  
204 18 ▶sub _error { return Game::Dominoes::Error->throw($_[1], @_[2 .. $#_]) }
Subroutine _error: called CC=1 SCAR=0.0
205  
206 21237 ▶sub _move_of {
Subroutine _move_of: called CC=10 SCAR=23.6
207 21237 my ($self, $move) = @_;
208 21237 ▶ return undef unless defined $move;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $move 1 21236 21237
209  
210 21236 ▶ if (!ref $move) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $move) 24 21212 21236
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 21212 ▶ if (ref $move eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "HASH") 21212 0 21212
216 21212 my $tile = $move->{tile};
217 21212 ▶ return undef unless defined $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tile 1 21211 21212
218 0 ▶ $tile = eval { Game::Dominoes::Notation::parse_tile($tile) } unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 21211 21211
219 21211 ▶ return undef unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 21211 21211
220 21211 return { tile => $tile, arm => $move->{arm} };
221 }
222 0 return { tile => $move->tile, arm => $move->arm };
223 }
224  
225 21240 ▶sub play {
Subroutine play: called CC=17 SCAR=28.5
Pod documented
226 21240 my ($self, $seat, $move) = @_;
227  
228 21240 ▶ return $self->_error('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 21239 21240
229 21239 ▶ return $self->_error('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and defined $self->turn and $seat == $self->turn 2 21237 21239
Condition50%3/6
defined $seat and defined $self->turn and $seat == $self->turn
!ll&&!rl&&r
0221237
defined $seat and defined $self->turn
!ll&&!rl&&r
0021239
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 21237 my $want = $self->_move_of($move);
233 21237 ▶ return $self->_error('bad_move') unless $want && ref $want->{tile};
Branch: 1 branch
BranchTrueFalseTotal
unless $want and ref $want->{"tile"} 5 21232 21237
Condition66%2/3
$want and ref $want->{"tile"}
!ll&&!rl&&r
5021232
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 21232 my $hand = $self->hands->{$seat};
236 21232 ▶ return $self->_error('tile_not_held') unless $hand->holds($want->{tile});
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->holds($want->{"tile"}) 4 21228 21232
237  
238 21228 ▶ if (my $forced = $self->forced_tile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $forced = $self->forced_tile) 2312 18916 21228
239 return $self->_error('tile_not_held')
240 2312 ▶ unless $want->{tile}->id == $forced->id;
Branch: 1 branch
BranchTrueFalseTotal
unless $want->{"tile"}->id == $forced->id 0 2312 2312
241 }
242  
243 21228 my $layout = $self->layout;
244 21228 my $arm = $want->{arm};
245 21228 ▶ $arm = ($layout->arms_open)[0] unless defined $arm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm 0 21228 21228
246 return $self->_error('no_such_arm')
247 21228 ▶ unless grep { $_ eq $arm } @Game::Dominoes::Layout::ARMS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} @Game::Dominoes::Layout::ARMS 2 21226 21228
248 return $self->_error('arm_closed')
249 21226 ▶ unless grep { $_ eq $arm } $layout->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} $layout->arms_open 2 21224 21226
250 return $self->_error('end_mismatch')
251 21224 ▶ unless $layout->can_place($want->{tile}, $arm);
Branch: 1 branch
BranchTrueFalseTotal
unless $layout->can_place($want->{"tile"}, $arm) 2 21222 21224
252  
253 21222 $hand->remove($want->{tile});
254 21222 my $play = $layout->place($want->{tile}, $arm);
255 21222 $self->forced_tile(undef);
256 21222 $self->passes(0);
257  
258 21222 my $points = Game::Dominoes::Scoring::score_for($self->count, $self->scale);
259 21222 $play->points($points);
260 21222 ▶ $self->_award($seat, $points) if $points;
Branch: 1 branch
BranchTrueFalseTotal
if $points 4156 17066 21222
261 21222 $self->_note('play', seat => $seat, play => $play, points => $points);
262  
263 21222 ▶ return $play if $self->_check_target;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_target 27 21195 21222
264  
265 21195 ▶ if ($hand->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand->is_empty) 1002 20193 21195
266 1002 $self->_end_hand('out', $seat);
267 1002 return $play;
268 }
269  
270 20193 $self->_advance;
271 20193 return $play;
272 }
273  
274 5223 ▶sub _award {
Subroutine _award: called CC=1 SCAR=0.0
275 5223 my ($self, $seat, $points) = @_;
276 5223 $self->scores->{$seat} += $points;
277 5223 return;
278 }
279  
280 22288 ▶sub _check_target {
Subroutine _check_target: called CC=2 SCAR=6.9
281 22288 my ($self) = @_;
282 22288 my ($high) = sort { $b <=> $a } values %{ $self->scores };
283 22288 ▶ return 0 unless $high >= $self->target;
Branch: 1 branch
BranchTrueFalseTotal
unless $high >= $self->target 22257 31 22288
284 31 $self->_finish('target');
285 31 return 1;
286 }
287  
288 20193 ▶sub _advance {
Subroutine _advance: called CC=9 SCAR=22.0
289 20193 my ($self) = @_;
290  
291 20193 for (1 .. $self->players * 2) {
292 20670 $self->turn($self->turn % $self->players + 1);
293 20670 $self->forced_tile(undef);
294  
295 20670 my $hand = $self->hands->{ $self->turn };
296 20670 ▶ return if Game::Dominoes::Rules::can_play($self->layout, $hand);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Dominoes::Rules::can_play($self->layout, $hand) 17807 2863 20670
297  
298 2863 my $drawn = 0;
299 2863 ▶ while ($self->boneyard->can_draw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->boneyard->can_draw) 3943 541 4484
300 3943 my $tile = $self->boneyard->draw;
301 3943 $hand->add($tile);
302 3943 $drawn++;
303 3943 ▶ next unless Game::Dominoes::Rules::can_play($self->layout, $hand);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Dominoes::Rules::can_play($self->layout, $hand) 1621 2322 3943
304 2322 $self->forced_tile($tile);
305 2322 last;
306 }
307 2863 ▶ $self->_note('draw', seat => $self->turn, count => $drawn) if $drawn;
Branch: 1 branch
BranchTrueFalseTotal
if $drawn 2410 453 2863
308 2863 ▶ return if $self->forced_tile;
Branch: 1 branch
BranchTrueFalseTotal
if $self->forced_tile 2322 541 2863
309  
310 541 my @shut = $self->layout->open_ends;
311 541 $self->deductions->{ $self->turn }{$_} = 1 for @shut;
312  
313 541 $self->_note('pass', seat => $self->turn, cannot => [ sort { $a <=> $b } @shut ]);
314 541 $self->passes($self->passes + 1);
315 541 ▶ if ($self->passes >= $self->players) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->passes >= $self->players) 64 477 541
316 64 $self->_end_hand('blocked');
317 64 return;
318 }
319 }
320 0 return;
321 }
322  
323 1066 ▶sub _end_hand {
Subroutine _end_hand: called CC=8 SCAR=20.8
324 1066 my ($self, $reason, $out) = @_;
325  
326 1066 my %pips = map { $_ => $self->hands->{$_}->pips } $self->seats;
327  
328 1066 ▶ if ($reason eq 'out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason eq 'out') { } 1002 64 1066
329 1002 my $total = 0;
330 1002 $total += $pips{$_} for grep { $_ != $out } $self->seats;
331 1002 my $points = Game::Dominoes::Scoring::bonus($total, $self->scale);
332 1002 $self->_award($out, $points);
333 1002 $self->_note('hand_end', reason => 'out', seat => $out,
334 points => $points, pips => \%pips);
335 1002 $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 1066 ▶ return if $self->_check_target;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_target 4 1062 1066
366 1062 $self->_deal;
367 1062 return;
368 }
369  
370 36 ▶sub _finish {
Subroutine _finish: called CC=2 SCAR=6.9
371 36 my ($self, $reason, $seat) = @_;
372 36 $self->status('finished');
373 36 $self->turn(undef);
374 $self->result(Game::Dominoes::Result->new(
375 places => $self->places,
376 36 scores => { %{ $self->scores } },
377 reason => $reason,
378 ));
379 36 ▶ $self->_note('game_end', reason => $reason, (defined $seat ? (seat => $seat) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $seat ? : 5 31 36
380 36 return;
381 }
382  
383 39 ▶sub places {
Subroutine places: called CC=4 SCAR=13.9
Pod documented
384 39 my ($self) = @_;
385 39 my $scores = $self->scores;
386 39 my @order = sort { $scores->{$b} <=> $scores->{$a} } $self->seats;
387  
388 39 my (%place, $last, $rank);
389 39 my $seen = 0;
390 39 for my $seat (@order) {
391 111 $seen++;
392 111 ▶ if (!defined $last || $scores->{$seat} != $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last or $scores->{$seat} != $last) 110 1 111
Condition100%3/3
not defined $last or $scores->{$seat} != $last
l!l&&r!l&&!r
39711
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 110 $rank = $seen;
394 110 $last = $scores->{$seat};
395 }
396 111 $place{$seat} = $rank;
397 }
398 39 return \%place;
399 }
400  
401 5 ▶sub resign {
Subroutine resign: called CC=5 SCAR=16.8
Pod documented
402 5 my ($self, $seat) = @_;
403 5 ▶ return $self->_error('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 5 5
404 return $self->_error('not_your_turn')
405 5 ▶ unless defined $seat && $self->hands->{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $self->hands->{$seat} 0 5 5
Condition33%1/3
defined $seat and $self->hands->{$seat}
!ll&&!rl&&r
005
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 5 my $scores = $self->scores;
408 0 my ($high) = sort { $b <=> $a } map { $scores->{$_} } grep { $_ != $seat } $self->seats;
409 5 ▶ $scores->{$seat} = -1 if $scores->{$seat} >= $high;
Branch: 1 branch
BranchTrueFalseTotal
if $scores->{$seat} >= $high 1 4 5
410 5 $self->_finish('resign', $seat);
411 5 return $self->result;
412 }
413  
414 38 our @HEADER = qw/seed players variant target scale reserve resigned/;
415  
416 406 ▶sub to_text {
Subroutine to_text: called CC=10 SCAR=23.0
Pod documented
417 406 my ($self, %opt) = @_;
418 406 my (@moves, $resigned);
419 406 for my $event (@{ $self->history }) {
420 3601 ▶ push @moves, $event->{play} if $event->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "play" 2567 1034 3601
421 push @moves, { kind => 'draw', count => $event->{count} }
422 3601 ▶ if $event->{kind} eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "draw" 210 3391 3601
423 3601 ▶ push @moves, { kind => 'pass' } if $event->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "pass" 244 3357 3601
424 3601 ▶ push @moves, { kind => 'hand_end' } if $event->{kind} eq 'hand_end';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "hand_end" 76 3525 3601
425 $resigned = $event->{seat}
426 3601 ▶ if $event->{kind} eq 'game_end' && $event->{reason} eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "game_end" and $event->{"reason"} eq "resign" 5 3596 3601
Condition100%3/3
$event->{"kind"} eq "game_end" and $event->{"reason"} eq "resign"
!ll&&!rl&&r
3579175
MC/DC100%2/2
$event->{"kind"} eq "game_end" and $event->{"reason"} eq "resign"
$event->{"kind"} eq "game_end" $event->{"reason"} eq "resign"
Truth table100%3/3
A and B
A= $event->{"kind"} eq "game_end"B= $event->{"reason"} eq "resign"
ABresult
0X0
100
111
427 }
428 406 my $log = Game::Dominoes::Notation::to_text(\@moves);
429 406 ▶ return $log unless $opt{with_seed};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"with_seed"} 44 362 406
430  
431 my @lines = (
432 'seed ' . unpack('H*', $self->seed),
433 362 ▶ map({ "$_ " . $self->$_ } qw/players variant target scale reserve/),
Branch: 1 branch
BranchTrueFalseTotal
defined $resigned ? : 5 357 362
434 (defined $resigned ? "resigned $resigned" : ()),
435 "moves $log",
436 );
437 362 return join "\n", @lines;
438 }
439  
440 50 ▶sub from_text {
Subroutine from_text: called CC=26 SCAR=32.7
Pod documented
441 50 my ($class, $text, %args) = @_;
442  
443 50 ▶ die 'Game::Dominoes: from_text wants the text of a game'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 49 50
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1049
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
444 unless defined $text && !ref $text;
445  
446 49 my %known = map { $_ => 1 } @HEADER, 'moves';
447 49 my @lines = grep { /\S/ } split /\r?\n/, $text;
448 49 my (%field, $log);
449  
450 49 ▶ if (grep { /\A\s*(\w+)/ && $known{$1} } @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\A\s*(\w+)/ and $known{$1};} @lines) { } 47 2 49
Condition66%2/3
/\A\s*(\w+)/ && $known{$1}
!ll&&!rl&&r
03332
MC/DC50%1/2
/\A\s*(\w+)/ && $known{$1}
/\A\s*(\w+)/ $known{$1}
Truth table66%2/3
A && B
A= /\A\s*(\w+)/B= $known{$1}
ABresult
0X0
100
111
451 47 for my $line (@lines) {
452 321 $line =~ s/\A\s+//;
453 321 $line =~ s/\s+\z//;
454 321 my ($key, $value) = $line =~ /\A(\w+)(?:\s+(.*))?\z/;
455 die "Game::Dominoes: from_text does not know the line '$line'"
456 321 ▶ unless defined $key && $known{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and $known{$key} 1 320 321
Condition66%2/3
defined $key and $known{$key}
!ll&&!rl&&r
01320
MC/DC50%1/2
defined $key and $known{$key}
defined $key $known{$key}
Truth table66%2/3
A and B
A= defined $keyB= $known{$key}
ABresult
0X0
100
111
457 die "Game::Dominoes: from_text found two $key lines"
458 320 ▶ if exists $field{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $field{$key} 1 319 320
459 319 ▶ $field{$key} = defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 310 9 319
460 }
461 die 'Game::Dominoes: from_text found no moves line'
462 45 ▶ unless exists $field{moves};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $field{"moves"} 1 44 45
463 44 $log = delete $field{moves};
464  
465 44 ▶ if (exists $field{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field{"seed"}) 44 0 44
466 die 'Game::Dominoes: the seed must be 64 hexadecimal digits'
467 44 ▶ unless $field{seed} =~ /\A[0-9a-fA-F]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field{"seed"} =~ /\A[0-9a-fA-F]{64}\z/ 1 43 44
468 43 $field{seed} = pack 'H*', $field{seed};
469 }
470 43 for my $key (qw/players target scale reserve resigned/) {
471 211 ▶ next unless exists $field{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $field{$key} 39 172 211
472 die "Game::Dominoes: $key must be a whole number, not '$field{$key}'"
473 172 ▶ unless $field{$key} =~ /\A-?\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field{$key} =~ /\A-?\d+\z/ 1 171 172
474 }
475 }
476 else {
477 2 $log = $text;
478 }
479  
480 44 %field = (%args, %field);
481 44 my $resigned = delete $field{resigned};
482 die 'Game::Dominoes: from_text needs the seed, in the text or as an argument'
483 44 ▶ unless defined $field{seed};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field{"seed"} 1 43 44
484  
485 43 my $moves = eval { Game::Dominoes::Notation::from_text($log) };
486 43 ▶ unless ($moves) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($moves) 1 42 43
487 1 (my $why = $@) =~ s/ at \S+ line \d+\.?\n\z//;
488 1 die "Game::Dominoes: from_text could not read the moves: $why";
489 }
490  
491 42 my $game = $class->new(%field);
492 42 my $n = 0;
493 42 for my $move (@$moves) {
494 843 $n++;
495 843 ▶ next unless $move->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless $move->{"kind"} eq "play" 182 661 843
496 661 my $token = Game::Dominoes::Notation::move_text($move);
497 661 ▶ die "Game::Dominoes: move $n, $token, comes after the game is over"
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" 1 660 661
498 unless $game->status eq 'active';
499 660 my $out = $game->play($game->turn, { tile => $move->{tile}, arm => $move->{arm} });
500 660 ▶ die "Game::Dominoes: move $n, $token, was refused: " . $out->message
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Dominoes::Error" 1 659 660
501 if ref $out eq 'Game::Dominoes::Error';
502 }
503  
504 40 ▶ die 'Game::Dominoes: the draws, passes and ends of hands in the text are not the ones its plays make'
Branch: 1 branch
BranchTrueFalseTotal
unless $game->to_text eq Game::Dominoes::Notation::to_text($moves) 1 39 40
505 unless $game->to_text eq Game::Dominoes::Notation::to_text($moves);
506  
507 39 ▶ if (defined $resigned) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resigned) 3 36 39
508 die "Game::Dominoes: seat $resigned cannot resign here"
509 3 ▶ unless $game->status eq 'active' && $game->hands->{$resigned};
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "active" and $game->hands->{$resigned} 0 3 3
Condition33%1/3
$game->status eq "active" and $game->hands->{$resigned}
!ll&&!rl&&r
003
MC/DC0%0/2
$game->status eq "active" and $game->hands->{$resigned}
$game->status eq "active" $game->hands->{$resigned}
Truth table33%1/3
A and B
A= $game->status eq "active"B= $game->hands->{$resigned}
ABresult
0X0
100
111
510 3 $game->resign($resigned);
511 }
512  
513 39 return $game;
514 }
515  
516 38 1;
517  
518 __END__
« blib/lib/Game/Dominoes/Terminal.pm ↑ Summary blib/lib/Game/Dominoes/Bot.pm »