blib/lib/Game/Dominoes/Layout.pm

filter: Statementstmt 100% 115 / 115 Branchbran 94.2% 66 / 70 Conditioncond 77.7% 14 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 17 / 17 Podpod 100% 13 / 13 totaltotal 95.0% 232 / 244 CC 44 SCAR 19.9
CC
44
Cov
96%
place
15.0
face_of
8.0
ends
8.0
CRAP 7.3
« blib/lib/Game/Dominoes/Bot.pm ↑ Summary blib/lib/Game/Dominoes/Notation.pm »
1 package Game::Dominoes::Layout;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 41 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 41 ▶use Game::Dominoes::Play;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 41 our $VERSION = '0.05';
11  
12 41 our @ARMS = qw/L R U D/;
13  
14 has line => (
15 is => 'rw',
16 isa => ArrayRef,
17 default => []
18 41 );
19  
20 has spinner_index => (
21 is => 'rw',
22 isa => Int
23 41 );
24  
25 has u => (
26 is => 'rw',
27 isa => ArrayRef,
28 default => []
29 41 );
30  
31 has d => (
32 is => 'rw',
33 isa => ArrayRef,
34 default => []
35 41 );
36  
37 44000094 ▶sub is_empty {
Subroutine is_empty: called CC=2 SCAR=6.9
Pod documented
38 44000094 ▶ return scalar @{ $_[0]->line } ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->line;} ? : 43867907 132187 44000094
39 }
40  
41 443 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
42 443 my ($self) = @_;
43 443 return scalar(@{ $self->line }) + scalar(@{ $self->u }) + scalar(@{ $self->d });
44 }
45  
46 10580 ▶sub tiles {
Subroutine tiles: called CC=1 SCAR=0.0
Pod documented
47 10580 my ($self) = @_;
48 10580 return [ map { $_->{tile} } @{ $self->line }, @{ $self->u }, @{ $self->d } ];
49 }
50  
51 886 ▶sub pips {
Subroutine pips: called CC=2 SCAR=6.9
Pod documented
52 886 my ($self) = @_;
53 886 my $total = 0;
54 886 $total += $_->pips for @{ $self->tiles };
55 886 return $total;
56 }
57  
58 2332158 ▶sub spinner {
Subroutine spinner: called CC=2 SCAR=6.9
Pod documented
59 2332158 my ($self) = @_;
60 2332158 my $i = $self->spinner_index;
61 2332158 ▶ return undef unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 2373 2329785 2332158
62 2329785 return $self->line->[$i]{tile};
63 }
64  
65 16980006 ▶sub sides_covered {
Subroutine sides_covered: called CC=4 SCAR=13.9
Pod documented
66 16980006 my ($self) = @_;
67 16980006 my $i = $self->spinner_index;
68 16980006 ▶ return 0 unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 1 16980005 16980006
69 16980005 my $last = $#{ $self->line };
70 16980005 ▶ return ($i > 0 ? 1 : 0) + ($i < $last ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 15609033 1370972 16980005
$i < $last ? : 14807274 2172731 16980005
71 }
72  
73 21374815 ▶sub arms_open {
Subroutine arms_open: called CC=4 SCAR=13.9
Pod documented
74 21374815 my ($self) = @_;
75 21374815 ▶ return ('L') if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 66375 21308440 21374815
76 21308440 ▶ return ('L', 'R') unless defined $self->spinner_index && $self->sides_covered == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->spinner_index and $self->sides_covered == 2 7447358 13861082 21308440
Condition100%3/3
defined $self->spinner_index and $self->sides_covered == 2
!ll&&!rl&&r
4328443311891513861082
MC/DC100%2/2
defined $self->spinner_index and $self->sides_covered == 2
defined $self->spinner_index $self->sides_covered == 2
Truth table100%3/3
A and B
A= defined $self->spinner_indexB= $self->sides_covered == 2
ABresult
0X0
100
111
77 13861082 return @ARMS;
78 }
79  
80 11919150 ▶sub face_of {
Subroutine face_of: called CC=8 SCAR=20.8
Pod documented
81 11919150 my ($self, $arm) = @_;
82 11919150 ▶ return undef unless defined $arm && grep { $_ eq $arm } $self->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm and grep {$_ eq $arm;} $self->arms_open 1 11919149 11919150
Condition66%2/3
defined $arm and grep {$_ eq $arm;} $self->arms_open
!ll&&!rl&&r
0111919149
MC/DC50%1/2
defined $arm and grep {$_ eq $arm;} $self->arms_open
defined $arm grep {$_ eq $arm;} $self->arms_open
Truth table66%2/3
A and B
A= defined $armB= grep {$_ eq $arm;} $self->arms_open
ABresult
0X0
100
111
83 11919149 ▶ return undef if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 11919149 11919149
84 11919149 ▶ return $self->line->[0]{left} if $arm eq 'L';
Branch: 1 branch
BranchTrueFalseTotal
if $arm eq "L" 4055920 7863229 11919149
85 7863229 ▶ return $self->line->[-1]{right} if $arm eq 'R';
Branch: 1 branch
BranchTrueFalseTotal
if $arm eq "R" 3986807 3876422 7863229
86 3876422 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 1993962 1882460 3876422
87 3876422 ▶ return scalar @$arms ? $arms->[-1]{outer} : $self->spinner->high;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$arms ? : 1561078 2315344 3876422
88 }
89  
90 1272107 ▶sub open_ends {
Subroutine open_ends: called CC=2 SCAR=6.9
Pod documented
91 1272107 my ($self) = @_;
92 1272107 ▶ return () if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 299 1271808 1272107
93 1271808 return map { $self->face_of($_) } $self->arms_open;
94 }
95  
96 1271400 ▶sub ends {
Subroutine ends: called CC=8 SCAR=20.8
Pod documented
97 1271400 my ($self) = @_;
98 1271400 ▶ return () if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 300 1271100 1271400
99  
100 1271100 my $line = $self->line;
101 1271100 ▶ if (@$line == 1 && !@{ $self->u } && !@{ $self->d }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$line == 1 and not @{$self->u;} and not @{$self->d;}) 31426 1239674 1271100
Condition66%4/6
@$line == 1 and not @{$self->u;} and not @{$self->d;}
!ll&&!rl&&r
1239674031426
@$line == 1 and not @{$self->u;}
!ll&&!rl&&r
1239674031426
MC/DC33%1/3
@$line == 1 and not @{$self->u;} and not @{$self->d;}
@$line == 1 not @{$self->u;} not @{$self->d;}
Truth table50%2/4
A and not(B) and not(C)
A= @$line == 1B= not @{$self->u;}C= not @{$self->d;}
ABCresult
0XX0
10X0
1100
1111
102 return ({
103 arm => 'L',
104 tile => $line->[0]{tile},
105 face => $line->[0]{left},
106 31426 sole => 1,
107 });
108 }
109  
110 my @ends = (
111 { arm => 'L', tile => $line->[0]{tile}, face => $line->[0]{left}, sole => 0 },
112 1239674 { arm => 'R', tile => $line->[-1]{tile}, face => $line->[-1]{right}, sole => 0 },
113 );
114 1239674 for my $arm (qw/U D/) {
115 2479348 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 1239674 1239674 2479348
116 2479348 ▶ next unless @$arms;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arms 1794999 684349 2479348
117 push @ends, {
118 arm => $arm,
119 tile => $arms->[-1]{tile},
120 face => $arms->[-1]{outer},
121 684349 sole => 0,
122 };
123 }
124 1239674 return @ends;
125 }
126  
127 984501 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
128 984501 my ($self) = @_;
129 return ref($self)->new(
130 5325241 line => [ map { { %$_ } } @{ $self->line } ],
131 496352 u => [ map { { %$_ } } @{ $self->u } ],
132 984501 ▶ d => [ map { { %$_ } } @{ $self->d } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $self->spinner_index ? : 725473 259028 984501
133 (defined $self->spinner_index
134 ? (spinner_index => $self->spinner_index) : ()),
135 );
136 }
137  
138 6540140 ▶sub can_place {
Subroutine can_place: called CC=5 SCAR=16.1
Pod documented
139 6540140 my ($self, $tile, $arm) = @_;
140 6540140 ▶ return 0 unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 1 6540139 6540140
141 6540139 ▶ return 0 unless defined $arm && grep { $_ eq $arm } $self->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm and grep {$_ eq $arm;} $self->arms_open 2 6540137 6540139
Condition66%2/3
defined $arm and grep {$_ eq $arm;} $self->arms_open
!ll&&!rl&&r
026540137
MC/DC50%1/2
defined $arm and grep {$_ eq $arm;} $self->arms_open
defined $arm grep {$_ eq $arm;} $self->arms_open
Truth table66%2/3
A and B
A= defined $armB= grep {$_ eq $arm;} $self->arms_open
ABresult
0X0
100
111
142 6540137 ▶ return 1 if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 32516 6507621 6540137
143 6507621 return $tile->has_face($self->face_of($arm));
144 }
145  
146 1270291 ▶sub place {
Subroutine place: called CC=15 SCAR=27.1
Pod documented
147 1270291 my ($self, $tile, $arm) = @_;
148 1270291 ▶ die 'Game::Dominoes::Layout: place wants a tile' unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 1270291 1270291
149 1270291 ▶ $arm = 'L' unless defined $arm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm 0 1270291 1270291
150 die "Game::Dominoes::Layout: arm '$arm' is not open"
151 1270291 ▶ unless grep { $_ eq $arm } $self->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} $self->arms_open 1 1270290 1270291
152 1270290 ▶ die 'Game::Dominoes::Layout: ' . $tile->stringify . " does not match arm $arm"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_place($tile, $arm) 0 1270290 1270290
153 unless $self->can_place($tile, $arm);
154  
155 1270290 ▶ if ($self->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_empty) 31354 1238936 1270290
156 31354 $self->line([ { tile => $tile, left => $tile->high, right => $tile->low } ]);
157 31354 ▶ $self->spinner_index(0) if $tile->is_double;
Branch: 1 branch
BranchTrueFalseTotal
if $tile->is_double 8558 22796 31354
158 31354 return Game::Dominoes::Play->new(
159 tile => $tile,
160 arm => 'L',
161 spinner => $tile->is_double,
162 ends => [ $self->open_ends ],
163 );
164 }
165  
166 1238936 my $matched = $self->face_of($arm);
167 1238936 my $showing = $tile->other($matched);
168 1238936 ▶ my $made_spinner = ($tile->is_double && !defined $self->spinner_index) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tile->is_double && !defined($self->spinner_index) ? : 56280 1182656 1238936
Condition100%3/3
$tile->is_double and not defined $self->spinner_index
!ll&&!rl&&r
106136112129556280
MC/DC100%2/2
$tile->is_double and not defined $self->spinner_index
$tile->is_double not defined $self->spinner_index
Truth table100%3/3
A and not(B)
A= $tile->is_doubleB= not defined $self->spinner_index
ABresult
0X0
100
111
169  
170 1238936 ▶ if ($arm eq 'L') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arm eq 'L') { } 489171 749765 1238936
elsif ($arm eq 'R') { } 455361 294404 749765
171 489171 unshift @{ $self->line }, { tile => $tile, right => $matched, left => $showing };
172 489171 ▶ $self->spinner_index($self->spinner_index + 1) if defined $self->spinner_index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->spinner_index 334124 155047 489171
173 489171 ▶ $self->spinner_index(0) if $made_spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $made_spinner 28197 460974 489171
174 }
175 elsif ($arm eq 'R') {
176 455361 push @{ $self->line }, { tile => $tile, left => $matched, right => $showing };
177 455361 ▶ $self->spinner_index($#{ $self->line }) if $made_spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $made_spinner 28083 427278 455361
178 }
179 else {
180 294404 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 174724 119680 294404
181 294404 push @$arms, { tile => $tile, inner => $matched, outer => $showing };
182 }
183  
184 1238936 return Game::Dominoes::Play->new(
185 tile => $tile,
186 arm => $arm,
187 matched => $matched,
188 showing => $showing,
189 spinner => $made_spinner,
190 ends => [ $self->open_ends ],
191 );
192 }
193  
194 41 1;
195  
196 __END__
« blib/lib/Game/Dominoes/Bot.pm ↑ Summary blib/lib/Game/Dominoes/Notation.pm »