Puzzle.pm

filter: Statementstmt 12.1% 15 / 124 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 21.0% 4 / 19 Podpod 100% 9 / 9 totaltotal 11.8% 28 / 236 CC 40 SCAR 40.0
CC
40
Cov
8%
__build_grid
132.0
_grid_xy_char
90.0
_read_tables
42.0
CRAP 54.6
↑ Summary Puzzle/Cell.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Games::Crossword::Puzzle;
4 {
5 1 $Games::Crossword::Puzzle::VERSION = '0.003';
6 }
7 # ABSTRACT: six letters for "reusable unit of code"
8  
9  
10 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Games::Crossword::Puzzle::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 sub from_file {
15 0 ▶ my ($class, $filename) = @_;
Subroutine from_file: not called CC=4 SCAR=30.0
Pod documented
16  
17 0 my $self = bless {} => $class;
18  
19 0 ▶ open my $fh, $filename or die "couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $filename or die "couldn't open file: $!" 0 0 0
20  
21 0 seek $fh, 2, 0;
22 0 read $fh, (my $magic), 12;
23 0 ▶ die "file is not a valid puzzle" unless $magic eq "ACROSS&DOWN\0";
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "ACROSS&DOWN\0" 0 0 0
24  
25 {
26 0 seek $fh, 0x2C, 0;
27 0 read $fh, (my $size), 2;
28 0 $self->{width} = ord substr $size, 0, 1;
29 0 $self->{height} = ord substr $size, 1, 1;
30 }
31  
32 0 seek $fh, 0x34, 0;
33 0 read $fh, (my $solution), $self->height * $self->width;
34 0 read $fh, (my $guess), $self->height * $self->width;
35  
36 0 seek $fh, 0x2E, 0;
37 0 read $fh, (my $clues), 2;
38 0 $clues = unpack 'v2', $clues;
39  
40 0 seek $fh, 0x34 + 2 * $self->height * $self->width, 0;
41 0 $self->{title} = $self->_read_nul_string($fh);
42 0 $self->{author} = $self->_read_nul_string($fh);
43 0 $self->{copyright} = $self->_read_nul_string($fh);
44  
45 0 my @clues;
46 0 for (1 .. $clues) {
47 0 my $clue = $self->_read_nul_string($fh);
48 0 push @clues, $clue;
49 }
50  
51 0 $self->{notes} = $self->_read_nul_string($fh);
52  
53 0 my $tables = $self->_read_tables($fh);
54  
55 0 $self->__build_grid(\$solution, \$guess, \@clues, $tables);
56  
57 0 return $self;
58 }
59  
60 sub _read_tables {
61 0 ▶ my ($self, $fh) = @_;
Subroutine _read_tables: not called CC=6 SCAR=37.4
62  
63 0 my %return;
64  
65 0 ▶ while (! eof $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
66 0 ▶ read($fh, my $front, 8) or die "error reading table: $!";
Branch: 1 branch
BranchTrueFalseTotal
read $fh, my $front, 8 or die "error reading table: $!" 0 0 0
67  
68 0 my ($title, $len, $ck) = unpack "A4SS", $front;
69 0 ▶ read($fh, my $data, $len) or die "error reading table $title: $!";
Branch: 1 branch
BranchTrueFalseTotal
read $fh, my $data, $len or die "error reading table ${title}: $!" 0 0 0
70  
71 0 ▶ read($fh, my $nul, 1) or die "error reading table $title: $!";
Branch: 1 branch
BranchTrueFalseTotal
read $fh, my $nul, 1 or die "error reading table ${title}: $!" 0 0 0
72 0 ▶ die "data for $title table not nul-terminated" unless $nul eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
unless $nul eq "\0" 0 0 0
73  
74 0 $return{ $title } = $data;
75 }
76  
77 0 return \%return;
78 }
79  
80  
81 0 ▶sub height { $_[0]->{height} }
Subroutine height: not called CC=1 SCAR=6.9
Pod documented
82 0 ▶sub width { $_[0]->{width} }
Subroutine width: not called CC=1 SCAR=6.9
Pod documented
83  
84  
85 sub rows {
86 0 ▶ my ($self) = @_;
Subroutine rows: not called CC=1 SCAR=6.9
Pod documented
87  
88 0 return @{ $self->{grid} };
89 }
90  
91  
92 sub cell {
93 0 ▶ my ($self, $number) = @_;
Subroutine cell: not called CC=2 SCAR=17.9
Pod documented
94  
95 Carp::croak "invalid cell ($number) requested"
96 0 ▶ unless exists $self->{number}{$number};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"number"}{$number} 0 0 0
97  
98 0 return $self->{number}{$number};
99 }
100  
101  
102 0 ▶sub title { $_[0]->{title} }
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
103 0 ▶sub author { $_[0]->{author} }
Subroutine author: not called CC=1 SCAR=6.9
Pod documented
104 0 ▶sub copyright { $_[0]->{copyright} }
Subroutine copyright: not called CC=1 SCAR=6.9
Pod documented
105 0 ▶sub note { $_[0]->{note} }
Subroutine note: not called CC=1 SCAR=6.9
Pod documented
106  
107 # Iterate through the grid, building the Cell objects.
108 # Figure out which cells are going to have clues and assign the clues from the
109 # input stack to cells.
110 sub __build_grid {
111 0 ▶ my ($self, $solution_ref, $guess_ref, $clues, $tables) = @_;
Subroutine __build_grid: not called CC=11 SCAR=48.8
112 0 my @grid;
113 0 $#grid = $self->height - 1;
114  
115 0 my %number;
116  
117 0 my $current_number = 1;
118  
119 0 for my $row (0 .. $#grid) {
120 0 my @row;
121 0 $#row = $self->width - 1;
122  
123 0 for my $col (0 .. $#row) {
124 0 my $byte = $row * $self->width + $col;
125 0 my %square = (
126 value => $self->_grid_xy_char($solution_ref, $col, $row, $tables),
127 guess => $self->_grid_xy_char($guess_ref, $col, $row),
128 );
129  
130 0 ▶ delete $square{value} if $square{value} eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $square{"value"} eq "." 0 0 0
131 0 ▶ delete $square{guess} if $square{guess} eq '.' or $square{guess} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $square{"guess"} eq "." or $square{"guess"} eq "-" 0 0 0
Condition0%0/3
$square{"guess"} eq "." or $square{"guess"} eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
$square{"guess"} eq "." or $square{"guess"} eq "-"
$square{"guess"} eq "." $square{"guess"} eq "-"
Truth table0%0/3
A or B
A= $square{"guess"} eq "."B= $square{"guess"} eq "-"
ABresult
1X1
011
000
132  
133 0 my $across = $self->_has_across_clue(\%square, $solution_ref, $col, $row);
134 0 my $down = $self->_has_down_clue(\%square, $solution_ref, $col, $row);
135  
136 0 ▶ $square{number} = $current_number++ if $across or $down;
Branch: 1 branch
BranchTrueFalseTotal
if $across or $down 0 0 0
Condition0%0/3
$across or $down
l!l&&r!l&&!r
000
MC/DC0%0/2
$across or $down
$across $down
Truth table0%0/3
A or B
A= $acrossB= $down
ABresult
1X1
011
000
137  
138 0 ▶ if ($square{number}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($square{"number"}) 0 0 0
139 0 $number{ $square{number} } = \%square;
140  
141 0 ▶ $square{across} = shift @$clues if $across;
Branch: 1 branch
BranchTrueFalseTotal
if $across 0 0 0
142 0 ▶ $square{down} = shift @$clues if $down;
Branch: 1 branch
BranchTrueFalseTotal
if $down 0 0 0
143 }
144  
145 0 $row[ $col ] = Games::Crossword::Puzzle::Cell->new(\%square);
146 }
147  
148 0 $grid[ $row ] = \@row;
149 }
150  
151 0 $self->{grid} = \@grid;
152 0 $self->{number} = \%number;
153 }
154  
155 # I'd worry more about the efficiency of doing this if it wasn't always for
156 # such short strings. -- rjbs, 2007-04-27
157 sub _read_nul_string {
158 0 ▶ my ($self, $fh) = @_;
Subroutine _read_nul_string: not called CC=3 SCAR=24.8
159  
160 0 my $string;
161 0 ▶ while (read $fh, my $char, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $char, 1) 0 0 0
162 0 ▶ last if $char eq "\0";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\0" 0 0 0
163 0 $string .= $char;
164 }
165  
166 0 return $string;
167 }
168  
169 sub _grid_xy_char {
170 0 ▶ my ($self, $str_ref, $x, $y, $tables) = @_;
Subroutine _grid_xy_char: not called CC=9 SCAR=45.0
171  
172 0 ▶ return if $x >= $self->width or $y >= $self->height;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $self->width or $y >= $self->height 0 0 0
Condition0%0/3
$x >= $self->width or $y >= $self->height
l!l&&r!l&&!r
000
MC/DC0%0/2
$x >= $self->width or $y >= $self->height
$x >= $self->width $y >= $self->height
Truth table0%0/3
A or B
A= $x >= $self->widthB= $y >= $self->height
ABresult
1X1
011
000
173  
174 0 my $index = $y * $self->width + $x;
175  
176 0 ▶ if ($tables and $tables->{GRBS} and $tables->{RTBL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tables and $tables->{"GRBS"} and $tables->{"RTBL"}) 0 0 0
Condition0%0/6
$tables and $tables->{"GRBS"} and $tables->{"RTBL"}
!ll&&!rl&&r
000
$tables and $tables->{"GRBS"}
!ll&&!rl&&r
000
MC/DC0%0/3
$tables and $tables->{"GRBS"} and $tables->{"RTBL"}
$tables $tables->{"GRBS"} $tables->{"RTBL"}
Truth table0%0/4
A and B and C
A= $tablesB= $tables->{"GRBS"}C= $tables->{"RTBL"}
ABCresult
0XX0
10X0
1100
1111
177 0 my $rebus = substr $tables->{GRBS}, $index, 1;
178 0 ▶ if (my $which = ord $rebus) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $which = ord $rebus) 0 0 0
179 # do this elsewhere, cache it; forget about it, Jake, it's Chinatown
180 0 my %for;
181 0 for my $pair (split /;/, $tables->{RTBL}) {
182 0 my ($k, $v) = split /:/, $pair;
183 0 $k =~ s/ //g;
184 0 $for{$k} = $v;
185 }
186 0 ▶ die "can't resolve rebus" unless defined $for{ $which - 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $for{$which - 1} 0 0 0
187 0 return $for{ $which - 1 }
188 }
189 }
190  
191 0 return substr $$str_ref, $index, 1;
192 }
193  
194 sub _has_across_clue {
195 0 ▶ my ($self, $square, $sol_ref, $x, $y) = @_;
Subroutine _has_across_clue: not called CC=6 SCAR=37.4
196  
197 0 ▶ return unless defined $square->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square->{"value"} 0 0 0
198 0 ▶ return if $x >= $self->width - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $self->width - 1 0 0 0
199 0 ▶ return if $self->_grid_xy_char($sol_ref, $x+1, $y) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_grid_xy_char($sol_ref, $x + 1, $y) eq "." 0 0 0
200 0 ▶ return 1 if $x == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x == 0 0 0 0
201 0 ▶ return if $self->_grid_xy_char($sol_ref, $x-1, $y) ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_grid_xy_char($sol_ref, $x - 1, $y) ne "." 0 0 0
202 0 return 1;
203 }
204  
205 sub _has_down_clue {
206 0 ▶ my ($self, $square, $sol_ref, $x, $y) = @_;
Subroutine _has_down_clue: not called CC=6 SCAR=37.4
207  
208 0 ▶ return unless defined $square->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square->{"value"} 0 0 0
209 0 ▶ return if $y >= $self->height - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $y >= $self->height - 1 0 0 0
210 0 ▶ return if $self->_grid_xy_char($sol_ref, $x, $y+1) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_grid_xy_char($sol_ref, $x, $y + 1) eq "." 0 0 0
211 0 ▶ return 1 if $y == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $y == 0 0 0 0
212 0 ▶ return if $self->_grid_xy_char($sol_ref, $x, $y-1) ne '.';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_grid_xy_char($sol_ref, $x, $y - 1) ne "." 0 0 0
213 0 return 1;
214 }
215  
216  
217 1 1;
218  
219 __END__
↑ Summary Puzzle/Cell.pm »