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
« Dominoes.pm ↑ Summary Dominoes/Notation.pm »
1 package Game::Dominoes::Layout;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Game::Dominoes::Play;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 our $VERSION = '0.03';
11  
12 15 our @ARMS = qw/L R U D/;
13  
14 has line => (
15 is => 'rw',
16 isa => ArrayRef,
17 default => []
18 15 );
19  
20 has spinner_index => (
21 is => 'rw',
22 isa => Int
23 15 );
24  
25 has u => (
26 is => 'rw',
27 isa => ArrayRef,
28 default => []
29 15 );
30  
31 has d => (
32 is => 'rw',
33 isa => ArrayRef,
34 default => []
35 15 );
36  
37 31261874 ▶sub is_empty {
Subroutine is_empty: called CC=2 SCAR=6.9
Pod documented
38 31261874 ▶ return scalar @{ $_[0]->line } ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->line;} ? : 31143534 118340 31261874
39 }
40  
41 166 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
42 166 my ($self) = @_;
43 166 return scalar(@{ $self->line }) + scalar(@{ $self->u }) + scalar(@{ $self->d });
44 }
45  
46 6703 ▶sub tiles {
Subroutine tiles: called CC=1 SCAR=0.0
Pod documented
47 6703 my ($self) = @_;
48 6703 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 1640405 ▶sub spinner {
Subroutine spinner: called CC=2 SCAR=6.9
Pod documented
59 1640405 my ($self) = @_;
60 1640405 my $i = $self->spinner_index;
61 1640405 ▶ return undef unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 2373 1638032 1640405
62 1638032 return $self->line->[$i]{tile};
63 }
64  
65 11541770 ▶sub sides_covered {
Subroutine sides_covered: called CC=4 SCAR=13.9
Pod documented
66 11541770 my ($self) = @_;
67 11541770 my $i = $self->spinner_index;
68 11541770 ▶ return 0 unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 1 11541769 11541770
69 11541769 my $last = $#{ $self->line };
70 11541769 ▶ return ($i > 0 ? 1 : 0) + ($i < $last ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$i > 0 ? : 10479344 1062425 11541769
$i < $last ? : 9761608 1780161 11541769
71 }
72  
73 15190091 ▶sub arms_open {
Subroutine arms_open: called CC=4 SCAR=13.9
Pod documented
74 15190091 my ($self) = @_;
75 15190091 ▶ return ('L') if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 59454 15130637 15190091
76 15130637 ▶ 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 6059111 9071526 15130637
Condition100%3/3
defined $self->spinner_index and $self->sides_covered == 2
!ll&&!rl&&r
358887624702359071526
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 9071526 return @ARMS;
78 }
79  
80 8405764 ▶sub face_of {
Subroutine face_of: called CC=8 SCAR=20.8
Pod documented
81 8405764 my ($self, $arm) = @_;
82 8405764 ▶ 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 8405763 8405764
Condition66%2/3
defined $arm and grep {$_ eq $arm;} $self->arms_open
!ll&&!rl&&r
018405763
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 8405763 ▶ return undef if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 8405763 8405763
84 8405763 ▶ return $self->line->[0]{left} if $arm eq 'L';
Branch: 1 branch
BranchTrueFalseTotal
if $arm eq "L" 2971449 5434314 8405763
85 5434314 ▶ return $self->line->[-1]{right} if $arm eq 'R';
Branch: 1 branch
BranchTrueFalseTotal
if $arm eq "R" 2913498 2520816 5434314
86 2520816 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 1296150 1224666 2520816
87 2520816 ▶ return scalar @$arms ? $arms->[-1]{outer} : $self->spinner->high;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$arms ? : 897225 1623591 2520816
88 }
89  
90 902204 ▶sub open_ends {
Subroutine open_ends: called CC=2 SCAR=6.9
Pod documented
91 902204 my ($self) = @_;
92 902204 ▶ return () if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 247 901957 902204
93 901957 return map { $self->face_of($_) } $self->arms_open;
94 }
95  
96 901695 ▶sub ends {
Subroutine ends: called CC=8 SCAR=20.8
Pod documented
97 901695 my ($self) = @_;
98 901695 ▶ return () if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 248 901447 901695
99  
100 901447 my $line = $self->line;
101 901447 ▶ if (@$line == 1 && !@{ $self->u } && !@{ $self->d }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$line == 1 and not @{$self->u;} and not @{$self->d;}) 28046 873401 901447
Condition66%4/6
@$line == 1 and not @{$self->u;} and not @{$self->d;}
!ll&&!rl&&r
873401028046
@$line == 1 and not @{$self->u;}
!ll&&!rl&&r
873401028046
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 28046 sole => 1,
107 });
108 }
109  
110 my @ends = (
111 { arm => 'L', tile => $line->[0]{tile}, face => $line->[0]{left}, sole => 0 },
112 873401 { arm => 'R', tile => $line->[-1]{tile}, face => $line->[-1]{right}, sole => 0 },
113 );
114 873401 for my $arm (qw/U D/) {
115 1746802 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 873401 873401 1746802
116 1746802 ▶ next unless @$arms;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arms 1354018 392784 1746802
117 push @ends, {
118 arm => $arm,
119 tile => $arms->[-1]{tile},
120 face => $arms->[-1]{outer},
121 392784 sole => 0,
122 };
123 }
124 873401 return @ends;
125 }
126  
127 697796 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
128 697796 my ($self) = @_;
129 return ref($self)->new(
130 3522749 line => [ map { { %$_ } } @{ $self->line } ],
131 257511 u => [ map { { %$_ } } @{ $self->u } ],
132 697796 ▶ d => [ map { { %$_ } } @{ $self->d } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $self->spinner_index ? : 481883 215913 697796
133 (defined $self->spinner_index
134 ? (spinner_index => $self->spinner_index) : ()),
135 );
136 }
137  
138 4705520 ▶sub can_place {
Subroutine can_place: called CC=5 SCAR=16.1
Pod documented
139 4705520 my ($self, $tile, $arm) = @_;
140 4705520 ▶ return 0 unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 1 4705519 4705520
141 4705519 ▶ 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 4705517 4705519
Condition66%2/3
defined $arm and grep {$_ eq $arm;} $self->arms_open
!ll&&!rl&&r
024705517
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 4705517 ▶ return 1 if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 29085 4676432 4705517
143 4676432 return $tile->has_face($self->face_of($arm));
144 }
145  
146 900970 ▶sub place {
Subroutine place: called CC=15 SCAR=27.1
Pod documented
147 900970 my ($self, $tile, $arm) = @_;
148 900970 ▶ die 'Game::Dominoes::Layout: place wants a tile' unless ref $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tile 0 900970 900970
149 900970 ▶ $arm = 'L' unless defined $arm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arm 0 900970 900970
150 die "Game::Dominoes::Layout: arm '$arm' is not open"
151 900970 ▶ unless grep { $_ eq $arm } $self->arms_open;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $arm;} $self->arms_open 1 900969 900970
152 900969 ▶ die 'Game::Dominoes::Layout: ' . $tile->stringify . " does not match arm $arm"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_place($tile, $arm) 0 900969 900969
153 unless $self->can_place($tile, $arm);
154  
155 900969 ▶ if ($self->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_empty) 28022 872947 900969
156 28022 $self->line([ { tile => $tile, left => $tile->high, right => $tile->low } ]);
157 28022 ▶ $self->spinner_index(0) if $tile->is_double;
Branch: 1 branch
BranchTrueFalseTotal
if $tile->is_double 7743 20279 28022
158 28022 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 872947 my $matched = $self->face_of($arm);
167 872947 my $showing = $tile->other($matched);
168 872947 ▶ my $made_spinner = ($tile->is_double && !defined $self->spinner_index) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tile->is_double && !defined($self->spinner_index) ? : 46590 826357 872947
Condition100%3/3
$tile->is_double and not defined $self->spinner_index
!ll&&!rl&&r
7479437841446590
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 872947 ▶ if ($arm eq 'L') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arm eq 'L') { } 360328 512619 872947
elsif ($arm eq 'R') { } 332134 180485 512619
171 360328 unshift @{ $self->line }, { tile => $tile, right => $matched, left => $showing };
172 360328 ▶ $self->spinner_index($self->spinner_index + 1) if defined $self->spinner_index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->spinner_index 232494 127834 360328
173 360328 ▶ $self->spinner_index(0) if $made_spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $made_spinner 23128 337200 360328
174 }
175 elsif ($arm eq 'R') {
176 332134 push @{ $self->line }, { tile => $tile, left => $matched, right => $showing };
177 332134 ▶ $self->spinner_index($#{ $self->line }) if $made_spinner;
Branch: 1 branch
BranchTrueFalseTotal
if $made_spinner 23462 308672 332134
178 }
179 else {
180 180485 ▶ my $arms = $arm eq 'U' ? $self->u : $self->d;
Branch: 1 branch
BranchTrueFalseTotal
$arm eq 'U' ? : 107763 72722 180485
181 180485 push @$arms, { tile => $tile, inner => $matched, outer => $showing };
182 }
183  
184 872947 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 15 1;
195  
196 __END__
« Dominoes.pm ↑ Summary Dominoes/Notation.pm »