blib/lib/Games/Cryptoquote.pm

filter: Statementstmt 84.0% 205 / 244 Branchbran 65.2% 47 / 72 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 14 totaltotal 74.3% 276 / 371 CC 73 SCAR 26.2
CC
73
Cov
78%
write_patterns
56.0
build_dictionary
21.0
solve
14.1
CRAP 13.8
↑ Summary
1 # Cryptoquote.pm -- Solves Cryptoquote puzzles.
2 #
3 # Copyright (C) 1999-2002 Bob O'Neill
4 # All rights reserved.
5 #
6 # Thanks to Adam Foxson for the prodding and know-how that made
7 # it possible for this module to make it to CPAN.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19  
20 package Games::Cryptoquote;
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 $VERSION = '1.30';
27 1 local $^W = 1;
28  
29 sub new
30 1 ▶{
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
31 1 my $type = shift;
32 1 ▶ my $class = ref($type) || $type;
Condition33%1/3
ref $type || $type
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table33%1/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
33 1 return bless {}, $class;
34 }
35  
36 sub quote
37 3 ▶{
Subroutine quote: called CC=2 SCAR=6.9
Pod undocumented
38 3 my $self = shift;
39 3 ▶ $self->{'quote'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
40 3 return $self->{'quote'}
41 }
42  
43 sub source
44 3 ▶{
Subroutine source: called CC=2 SCAR=6.9
Pod undocumented
45 3 my $self = shift;
46 3 ▶ $self->{'source'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
47 3 return $self->{'source'}
48 }
49  
50 sub timeout
51 2 ▶{
Subroutine timeout: called CC=2 SCAR=6.9
Pod undocumented
52 2 my $self = shift;
53 2 ▶ $self->{'timeout'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
54 2 return $self->{'timeout'}
55 }
56  
57 sub build_dictionary
58 1 ▶{
Subroutine build_dictionary: called CC=10 SCAR=30.4
Pod undocumented
59 1 my $self = shift;
60  
61 1 ▶ croak "Invalid arguments (expecting even number)" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
62 1 my %options = @_;
63 1 my @bad_opts = grep { !/^(?:type|file)$/ } keys %options;
64 1 ▶ croak "Invalid argument(s): (@bad_opts)" if @bad_opts;
Branch: 1 branch
BranchTrueFalseTotal
if @bad_opts 0 1 1
65  
66 1 my $file = $options{'file'};
67 1 my $type = $options{'type'};
68  
69 1 ▶ if ($type eq 'dict')
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'dict') { } 0 1 1
elsif ($type eq 'patterns') { } 1 0 1
70 {
71 0 ▶ if (open DICT, "$file")
Branch: 1 branch
BranchTrueFalseTotal
if (open DICT, "$file") { } 0 0 0
72 {
73 0 my ( $words, $file );
74 0 ▶ while ( <DICT> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DICT)) 0 0 0
75 {
76 0 chomp;
77 0 $self->{'dict_patterns'}{&build_pattern($_)}{$_} = 1;
78 }
79 0 close DICT;
80 0 return 1;
81 }
82 else
83 {
84 0 my ($caller, $line) = (caller(0))[1..2];
85 0 carp "Couldn't read $file: $! (called by $caller line $line)";
86 0 return 0;
87 }
88 }
89 elsif ($type eq 'patterns')
90 {
91 1 ▶ if (open DICT, "$file")
Branch: 1 branch
BranchTrueFalseTotal
if (open DICT, "$file") { } 1 0 1
92 {
93 1 my ( $words, $pattern );
94 1 ▶ while ( <DICT> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DICT)) 9440 1 9441
95 {
96 9440 chomp;
97 9440 ($pattern, $words) = split /:/;
98 9440 for ( split /\|/, $words )
99 {
100 45425 $self->{'dict_patterns'}{$pattern}{$_} = 1;
101 }
102 }
103 1 close DICT;
104 1 return 1;
105 }
106 else
107 {
108 0 my ($caller, $line) = (caller(0))[1..2];
109 0 carp "Couldn't read $file: $! (called by $caller line $line)";
110 0 return 0;
111 }
112 }
113 else
114 {
115 0 croak "Invalid dictionary type ($type)";
116 }
117 }
118  
119 sub write_patterns
120 0 ▶{
Subroutine write_patterns: not called CC=7 SCAR=40.3
Pod undocumented
121 0 my $self = shift;
122  
123 0 ▶ croak "Invalid arguments (expecting even number)" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
124 0 my %options = @_;
125 0 my @bad_opts = grep { !/^(?:dict_file|pattern_file)$/ } keys %options;
126 0 ▶ croak "Invalid argument(s): (@bad_opts)" if @bad_opts;
Branch: 1 branch
BranchTrueFalseTotal
if @bad_opts 0 0 0
127  
128 0 my $dict_file = $options{'dict_file'};
129 0 my $pattern_file = $options{'pattern_file'};
130  
131 0 $self->build_dictionary(file => $dict_file, type => 'dict');
132  
133 0 my %patterns_hash = %{ $self->{'dict_patterns'} };
134  
135 0 ▶ if (-e $pattern_file)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pattern_file) 0 0 0
136 {
137 0 carp "$pattern_file exists. I won't overwrite it.\n";
138 0 carp "You probably want to remove the call to write_patterns.\n";
139 }
140  
141 0 ▶ open PATTERNS, ">$pattern_file" or croak "Couldn't write $pattern_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open PATTERNS, ">$pattern_file" or croak("Couldn't write ${pattern_file}: $!") 0 0 0
142 0 for my $pattern (sort keys %patterns_hash)
143 {
144 0 print PATTERNS "$pattern:";
145  
146 0 for my $word (sort keys %{ $patterns_hash{$pattern} })
147 {
148 0 print PATTERNS "$word|";
149 }
150  
151 0 print PATTERNS "\n";
152 }
153 0 close PATTERNS;
154 }
155  
156 sub solve
157 1 ▶{
Subroutine solve: called CC=12 SCAR=26.5
Pod undocumented
158 1 my $self = shift;
159  
160 1 ▶ my $quote = $self->quote() || '';
Condition50%1/2
$self->quote || ''
l!l
10
MC/DC0%0/1
$self->quote || ''
$self->quote
Truth table50%1/2
A
A= $self->quote
Aresult
11
00
161 1 ▶ my $source = $self->source() || '';
Condition50%1/2
$self->source || ''
l!l
10
MC/DC0%0/1
$self->source || ''
$self->source
Truth table50%1/2
A
A= $self->source
Aresult
11
00
162 1 ▶ my $timeout = $self->timeout() || 0;
Condition50%1/2
$self->timeout || 0
l!l
10
MC/DC0%0/1
$self->timeout || 0
$self->timeout
Truth table50%1/2
A
A= $self->timeout
Aresult
11
00
163 1 ▶ croak "Invalid timeout value $timeout" unless $timeout =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout =~ /^\d+$/ 0 1 1
164  
165 1 $self->{'let_let'} = ();
166 1 $self->{'bad_let_let'} = ();
167  
168 # We will have to do away with the uc() for the quote that's saved
169 # (currently, globalquote or globalquote2).
170 1 $self->{'globalquote'} = $quote;
171 1 $self->{'globalquote'} = uc $quote;
172  
173 1 for ( split /\s/, $self->{'globalquote'} )
174 {
175 12 s/[^a-z]//gi;
176 12 ▶ next if exists $self->{'word_word'}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"word_word"}{$_} 0 12 12
177 12 $self->{'word_word'}{$_} = $self->{'dict_patterns'}{&build_pattern($_)};
178 }
179  
180 1 my $last_num_poss = -1;
181 1 my $time_in = time;
182 1 my $solution = {};
183 1 while ( 1 )
184 {
185 4 my ($current_num_poss,$quote_soln,$hash_ref) = $self->narrow_possibilities();
186 4 ▶ if ( $current_num_poss == $last_num_poss or $current_num_poss == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($current_num_poss == $last_num_poss or $current_num_poss == 0) 1 3 4
Condition66%2/3
$current_num_poss == $last_num_poss or $current_num_poss == 0
l!l&&r!l&&!r
013
MC/DC50%1/2
$current_num_poss == $last_num_poss or $current_num_poss == 0
$current_num_poss == $last_num_poss $current_num_poss == 0
Truth table66%2/3
A or B
A= $current_num_poss == $last_num_possB= $current_num_poss == 0
ABresult
1X1
011
000
187 {
188 1 my $source_soln;
189 1 ▶ if ($quote_soln !~ /\|/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_soln =~ /\|/) 1 0 1
190 {
191 1 $quote_soln = &apply_mapping($self->quote(), $hash_ref);
192 1 $source_soln = &apply_mapping($self->source(), $hash_ref);
193 }
194 1 $self->{'solution'}{'quote'} = $quote_soln;
195 1 $self->{'solution'}{'source'} = $source_soln;
196 1 return 1;
197 }
198 3 $last_num_poss = $current_num_poss;
199  
200 3 ▶ if ($timeout and time - $time_in > $timeout)
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and time - $time_in > $timeout) 0 3 3
Condition33%1/3
$timeout and time - $time_in > $timeout
!ll&&!rl&&r
030
MC/DC0%0/2
$timeout and time - $time_in > $timeout
$timeout time - $time_in > $timeout
Truth table33%1/3
A and B
A= $timeoutB= time - $time_in > $timeout
ABresult
0X0
100
111
201 {
202 0 $self->{'solution'}{'quote'} = '';
203 0 $self->{'solution'}{'source'} = '';
204 0 return 0;
205 }
206 }
207 }
208  
209 sub narrow_possibilities
210 4 ▶{
Subroutine narrow_possibilities: called CC=5 SCAR=16.1
Pod undocumented
211 4 my $self = shift;
212 4 $self->{'let_word_let'} = {};
213 4 $self->{'word_let_let_word'} = {};
214 4 $self->{'best_word_word'} = {};
215  
216 4 $self->algorithm_one();
217 4 $self->algorithm_two();
218 4 $self->algorithm_three();
219  
220 4 $self->{'word_word'} = {};
221 4 $self->{'word_word'} = $self->{'best_word_word'};
222  
223 4 my $num_poss = 0;
224 4 my $soln = '';
225 4 my %good_let_let;
226 4 for my $word1 ( split /\s/, $self->{'globalquote'} )
227 {
228 48 $word1 =~ s/[^a-z]//gi;
229  
230 48 my @temp = keys %{$self->{'best_word_word'}{$word1}};
231 48 $soln .= join( '|', (@temp)) . ' ';
232 48 ▶ $soln .= " #$word1#" if $#temp == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#temp == -1 0 48 48
233 48 $num_poss += $#temp;
234  
235 48 my @chars = split('',$word1);
236 48 for my $poss (@temp) # <-- possibilities for $word1
237 {
238 128 my @poss_chars = split('',$poss);
239 128 for my $i (0..$#chars)
240 {
241 597 $good_let_let{$chars[$i]}{$poss_chars[$i]} = 1;
242 }
243 }
244 }
245  
246 4 return ($num_poss, $soln, \%good_let_let);
247 }
248  
249 sub algorithm_one
250 4 ▶{
Subroutine algorithm_one: called CC=10 SCAR=23.0
Pod undocumented
251 4 my $self = shift;
252 4 for my $word1 ( sort {scalar(keys %{$self->{'word_word'}{$a}}) <=> scalar(keys %{$self->{'word_word'}{$b}})} keys %{$self->{'word_word'}} )
253 {
254 48 WORD2:for my $word2 ( keys %{$self->{'word_word'}{$word1}} )
255 {
256 14945 for my $i ( 0..length($word2) - 1)
257 {
258 33306 ▶ next WORD2 if exists $self->{'bad_let_let'}{substr($word1,$i,1)}{substr($word2,$i,1)};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bad_let_let"}{substr $word1, $i, 1}{substr $word2, $i, 1} 13909 19397 33306
259 }
260  
261 1036 for my $i ( 0..length($word2) - 1)
262 {
263 5081 my $char1 = substr($word1,$i,1);
264 5081 my $char2 = substr($word2,$i,1);
265  
266 5081 $self->{'let_word_let'}{$char1}{$word1}{$char2} = 1;
267 5081 $self->{'let_let'}{$char1}{$char2} = 1;
268 5081 $self->{'word_let_let_word'}{$word1}{$char1}{$char2}{$word2} = 1;
269 }
270 }
271  
272 48 for my $char1 ( keys %{$self->{'let_let'}} )
273 {
274 880 CHAR2:for my $char2 ( keys %{$self->{'let_let'}{$char1}} )
275 {
276 3829 for my $word1 ( keys %{$self->{'let_word_let'}{$char1}} )
277 {
278 4243 ▶ unless ( exists $self->{'let_word_let'}{$char1}{$word1}{$char2} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"let_word_let"}{$char1}{$word1}{$char2}) 537 3706 4243
279 {
280 537 $self->{'bad_let_let'}{$char1}{$char2} = 1;
281 537 delete $self->{'let_let'}{$char1}{$char2};
282 537 next CHAR2;
283 }
284 }
285 }
286 }
287 }
288 }
289  
290 sub algorithm_two
291 4 ▶{
Subroutine algorithm_two: called CC=9 SCAR=22.0
Pod undocumented
292 4 my $self = shift;
293 4 my $took_out = 0;
294  
295 4 for my $word1 ( keys %{$self->{'word_let_let_word'}} )
296 {
297 48 for my $char1 ( keys %{$self->{'word_let_let_word'}{$word1}} )
298 {
299 228 my @chars2 = keys %{$self->{'word_let_let_word'}{$word1}{$char1}};
300  
301 228 ▶ if ( $#chars2 == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($#chars2 == 0) 182 46 228
302 {
303 182 for my $word3 ( keys %{$self->{'word_let_let_word'}} )
304 {
305 2184 for my $char3 ( keys %{$self->{'word_let_let_word'}{$word3}} )
306 {
307 10374 ▶ next unless exists $self->{'word_let_let_word'}{$word3}{$char3}{$chars2[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"word_let_let_word"}{$word3}{$char3}{$chars2[0]} 9536 838 10374
308 838 ▶ if ( $char1 eq $char3 )
Branch: 1 branch
BranchTrueFalseTotal
if ($char1 eq $char3) { } 687 151 838
309 {
310 687 ▶ if ( scalar(keys %{$self->{'word_let_let_word'}{$word3}{$char3}}) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"word_let_let_word"}{$word3}{$char3};} > 1) 15 672 687
311 {
312 15 my $temp = $self->{'word_let_let_word'}{$word3}{$char3}{$chars2[0]};
313 15 delete $self->{'word_let_let_word'}{$word3}{$char3};
314 15 $self->{'word_let_let_word'}{$word3}{$char3}{$chars2[0]} = $temp;
315 15 $took_out++;
316 }
317 }
318 else
319 {
320 151 delete $self->{'word_let_let_word'}{$word3}{$char3}{$chars2[0]};
321 151 $took_out++;
322 }
323 }
324 }
325 }
326 }
327 }
328 }
329  
330 sub algorithm_three
331 4 ▶{
Subroutine algorithm_three: called CC=7 SCAR=19.5
Pod undocumented
332 4 my $self = shift;
333  
334 4 for my $word1 ( keys %{$self->{'word_let_let_word'}} )
335 {
336 48 my $const1 = ( keys %{$self->{'word_let_let_word'}{$word1}} )[0];
337
338 48 for my $const2 ( keys %{$self->{'word_let_let_word'}{$word1}{$const1}} )
339 {
340 126 WORD2:for my $word2 ( keys %{$self->{'word_let_let_word'}{$word1}{$const1}{$const2}} )
341 {
342 579 CHAR1:for my $char1 ( keys %{$self->{'word_let_let_word'}{$word1}} )
343 {
344 1657 for my $char2 ( keys %{$self->{'word_let_let_word'}{$word1}{$char1}} )
345 {
346 10099 ▶ next CHAR1 if exists $self->{'word_let_let_word'}{$word1}{$char1}{$char2}{$word2};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"word_let_let_word"}{$word1}{$char1}{$char2}{$word2} 1206 8893 10099
347 }
348 451 next WORD2;
349 }
350 128 $self->{'best_word_word'}{$word1}{$word2} = 1;
351 }
352 }
353 }
354 }
355  
356 sub build_pattern
357 12 ▶{
Subroutine build_pattern: called CC=3 SCAR=11.0
Pod undocumented
358 12 my @chars = split '', shift;
359 12 my $pattern = '';
360 12 my $string = '';
361  
362 12 for my $i ( 0..$#chars )
363 {
364 65 ▶ if ($string =~ /$chars[$i]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$chars[$i]/) { } 8 57 65
365 {
366 8 $pattern .= (index( $string, $chars[$i] ) + 1) . '|';
367 }
368 else
369 {
370 57 $pattern .= ( $i + 1 ) . '|';
371 57 $string .= $chars[$i];
372 }
373 }
374  
375 12 chop $pattern; # <-- chop trailing '|'
376 12 return $pattern;
377 }
378  
379 sub apply_mapping
380 2 ▶{
Subroutine apply_mapping: called CC=13 SCAR=25.7
Pod undocumented
381 2 my $ciphertext = shift;
382 2 my $mapping_ref = shift;
383  
384 2 ▶ return "No solution.\n" unless ref $mapping_ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mapping_ref eq "HASH" 0 2 2
385  
386 # Clean up the mapping, reducing us to
387 # one solution, but that's ok for now.
388 2 my %mapping;
389 2 for my $from (keys %$mapping_ref)
390 {
391 40 for my $to (keys %{$mapping_ref->{$from}})
392 {
393 40 $mapping{$from} = $to;
394 40 last;
395 }
396 }
397  
398 2 my @my_chars;
399 2 for my $i (0..length($ciphertext))
400 {
401 90 my $crypt_char = substr($ciphertext,$i,1);
402  
403 90 ▶ if ($crypt_char =~ /^[a-z]$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($crypt_char =~ /^[a-z]$/) { } 70 20 90
elsif ($crypt_char =~ /^[A-Z]$/) { } 4 16 20
404 {
405 70 $my_chars[$i] = '%';
406 }
407 elsif ($crypt_char =~ /^[A-Z]$/)
408 {
409 4 $my_chars[$i] = '#';
410 }
411 else
412 {
413 16 $my_chars[$i] = $crypt_char;
414 }
415 }
416 2 my $plaintext = join('',@my_chars);
417  
418 2 for my $letter (keys %mapping)
419 {
420 40 ▶ next unless $mapping{$letter};
Branch: 1 branch
BranchTrueFalseTotal
unless $mapping{$letter} 0 40 40
421  
422 40 my @my_locations;
423 40 for my $j (0..length($ciphertext))
424 {
425 1800 ▶ if (substr($ciphertext,$j,1) =~ /^$letter$/i)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($ciphertext, $j, 1) =~ /^$letter$/i) 74 1726 1800
426 {
427 74 push @my_locations, $j;
428 }
429 }
430 40 my @new_chars = split('',$plaintext);
431 40 for my $i (0..$#my_locations)
432 {
433 74 my $index = $my_locations[$i];
434  
435 74 ▶ if ($new_chars[$index] eq '#')
Branch: 1 branch
BranchTrueFalseTotal
if ($new_chars[$index] eq '#') { } 4 70 74
436 {
437 # It's uppercase.
438 4 $new_chars[$index] = $mapping{$letter};
439 }
440 else
441 {
442 # It's lowercase, so keep it that way.
443 70 $new_chars[$index] = lc $mapping{$letter};
444 }
445 }
446 40 $plaintext = join('',@new_chars);
447 }
448  
449 2 return $plaintext;
450 }
451  
452 sub get_solution
453 2 ▶{
Subroutine get_solution: called CC=2 SCAR=7.0
Pod undocumented
454 2 my $self = shift;
455 2 my $type = shift;
456 2 ▶ croak "Invalid solution type: $type" unless $type =~ /^(quote|source)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^(quote|source)$/ 0 2 2
457  
458 2 return $self->{'solution'}{$type};
459 }
460  
461 1 1; # because Perl is number one.
462  
463 __END__
↑ Summary