Complete/Shape.pm

filter: Statementstmt 43.2% 32 / 74 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 31.8% 7 / 22 Podpod 0.0% 0 / 2 totaltotal 31.9% 39 / 122 CC 14 SCAR 27.0
CC
14
Cov
34%
covers
42.0
_build_char
30.0
covered_points
20.0
CRAP 14.8
↑ Summary Complete.pm »
1 package Games::Tetris::Complete::Shape;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.03';
9  
10 # Upper left corner of grid
11 1 for ( qw( ulx uly ) ) {
12 has $_ => (
13 is => 'rw',
14 isa => 'Int',
15 0 ▶ default => sub { -1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 2 traits => [ 'Counter' ],
17 handles => {
18 "inc_$_" => 'inc',
19 "dec_$_" => 'dec',
20 },
21 );
22 }
23  
24 0 ▶subtype 'Char' => as 'Str' => where { $_ ne ' ' && length $_ == 1 } =>
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ ne " " and length $_ == 1 0 0 0
25 0 ▶ message { "Not a string of length 1 ($_)" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶subtype 'Blank' => as 'Str' => where { $_ eq ' ' } =>
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 ▶ message { "Not a blank char ($_)" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 subtype 'GameGrid' => as 'ArrayRef[ArrayRef[Char|Blank]]' =>
29 0 ▶ message { "bad grid: " . Dumper( $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
30 0 ▶coerce 'GameGrid' => from 'ArrayRef[Str]' => via { [ map [ split // ], @$_ ] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
31  
32 1 has 'char' => (
33 is => 'ro',
34 isa => 'Char',
35 lazy_build => 1,
36 );
37  
38 0 ▶sub _build_char {
Subroutine _build_char: not called CC=5 SCAR=34.0
39 0 my $self = shift;
40 0 my $grid = $self->grid;
41 0 my %chars;
42 0 for my $y ( 0 .. $self->ny - 1 ) {
43 $chars{ $_ }++
44 0 for grep $_ ne ' ', map $grid->[ $y ][ $_ ], 0 .. $self->nx - 1;
45 }
46 0 my @c = keys %chars;
47 0 ▶ confess "No non-blank chars in grid!" unless @c;
Branch: 1 branch
BranchTrueFalseTotal
unless @c 0 0 0
48 0 ▶ confess "More than one char in grid (" . join( ',', @c ) . ")"
Branch: 1 branch
BranchTrueFalseTotal
if @c > 1 0 0 0
49 if @c > 1;
50 0 $c[ 0 ];
51 }
52  
53 1 has [ qw( nx ny ) ] => (
54 is => 'rw',
55 isa => 'Int',
56 lazy_build => 1,
57 );
58 1 has 'grid' => (
59 is => 'rw',
60 isa => 'GameGrid',
61 required => 1,
62 coerce => 1,
63 );
64  
65 #------------------------------------------------------------------------------#
66  
67 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 0 ▶sub _build_nx {
Subroutine _build_nx: not called CC=1 SCAR=6.9
71 0 my $self = shift;
72 0 scalar @{ $self->grid->[ 0 ] };
73 }
74  
75 0 ▶sub _build_ny {
Subroutine _build_ny: not called CC=1 SCAR=6.9
76 0 my $self = shift;
77 0 scalar @{ $self->grid };
78 }
79  
80 # Test if given board point is covered by this shape
81 0 ▶sub covers {
Subroutine covers: not called CC=6 SCAR=37.4
Pod undocumented
82 0 my ( $self, $y, $x ) = @_;
83  
84 # Check if the grid covers the point (undef)
85 0 my ( $ulx, $uly ) = ( $self->ulx, $self->uly );
86 0 ▶ return if $ulx > $x || $uly > $y;
Branch: 1 branch
BranchTrueFalseTotal
if $ulx > $x or $uly > $y 0 0 0
Condition0%0/3
$ulx > $x or $uly > $y
l!l&&r!l&&!r
000
MC/DC0%0/2
$ulx > $x or $uly > $y
$ulx > $x $uly > $y
Truth table0%0/3
A or B
A= $ulx > $xB= $uly > $y
ABresult
1X1
011
000
87 0 my ( $grid_x, $grid_y ) = ( $x - $ulx, $y - $uly );
88 0 ▶ return if $grid_x >= $self->nx || $grid_y >= $self->ny;
Branch: 1 branch
BranchTrueFalseTotal
if $grid_x >= $self->nx or $grid_y >= $self->ny 0 0 0
Condition0%0/3
$grid_x >= $self->nx or $grid_y >= $self->ny
l!l&&r!l&&!r
000
MC/DC0%0/2
$grid_x >= $self->nx or $grid_y >= $self->ny
$grid_x >= $self->nx $grid_y >= $self->ny
Truth table0%0/3
A or B
A= $grid_x >= $self->nxB= $grid_y >= $self->ny
ABresult
1X1
011
000
89  
90 # Check if the value at the given point is a Blank (0) or Char (1)
91 0 my $char = $self->grid->[ $grid_y ][ $grid_x ];
92 0 ▶ confess "No char at ($grid_x,$grid_y)!" unless $char;
Branch: 1 branch
BranchTrueFalseTotal
unless $char 0 0 0
93 match_on_type $char => (
94 0 ▶ Blank => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 ▶ Char => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ => sub { confess "the fuck is this: '$char'" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 );
98 }
99  
100 0 ▶sub covered_points {
Subroutine covered_points: not called CC=4 SCAR=30.0
Pod undocumented
101 0 my $self = shift;
102 0 my ( $ulx, $uly ) = ( $self->ulx, $self->uly );
103 0 my $grid = $self->grid;
104 0 my @points;
105 0 for my $yi ( 0 .. $self->ny - 1 ) {
106 0 for my $xi ( 0 .. $self->nx - 1 ) {
107 # match_on_type $grid->[ $yi ][ $xi ] => (
108 # Char => sub { push @points, [ $uly + $yi, $ulx + $xi ] },
109 # Blank => sub { }
110 # );
111 # print "($yi,$xi) ";
112 # print "char: '", $grid->[ $yi ][ $xi ], "'\n";
113 0 ▶ if ( $grid->[ $yi ][ $xi ] ne ' ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grid->[$yi][$xi] ne " ") 0 0 0
114 0 push @points, [ $uly + $yi, $ulx + $xi ];
115 }
116 }
117 }
118 0 @points;
119 }
120  
121 1 1;
↑ Summary Complete.pm »