blib/lib/Games/LMSolve/Alice.pm

filter: Statementstmt 13.6% 16 / 117 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 28.5% 4 / 14 Podpod 100% 8 / 8 totaltotal 14.5% 28 / 193 SCAR 59.3
CC
22
Cov
10%
input_board
210.0
perform_move
72.0
__ANON__
6.0
CRAP 377.7
↑ Summary blib/lib/Games/LMSolve/Minotaur.pm »
1 package Games::LMSolve::Alice;
2 3 $Games::LMSolve::Alice::VERSION = '0.14.0';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Games::LMSolve::Base qw(%cell_dirs);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 @ISA = qw(Games::LMSolve::Base);
11  
12 3 my %cell_flags = (
13 'ADD' => 1,
14 'SUB' => -1,
15 'GOAL' => 0,
16 'START' => 1,
17 'BLANK' => 0,
18 );
19  
20 sub input_board
21 {
22 0 ▶ my $self = shift;
Subroutine input_board: not called CC=14 SCAR=53.5
Pod documented
23  
24 0 my $filename = shift;
25  
26 0 my $spec = {
27 'dims' => { 'type' => "xy(integer)", 'required' => 1 },
28 'layout' => { 'type' => "layout", 'required' => 1 },
29 };
30  
31 0 my $input_obj = Games::LMSolve::Input->new();
32 0 my $input_fields = $input_obj->input_board( $filename, $spec );
33  
34 my ( $width, $height ) =
35 0 @{ $input_fields->{'dims'}->{'value'} }{ 'x', 'y' };
36  
37 0 my (@board);
38  
39 my $line;
40 0 my $line_number = 0;
41 0 my $lines_ref = $input_fields->{'layout'}->{'value'};
42  
43 my $read_line = sub {
44 0 ▶ if ( scalar(@$lines_ref) == $line_number )
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$lines_ref == $line_number) 0 0 0
45 {
46 0 return 0;
47 }
48 0 $line = $lines_ref->[$line_number];
49 0 $line_number++;
50 0 return 1;
51 0 };
52  
53 my $gen_exception = sub {
54 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 die "$text on $filename at line "
56 0 . ( $input_fields->{'layout'}->{'line_num'} + $line_number + 1 )
57 . "!\n";
58 0 };
59  
60 0 my ( $y, $x );
61 0 my ( $start_x, $start_y );
62  
63 0 $y = 0;
64 0 $x = 0;
65  
66 0 ▶INPUT_LOOP: while ( $read_line->() )
Branch: 1 branch
BranchTrueFalseTotal
if (&$read_line()) 0 0 0
67 {
68 0 ▶ while ( length($line) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > 0) 0 0 0
69 {
70 0 $line =~ s/^\s+//;
71 0 ▶ if ( $line =~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\S/) 0 0 0
72 {
73 0 ▶ if ( $line =~ /^\[([^\]]*)\]/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[([^\]]*)\]/) { } 0 0 0
elsif ($line =~ /^#/) { } 0 0 0
74 {
75 0 my $flags_string = uc($1);
76 0 my @flags = ( split( /,/, $flags_string ) );
77 0 my @dirs = ( grep { exists( $cell_dirs{$_} ) } @flags );
78 my @flag_flags =
79 0 ( grep { exists( $cell_flags{$_} ) } @flags );
80 my @unknown_flags = (
81 grep {
82 0 ( !exists( $cell_dirs{$_} ) )
83 0 ▶ && ( !exists( $cell_flags{$_} ) )
Condition0%0/3
exists $Games::LMSolve::Alice::cell_dirs{$_} || exists $cell_flags{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $Games::LMSolve::Alice::cell_dirs{$_} || exists $cell_flags{$_}
exists $Games::LMSolve::Alice::cell_dirs{$_} exists $cell_flags{$_}
Truth table0%0/3
A || B
A= exists $Games::LMSolve::Alice::cell_dirs{$_}B= exists $cell_flags{$_}
ABresult
1X1
011
000
84 } @flags
85 );
86 0 ▶ if ( scalar(@unknown_flags) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @unknown_flags) 0 0 0
87 {
88 0 $gen_exception->( "Unknown Flags on Cell ("
89 . join( ",", @unknown_flags )
90 . ")" );
91 }
92 $board[$y][$x] = {
93 0 'dirs' => { map { $_ => $cell_dirs{$_} } @dirs },
94 'flags' =>
95 0 { map { $_ => $cell_flags{$_} } @flag_flags },
96 };
97  
98 0 ▶ if ( exists( $board[$y][$x]->{'flags'}->{'START'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $board[$y][$x]{"flags"}{"START"}) 0 0 0
99 {
100 0 ▶ if ( defined($start_x) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start_x) 0 0 0
101 {
102 0 $gen_exception->("Two starts were defined!\n");
103 }
104 0 $start_x = $x;
105 0 $start_y = $y;
106 }
107 0 $x++;
108 0 ▶ if ( $x == $width )
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $width) 0 0 0
109 {
110 0 $x = 0;
111 0 $y++;
112 0 ▶ if ( $y == $height )
Branch: 1 branch
BranchTrueFalseTotal
if ($y == $height) 0 0 0
113 {
114 0 last INPUT_LOOP;
115 }
116 }
117 0 $line =~ s/^.*?\]//;
118 }
119 elsif ( $line =~ /^#/ )
120 {
121 # Do nothing - it's a comment
122 0 $line = "";
123 }
124 else
125 {
126 0 $gen_exception->("Junk at Line");
127 }
128 }
129 }
130 }
131  
132 0 ▶ if ( $y != $height )
Branch: 1 branch
BranchTrueFalseTotal
if ($y != $height) 0 0 0
133 {
134 0 $gen_exception->(
135 "Input Terminated Prematurely after reading y=$y x=$x");
136 }
137  
138 0 ▶ if ( !defined($start_x) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_x) 0 0 0
139 {
140 0 $gen_exception->("The Starting Position was not defined anywhere");
141 }
142  
143 0 $self->{'height'} = $height;
144 0 $self->{'width'} = $width;
145 0 $self->{'board'} = \@board;
146  
147 0 return [ $start_x, $start_y, 1 ];
148 }
149  
150 # A function that accepts the expanded state (as an array ref)
151 # and returns an atom that represents it.
152 sub pack_state
153 {
154 0 ▶ my $self = shift;
Subroutine pack_state: not called CC=1 SCAR=6.9
Pod documented
155 0 my $state_vector = shift;
156 0 return pack( "ccc", @{$state_vector} );
157 }
158  
159 # A function that accepts an atom that represents a state
160 # and returns an array ref that represents it.
161 sub unpack_state
162 {
163 0 ▶ my $self = shift;
Subroutine unpack_state: not called CC=1 SCAR=6.9
Pod documented
164 0 my $state = shift;
165 0 return [ unpack( "ccc", $state ) ];
166 }
167  
168 # Accept an atom that represents a state and output a
169 # user-readable string that describes it.
170 sub display_state
171 {
172 0 ▶ my $self = shift;
Subroutine display_state: not called CC=1 SCAR=6.9
Pod documented
173 0 my $state = shift;
174 0 my ( $x, $y, $d ) = @{ $self->unpack_state($state) };
175 0 return sprintf( "X = %i ; Y = %i ; d = %i", $x + 1, $y + 1, $d );
176 }
177  
178 # This function checks if a state it receives as an argument is a
179 # dead-end one.
180 sub check_if_unsolvable
181 {
182 0 ▶ my $self = shift;
Subroutine check_if_unsolvable: not called CC=1 SCAR=6.9
Pod documented
183 0 my $coords = shift;
184 0 return ( $coords->[2] == 0 );
185 }
186  
187 sub check_if_final_state
188 {
189 0 ▶ my $self = shift;
Subroutine check_if_final_state: not called CC=1 SCAR=6.9
Pod documented
190  
191 0 my $coords = shift;
192 return
193 exists(
194 0 $self->{'board'}->[ $coords->[1] ][ $coords->[0] ]->{'flags'}->{'GOAL'}
195 );
196 }
197  
198 # This function enumerates the moves accessible to the state.
199 # If it returns a move, it still does not mean that it is a valid
200 # one. I.e: it is possible that it is illegal to perform it.
201 sub enumerate_moves
202 {
203 0 ▶ my $self = shift;
Subroutine enumerate_moves: not called CC=1 SCAR=6.9
Pod documented
204  
205 0 my $coords = shift;
206 return
207 keys(
208 0 %{ $self->{'board'}->[ $coords->[1] ][ $coords->[0] ]->{'dirs'} } );
209 }
210  
211 # This function accepts a state and a move. It tries to perform the
212 # move on the state. If it is succesful, it returns the new state.
213 #
214 # Else, it returns undef to indicate that the move is not possible.
215 sub perform_move
216 {
217 0 ▶ my $self = shift;
Subroutine perform_move: not called CC=8 SCAR=42.8
Pod documented
218  
219 0 my $coords = shift;
220 0 my $m = shift;
221  
222 0 my $offsets = [ map { $_ * $coords->[2] } @{ $cell_dirs{$m} } ];
223 0 my @new_coords = @$coords;
224 0 $new_coords[0] += $offsets->[0];
225 0 $new_coords[1] += $offsets->[1];
226  
227 my $new_cell =
228 0 $self->{'board'}->[ $new_coords[1] ][ $new_coords[0] ]->{'flags'};
229  
230 # Check if we are out of the bounds of the board.
231 0 ▶ if ( ( $new_coords[0] < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($new_coords[0] < 0 or $new_coords[0] >= $self->{"width"} or $new_coords[1] < 0 or $new_coords[1] >= $self->{"height"} or exists $new_cell->{"BLANK"}) 0 0 0
Condition0%0/12
$new_coords[0] < 0 or $new_coords[0] >= $self->{"width"} or $new_coords[1] < 0 or $new_coords[1] >= $self->{"height"} or exists $new_cell->{"BLANK"}
l!l&&r!l&&!r
000
$new_coords[0] < 0 or $new_coords[0] >= $self->{"width"} or $new_coords[1] < 0 or $new_coords[1] >= $self->{"height"}
l!l&&r!l&&!r
000
$new_coords[0] < 0 or $new_coords[0] >= $self->{"width"} or $new_coords[1] < 0
l!l&&r!l&&!r
000
$new_coords[0] < 0 or $new_coords[0] >= $self->{"width"}
l!l&&r!l&&!r
000
MC/DC0%0/5
$new_coords[0] < 0 or $new_coords[0] >= $self->{"width"} or $new_coords[1] < 0 or $new_coords[1] >= $self->{"height"} or exists $new_cell->{"BLANK"}
$new_coords[0] < 0 $new_coords[0] >= $self->{"width"} $new_coords[1] < 0 $new_coords[1] >= $self->{"height"} exists $new_cell->{"BLANK"}
Truth table0%0/6
A or B or C or D or E
A= $new_coords[0] < 0B= $new_coords[0] >= $self->{"width"}C= $new_coords[1] < 0D= $new_coords[1] >= $self->{"height"}E= exists $new_cell->{"BLANK"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
232 || ( $new_coords[0] >= $self->{'width'} )
233 || ( $new_coords[1] < 0 )
234 || ( $new_coords[1] >= $self->{'height'} )
235 || exists( $new_cell->{'BLANK'} ) )
236 {
237 0 return;
238 }
239  
240 0 ▶ if ( exists( $new_cell->{'ADD'} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $new_cell->{'ADD'}) { } 0 0 0
elsif (exists $new_cell->{'SUB'}) { } 0 0 0
241 {
242 0 $new_coords[2]++;
243 }
244 elsif ( exists( $new_cell->{'SUB'} ) )
245 {
246 0 $new_coords[2]--;
247 }
248  
249 0 return [@new_coords];
250 }
251  
252 3 1;
253  
254 __END__
↑ Summary blib/lib/Games/LMSolve/Minotaur.pm »