Karel/Robot/WithGrid.pm

filter: Statementstmt 98.8% 167 / 169 Branchbran 79.5% 35 / 44 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 46 / 46 Podpod 100% 17 / 17 totaltotal 94.3% 268 / 284 CC 26 SCAR 7.5
CC
26
Cov
94%
condition
5.0
current
4.0
step
4.0
CRAP 2.1
« Karel/Parser.pm ↑ Summary Karel/UI/Text.pm »
1 package Karel::Robot::WithGrid;
2  
3 =head1 NAME
4  
5 Karel::Robot::WithGrid
6  
7 =head1 DESCRIPTION
8  
9 A robot with an associated grid. To create the robot, use
10  
11 my $robot = 'Karel::Robot'->new;
12 my $grid = 'Karel::Grid'->new(x => 10, y => 12);
13 $robot = $robot->set_grid($grid, 1, 1);
14  
15 =head1 METHODS
16  
17 =over 4
18  
19 =cut
20  
21 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use parent 'Karel::Robot';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Karel::Util qw{ positive_int };
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use List::Util qw{ first };
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Clone qw{ clone };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 CONTINUE => 0,
30 FINISHED => 1,
31 FINISHED_DELAYED => 2,
32 QUIT => -1,
33 7 };
34 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 requires qw{ set_grid knows };
36  
37 =item $robot->x, $robot->y
38  
39 my ($x, $y) = map $robot->$_, qw( x y );
40  
41 Coordinates of the robot in its grid.
42  
43 =cut
44  
45 7 has [qw[ x y ]] => ( is => 'rwp',
46 isa => \&positive_int,
47 );
48  
49 =item $robot->grid
50  
51 my $grid = $robot->grid;
52  
53 The associated C<Karel::Grid> object.
54  
55 =cut
56  
57 45 ▶my $grid_type = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
58 45 my ($grid) = @_;
59 45 ▶ eval { $grid->isa('Karel::Grid') } or croak "Invalid grid type\n";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $grid->isa("Karel::Grid") } } or croak("Invalid grid type\n") 45 0 45
60 7 };
61  
62  
63 7 has grid => ( is => 'rwp',
64 isa => $grid_type,
65 );
66  
67 =item $robot->set_grid($grid, $x, $y, $direction);
68  
69 Initialize the grid. Grid must be an object of the C<Karel::Grid>
70 type, C<$x> and C<$y> are coordinates of the robot, C<$direction> is
71 one of C<N E S W> (defaults to C<N>). Dies if the robot's place is
72 occupied by a wall.
73  
74 =cut
75  
76 45 ▶around set_grid => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
77 45 my (undef, $self, $grid, $x, $y, $direction) = @_;
78 45 $self->_set_grid($grid);
79 45 $self->_set_x($x);
80 45 $self->_set_y($y);
81 45 ▶ $self->_set_direction($direction) if $direction;
Branch: 1 branch
BranchTrueFalseTotal
if $direction 45 0 45
82 7 };
83  
84 =item $robot->drop_mark
85  
86 Drop mark in the current location. Dies if there are already 9 marks.
87  
88 =cut
89  
90 42 ▶sub drop_mark {
Subroutine drop_mark: called CC=1 SCAR=0.0
Pod documented
91 42 my ($self) = shift;
92 42 $self->grid->drop_mark($self->coords);
93 42 return 1
94 }
95  
96 =item $robot->pick_mark
97  
98 Picks up one mark from the current location. Dies if there's nothing
99 to pick.
100  
101 =cut
102  
103 19 ▶sub pick_mark {
Subroutine pick_mark: called CC=1 SCAR=0.0
Pod documented
104 19 my ($self) = shift;
105 19 $self->grid->pick_mark($self->coords);
106 19 return 1
107 }
108  
109 =item $robot->direction
110  
111 my $direction = $robot->direction;
112  
113 Returns the robot's direction: one of C<qw( N W S E )>.
114  
115 =cut
116  
117 7 ▶my $string_list = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 7 do {
119 7 my %strings = map { $_ => 1 } @_;
120 351 ▶ sub { $strings{+shift} or croak "Invalid string" }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$strings{shift()} or croak("Invalid string") 351 0 351
121 7 }
122 7 };
123  
124 7 has direction => ( is => 'rwp',
125 isa => $string_list->(qw( N W S E )),
126 default => 'N',
127 );
128  
129 =item $robot->left
130  
131 Turn the robot to the left.
132  
133 =cut
134  
135 7 my @directions = qw( N W S E );
136 263 ▶sub left {
Subroutine left: called CC=1 SCAR=0.0
Pod documented
137 263 my ($self) = @_;
138 263 my $dir = $self->direction;
139 263 ▶ my $idx = first { $directions[$_] eq $dir } 0 .. $#directions;
Subroutine __ANON__: called CC=1 SCAR=0.0
140 263 $self->_set_direction($directions[ ($idx + 1) % @directions ]);
141 263 return FINISHED
142 }
143  
144 =item $robot->coords
145  
146 Returns the robot's coordinates, i.e. C<x> and C<y>.
147  
148 =cut
149  
150 129 ▶sub coords {
Subroutine coords: called CC=1 SCAR=0.0
Pod documented
151 129 my ($self) = @_;
152 129 return ($self->x, $self->y)
153 }
154  
155 =item $robot->cover
156  
157 Returns the grid element at the robot's coordinates, i.e.
158  
159 $r->grid->at($r->coords)
160  
161 =cut
162  
163 18 ▶sub cover {
Subroutine cover: called CC=1 SCAR=0.0
Pod documented
164 18 my ($self) = @_;
165 18 return $self->grid->at($self->coords)
166 }
167  
168 =item $robot->facing_coords
169  
170 Returns the coordinates of the grid element the robot is facing.
171  
172 =cut
173  
174 7 my %facing = ( N => [0, -1],
175 E => [1, 0],
176 S => [0, 1],
177 W => [-1, 0]
178 );
179  
180 50 ▶sub facing_coords {
Subroutine facing_coords: called CC=2 SCAR=6.9
Pod documented
181 50 my ($self) = @_;
182 50 my $direction = $self->direction;
183 50 my @coords = $self->coords;
184 50 $coords[$_] += $facing{$direction}[$_] for 0, 1;
185 return @coords
186 50 }
187  
188 =item $robot->facing
189  
190 Returns the contents of the grid element the robot is facing.
191  
192 =cut
193  
194 37 ▶sub facing {
Subroutine facing: called CC=1 SCAR=0.0
Pod documented
195 37 my ($self) = @_;
196 37 $self->grid->at($self->facing_coords)
197 }
198  
199  
200 has _stack => ( is => 'rwp',
201 predicate => 'is_running',
202 clearer => 'not_running',
203 30 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
204 30 my $s = shift;
205 30 ▶ 'ARRAY' eq ref $s or croak "Invalid stack";
Branch: 1 branch
BranchTrueFalseTotal
"ARRAY" eq ref $s or croak("Invalid stack") 30 0 30
206 30 ▶ ! grep 'ARRAY' ne ref $_, @$s
Branch: 1 branch
BranchTrueFalseTotal
not grep(("ARRAY" ne ref $_), @$s) or croak("Invalid stack element") 30 0 30
207 or croak "Invalid stack element";
208 }
209 7 );
210  
211 340 ▶sub _pop_stack {
Subroutine _pop_stack: called CC=2 SCAR=6.9
212 340 my $self = shift;
213 340 shift @{ $self->_stack };
214 340 ▶ $self->not_running unless @{ $self->_stack };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_stack;} 25 315 340
215 }
216  
217 312 ▶sub _push_stack {
Subroutine _push_stack: called CC=2 SCAR=6.9
218 312 my ($self, $commands, $current) = @_;
219 312 ▶ $current = $self->_stack->[1][-1]
Branch: 1 branch
BranchTrueFalseTotal
unless $current 221 91 312
220 unless $current;
221 312 unshift @{ $self->_stack }, [ clone($commands), 0, $current ];
222 }
223  
224  
225 1212 ▶sub _stacked { shift->_stack->[0] }
Subroutine _stacked: called CC=1 SCAR=0.0
226  
227 178 ▶sub _stack_command {
Subroutine _stack_command: called CC=1 SCAR=0.0
228 178 my $self = shift;
229 178 my ($commands, $index) = @{ $self->_stacked };
230 178 return $commands->[$index]
231 }
232  
233 122 ▶sub _stack_previous_commands {
Subroutine _stack_previous_commands: called CC=1 SCAR=0.0
234 122 shift->_stack->[1][0]
235 }
236  
237 122 ▶sub _stack_previous_index {
Subroutine _stack_previous_index: called CC=1 SCAR=0.0
238 122 shift->_stack->[1][1]
239 }
240  
241 122 ▶sub _stack_delay_finish {
Subroutine _stack_delay_finish: called CC=1 SCAR=0.0
242 122 my ($self) = @_;
243 122 $self->_stack_previous_commands
244 ->[ $self->_stack_previous_index ][0] = 'x';
245 }
246  
247 =item $robot->current
248  
249 For debugging Karel programs: returns the source of the currently
250 executed command, current position in the source and the length of the
251 command.
252  
253 =cut
254  
255 373 ▶sub current {
Subroutine current: called CC=4 SCAR=13.9
Pod documented
256 373 my ($self) = @_;
257 373 ▶ my $command = (first { 'x' ne $_->[0][0][0] } @{ $self->_stack })
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition66%2/3
&first(sub { "x" ne $_->[0][0][0]; } , @{$self->_stack;}) // $self->_stacked
l!l&&r!l&&!r
37210
MC/DC0%0/2
&first(sub { "x" ne $_->[0][0][0]; } , @{$self->_stack;}) // $self->_stacked
&first(sub { "x" ne $_->[0][0][0]; } , @{$self->_stack;}) $self->_stacked
Truth table66%2/3
A // B
A= &first(sub { "x" ne $_->[0][0][0]; } , @{$self->_stack;})B= $self->_stacked
ABresult
1X1
011
000
258 // $self->_stacked;
259 373 my $current = $command->[-1];
260 373 my ($from, $length) = @{ $command->[0][ $command->[-2] ][-1] };
261 373 ▶ my $known = $self->knowledge->{ $current // q() };
Condition50%1/2
$current // ""
l!l
3730
MC/DC0%0/1
$current // ""
$current
Truth table50%1/2
A
A= $current
Aresult
11
00
262 373 ▶ my $src = ref $current ? $current->[0] : $known->[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $current ? : 2 371 373
263 373 return $src, $from, $length
264 }
265  
266 30 ▶sub _run {
Subroutine _run: called CC=1 SCAR=0.0
267 30 my ($self, $prog, $current) = @_;
268 30 $self->_set__stack([ [ $prog, 0, $current ] ]);
269 }
270  
271 =item $robot->run($command_name)
272  
273 Run the given command.
274  
275 =cut
276  
277 15 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
278 15 my ($self, $command) = @_;
279 15 my $parsed = $self->parser->parse("run $command");
280 14 $self->_run($$parsed, [$command]);
281 }
282  
283 =item $robot->forward
284  
285 Moves the robot one cell forward in its direction.
286  
287 =cut
288  
289 11 ▶sub forward {
Subroutine forward: called CC=2 SCAR=7.0
Pod documented
290 11 my ($self) = @_;
291 11 ▶ croak "Can't walk through walls" if $self->facing =~ /w/i;
Branch: 1 branch
BranchTrueFalseTotal
if $self->facing =~ /w/i 0 11 11
292 11 my ($x, $y) = $self->facing_coords;
293 11 $self->_set_x($x);
294 11 $self->_set_y($y);
295 11 return FINISHED
296 }
297  
298 =item $robot->repeat($count, $commands)
299  
300 Runs the C<repeat> command: decreases the counter, and if it's
301 non-zero, pushes the body to the stack. Returns 0 (CONTINUE) when it
302 should stay in the stack, 1 (FINISHED) otherwise.
303  
304 =cut
305  
306 240 ▶sub repeat {
Subroutine repeat: called CC=2 SCAR=6.9
Pod documented
307 240 my ($self, $count, $commands) = @_;
308 240 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) { } 178 62 240
309 178 $self->_stack_command->[1] = $count - 1;
310 178 $self->_push_stack($commands);#, $self->_stack->[1][-1]);
311 178 return CONTINUE
312  
313 } else {
314 62 return FINISHED
315 }
316 }
317  
318 =item $isnot_south = $robot->condition('!S')
319  
320 Solve the given condition. Supported parameters are:
321  
322 =over 4
323  
324 =item * N E S W
325  
326 Facing North, East, South, West
327  
328 =item * m
329  
330 Covering mark(s).
331  
332 =item * w
333  
334 Facing a wall.
335  
336 =item * !
337  
338 Negates the condition.
339  
340 =back
341  
342 Returns true or false, dies on invalid condition.
343  
344 =cut
345  
346 51 ▶sub condition {
Subroutine condition: called CC=5 SCAR=16.2
Pod documented
347 51 my ($self, $condition) = @_;
348 51 my $negation = $condition =~ s/!//;
349 51 my $result;
350  
351 51 ▶ if ($condition =~ /^[NESW]$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($condition =~ /^[NESW]$/) { } 18 33 51
elsif ($condition eq 'w') { } 22 11 33
elsif ($condition eq 'm') { } 11 0 11
352 18 $result = $self->direction eq $condition;
353  
354 } elsif ($condition eq 'w') {
355 22 $result = $self->facing =~ /w/i;
356  
357 } elsif ($condition eq 'm') {
358 11 $result = $self->cover =~ /^[1-9]$/;
359  
360 } else {
361 0 croak "Invalid condition '$condition'"
362 }
363  
364 51 ▶ $result = ! $result if $negation;
Branch: 1 branch
BranchTrueFalseTotal
if $negation 28 23 51
365 51 return $result
366 }
367  
368 =item $robot->If($condition, $commands, $else)
369  
370 If $condition is true, puts $commands to the stack, otherwise puts
371 $else to the stack. Returns 2 (FINISH_DELAYED) in the former case, 1
372 (FINISHED) in the latter one.
373  
374 =cut
375  
376 32 ▶sub If {
Subroutine If: called CC=3 SCAR=11.0
Pod documented
377 32 my ($self, $condition, $commands, $else) = @_;
378 32 ▶ if ($self->condition($condition)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->condition($condition)) { } 27 5 32
elsif ($else) { } 4 1 5
379 27 $self->_push_stack($commands);
380 } elsif ($else) {
381 4 $self->_push_stack($else);
382 } else {
383 1 return FINISHED
384 }
385 31 return FINISHED_DELAYED
386 }
387  
388 =item $robot->While($condition, $commands)
389  
390 Similar to C<If>, but returns 0 (CONTINUE) if the condition is true,
391 i.e. it stays in the stack.
392  
393 =cut
394  
395 19 ▶sub While {
Subroutine While: called CC=2 SCAR=6.9
Pod documented
396 19 my ($self, $condition, $commands) = @_;
397 19 ▶ if ($self->condition($condition)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->condition($condition)) { } 12 7 19
398 12 $self->_push_stack($commands);
399 12 return CONTINUE
400  
401 } else {
402 7 return FINISHED
403 }
404 }
405  
406 =item $robot->call($command)
407  
408 Checks whether the robot knows the command, and if so, pushes its
409 definition to the stack. Dies otherwise. Returns 2 (FINISH_DELAYED).
410  
411 =cut
412  
413 91 ▶sub call {
Subroutine call: called CC=2 SCAR=7.2
Pod documented
414 91 my ($self, $command_name) = @_;
415 91 my $commands = $self->knows($command_name);
416 91 ▶ if ($commands) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commands) { } 91 0 91
417 91 $self->_push_stack($commands, $command_name);
418 } else {
419 0 croak "Unknown command $command_name.";
420 }
421 91 return FINISHED_DELAYED
422 }
423  
424 =item $robot->stop
425  
426 Stops execution of the current program and clears the stack. Returns
427 -1 (QUIT).
428  
429 =cut
430  
431 3 ▶sub stop { shift->not_running; QUIT }
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
432  
433 =item $robot->step
434  
435 Makes one step in the currently running program.
436  
437 =cut
438  
439 845 ▶sub step {
Subroutine step: called CC=4 SCAR=13.9
Pod documented
440 845 my ($self) = @_;
441 845 ▶ croak 'Not running!' unless $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 1 844 845
442  
443 844 my ($commands, $index) = @{ $self->_stacked };
444  
445 844 my $command;
446 844 ▶ $command = defined $index ? $commands->[$index] : ['x'];
Branch: 1 branch
BranchTrueFalseTotal
defined $index ? : 836 8 844
447 my $action = { f => 'forward',
448 l => 'left',
449 p => 'pick_mark',
450 d => 'drop_mark',
451 r => 'repeat',
452 i => 'If',
453 w => 'While',
454 q => 'stop',
455 c => 'call',
456 129 ▶ x => sub { FINISHED },
Subroutine __ANON__: called CC=1 SCAR=0.0
457 844 }->{ $command->[0] };
458 844 ▶ croak "Unknown action " . $command->[0] unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 844 844
459  
460 844 my $finished = $self->$action(@{ $command }[ 1 .. $#$command ]);
461 # warn "$command->[0], $finished.\n";
462 # use Data::Dump; warn Data::Dump::dump($self->_stack);
463  
464 529 ▶ { FINISHED, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
465 529 ▶ if (++$index > $#$commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$index > $commands->$#*) { } 340 189 529
466 340 $self->_pop_stack;
467  
468 } else {
469 189 $self->_stacked->[1] = $index;
470 }
471 },
472 190 ▶ CONTINUE, sub { @_ = ($self); goto &step },
Subroutine __ANON__: called CC=1 SCAR=0.0
473 122 ▶ FINISHED_DELAYED, sub { $self->_stack_delay_finish },
Subroutine __ANON__: called CC=1 SCAR=0.0
474 3 ▶ QUIT, sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
475 844 }->{$finished}->();
476 }
477  
478 =back
479  
480 =head1 RETURN VALUES
481  
482 There are three special return values corresponding to the stack handling:
483  
484 0 CONTINUE
485 1 FINISHED
486 2 FINISHED_DELAYED
487  
488 If a command returns C<CONTINUE>, the stack doesn't change. If it
489 returns C<FINISHED>, the following command in the stack is executed.
490 If it returns C<FINISHED_DELAYED>, new commands are put in the stack,
491 but once they're finished, the command behaves as if finished, too.
492  
493 =cut
494  
495 7 __PACKAGE__
« Karel/Parser.pm ↑ Summary Karel/UI/Text.pm »