blib/lib/Games/Jumble.pm

filter: Statementstmt 13.0% 20 / 153 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 26.3% 5 / 19 Podpod 100% 7 / 7 totaltotal 12.5% 32 / 256 CC 41 SCAR 41.2
CC
41
Cov
9%
create_jumble
182.0
solve_word
56.0
solve_crossword
56.0
CRAP 61.6
↑ Summary
1 package Games::Jumble;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Games::Jumble - Create and solve Jumble word puzzles.
11  
12 =head1 VERSION
13  
14 Version 0.09
15  
16 =cut
17  
18 2 our $VERSION = '0.09';
19  
20 =head1 SYNOPSIS
21  
22 use Games::Jumble;
23  
24 my $jumble = Games::Jumble->new();
25 $jumble->set_num_words(6);
26 $jumble->set_word_lengths_allowed(5,6);
27 $jumble->set_word_lengths_not_allowed(7,8);
28 $jumble->set_dict('/home/doug/crossword_dict/unixdict.txt');
29  
30 my @jumble = $jumble->create_jumble;
31  
32 foreach my $word (@jumble) {
33 print "$word\n";
34 }
35  
36 # Solve jumbled word
37 my @good_words = $jumble->solve_word('rta');
38  
39 if (@good_words) {
40 foreach my $good_word (@good_words) {
41 print "$good_word\n";
42 }
43 } else {
44 print "No words found\n";
45 }
46  
47 # Create jumbled word
48 my $word = 'camel';
49 my $jumbled_word = $jumble->jumble_word($word);
50  
51 print "$jumbled_word ($word)\n";
52  
53 =head1 DESCRIPTION
54  
55 C<Games::Jumble> is used to create and solve Jumble word puzzles.
56  
57 Currently C<Games::Jumble> will create random five- and six-letter
58 jumbled words from dictionary. Future versions of C<Games::Jumble> will
59 allow user to create custom jumbles by using a user defined word file
60 with words of any length.
61 Individual words of any length may be jumbled by using the
62 C<jumble_word()> method.
63  
64 Default number of words is 5.
65 Default dictionary is '/usr/dict/words'.
66 Dictionary file must contain one word per line.
67  
68 =cut
69  
70 {
71 # Encapsulated data
72 2 my %_attr_data = # DEFAULT ACCESSIBILITY
73 (
74 _num_words => [ 5, 'read/write' ],
75 _dict => [ '/usr/dict/words', 'read/write' ],
76 _word_lengths_allowed => [ '', 'read' ],
77 _word_lengths_not_allowed => [ '', 'read' ],
78 );
79  
80 # Class methods, to operate on encapsulated class data
81 0 ▶ sub _accessible {
Subroutine _accessible: not called CC=1 SCAR=6.9
82 0 my ( $self, $attr, $mode ) = @_;
83 0 $_attr_data{$attr}[1] =~ /$mode/;
84 }
85  
86 # Classwide efault value for a specified object attribute
87 0 ▶ sub _default_for {
Subroutine _default_for: not called CC=1 SCAR=6.9
88 0 my ( $self, $attr ) = @_;
89 0 $_attr_data{$attr}[0];
90 }
91  
92 # List of names of all specified object attributes
93 0 ▶ sub _standard_keys {
Subroutine _standard_keys: not called CC=1 SCAR=6.9
94 0 keys %_attr_data;
95 }
96 }
97  
98 =head2 new
99  
100 This is the constructor for a new Games::Jumble object.
101  
102 my $jumble = Games::Jumble->new();
103  
104 If C<num_words> is passed, this method will set the number of words for the puzzle, otherwise number of words is set to default value of 5.
105  
106 my $jumble = Games::Jumble->new(num_words=>$num_words);
107  
108 =cut
109  
110 2 ▶sub new {
Subroutine new: not called CC=5 SCAR=32.7
Pod documented
111 0 my ( $caller, %arg ) = @_;
112 0 my $caller_is_obj = ref($caller);
113 0 ▶ my $class = $caller_is_obj || $caller;
Condition0%0/3
$caller_is_obj || $caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$caller_is_obj || $caller
$caller_is_obj $caller
Truth table0%0/3
A || B
A= $caller_is_objB= $caller
ABresult
1X1
011
000
114 0 my $self = bless {}, $class;
115 0 foreach my $attrname ( $self->_standard_keys() ) {
116 0 my ($argname) = ( $attrname =~ /^_(.*)/ );
117 0 ▶ if ( exists $arg{$argname} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg{$argname}) { } 0 0 0
elsif ($caller_is_obj) { } 0 0 0
118 0 $self->{$attrname} = $arg{$argname};
119 }
120 elsif ($caller_is_obj) {
121 0 $self->{$attrname} = $caller->{$attrname};
122 }
123 else {
124 0 $self->{$attrname} = $self->_default_for($attrname);
125 }
126 }
127 0 return $self;
128 }
129  
130 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
131  
132 # This space deliberately left blank
133 }
134  
135 # Non autoloaded methods here
136  
137 =head2 set_word_lengths_allowed ( length1 [, length2, length3,...] )
138  
139 If C<lengthx> is(are) passed, this method will set word lengths
140 that will be used when creating jumble.
141 The default setting will use all word lengths.
142 Note: Allow all is designated by empty hash.
143  
144 =cut
145  
146 0 ▶sub set_word_lengths_allowed {
Subroutine set_word_lengths_allowed: not called CC=3 SCAR=24.8
Pod documented
147 0 my($self) = shift;
148 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
149 0 my %allowed;
150 0 foreach my $allow( @_ ) {
151 0 $allowed{$allow}++;
152 }
153 0 $self->{_word_lengths_allowed} = \%allowed;
154 }
155 }
156  
157 =head2 set_word_lengths_not_allowed ( length1 [, length2, length3,...] )
158  
159 If C<lengthx> is(are) passed, this method will set word lengths
160 that will be skipped when creating jumble.
161 The default setting will not skip any word lengths.
162 Note: Skip none is designated by empty hash.
163  
164 =cut
165  
166 0 ▶sub set_word_lengths_not_allowed {
Subroutine set_word_lengths_not_allowed: not called CC=3 SCAR=24.8
Pod documented
167 0 my($self) = shift;
168 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
169 0 my %not_allowed;
170 0 foreach my $length( @_ ) {
171 0 $not_allowed{$length}++;
172 }
173 0 $self->{_word_lengths_not_allowed} = \%not_allowed;
174 }
175 }
176  
177 =head2 create_jumble
178  
179 This method creates the jumble.
180  
181 =cut
182  
183 0 ▶sub create_jumble {
Subroutine create_jumble: not called CC=13 SCAR=52.0
Pod documented
184  
185 0 my($self) = shift;
186 0 my @jumble;
187 my @jumble_out;
188 0 my %words;
189  
190 # Read dictionary and get words
191 0 ▶ open FH, $self->get_dict or croak "Cannot open $self->get_dict: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, $self->get_dict or croak("Cannot open $self->get_dict: $!") 0 0 0
192 0 ▶ while(<FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
193 0 chomp;
194 0 my $word = lc $_; # Lower case all words
195 0 ▶ next if $word !~ /^[a-z]+$/; # Letters only
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /^[a-z]+$/ 0 0 0
196  
197 # Sort letters so we can check for unique "unjumble"
198 0 my @temp_array = split(//, $word);
199 0 @temp_array = sort(@temp_array);
200 0 my $key = join('', @temp_array);
201  
202 # Check for word lengths allowed
203 0 ▶ if( $self->get_word_lengths_allowed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_word_lengths_allowed) 0 0 0
204 0 my $allowed_ref = $self->get_word_lengths_allowed;
205 0 ▶ next unless exists $allowed_ref->{length $_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$allowed_ref{length $_} 0 0 0
206 }
207  
208 # Check for word lengths not allowed
209 0 ▶ if( $self->get_word_lengths_not_allowed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_word_lengths_not_allowed) 0 0 0
210 0 my $not_allowed_ref = $self->get_word_lengths_not_allowed;
211 0 ▶ next if exists $not_allowed_ref->{length $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$not_allowed_ref{length $_} 0 0 0
212 }
213  
214 # perlreftut is your friend
215 0 push @{$words{$key}}, $_;
216
217 }
218 0 close FH;
219  
220 # Get words that only "unjumble" one way
221 0 my @unique_words;
222  
223 0 foreach my $word (keys %words) {
224 0 my $length = @{$words{$word}};
225 0 ▶ if ($length == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 1) 0 0 0
226 0 push @unique_words, @{$words{$word}};
227 }
228 }
229 0 @unique_words = sort @unique_words;
230  
231  
232 # Get random words for jumble
233 0 for (1..$self->get_num_words) {
234 0 my $el = $unique_words[rand @unique_words];
235 0 ▶ redo if $el =~ /(\w)\1+/; # No words like ii, ooo or aaa
Branch: 1 branch
BranchTrueFalseTotal
if $el =~ /(\w)\1+/ 0 0 0
236 0 push(@jumble, $el);
237 }
238  
239 # Scramble the words
240 0 foreach my $word (@jumble) {
241 0 my $jumbled_word = $self->jumble_word($word);
242 0 push @jumble_out, "$jumbled_word ($word)";
243 }
244  
245 0 return @jumble_out;
246  
247 }
248  
249 =head2 jumble_word ( WORD )
250  
251 This method will create a jumbled word.
252 Returns scalar containing jumbled word.
253  
254 =cut
255  
256 0 ▶sub jumble_word {
Subroutine jumble_word: not called CC=5 SCAR=34.0
Pod documented
257  
258 0 my($self) = shift;
259 0 my $word;
260  
261 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
262 0 $word = shift;
263 } else {
264 0 $word = undef;
265 0 return $word;
266 }
267  
268 0 my @temp_array = split(//, $word);
269  
270 # From the camel
271 0 my $array = \@temp_array;
272 0 my $jumbled_word = $word;
273  
274 # Make sure we actually scramble the word
275 0 ▶ while( $jumbled_word eq $word ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jumbled_word eq $word) 0 0 0
276 0 ▶ for (my $i = @$array; --$i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 0 0 0
277 0 my $j = int rand ($i+1);
278 0 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 0 0 0
279 0 @$array[$i,$j] = @$array[$j,$i];
280 }
281 0 $jumbled_word = join('', @temp_array);
282 }
283  
284 0 return $jumbled_word;
285 }
286  
287 =head2 solve_word ( WORD )
288  
289 This method will solve a jumbled word.
290 Returns list of solved words.
291  
292 =cut
293  
294 0 ▶sub solve_word {
Subroutine solve_word: not called CC=7 SCAR=40.3
Pod documented
295 0 my($self) = shift;
296 0 my @good_words;
297
298 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
299 0 $self->{word} = lc(shift);
300 } else {
301 0 croak "No word to solve\n";
302 }
303  
304 0 my @temp_array = split(//, $self->{word});
305 0 @temp_array = sort(@temp_array);
306 0 $self->{key} = join('', @temp_array);
307  
308 # Read dictionary and get words same length as $self->{word}
309 0 ▶ open FH, $self->get_dict or croak "Cannot open $self->get_dict: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, $self->get_dict or croak("Cannot open $self->get_dict: $!") 0 0 0
310 0 ▶ while(<FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
311 0 chomp;
312 0 my $word = lc $_; # Lower case all words
313 0 ▶ next if $word !~ /^[a-z]+$/; # Letters only
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /^[a-z]+$/ 0 0 0
314 0 ▶ next if length($word) ne length($self->{word});
Branch: 1 branch
BranchTrueFalseTotal
if length $word ne length $self->{"word"} 0 0 0
315  
316 # Sort letters so we can check for unique "unjumble"
317 0 my @temp_array = split(//, $word);
318 0 @temp_array = sort(@temp_array);
319 0 my $key = join('', @temp_array);
320  
321 0 ▶ if ($self->{key} eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key"} eq $key) 0 0 0
322 0 push @good_words, $word;
323 }
324 }
325 0 close FH;
326  
327 0 return @good_words;
328 }
329  
330 =head2 solve_crossword ( WORD )
331  
332 This method will solve an incomplete word as needed for a crossword.
333 WORD format: 'c?m?l' where question marks are used a placeholders
334 for unknown letter.
335 Returns list of solved words.
336  
337 =cut
338  
339 0 ▶sub solve_crossword {
Subroutine solve_crossword: not called CC=7 SCAR=40.3
Pod documented
340 0 my($self) = shift;
341 0 my @good_words;
342
343 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
344 0 $self->{word} = lc(shift);
345 } else {
346 0 croak "No word to solve\n";
347 }
348
349 # Set regex
350 0 ($self->{word_regex} = $self->{word}) =~ s/\?/\\w{1}/g;
351  
352 # Read dictionary and get all words same length as $self->{word}
353 0 ▶ open FH, $self->get_dict or croak "Cannot open $self->get_dict: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, $self->get_dict or croak("Cannot open $self->get_dict: $!") 0 0 0
354 0 ▶ while(<FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 0 0 0
355 0 chomp;
356 0 my $word = lc $_; # Lower case all words
357 0 ▶ next if $word !~ /^[a-z]+$/; # Letters only
Branch: 1 branch
BranchTrueFalseTotal
unless $word =~ /^[a-z]+$/ 0 0 0
358 0 ▶ next if length($word) ne length($self->{word});
Branch: 1 branch
BranchTrueFalseTotal
if length $word ne length $self->{"word"} 0 0 0
359  
360 0 ▶ if ($word =~ $self->{word_regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /$self->{"word_regex"}/) 0 0 0
361 0 push @good_words, $word;
362 }
363 }
364 0 close FH;
365  
366 0 return @good_words;
367 }
368  
369 ### autoloaded methods ###
370 # get_num_words, get_dict
371 # set_num_words, set_dict
372 # set_word_lengths_allowed, set_word_lengths_not_allowed
373  
374 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=5 SCAR=31.4
375 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
376 0 my ( $self, $newval ) = @_;
377  
378 # Was it a get_... method?
379 0 ▶ if ( $AUTOLOAD =~ /.*::get(_\w+)/ && $self->_accessible( $1, 'read' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Games::Jumble::AUTOLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1, "read")) 0 0 0
Condition0%0/3
$Games::Jumble::AUTOLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1, "read")
!ll&&!rl&&r
000
MC/DC0%0/2
$Games::Jumble::AUTOLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1, "read")
$Games::Jumble::AUTOLOAD =~ /.*::get(_\w+)/ $self->_accessible($1, "read")
Truth table0%0/3
A and B
A= $Games::Jumble::AUTOLOAD =~ /.*::get(_\w+)/B= $self->_accessible($1, "read")
ABresult
0X0
100
111
380 0 my $attr_name = $1;
381 0 ▶ *{$AUTOLOAD} = sub { return $_[0]->{$attr_name} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 0 return $self->{$attr_name};
383 }
384  
385 # Was it a set_... method?
386 0 ▶ if ( $AUTOLOAD =~ /.*::set(_\w+)/ && $self->_accessible( $1, 'write' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Games::Jumble::AUTOLOAD =~ /.*::set(_\w+)/ and $self->_accessible($1, "write")) 0 0 0
Condition0%0/3
$Games::Jumble::AUTOLOAD =~ /.*::set(_\w+)/ and $self->_accessible($1, "write")
!ll&&!rl&&r
000
MC/DC0%0/2
$Games::Jumble::AUTOLOAD =~ /.*::set(_\w+)/ and $self->_accessible($1, "write")
$Games::Jumble::AUTOLOAD =~ /.*::set(_\w+)/ $self->_accessible($1, "write")
Truth table0%0/3
A and B
A= $Games::Jumble::AUTOLOAD =~ /.*::set(_\w+)/B= $self->_accessible($1, "write")
ABresult
0X0
100
111
387 0 my $attr_name = $1;
388 0 ▶ *{$AUTOLOAD} = sub { $_[0]->{$attr_name} = $_[1]; return };
Subroutine __ANON__: not called CC=1 SCAR=6.9
389 0 $self->{$1} = $newval;
390 0 return;
391 }
392  
393 # Must have been a mistake then
394 0 croak "No such method: $AUTOLOAD";
395 }
396  
397 2 1;
398  
399 =head1 AUTHOR
400  
401 Doug Sparling, C<< <usr_bin_perl at yahoo.com> >>
402  
403 =head1 BUGS
404  
405 Please report any bugs or feature requests to
406 C<bug-games-jumble at rt.cpan.org>, or through the web interface at
407 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Jumble>.
408 I will be notified, and then you'll automatically be notified of progress on
409 your bug as I make changes.
410  
411 =head1 SUPPORT
412  
413 You can find documentation for this module with the perldoc command.
414  
415 perldoc Games::Jumble
416  
417 You can also look for information at:
418  
419 =over 4
420  
421 =item * AnnoCPAN: Annotated CPAN documentation
422  
423 L<http://annocpan.org/dist/Games-Jumble>
424  
425 =item * CPAN Ratings
426  
427 L<http://cpanratings.perl.org/d/Games-Jumble>
428  
429 =item * RT: CPAN's request tracker
430  
431 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Jumble>
432  
433 =item * Search CPAN
434  
435 L<http://search.cpan.org/dist/Games-Jumble>
436  
437 =back
438  
439 =head1 ACKNOWLEDGEMENTS
440  
441 Tim Maher for pointing out some outdated documentation in the Synopsis.
442  
443 =head1 COPYRIGHT & LICENSE
444  
445 Copyright 2001-2007 Doug Sparling, all rights reserved.
446  
447 This program is free software; you can redistribute it and/or modify it
448 under the same terms as Perl itself.
449  
450 =cut
451  
452 2 1; # End of Games::Jumble
↑ Summary