blib/lib/Games/LMSolve/Plank/Base.pm

filter: Statementstmt 83.8% 202 / 241 Branchbran 65.6% 63 / 96 Conditioncond 69.7% 23 / 33 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 94.4% 17 / 18 Podpod 100% 9 / 9 totaltotal 78.3% 325 / 415 CC 75 SCAR 33.9
CC
75
Cov
78%
__ANON__
93.0
enumerate_moves
21.0
input_board
18.7
CRAP 29.5
« blib/lib/Games/LMSolve/Tilt/RedBlue.pm ↑ Summary blib/lib/Games/LMSolve/Tilt/Base.pm »
1 package Games::LMSolve::Plank::Base;
2 5 $Games::LMSolve::Plank::Base::VERSION = '0.14.1';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Games::LMSolve::Base qw(%cell_dirs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 @ISA = qw(Games::LMSolve::Base);
11  
12 5 ▶use Games::LMSolve::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub initialize
15 {
16 1 ▶ my $self = shift;
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
17  
18 1 $self->SUPER::initialize(@_);
19  
20 1 $self->{'dirs'} = [qw(E W S N)];
21 }
22  
23 sub input_board
24 {
25 1 ▶ my $self = shift;
Subroutine input_board: called CC=11 SCAR=29.3
Pod documented
26  
27 1 my $filename = shift;
28  
29 1 my $spec = {
30 'dims' => { 'type' => "xy(integer)", 'required' => 1, },
31 'planks' => {
32 'type' => "array(start_end(xy(integer)))",
33 'required' => 1,
34 },
35 'layout' => { 'type' => "layout", 'required' => 1, },
36 };
37  
38 1 my $input_obj = Games::LMSolve::Input->new();
39  
40 1 my $input_fields = $input_obj->input_board( $filename, $spec );
41 my ( $width, $height ) =
42 1 @{ $input_fields->{'dims'}->{'value'} }{ 'x', 'y' };
43 1 my ( $goal_x, $goal_y );
44  
45 1 ▶ if ( scalar( @{ $input_fields->{'layout'}->{'value'} } ) < $height )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$input_fields->{"layout"}{"value"};} < $height) 0 1 1
46 {
47 0 die
48 "Incorrect number of lines in board layout (does not match dimensions";
49 }
50 1 my @board;
51 1 my $lines = $input_fields->{'layout'}->{'value'};
52 1 ▶ for ( my $y = 0 ; $y < $height ; $y++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $height) 5 1 6
53 {
54 5 my $l = [];
55 5 ▶ if ( length( $lines->[$y] ) < $width )
Branch: 1 branch
BranchTrueFalseTotal
if (length $lines->[$y] < $width) 0 5 5
56 {
57 die "Too few characters in board layout in line No. "
58 0 . ( $input_fields->{'layout'}->{'line_num'} + $y + 1 );
59 }
60 5 my $x = 0;
61 5 foreach my $c ( split( //, $lines->[$y] ) )
62 {
63 35 push @$l, ( $c ne " " );
64 35 ▶ if ( $c eq "G" )
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "G") 1 34 35
65 {
66 1 ▶ if ( defined($goal_x) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $goal_x) 0 1 1
67 {
68 0 die "Goal was defined twice!";
69 }
70 1 ( $goal_x, $goal_y ) = ( $x, $y );
71 }
72 35 $x++;
73 }
74 5 push @board, $l;
75 }
76 1 ▶ if ( !defined($goal_x) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $goal_x) 0 1 1
77 {
78 0 die "The Goal was not defined in the layout";
79 }
80  
81 1 my $planks_in = $input_fields->{'planks'}->{'value'};
82  
83 1 my @planks;
84  
85 my $get_plank = sub {
86 4 ▶ my $p = shift;
Subroutine __ANON__: called CC=19 SCAR=45.3
87  
88 my ( $start_x, $start_y ) =
89 4 ( $p->{'start'}->{'x'}, $p->{'start'}->{'y'} );
90 4 my ( $end_x, $end_y ) = ( $p->{'end'}->{'x'}, $p->{'end'}->{'y'} );
91  
92 my $check_endpoints = sub {
93 4 ▶ if ( !$board[$start_y]->[$start_x] )
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
unless ($board[$start_y][$start_x]) 0 4 4
94 {
95 0 die "Plank cannot be placed at point ($start_x,$start_y)!";
96 }
97 4 ▶ if ( !$board[$end_y]->[$end_x] )
Branch: 1 branch
BranchTrueFalseTotal
if (not $board[$end_y][$end_x]) 0 4 4
98 {
99 0 die "Plank cannot be placed at point ($end_x,$end_y)!";
100 }
101 4 };
102  
103 4 my $plank_str = "Plank ($start_x,$start_y) ==> ($end_x,$end_y)";
104  
105 4 ▶ if ( ( $start_x >= $width )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_x >= $width or $end_x >= $width or $start_y >= $height or $end_y >= $height) 0 4 4
Condition33%3/9
$start_x >= $width or $end_x >= $width or $start_y >= $height or $end_y >= $height
l!l&&r!l&&!r
004
$start_x >= $width or $end_x >= $width or $start_y >= $height
l!l&&r!l&&!r
004
$start_x >= $width or $end_x >= $width
l!l&&r!l&&!r
004
MC/DC0%0/4
$start_x >= $width or $end_x >= $width or $start_y >= $height or $end_y >= $height
$start_x >= $width $end_x >= $width $start_y >= $height $end_y >= $height
Truth table20%1/5
A or B or C or D
A= $start_x >= $widthB= $end_x >= $widthC= $start_y >= $heightD= $end_y >= $height
ABCDresult
1XXX1
01XX1
001X1
00011
00000
106 || ( $end_x >= $width )
107 || ( $start_y >= $height )
108 || ( $end_y >= $height ) )
109 {
110 0 die "$plank_str is out of the boundaries of the board";
111 }
112  
113 4 ▶ if ( $start_x == $end_x )
Branch: 3 branches
BranchTrueFalseTotal
if ($start_x == $end_x) { } 1 3 4
elsif ($start_y == $end_y) { } 3 0 3
elsif ($end_x - $start_x == $end_y - $start_y) { } 0 0 0
114 {
115 1 ▶ if ( $start_y == $end_y )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_y == $end_y) 0 1 1
116 {
117 0 die "$plank_str has zero length!";
118 }
119 1 $check_endpoints->();
120 1 ▶ if ( $start_y > $end_y )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_y > $end_y) 0 1 1
121 {
122 0 ( $start_y, $end_y ) = ( $end_y, $start_y );
123 }
124 1 foreach my $y ( ( $start_y + 1 ) .. ( $end_y - 1 ) )
125 {
126 0 ▶ if ( $board[$y]->[$start_x] )
Branch: 1 branch
BranchTrueFalseTotal
if ($board[$y][$start_x]) 0 0 0
127 {
128 0 die "$plank_str crosses logs!";
129 }
130 }
131 return {
132 1 'len' => ( $end_y - $start_y ),
133 'start' => { 'x' => $start_x, 'y' => $start_y },
134 'dir' => "S"
135 };
136 }
137 elsif ( $start_y == $end_y )
138 {
139 3 $check_endpoints->();
140 3 ▶ if ( $start_x > $end_x )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_x > $end_x) 0 3 3
141 {
142 0 ( $start_x, $end_x ) = ( $end_x, $start_x );
143 }
144 3 foreach my $x ( ( $start_x + 1 ) .. ( $end_x - 1 ) )
145 {
146 3 ▶ if ( $board[$start_y]->[$x] )
Branch: 1 branch
BranchTrueFalseTotal
if ($board[$start_y][$x]) 0 3 3
147 {
148 0 die "$plank_str crosses logs!";
149 }
150 }
151 return {
152 3 'len' => ( $end_x - $start_x ),
153 'start' => { 'x' => $start_x, 'y' => $start_y },
154 'dir' => "E"
155 };
156 }
157 elsif ( ( $end_x - $start_x ) == ( $end_y - $start_y ) )
158 {
159 0 $check_endpoints->();
160 0 ▶ if ( $start_x > $end_x )
Branch: 1 branch
BranchTrueFalseTotal
if ($start_x > $end_x) 0 0 0
161 {
162 0 ( $start_x, $end_x ) = ( $end_x, $start_x );
163 0 ( $start_y, $end_y ) = ( $end_y, $start_y );
164 }
165 0 foreach my $i ( 1 .. ( $end_x - $start_x - 1 ) )
166 {
167 0 ▶ if ( $board[ $start_y + $i ]->[ $start_x + $i ] )
Branch: 1 branch
BranchTrueFalseTotal
if ($board[$start_y + $i][$start_x + $i]) 0 0 0
168 {
169 0 die "$plank_str crosses logs!";
170 }
171 }
172 0 ▶ if ( !grep { $_ eq "SE" } @{ $self->{'dirs'} } )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "SE";} @{$$self{"dirs"};}) 0 0 0
173 {
174 0 die "$plank_str is not aligned horizontally or vertically.";
175 }
176 return {
177 0 'len' => ( $end_x - $start_x ),
178 'start' => {
179 'x' => $start_x,
180 'y' => $start_y,
181 },
182 'dir' => "SE",
183 };
184 }
185 else
186 {
187 0 die "$plank_str is not aligned horizontally or vertically.";
188 }
189 1 };
190  
191 1 foreach my $p (@$planks_in)
192 {
193 4 push @planks, $get_plank->($p);
194 }
195  
196 1 $self->{'width'} = $width;
197 1 $self->{'height'} = $height;
198 1 $self->{'goal_x'} = $goal_x;
199 1 $self->{'goal_y'} = $goal_y;
200 1 $self->{'board'} = \@board;
201 1 $self->{'plank_lens'} = [ map { $_->{'len'} } @planks ];
202  
203 my $state = [
204 0,
205 (
206 map {
207 1 (
208 $_->{'start'}->{'x'},
209 $_->{'start'}->{'y'},
210 (
211 ( $_->{'dir'} eq "E" ) ? 0
212 4 ▶ : ( $_->{'dir'} eq "SE" ) ? 2
Branch: 2 branches
BranchTrueFalseTotal
$_->{'dir'} eq 'E' ? : 3 1 4
$_->{'dir'} eq 'SE' ? : 0 1 1
213 : 1
214 )
215 )
216 } @planks
217 )
218 ];
219 1 $self->_process_plank_data($state);
220  
221 #{
222 # use Data::Dumper;
223 #
224 # my $d = Data::Dumper->new([$self, $state], ["\$self", "\$state"]);
225 # print $d->Dump();
226 #}
227  
228 1 return $state;
229 }
230  
231 sub _process_plank_data
232 {
233 642 ▶ my $self = shift;
Subroutine _process_plank_data: called CC=13 SCAR=25.7
234  
235 642 my $state = shift;
236  
237 642 my $active = $state->[0];
238  
239 my @planks = (
240 map {
241 {
242 2568 'len' => $self->{'plank_lens'}->[$_],
243 'x' => $state->[ $_ * 3 + 1 ],
244 'y' => $state->[ $_ * 3 + 1 + 1 ],
245 'dir' => $state->[ $_ * 3 + 2 + 1 ],
246 'active' => 0,
247 }
248 642 } ( 0 .. ( scalar( @{ $self->{'plank_lens'} } ) - 1 ) )
249 );
250  
251 642 foreach my $p (@planks)
252 {
253 2568 my $p_dir = $p->{'dir'};
254 2568 ▶ my $dir = ( $p_dir == 0 ) ? "E" : ( $p_dir == 1 ) ? "S" : "SE";
Branch: 2 branches
BranchTrueFalseTotal
$p_dir == 0 ? : 1338 1230 2568
$p_dir == 1 ? : 1230 0 1230
255 2568 $p->{'dir'} = $dir;
256  
257 2568 $p->{'end_x'} = $p->{'x'} + $cell_dirs{$dir}->[0] * $p->{'len'};
258 2568 $p->{'end_y'} = $p->{'y'} + $cell_dirs{$dir}->[1] * $p->{'len'};
259 }
260  
261 # $ap is short for active plank
262 642 my $ap = $planks[$active];
263 642 $ap->{'active'} = 1;
264  
265 642 my (@queue);
266 642 push @queue, [ $ap->{'x'}, $ap->{'y'} ], [ $ap->{'end_x'}, $ap->{'end_y'} ];
267 642 undef($ap);
268 642 ▶ while ( my $point = pop(@queue) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $point = pop @queue) 2113 642 2755
269 {
270 2113 my ( $x, $y ) = @$point;
271 2113 foreach my $p (@planks)
272 {
273 8452 ▶ if ( $p->{'active'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"active"}) 3901 4551 8452
274 {
275 3901 next;
276 }
277 4551 ▶ if ( ( $p->{'x'} == $x ) && ( $p->{'y'} == $y ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"x"} == $x and $p->{"y"} == $y) 464 4087 4551
Condition100%3/3
$p->{"x"} == $x and $p->{"y"} == $y
!ll&&!rl&&r
3772315464
MC/DC100%2/2
$p->{"x"} == $x and $p->{"y"} == $y
$p->{"x"} == $x $p->{"y"} == $y
Truth table100%3/3
A and B
A= $p->{"x"} == $xB= $p->{"y"} == $y
ABresult
0X0
100
111
278 {
279 464 $p->{'active'} = 1;
280 464 push @queue, [ $p->{'end_x'}, $p->{'end_y'} ];
281 }
282 4551 ▶ if ( ( $p->{'end_x'} == $x ) && ( $p->{'end_y'} == $y ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"end_x"} == $x and $p->{"end_y"} == $y) 365 4186 4551
Condition100%3/3
$p->{"end_x"} == $x and $p->{"end_y"} == $y
!ll&&!rl&&r
3397789365
MC/DC100%2/2
$p->{"end_x"} == $x and $p->{"end_y"} == $y
$p->{"end_x"} == $x $p->{"end_y"} == $y
Truth table100%3/3
A and B
A= $p->{"end_x"} == $xB= $p->{"end_y"} == $y
ABresult
0X0
100
111
283 {
284 365 $p->{'active'} = 1;
285 365 push @queue, [ $p->{'x'}, $p->{'y'} ];
286 }
287 }
288 }
289 642 foreach my $i ( 0 .. $#planks )
290 {
291 964 ▶ if ( $planks[$i]->{'active'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($planks[$i]{"active"}) 642 322 964
292 {
293 642 $state->[0] = $i;
294 642 return \@planks;
295 }
296 }
297 }
298  
299 sub pack_state
300 {
301 239 ▶ my $self = shift;
Subroutine pack_state: called CC=1 SCAR=0.0
Pod documented
302  
303 239 my $state_vector = shift;
304 239 return pack( "c*", @$state_vector );
305 }
306  
307 sub unpack_state
308 {
309 83 ▶ my $self = shift;
Subroutine unpack_state: called CC=1 SCAR=0.0
Pod documented
310 83 my $state = shift;
311 83 return [ unpack( "c*", $state ) ];
312 }
313  
314 sub display_state
315 {
316 0 ▶ my $self = shift;
Subroutine display_state: not called CC=3 SCAR=24.8
Pod documented
317 0 my $packed_state = shift;
318  
319 0 my $state = $self->unpack_state($packed_state);
320  
321 0 my $plank_data = $self->_process_plank_data($state);
322  
323 0 my @strings;
324 0 foreach my $p (@$plank_data)
325 {
326 push @strings,
327 sprintf( "(%i,%i) -> (%i,%i) %s",
328 $p->{'x'}, $p->{'y'}, $p->{'end_x'}, $p->{'end_y'},
329 0 ▶ ( $p->{'active'} ? "[active]" : "" ) );
Branch: 1 branch
BranchTrueFalseTotal
$p->{'active'} ? : 0 0 0
330 }
331 0 return join( " ; ", @strings );
332 }
333  
334 sub check_if_final_state
335 {
336 83 ▶ my $self = shift;
Subroutine check_if_final_state: called CC=4 SCAR=14.7
Pod documented
337  
338 83 my $state = shift;
339  
340 83 my $plank_data = $self->_process_plank_data($state);
341  
342 83 my $goal_x = $self->{'goal_x'};
343 83 my $goal_y = $self->{'goal_y'};
344  
345 return (
346 scalar(
347 grep {
348 83 ( ( $_->{'x'} == $goal_x ) && ( $_->{'y'} == $goal_y ) )
349 || ( ( $_->{'end_x'} == $goal_x )
350 332 ▶ && ( $_->{'end_y'} == $goal_y ) )
Branch: 1 branch
BranchTrueFalseTotal
$_->{"x"} == $goal_x and $_->{"y"} == $goal_y or $_->{"end_x"} == $goal_x and $_->{"end_y"} == $goal_y 332 0 332
Condition55%5/9
$_->{'x'} == $goal_x && $_->{'y'} == $goal_y || $_->{'end_x'} == $goal_x && $_->{'end_y'} == $goal_y
l!l&&r!l&&!r
01331
$_->{"x"} == $goal_x and $_->{"y"} == $goal_y
!ll&&!rl&&r
33200
$_->{"end_x"} == $goal_x and $_->{"end_y"} == $goal_y
!ll&&!rl&&r
33101
MC/DC25%1/4
$_->{'x'} == $goal_x && $_->{'y'} == $goal_y || $_->{'end_x'} == $goal_x && $_->{'end_y'} == $goal_y
$_->{"x"} == $goal_x $_->{"y"} == $goal_y $_->{"end_x"} == $goal_x $_->{"end_y"} == $goal_y
Truth table28%2/7
A and B || C and D
A= $_->{"x"} == $goal_xB= $_->{"y"} == $goal_yC= $_->{"end_x"} == $goal_xD= $_->{"end_y"} == $goal_y
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
351 } @$plank_data
352 ) > 0
353 );
354 }
355  
356 sub enumerate_moves
357 {
358 82 ▶ my $self = shift;
Subroutine enumerate_moves: called CC=21 SCAR=30.4
Pod documented
359  
360 82 my $state = shift;
361  
362 82 my $plank_data = $self->_process_plank_data($state);
363  
364 # Declare some accessors
365 82 my $board = $self->{'board'};
366 82 my $width = $self->{'width'};
367 82 my $height = $self->{'height'};
368  
369 82 my $dirs_ptr = $self->{'dirs'};
370  
371 82 my @moves;
372  
373 my %current;
374 my $serialize = sub {
375 2851 ▶ return join "\t", @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
376 82 };
377  
378 82 foreach my $plank (@$plank_data)
379 {
380 $current{ $serialize->( @$plank{qw(x y end_x end_y)} ) } =
381 328 $current{ $serialize->( @$plank{qw(end_x end_y x y)} ) } = 1;
382 }
383  
384 82 for my $to_move_idx ( 0 .. $#$plank_data )
385 {
386 328 my $to_move = $plank_data->[$to_move_idx];
387 328 my $len = $to_move->{'len'};
388 328 ▶ if ( !( $to_move->{'active'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($to_move->{"active"}) 166 162 328
389 {
390 166 next;
391 }
392 162 foreach my $move_to (@$plank_data)
393 {
394 648 ▶ if ( !( $move_to->{'active'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($move_to->{"active"}) 256 392 648
395 {
396 256 next;
397 }
398 392 for my $point (
399 [ $move_to->{'x'}, $move_to->{'y'} ],
400 [ $move_to->{'end_x'}, $move_to->{'end_y'} ]
401 )
402 {
403 784 my ( $x, $y ) = @$point;
404 784 DIR_LOOP: for my $dir (@$dirs_ptr) # (qw(E W S N))
405 {
406 # Find the other ending points of the plank
407 3136 my $other_x = $x + $cell_dirs{$dir}->[0] * $len;
408 3136 my $other_y = $y + $cell_dirs{$dir}->[1] * $len;
409  
410 # Check if we are within bounds
411 3136 ▶ if ( ( $other_x < 0 ) || ( $other_x >= $width ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($other_x < 0 or $other_x >= $width) 348 2788 3136
Condition100%3/3
$other_x < 0 or $other_x >= $width
l!l&&r!l&&!r
1022462788
MC/DC100%2/2
$other_x < 0 or $other_x >= $width
$other_x < 0 $other_x >= $width
Truth table100%3/3
A or B
A= $other_x < 0B= $other_x >= $width
ABresult
1X1
011
000
412 {
413 348 next;
414 }
415 2788 ▶ if ( ( $other_y < 0 ) || ( $other_y >= $height ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($other_y < 0 or $other_y >= $height) 593 2195 2788
Condition100%3/3
$other_y < 0 or $other_y >= $height
l!l&&r!l&&!r
2393542195
MC/DC100%2/2
$other_y < 0 or $other_y >= $height
$other_y < 0 $other_y >= $height
Truth table100%3/3
A or B
A= $other_y < 0B= $other_y >= $height
ABresult
1X1
011
000
416 {
417 593 next;
418 }
419  
420 # Check that we're not moving one plank on top of the
421 # other or itself.
422 2195 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $current{&$serialize($x, $y, $other_x, $other_y)}) 558 1637 2195
423 exists
424 $current{ $serialize->( $x, $y, $other_x, $other_y ) } )
425 {
426 558 next DIR_LOOP;
427 }
428  
429 # Check if there is a stump at the other end-point
430 1637 ▶ if ( !$board->[$other_y]->[$other_x] )
Branch: 1 branch
BranchTrueFalseTotal
unless ($board->[$other_y][$other_x]) 1378 259 1637
431 {
432 1378 next;
433 }
434  
435 # Check the validity of the intermediate points.
436 259 ▶ for ( my $offset = 1 ; $offset < $len ; $offset++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $len) 276 238 514
437 {
438 276 my $ix = $x + $cell_dirs{$dir}->[0] * $offset;
439 276 my $iy = $y + $cell_dirs{$dir}->[1] * $offset;
440  
441 276 ▶ if ( $board->[$iy]->[$ix] )
Branch: 1 branch
BranchTrueFalseTotal
if ($board->[$iy][$ix]) 15 261 276
442 {
443 15 next DIR_LOOP;
444 }
445  
446 # Check if another plank has this point in between
447 261 my $collision_plank_idx = 0;
448 261 for my $plank (@$plank_data)
449 {
450 # Make sure we don't test a plank against
451 # a collisions with itself.
452 1032 ▶ if ( $collision_plank_idx == $to_move_idx )
Branch: 1 branch
BranchTrueFalseTotal
if ($collision_plank_idx == $to_move_idx) 261 771 1032
453 {
454 261 next;
455 }
456 771 my $p_x = $plank->{'x'};
457 771 my $p_y = $plank->{'y'};
458 771 my $plank_dir = $plank->{'dir'};
459 771 for my $i ( 0 .. $plank->{'len'} )
460 {
461 1892 ▶ if ( ( $p_x == $ix ) && ( $p_y == $iy ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($p_x == $ix and $p_y == $iy) 6 1886 1892
Condition100%3/3
$p_x == $ix and $p_y == $iy
!ll&&!rl&&r
15623246
MC/DC100%2/2
$p_x == $ix and $p_y == $iy
$p_x == $ix $p_y == $iy
Truth table100%3/3
A and B
A= $p_x == $ixB= $p_y == $iy
ABresult
0X0
100
111
462 {
463 6 next DIR_LOOP;
464 }
465 }
466 continue
467 {
468 1886 $p_x += $cell_dirs{$plank_dir}->[0];
469 1886 $p_y += $cell_dirs{$plank_dir}->[1];
470 }
471 }
472 continue
473 {
474 1026 $collision_plank_idx++;
475 }
476 }
477  
478 # A perfectly valid move - let's add it.
479 238 push @moves,
480 {
481 'p' => $to_move_idx,
482 'x' => $x,
483 'y' => $y,
484 'dir' => $dir
485 };
486 }
487 }
488 }
489 }
490  
491 82 return @moves;
492 }
493  
494 sub perform_move
495 {
496 238 ▶ my $self = shift;
Subroutine perform_move: called CC=7 SCAR=20.6
Pod documented
497  
498 238 my $state = shift;
499 238 my $m = shift;
500  
501 238 my $plank_data = $self->_process_plank_data($state);
502  
503 238 my ( $x, $y, $p, $dir ) = @{$m}{qw(x y p dir)};
504 238 my $dir_idx;
505 238 ▶ if ( $dir eq "S" )
Branch: 6 branches
BranchTrueFalseTotal
if ($dir eq 'S') { } 34 204 238
elsif ($dir eq 'E') { } 21 183 204
elsif ($dir eq 'N') { } 76 107 183
elsif ($dir eq 'W') { } 107 0 107
elsif ($dir eq 'NW') { } 0 0 0
elsif ($dir eq 'SE') { } 0 0 0
506 {
507 34 $dir_idx = 1;
508 }
509 elsif ( $dir eq "E" )
510 {
511 21 $dir_idx = 0;
512 }
513 elsif ( $dir eq "N" )
514 {
515 76 $dir_idx = 1;
516 76 $y -= $self->{'plank_lens'}->[$p];
517 }
518 elsif ( $dir eq "W" )
519 {
520 107 $dir_idx = 0;
521 107 $x -= $self->{'plank_lens'}->[$p];
522 }
523 elsif ( $dir eq "NW" )
524 {
525 0 $dir_idx = 2;
526 0 $y -= $self->{'plank_lens'}->[$p];
527 0 $x -= $self->{'plank_lens'}->[$p];
528 }
529 elsif ( $dir eq "SE" )
530 {
531 0 $dir_idx = 2;
532 }
533  
534 238 my $new_state = [@$state];
535  
536 238 @$new_state[0] = $p;
537 238 @$new_state[ ( 1 + $p * 3 ) .. ( 1 + $p * 3 + 2 ) ] = ( $x, $y, $dir_idx );
538  
539 238 $self->_process_plank_data($new_state);
540  
541 238 return $new_state;
542 }
543  
544 sub render_move
545 {
546 19 ▶ my $self = shift;
Subroutine render_move: called CC=2 SCAR=7.2
Pod documented
547  
548 19 my $move = shift;
549  
550 19 ▶ if ($move)
Branch: 1 branch
BranchTrueFalseTotal
if ($move) { } 19 0 19
551 {
552 return sprintf(
553 "Move the Plank of Length %i to (%i,%i) %s",
554 $self->{'plank_lens'}->[ $move->{'p'} ],
555 19 @{$move}{qw(x y dir)}
556 );
557 }
558 else
559 {
560 0 return "";
561 }
562 }
563  
564 5 1;
565  
566 __END__
« blib/lib/Games/LMSolve/Tilt/RedBlue.pm ↑ Summary blib/lib/Games/LMSolve/Tilt/Base.pm »