File Coverage

blib/lib/Games/2048/Grid.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 4 100.0
condition 6 6 100.0
subroutine 12 12 100.0
pod 0 9 0.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             package Games::2048::Grid;
2 4     4   65 use 5.012;
  4         22  
  4         147  
3 4     4   18 use Moo;
  4         6  
  4         18  
4              
5             has size => is => 'ro', default => 4;
6             has _tiles => is => 'lazy';
7              
8             sub _build__tiles {
9 55     55   1061 my $self = shift;
10 55         771 [ map [ (undef) x $self->size ], 1..$self->size ];
11             }
12              
13             sub each_cell {
14 278     278 0 1104 my $self = shift;
15 1077         968 map {
16 278         995 my $y = $_;
17 1077         4593 map [$_, $y], 0..$self->size-1;
18             } 0..$self->size-1;
19             }
20              
21             sub each_tile {
22 144     144 0 2072 my $self = shift;
23 144         266 map $self->tile($_), $self->tile_cells;
24             }
25              
26             sub tile_cells {
27 182     182 0 194 my $self = shift;
28 182         367 grep $self->tile($_), $self->each_cell;
29             }
30              
31             sub available_cells {
32 41     41 0 856 my $self = shift;
33 41         89 grep !$self->tile($_), $self->each_cell;
34             }
35              
36             sub has_available_cells {
37 7     7 0 18 my $self = shift;
38 7         16 !!scalar $self->available_cells;
39             }
40              
41             sub within_bounds {
42 7341     7341 0 6316 my ($self, $cell) = @_;
43 7341 100 100     59842 $cell->[0] >= 0 and $cell->[0] < $self->size and
      100        
44             $cell->[1] >= 0 and $cell->[1] < $self->size;
45             }
46              
47             sub tile {
48 6770     6770 0 31562 my ($self, $cell) = @_;
49 6770 100       8479 return if !$self->within_bounds($cell);
50 6619         123414 $self->_tiles->[$cell->[1]][$cell->[0]];
51             }
52              
53             sub clear_tile {
54 162     162 0 177 my ($self, $cell) = @_;
55 162         3200 $self->_tiles->[$cell->[1]][$cell->[0]] = undef;
56             }
57              
58             sub set_tile {
59 636     636 0 843 my ($self, $cell, $tile) = @_;
60 636         14423 $self->_tiles->[$cell->[1]][$cell->[0]] = $tile;
61             }
62              
63             1;