Karel/Grid.pm

filter: Statementstmt 100% 65 / 65 Branchbran 91.6% 11 / 12 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 20 / 20 Podpod 85.7% 6 / 7 totaltotal 94.6% 106 / 112 CC 11 SCAR 6.8
CC
11
Cov
96%
__ANON__
3.5
BUILD
3.0
drop_mark
3.0
CRAP 2.0
« Karel/UI/Text.pm ↑ Summary Karel.pm »
1 package Karel::Grid;
2  
3 =head1 NAME
4  
5 Karel::Grid
6  
7 =head1 DESCRIPTION
8  
9 Represents the map in which the robot moves.
10  
11 =head1 METHODS
12  
13 =over 4
14  
15 =item 'Karel::Grid'->new
16  
17 my $grid = 'Karel::Grid'->new( x => 10, y => 12 );
18  
19 The constructor creates an empty grid of the given size.
20  
21 =cut
22  
23 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use Karel::Util qw{ positive_int m_to_n };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use List::Util qw{ any none };
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =item $grid->x, $grid->y
33  
34 my ($x, $y) = map $grid->$_, qw( x y );
35  
36 Returns the size of the grid.
37  
38 =cut
39  
40 9 has [qw[ x y ]] => (is => 'ro',
41 isa => \&positive_int,
42 required => 1,
43 );
44  
45  
46 has _grid => ( is => 'rw',
47 62 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
48 croak "Grid should be an AoA!"
49 if 'ARRAY' ne ref $_[0]
50 62 ▶ || any { 'ARRAY' ne ref } @{ $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.5
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $_[0] or &any(sub { "ARRAY" ne ref $_; } , @{$_[0];}) 0 62 62
Condition33%1/3
"ARRAY" ne ref $_[0] or &any(sub { "ARRAY" ne ref $_; } , @{$_[0];})
l!l&&r!l&&!r
0062
MC/DC0%0/2
"ARRAY" ne ref $_[0] or &any(sub { "ARRAY" ne ref $_; } , @{$_[0];})
"ARRAY" ne ref $_[0] &any(sub { "ARRAY" ne ref $_; } , @{$_[0];})
Truth table33%1/3
A or B
A= "ARRAY" ne ref $_[0]B= &any(sub { "ARRAY" ne ref $_; } , @{$_[0];})
ABresult
1X1
011
000
51 },
52 9 );
53  
54 # Create an empty grid
55 62 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
56 62 my ($self) = @_;
57 62 my ($x, $y) = map $self->$_, qw( x y );
58 62 $self->_grid([ map [ (' ') x ($y + 2) ], 0 .. $x + 1 ]);
59 62 $self->_set($_, 0, 'W'), $self->_set($_, $y + 1, 'W') for 0 .. $x + 1;
60 62 $self->_set(0, $_, 'W'), $self->_set($x + 1, $_, 'W') for 0 .. $y + 1;
61 62 return $self
62 }
63  
64 =item $grid->at($x, $y)
65  
66 Returns a space if there's nothing at the given position. For marks,
67 it returns 1 - 9. For walls, it returns "W" (outer walls) or "w"
68 (inner walls).
69  
70 =cut
71  
72 253 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
73 253 my ($self, $x, $y) = @_;
74 253 m_to_n($x, 0, $self->x + 1);
75 252 m_to_n($y, 0, $self->y + 1);
76 252 return $self->_grid->[$x][$y]
77 }
78  
79  
80 1443 ▶sub _set {
Subroutine _set: called CC=2 SCAR=6.9
81 1443 my ($self, $x, $y, $what) = @_;
82 1443 m_to_n($x, 0, $self->x + 1);
83 1443 m_to_n($y, 0, $self->y + 1);
84 croak "Unknown object '$what'."
85 1443 ▶ if none { $_ eq $what } ' ', '0' .. '9', 'w', 'W';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $what; } , " ", 0..9, "w", "W") 1 1442 1443
86 1442 $self->_grid->[$x][$y] = $what;
87 }
88  
89 =item $grid->build_wall($x, $y)
90  
91 Builds a wall ("w") at the given coordinates.
92  
93 =cut
94  
95 10 ▶sub build_wall {
Subroutine build_wall: called CC=1 SCAR=0.0
Pod documented
96 10 my ($self, $x, $y) = @_;
97 10 m_to_n($x, 1, $self->x);
98 10 m_to_n($y, 1, $self->y);
99 10 $self->_set($x, $y, 'w');
100 }
101  
102 =item $gird->remove_wall($x, $y)
103  
104 Removes a wall ("w") from the given coordinates. Dies if there's no
105 wall.
106  
107 =cut
108  
109 2 ▶sub remove_wall {
Subroutine remove_wall: called CC=2 SCAR=6.9
Pod documented
110 2 my ($self, $x, $y) = @_;
111 2 ▶ croak "Not a removable wall at $x, $y." unless 'w' eq $self->at($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless "w" eq $self->at($x, $y) 1 1 2
112 1 $self->_set($x, $y, ' ');
113 }
114  
115 =item $grid->drop_mark($x, $y)
116  
117 Drop a mark at the given position. There must be an empty place or
118 less than 9 marks, otherwise the method dies.
119  
120 =cut
121  
122 138 ▶sub drop_mark {
Subroutine drop_mark: called CC=3 SCAR=11.0
Pod documented
123 138 my ($self, $x, $y) = @_;
124 138 my $previous = $self->at($x, $y);
125 croak "Can't drop mark to '$previous'."
126 138 ▶ if none { $_ eq $previous } ' ', '1' .. '8';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $previous; } , " ", 1..8) 1 137 138
127 137 ▶ $previous = 0 if ' ' eq $previous;
Branch: 1 branch
BranchTrueFalseTotal
if " " eq $previous 27 110 137
128 137 $self->_set($x, $y, $previous + 1);
129 }
130  
131 =item $grid->pick_mark($x, $y)
132  
133 Pick up a mark from the given position. Dies if there's no mark.
134  
135 =cut
136  
137 30 ▶sub pick_mark {
Subroutine pick_mark: called CC=3 SCAR=11.0
Pod documented
138 30 my ($self, $x, $y) = @_;
139 30 my $previous = $self->at($x, $y);
140 croak "Can't pick mark from '$previous'."
141 30 ▶ if none { $_ eq $previous } '1' .. '9';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $previous; } , 1..9) 1 29 30
142 29 ▶ $self->_set($x, $y, ($previous - 1) || ' ');
Condition100%2/2
$previous - 1 || ' '
l!l
254
MC/DC100%1/1
$previous - 1 || ' '
$previous - 1
Truth table100%2/2
A
A= $previous - 1
Aresult
11
00
143 }
144  
145 =item $grid->clear($x, $y)
146  
147 Set the given position to empty (" ").
148  
149 =cut
150  
151 106 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
152 106 my ($self, $x, $y) = @_;
153 106 m_to_n($x, 1, $self->x);
154 105 m_to_n($y, 1, $self->y);
155 105 $self->_set($x, $y, ' ');
156 }
157  
158 =back
159  
160 =cut
161  
162 9 __PACKAGE__
« Karel/UI/Text.pm ↑ Summary Karel.pm »