SVG/Rect.pm

filter: Statementstmt 97.0% 98 / 101 Branchbran 96.1% 25 / 26 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 94.4% 17 / 18 Podpod 100% 10 / 10 totaltotal 95.0% 155 / 163 CC 18 SCAR 15.9
CC
18
Cov
96%
transform_grid
11.0
remove_horiz_padding
5.1
new
2.0
CRAP 4.9
« SVG/HexCells.pm ↑ Summary SVG/Hex.pm »
1 # SVG maze output
2 # Performs transformation, cleanup, and printing of output of Games::Maze
3  
4 package Games::Maze::SVG::Rect;
5  
6 28 ▶use base Games::Maze::SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Games::Maze;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Games::Maze::SVG::Rect - Build rectangular mazes in SVG.
16  
17 =head1 VERSION
18  
19 Version 0.90
20  
21 =cut
22  
23 28 our $VERSION = 0.90;
24  
25 =head1 SYNOPSIS
26  
27 Games::Maze::SVG::Rect uses the Games::Maze module to create mazes in SVG.
28  
29 use Games::Maze::SVG;
30  
31 my $foo = Games::Maze::SVG->new( 'Rect' );
32 ...
33  
34 =cut
35  
36 28 ▶use constant DELTA_X => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 28 ▶use constant DELTA_Y => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # ----------------
40 # Shape transformation tables
41 28 my %Blocks = (
42 ': - |' => 'ul',
43 ':- |' => 'ur',
44 ': -| ' => 'll',
45 ':- | ' => 'lr',
46 ':-- ' => 'h',
47 '-:: ' => 'h',
48 ': ||' => 'v',
49 '| ::' => 'v',
50 ':- ' => 'l',
51 ': - ' => 'r',
52 ': | ' => 't',
53 ': |' => 'd',
54 ': -||' => 'tr',
55 ':- ||' => 'tl',
56 ':--| ' => 'tu',
57 ':-- |' => 'td',
58 ':--||' => 'cross',
59 ':.- |' => 'oul',
60 ':- .|' => 'our',
61 ': -.|' => 'oul',
62 ':-. |' => 'our',
63 ':.-.|' => 'oul',
64 ':-..|' => 'our',
65 ':.-| ' => 'oll',
66 ':-.| ' => 'olr',
67 ': -|.' => 'oll',
68 ':- |.' => 'olr',
69 ':.-|.' => 'oll',
70 ':-.|.' => 'olr',
71 ':--. ' => 'oh',
72 '-::. ' => 'oh',
73 ':-- .' => 'oh',
74 '-:: .' => 'oh',
75 ':. ||' => 'ov',
76 '|. ::' => 'ov',
77 ': .||' => 'ov',
78 '| .::' => 'ov',
79 ':- . ' => 'ol',
80 ': -. ' => 'or',
81 ':-. ' => 'ol',
82 ':.- ' => 'or',
83 ':- .' => 'ol',
84 ': - .' => 'or',
85 ':. | ' => 'ot',
86 ':. |' => 'od',
87 ': .| ' => 'ot',
88 ': . |' => 'od',
89 ': |.' => 'ot',
90 ': .|' => 'od',
91 ':. |.' => 'ot',
92 ':. .|' => 'od',
93 ': .|.' => 'ot',
94 ': ..|' => 'od',
95 ':.-||' => 'otr',
96 ':-.||' => 'otl',
97 ':--|.' => 'otu',
98 ':--.|' => 'otd',
99 );
100  
101 28 my %Walls = _get_wall_forms();
102  
103 =head1 FUNCTIONS
104  
105 =cut
106  
107 # ----------------------------------------------
108 # Subroutines
109  
110 =over 4
111  
112 =item new
113  
114 Create a new Games::Maze::SVG::Rect object. Supports the following named
115 parameters:
116  
117 =over 4
118  
119 =item wallform
120  
121 String naming the wall format. Legal values are bevel, round, roundcorners,
122 and straight.
123  
124 =item crumb
125  
126 String describing the breadcrumb design. Legal values are dash,
127 dot, line, and none
128  
129 =item dir
130  
131 Directory in which to find the ecmascript for the maze interactivity. Should
132 either be relative, or in URL form.
133  
134 =back
135  
136 =cut
137  
138 sub new
139 17 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
140 17 my $class = shift;
141  
142 17 my $obj = { Games::Maze::SVG::init_object( @_ ), @_, };
143  
144 17 ▶ if( !exists $Walls{ $obj->{wallform} } )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Walls{$obj->{"wallform"}}) 1 16 17
145 {
146 1 my $forms = join( ", ", sort keys %Walls );
147 1 croak "\n'$obj->{wallform}' is not a valid wall form.\nTry one of: $forms\n\n";
148 }
149  
150 16 $obj->{mazeparms}->{cell} = 'Quad';
151 16 $obj->{mazeparms}->{form} = 'Rectangle';
152 16 $obj->{scriptname} = "rectmaze.es";
153 16 $obj->{dx} = DELTA_X;
154 16 $obj->{dy} = DELTA_Y;
155  
156 16 return bless $obj, $class;
157 }
158  
159 =item is_hex
160  
161 Method always returns false.
162  
163 =cut
164  
165 sub is_hex
166 2 ▶{
Subroutine is_hex: called CC=1 SCAR=0.0
Pod documented
167 2 return;
168 }
169  
170 =item is_hex_shaped
171  
172 Method always returns false.
173  
174 =cut
175  
176 sub is_hex_shaped
177 2 ▶{
Subroutine is_hex_shaped: called CC=1 SCAR=0.0
Pod documented
178 2 return;
179 }
180  
181 =item set_wall_form
182  
183 Set the wall format for the current maze.
184  
185 =over 4
186  
187 =item $form
188  
189 String specifying a wall format.
190  
191 =back
192  
193 Returns a reference to self for chaining.
194  
195 =cut
196  
197 sub set_wall_form
198 5 ▶{
Subroutine set_wall_form: called CC=2 SCAR=6.9
Pod documented
199 5 my $self = shift;
200 5 my $form = shift;
201  
202 5 ▶ if( exists $Walls{$form} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Walls{$form}) { } 4 1 5
203 {
204 4 $self->{wallform} = $form;
205 }
206 else
207 {
208 1 my $forms = join( ", ", sort keys %Walls );
209 1 croak "\n'$form' is not a valid wall form.\nTry one of: $forms\n\n";
210 }
211 4 return $self;
212 }
213  
214 =item transform_grid
215  
216 Convert the rectangular grid from ascii format to SVG definition
217 references.
218  
219 =over 4
220  
221 =item $rows
222  
223 Reference to an array of rows
224  
225 =item $walls
226  
227 String specifying wall format.
228  
229 =back
230  
231 =cut
232  
233 sub transform_grid
234 15 ▶{
Subroutine transform_grid: called CC=11 SCAR=24.0
Pod documented
235 15 my $self = shift;
236 15 my $rows = shift;
237 15 my $walls = shift;
238 15 my @out = ();
239  
240 15 ▶ my $sp = 'bevel' eq ( $walls || q{} ) ? q{.} : q{ };
Branch: 1 branch
BranchTrueFalseTotal
'bevel' eq ($walls || '') ? : 7 8 15
Condition100%2/2
$walls || ''
l!l
141
MC/DC100%1/1
$walls || ''
$walls
Truth table100%2/2
A
A= $walls
Aresult
11
00
241 15 remove_horiz_padding( $rows );
242  
243 # transform the printout into block commands
244 15 my $height = @{$rows};
245 15 my $width = @{ $rows->[0] };
246 15 ▶ for ( my $r = 0; $r < $height; ++$r )
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $height) 89 13 102
247 {
248 89 ▶ for ( my $c = 0; $c < $width; ++$c )
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $width) 695 87 782
249 {
250 695 ▶ if( $rows->[$r]->[$c] eq q{ } )
Branch: 1 branch
BranchTrueFalseTotal
if ($rows->[$r][$c] eq ' ') { } 269 426 695
251 {
252 269 $out[$r]->[$c] = 0;
253 }
254 else
255 {
256  
257 # convert the cell and its neighbors into a signature
258 426 ▶ my $sig = $rows->[$r]->[$c] # cell
Branch: 3 branches
BranchTrueFalseTotal
$c == 0 ? : 89 337 426
$r == 0 ? : 81 345 426
$$rows[$r + 1] ? : 345 81 426
Condition66%2/3
$rows->[$r][$c + 1] || $sp
l!l&&r!l&&!r
339870
MC/DC0%0/2
$rows->[$r][$c + 1] || $sp
$rows->[$r][$c + 1] $sp
Truth table66%2/3
A || B
A= $rows->[$r][$c + 1]B= $sp
ABresult
1X1
011
000
259 . ( $c == 0 ? $sp : $rows->[$r]->[ $c - 1 ] ) # left neighbor
260 . ( $rows->[$r]->[ $c + 1 ] || $sp ) # right neighbor
261 . ( $r == 0 ? $sp : $rows->[ $r - 1 ]->[$c] ) # up neighbor
262 . ( $rows->[ $r + 1 ] ? $rows->[ $r + 1 ]->[$c] : $sp ); # down neighbor
263 # convert the signature into the block name
264 426 ▶ croak "Missing block for '$sig'.\n" unless exists $Blocks{$sig};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Blocks{$sig} 2 424 426
265 424 $out[$r]->[$c] = $Blocks{$sig};
266 }
267 }
268 }
269  
270 13 return @{$rows} = @out;
271 }
272  
273 =item remove_horiz_padding
274  
275 Remove the extra horizontal space inserted to regularize the look
276 of the rectangular maze
277  
278 =over 4
279  
280 =item $rows
281  
282 Reference to an array of rows
283  
284 =back
285  
286 =cut
287  
288 sub remove_horiz_padding
289 15 ▶{
Subroutine remove_horiz_padding: called CC=5 SCAR=16.2
Pod documented
290 15 my $rows = shift;
291  
292 15 ▶ for ( my $i = $#{ $rows->[0] }; $i > 0; $i -= 3 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 39 15 54
293 {
294 39 splice( @{$_}, $i - 1, 1 ) foreach ( @{$rows} );
295 }
296  
297 # apparently trailing spaces that I wasn't aware of.
298 15 foreach my $r ( @{$rows} )
299 {
300 0 ▶ pop @{$r} if $r->[-1] eq q{ };
Branch: 1 branch
BranchTrueFalseTotal
if $r->[-1] eq " " 0 89 89
301 }
302  
303 15 return;
304 }
305  
306 =item wall_definitions
307  
308 Method that returns the definition for the shapes used to build the walls.
309  
310 =cut
311  
312 sub wall_definitions
313 0 ▶{
Subroutine wall_definitions: not called CC=1 SCAR=6.9
Pod documented
314 0 my $self = shift;
315  
316 0 return $Walls{ $self->{wallform} };
317 }
318  
319 # _get_wall_forms
320 #
321 #Extract the wall forms from the DATA file handle.
322 #
323 #Returns a hash of wall forms.
324  
325 sub _get_wall_forms
326 28 ▶{
Subroutine _get_wall_forms: called CC=1 SCAR=0.0
327 28 local $/ = "\n===\n";
328 28 chomp( my @list = <DATA> );
329  
330 28 $/ = "\n";
331 28 chomp( @list );
332  
333 28 return @list;
334 }
335  
336 =item convert_start_position
337  
338 Convert the supplied x and y coordinates into the appropriate real coordinates
339 for a start position on this map.
340  
341 =over 4
342  
343 =item $x x coord from the maze
344  
345 =item $y y coord from the maze
346  
347 =back
348  
349 returns a two element list containing (x, y).
350  
351 =cut
352  
353 sub convert_start_position
354 2 ▶{
Subroutine convert_start_position: called CC=1 SCAR=0.0
Pod documented
355 2 my $self = shift;
356 2 my ( $x, $y ) = @_;
357  
358 2 $x = 2 * ( $x - 1 ) + 1;
359 2 $y = 2 * ( $y - 1 );
360  
361 2 return ( $x, $y );
362 }
363  
364 =item convert_end_position
365  
366 Convert the supplied x and y coordinates into the appropriate real coordinates
367 for a end position on this map.
368  
369 =over 4
370  
371 =item $x x coord from the maze
372  
373 =item $y y coord from the maze
374  
375 =back
376  
377 returns a two element list containing (x, y).
378  
379 =cut
380  
381 sub convert_end_position
382 2 ▶{
Subroutine convert_end_position: called CC=1 SCAR=0.0
Pod documented
383 2 my $self = shift;
384 2 my ( $x, $y ) = @_;
385  
386 2 $x = 2 * ( $x - 1 ) + 1;
387 2 $y = 2 * ( $y - 1 ) + 2;
388  
389 2 return ( $x, $y );
390 }
391  
392 =item convert_sign_position
393  
394 Convert the supplied x and y coordinates into the appropriate real coordinates
395 for a the position of the exit sign.
396  
397 =over 4
398  
399 =item $x x coord from the maze
400  
401 =item $y y coord from the maze
402  
403 =back
404  
405 returns a two element list containing (x, y).
406  
407 =cut
408  
409 sub convert_sign_position
410 4 ▶{
Subroutine convert_sign_position: called CC=2 SCAR=6.9
Pod documented
411 4 my $self = shift;
412 4 my ( $x, $y ) = @_;
413  
414 4 $x *= $self->dx();
415 4 $y *= $self->dy();
416  
417 4 $x += 0.5 * $self->dx();
418  
419 # adjust bottom
420 4 ▶ if( $y > $self->{height} / 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($y > $self->{'height'} / 2) { } 2 2 4
421 {
422 2 $y += 2 * $self->dy();
423 }
424 else
425 {
426 2 $y -= $self->dy();
427 }
428  
429 4 return ( $x, $y );
430 }
431  
432 =back
433  
434 =head1 AUTHOR
435  
436 G. Wade Johnson, C<< <wade@anomaly.org> >>
437  
438 =head1 BUGS
439  
440 Please report any bugs or feature requests to
441 C<bug-game-maze-svg@rt.cpan.org>, or through the web interface at
442 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Game-Maze-SVG>.
443 I will be notified, and then you'll automatically be notified of progress on
444 your bug as I make changes.
445  
446 =head1 ACKNOWLEDGEMENTS
447  
448 Thanks go to Valen Johnson and Jason Wood for extensive test play of the
449 mazes.
450  
451 =head1 COPYRIGHT & LICENSE
452  
453 Copyright 2004-2006 G. Wade Johnson, all rights reserved.
454  
455 This program is free software; you can redistribute it and/or modify it
456 under the same terms as Perl itself.
457  
458 =cut
459  
460 28 1;
461  
462 __DATA__
« SVG/HexCells.pm ↑ Summary SVG/Hex.pm »