line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Convert::AcrossLite; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
40172
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
71
|
|
4
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
136
|
|
5
|
1
|
|
|
1
|
|
8
|
use Carp; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
214
|
|
6
|
1
|
|
|
1
|
|
9
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7138
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.10'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
12
|
0
|
|
|
|
|
|
my %conf = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
my $self = {}; |
15
|
0
|
|
0
|
|
|
|
$self->{in_file} = $conf{in_file} || 'Default.puz'; |
16
|
0
|
|
|
|
|
|
$self->{is_parsed} = 0; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
bless($self, $class); |
19
|
0
|
|
|
|
|
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub in_file { |
23
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
24
|
0
|
0
|
|
|
|
|
if(@_) { $self->{in_file} = shift } |
|
0
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
return $self->{in_file}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub out_file { |
29
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
30
|
0
|
0
|
|
|
|
|
if(@_) { $self->{out_file} = shift } |
|
0
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
return $self->{out_file}; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub puz2text { |
35
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
36
|
0
|
|
|
|
|
|
my $text; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Parse puz file |
39
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Format across clues |
42
|
0
|
|
|
|
|
|
my @aclues = split("\n", $self->{aclues}); |
43
|
0
|
|
|
|
|
|
foreach my $aclue(@aclues) { |
44
|
0
|
|
|
|
|
|
$aclue =~ s/\d+\s+-\s+//; |
45
|
0
|
|
|
|
|
|
$aclue = "\t$aclue"; |
46
|
|
|
|
|
|
|
} |
47
|
0
|
|
|
|
|
|
$self->{aclues} = join("\n",@aclues); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Format down clues |
50
|
0
|
|
|
|
|
|
my @dclues = split("\n", $self->{dclues}); |
51
|
0
|
|
|
|
|
|
foreach my $dclue(@dclues) { |
52
|
0
|
|
|
|
|
|
$dclue =~ s/\d+\s+-\s+//; |
53
|
0
|
|
|
|
|
|
$dclue = "\t$dclue"; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
$self->{dclues} = join("\n",@dclues); |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$text = "\n"; |
58
|
0
|
|
|
|
|
|
$text .= "\n"; |
59
|
0
|
|
|
|
|
|
$text .= "\t$self->{title}\n"; |
60
|
0
|
|
|
|
|
|
$text .= "\n"; |
61
|
0
|
|
|
|
|
|
$text .= "\t$self->{author}\n"; |
62
|
0
|
|
|
|
|
|
$text .= "\n"; |
63
|
0
|
|
|
|
|
|
$text .= "\t$self->{copyright}\n"; |
64
|
0
|
|
|
|
|
|
$text .= "\n"; |
65
|
0
|
|
|
|
|
|
$text .= "\t$self->{rows}x$self->{columns}\n"; |
66
|
0
|
|
|
|
|
|
$text .= "\n"; |
67
|
0
|
|
|
|
|
|
my $solref = $self->{solution}; |
68
|
0
|
|
|
|
|
|
my @sol = @$solref; |
69
|
0
|
|
|
|
|
|
foreach my $sol (@sol) { |
70
|
0
|
|
|
|
|
|
$text .= "\t$sol\n"; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
$text .= "\n"; |
73
|
0
|
|
|
|
|
|
$text .= "$self->{aclues}\n"; |
74
|
0
|
|
|
|
|
|
$text .= "\n"; |
75
|
0
|
|
|
|
|
|
$text .= "$self->{dclues}\n"; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if( defined $self->out_file ) { |
78
|
0
|
|
|
|
|
|
my $PUZ_OUT = $self->out_file; |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
open FH, ">$PUZ_OUT" or croak "Can't open $PUZ_OUT: $!"; |
81
|
0
|
|
|
|
|
|
print FH $text; |
82
|
0
|
|
|
|
|
|
close FH; |
83
|
|
|
|
|
|
|
} else { |
84
|
0
|
|
|
|
|
|
return $text; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get_across_down { |
89
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
90
|
0
|
|
|
|
|
|
my $across_hashref = get_across($self); |
91
|
0
|
|
|
|
|
|
my $down_hashref = get_down($self); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
return($across_hashref, $down_hashref); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub get_across { |
97
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
98
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
################################################### |
101
|
|
|
|
|
|
|
# _parse_file will set direction, number and clue # |
102
|
|
|
|
|
|
|
# as well as a two-dimension array for solution # |
103
|
|
|
|
|
|
|
################################################### |
104
|
0
|
|
|
|
|
|
my $sol_two_ref = $self->{solution_two}; |
105
|
0
|
|
|
|
|
|
my @sol_two = @$sol_two_ref; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
### Get row, column, solution, length ### |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
################################################### |
110
|
|
|
|
|
|
|
# We're setting found squares to 1.... |
111
|
|
|
|
|
|
|
# We need to set them to row and col so we can |
112
|
|
|
|
|
|
|
# do a "lookup" later. That is, given a row |
113
|
|
|
|
|
|
|
# and col number, we need to know what the clue |
114
|
|
|
|
|
|
|
# number is. Or maybe we need to make a key |
115
|
|
|
|
|
|
|
# that is row/col so we can look up clue num. |
116
|
|
|
|
|
|
|
################################################### |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
###################################################### |
119
|
|
|
|
|
|
|
# Determine which squares start with either |
120
|
|
|
|
|
|
|
# an across word (%across_start_squares) |
121
|
|
|
|
|
|
|
# or a down word (%down_start_squares). |
122
|
|
|
|
|
|
|
###################################################### |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Across |
125
|
0
|
|
|
|
|
|
my $square_num = 0; |
126
|
0
|
|
|
|
|
|
my %across_start_squares; |
127
|
0
|
|
|
|
|
|
my $diagram_ref = $self->{diagram}; |
128
|
0
|
|
|
|
|
|
my @diagram = @$diagram_ref; |
129
|
0
|
|
|
|
|
|
for (my $j=0;$j<$self->{rows};$j++) { # height |
130
|
0
|
|
|
|
|
|
for(my $k=0;$k<$self->{columns};$k++) { # width |
131
|
|
|
|
|
|
|
# Check position for across number |
132
|
|
|
|
|
|
|
# Left edge non-black followed by non-black |
133
|
0
|
0
|
0
|
|
|
|
if( ($k == 0 && |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
134
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && # Row $j, Col 0 (k) |
135
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.') || # Row $j, Col 1 (k+1) |
136
|
|
|
|
|
|
|
# Previous black - nonblack - nonblack |
137
|
|
|
|
|
|
|
( ($k+1)<$self->{columns} && # Not last col |
138
|
|
|
|
|
|
|
($k-1)>=0 && # Not first col |
139
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
140
|
|
|
|
|
|
|
substr($diagram[$j],$k-1,1) eq '.' && |
141
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.' ) ) { |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
$across_start_squares{$square_num}++; |
144
|
0
|
|
|
|
|
|
$square_num++ |
145
|
|
|
|
|
|
|
} else { |
146
|
0
|
|
|
|
|
|
$square_num++ |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# Down |
152
|
0
|
|
|
|
|
|
my %down_start_squares; |
153
|
0
|
|
|
|
|
|
$square_num = 0; |
154
|
0
|
|
|
|
|
|
for(my $k=0;$k<$self->{columns};$k++) { # width |
155
|
0
|
|
|
|
|
|
for (my $j=0;$j<$self->{rows};$j++) { # height |
156
|
|
|
|
|
|
|
# Check position for down number |
157
|
0
|
0
|
0
|
|
|
|
if( ($j == 0 && # Row 0 |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
158
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
159
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) ne '.') || |
160
|
|
|
|
|
|
|
# Black above - nonblack - nonblack below |
161
|
|
|
|
|
|
|
( ($j-1)>=0 && # Not first row |
162
|
|
|
|
|
|
|
($j+1)<$self->{rows} && # Not last row |
163
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
164
|
|
|
|
|
|
|
substr($diagram[$j-1],$k,1) eq '.' && |
165
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) ne '.' ) ) { |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
$down_start_squares{$square_num}++; |
168
|
0
|
0
|
|
|
|
|
if( $j >= $self->{rows}-1 ) { |
169
|
|
|
|
|
|
|
# Last row |
170
|
0
|
|
|
|
|
|
$square_num = $k+1; # col+1 |
171
|
|
|
|
|
|
|
} else { |
172
|
|
|
|
|
|
|
# Not last row |
173
|
0
|
|
|
|
|
|
$square_num += $self->{columns}; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
} else { |
176
|
0
|
0
|
|
|
|
|
if( $j >= $self->{rows}-1 ) { |
177
|
|
|
|
|
|
|
# Last row |
178
|
0
|
|
|
|
|
|
$square_num = $k+1; # col+1 |
179
|
|
|
|
|
|
|
} else { |
180
|
|
|
|
|
|
|
# Not last row |
181
|
0
|
|
|
|
|
|
$square_num += $self->{columns}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
########################################################## |
188
|
|
|
|
|
|
|
# Go back through grid from square 0 to square |
189
|
|
|
|
|
|
|
# (rows x cols - 1) and set the clue number on each |
190
|
|
|
|
|
|
|
# square that is found in the across_start_squares and |
191
|
|
|
|
|
|
|
# down_start_square hashes from above. |
192
|
|
|
|
|
|
|
# |
193
|
|
|
|
|
|
|
# We create two versions.... |
194
|
|
|
|
|
|
|
# 1) Row/Col - $clue_numbers[$row][$col] = $clue_number |
195
|
|
|
|
|
|
|
# [0,0] to [14,14] for 15x15 ([0,0] to [rows-1,cols-1]) |
196
|
|
|
|
|
|
|
# 2) Square Num - $clue_numbers{$square} = $clue_number |
197
|
|
|
|
|
|
|
# Square 0 to 224 for 15x15 (0 to row*cols-1) |
198
|
|
|
|
|
|
|
########################################################### |
199
|
|
|
|
|
|
|
# Across |
200
|
0
|
|
|
|
|
|
my $counter = 0; |
201
|
0
|
|
|
|
|
|
my $clue_num = 1; |
202
|
0
|
|
|
|
|
|
my @clue_numbers; |
203
|
|
|
|
|
|
|
my %clue_numbers; |
204
|
0
|
|
|
|
|
|
for(my $row = 0; $row < $self->{rows}; $row++) { |
205
|
0
|
|
|
|
|
|
for(my $col = 0; $col < $self->{columns}; $col++) { |
206
|
0
|
0
|
0
|
|
|
|
if( $across_start_squares{$counter} || $down_start_squares{$counter} ) { |
207
|
|
|
|
|
|
|
# Hash - Square number |
208
|
0
|
|
|
|
|
|
$clue_numbers{$counter} = $clue_num; |
209
|
|
|
|
|
|
|
# Array - row/col |
210
|
0
|
|
|
|
|
|
$clue_numbers[$row][$col] = $clue_num; |
211
|
0
|
|
|
|
|
|
$clue_num++; |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
$counter++; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# Now get data and set hash of hashes |
218
|
|
|
|
|
|
|
# Across |
219
|
0
|
|
|
|
|
|
my $start_row = 0; # Square number - row |
220
|
0
|
|
|
|
|
|
my $start_col = 0; # Square number - col |
221
|
0
|
|
|
|
|
|
my $start_square = 0; |
222
|
0
|
|
|
|
|
|
my $length = 0; # Length of current solution word |
223
|
0
|
|
|
|
|
|
my $square = 0; # Grid square |
224
|
0
|
|
|
|
|
|
my $sol_word = ''; # Solution word |
225
|
0
|
|
|
|
|
|
my $last_square = ''; # Contents of last square visited |
226
|
0
|
|
|
|
|
|
my $last_square_two = ''; # Contents of two squares ago |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
|
for(my $row = 0; $row < $self->{rows}; $row++) { |
229
|
0
|
|
|
|
|
|
for(my $col = 0; $col < $self->{columns}; $col++) { |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# If $sol_two[$row][$col] eq '.', then we *probably* want to |
232
|
|
|
|
|
|
|
# save our data...if $col == $self->{columns} - 1, then we |
233
|
|
|
|
|
|
|
# definitely want to. |
234
|
0
|
0
|
0
|
|
|
|
if ( $sol_two[$row][$col] eq '.' || $col >= $self->{columns} - 1 ) { |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# If this is first square and it contains '.', |
237
|
|
|
|
|
|
|
# don't save data. Just set a few vars and move on. |
238
|
0
|
0
|
0
|
|
|
|
if( $col == 0 && $sol_two[$row][$col] eq '.' ) { |
239
|
0
|
|
|
|
|
|
$square++; |
240
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
241
|
0
|
|
|
|
|
|
$last_square = '.'; |
242
|
0
|
|
|
|
|
|
$start_col++; |
243
|
0
|
|
|
|
|
|
next; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# If this isn't first square in col, this square contains '.' |
247
|
|
|
|
|
|
|
# and the previous square contains '.', don't save data. |
248
|
|
|
|
|
|
|
# Just set a few vars and move on. |
249
|
|
|
|
|
|
|
# NOTE: US crossword rules state clue must be at least three letters |
250
|
|
|
|
|
|
|
# We're checking the length is at least two, since some crosswords |
251
|
|
|
|
|
|
|
# are built that way... |
252
|
0
|
0
|
0
|
|
|
|
if( $sol_two[$row][$col] eq '.' && $col != 0 && $last_square eq '.' ) { |
|
|
|
0
|
|
|
|
|
253
|
0
|
0
|
|
|
|
|
if( $col == $self->{columns} - 1 ) { |
254
|
0
|
|
|
|
|
|
$start_row++; |
255
|
0
|
|
|
|
|
|
$start_col = 0; |
256
|
|
|
|
|
|
|
} else { |
257
|
0
|
|
|
|
|
|
$start_col++; |
258
|
|
|
|
|
|
|
} |
259
|
0
|
|
|
|
|
|
$square++; |
260
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
261
|
0
|
|
|
|
|
|
$last_square = '.'; |
262
|
0
|
|
|
|
|
|
next; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# Get last square of row |
266
|
0
|
0
|
|
|
|
|
if( $col == $self->{columns} - 1 ) { |
267
|
0
|
0
|
|
|
|
|
unless( $sol_two[$row][$col] eq '.' ) { |
268
|
0
|
|
|
|
|
|
$sol_word .= $sol_two[$row][$col]; |
269
|
0
|
|
|
|
|
|
$length++; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# NOTE: US crossword rules state clue must be at least three letters |
274
|
|
|
|
|
|
|
# We're checking the length is at least two, since some crosswords |
275
|
|
|
|
|
|
|
# are built that way... |
276
|
0
|
0
|
|
|
|
|
if( $length < 2 ) { |
277
|
|
|
|
|
|
|
# Reset variables |
278
|
0
|
|
|
|
|
|
$length = 0; |
279
|
0
|
|
|
|
|
|
$sol_word = ''; |
280
|
0
|
0
|
|
|
|
|
if( $col >= $self->{columns} - 1 ) { |
281
|
0
|
|
|
|
|
|
$start_row++; |
282
|
0
|
|
|
|
|
|
$start_col = 0; |
283
|
|
|
|
|
|
|
} else { |
284
|
0
|
|
|
|
|
|
$start_col = $col + 1; |
285
|
|
|
|
|
|
|
} |
286
|
0
|
|
|
|
|
|
next; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
# Get key and clue num |
290
|
0
|
|
|
|
|
|
my $clue_num = $clue_numbers[$start_row][$start_col]; |
291
|
0
|
0
|
|
|
|
|
next unless defined $clue_num; |
292
|
0
|
|
|
|
|
|
my $key = $clue_num; |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# Store info into puzzle hash |
295
|
0
|
|
|
|
|
|
$self->{across}{$key}{length} = $length; |
296
|
0
|
|
|
|
|
|
$self->{across}{$key}{solution} = $sol_word; |
297
|
0
|
|
|
|
|
|
$self->{across}{$key}{row} = $start_row + 1; |
298
|
0
|
|
|
|
|
|
$self->{across}{$key}{column} = $start_col + 1; |
299
|
0
|
|
|
|
|
|
$self->{across}{$key}{clue_number} = $clue_num; |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
# Reset variables |
302
|
0
|
|
|
|
|
|
$length = 0; |
303
|
0
|
|
|
|
|
|
$sol_word = ''; |
304
|
0
|
0
|
|
|
|
|
if( $col >= $self->{columns} - 1 ) { |
305
|
0
|
|
|
|
|
|
$start_row++; |
306
|
0
|
|
|
|
|
|
$start_col = 0; |
307
|
|
|
|
|
|
|
} else { |
308
|
0
|
|
|
|
|
|
$start_col = $col + 1; |
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
} else { |
311
|
|
|
|
|
|
|
# TODO - might want to check if next square is '.' |
312
|
0
|
|
|
|
|
|
$sol_word .= $sol_two[$row][$col]; |
313
|
0
|
|
|
|
|
|
$length++; |
314
|
|
|
|
|
|
|
} |
315
|
0
|
|
|
|
|
|
$square++; |
316
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
317
|
0
|
|
|
|
|
|
$last_square = $sol_two[$row][$col]; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# Return across hash |
322
|
0
|
|
|
|
|
|
return ($self->{across}); |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub get_down { |
327
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
328
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
################################################### |
331
|
|
|
|
|
|
|
# _parse_file will set direction, number and clue # |
332
|
|
|
|
|
|
|
# as well as a two-dimension array for solution # |
333
|
|
|
|
|
|
|
################################################### |
334
|
0
|
|
|
|
|
|
my $sol_two_ref = $self->{solution_two}; |
335
|
0
|
|
|
|
|
|
my @sol_two = @$sol_two_ref; |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
### Get row, column, solution, length ### |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
################################################### |
340
|
|
|
|
|
|
|
# We're setting found squares to 1.... |
341
|
|
|
|
|
|
|
# We need to set them to row and col so we can |
342
|
|
|
|
|
|
|
# do a "lookup" later. That is, given a row |
343
|
|
|
|
|
|
|
# and col number, we need to know what the clue |
344
|
|
|
|
|
|
|
# number is. Or maybe we need to make a key |
345
|
|
|
|
|
|
|
# that is row/col so we can look up clue num. |
346
|
|
|
|
|
|
|
################################################### |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
###################################################### |
349
|
|
|
|
|
|
|
# Determine which squares start with either |
350
|
|
|
|
|
|
|
# an across word (%across_start_squares) |
351
|
|
|
|
|
|
|
# or a down word (%down_start_squares). |
352
|
|
|
|
|
|
|
###################################################### |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
# Across |
355
|
0
|
|
|
|
|
|
my $square_num = 0; |
356
|
0
|
|
|
|
|
|
my %across_start_squares; |
357
|
0
|
|
|
|
|
|
my $diagram_ref = $self->{diagram}; |
358
|
0
|
|
|
|
|
|
my @diagram = @$diagram_ref; |
359
|
0
|
|
|
|
|
|
for (my $j=0;$j<$self->{rows};$j++) { # height |
360
|
0
|
|
|
|
|
|
for(my $k=0;$k<$self->{columns};$k++) { # width |
361
|
|
|
|
|
|
|
# Check position for across number |
362
|
|
|
|
|
|
|
# Left edge non-black followed by non-black |
363
|
0
|
0
|
0
|
|
|
|
if( ($k == 0 && |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
364
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && # Row $j, Col 0 (k) |
365
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.') || # Row $j, Col 1 (k+1) |
366
|
|
|
|
|
|
|
# Previous black - nonblack - nonblack |
367
|
|
|
|
|
|
|
( ($k+1)<$self->{columns} && # Not last col |
368
|
|
|
|
|
|
|
($k-1)>=0 && # Not first col |
369
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
370
|
|
|
|
|
|
|
substr($diagram[$j],$k-1,1) eq '.' && |
371
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.' ) ) { |
372
|
|
|
|
|
|
|
|
373
|
0
|
|
|
|
|
|
$across_start_squares{$square_num}++; |
374
|
0
|
|
|
|
|
|
$square_num++ |
375
|
|
|
|
|
|
|
} else { |
376
|
0
|
|
|
|
|
|
$square_num++ |
377
|
|
|
|
|
|
|
} |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
# Down |
382
|
0
|
|
|
|
|
|
my %down_start_squares; |
383
|
0
|
|
|
|
|
|
$square_num = 0; |
384
|
0
|
|
|
|
|
|
for(my $k=0;$k<$self->{columns};$k++) { # width |
385
|
0
|
|
|
|
|
|
for (my $j=0;$j<$self->{rows};$j++) { # height |
386
|
|
|
|
|
|
|
# Check position for down number |
387
|
0
|
0
|
0
|
|
|
|
if( ($j == 0 && # Row 0 |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
388
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
389
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) ne '.') || |
390
|
|
|
|
|
|
|
# Black above - nonblack - nonblack below |
391
|
|
|
|
|
|
|
( ($j-1)>=0 && # Not first row |
392
|
|
|
|
|
|
|
($j+1)<$self->{rows} && # Not last row |
393
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
394
|
|
|
|
|
|
|
substr($diagram[$j-1],$k,1) eq '.' && |
395
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) ne '.' ) ) { |
396
|
|
|
|
|
|
|
|
397
|
0
|
|
|
|
|
|
$down_start_squares{$square_num}++; |
398
|
0
|
0
|
|
|
|
|
if( $j >= $self->{rows}-1 ) { |
399
|
|
|
|
|
|
|
# Last row |
400
|
0
|
|
|
|
|
|
$square_num = $k+1; # col+1 |
401
|
|
|
|
|
|
|
} else { |
402
|
|
|
|
|
|
|
# Not last row |
403
|
0
|
|
|
|
|
|
$square_num += $self->{columns}; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
} else { |
406
|
0
|
0
|
|
|
|
|
if( $j >= $self->{rows}-1 ) { |
407
|
|
|
|
|
|
|
# Last row |
408
|
0
|
|
|
|
|
|
$square_num = $k+1; # col+1 |
409
|
|
|
|
|
|
|
} else { |
410
|
|
|
|
|
|
|
# Not last row |
411
|
0
|
|
|
|
|
|
$square_num += $self->{columns}; |
412
|
|
|
|
|
|
|
} |
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
########################################################## |
418
|
|
|
|
|
|
|
# Go back through grid from square 0 to square |
419
|
|
|
|
|
|
|
# (rows x cols - 1) and set the clue number on each |
420
|
|
|
|
|
|
|
# sqaure that is found in the across_start_squares and |
421
|
|
|
|
|
|
|
# down_start_square hashes from above. |
422
|
|
|
|
|
|
|
# |
423
|
|
|
|
|
|
|
# We create two versions.... |
424
|
|
|
|
|
|
|
# 1) Row/Col - $clue_numbers[$row][$col] = $clue_number |
425
|
|
|
|
|
|
|
# [0,0] to [14,14] for 15x15 ([0,0] to [rows-1,cols-1]) |
426
|
|
|
|
|
|
|
# 2) Square Num - $clue_numbers{$square} = $clue_number |
427
|
|
|
|
|
|
|
# Square 0 to 224 for 15x15 (0 to row*cols-1) |
428
|
|
|
|
|
|
|
########################################################### |
429
|
|
|
|
|
|
|
# Across |
430
|
0
|
|
|
|
|
|
my $counter = 0; |
431
|
0
|
|
|
|
|
|
my $clue_num = 1; |
432
|
0
|
|
|
|
|
|
my @clue_numbers; |
433
|
|
|
|
|
|
|
my %clue_numbers; |
434
|
0
|
|
|
|
|
|
for(my $row = 0; $row < $self->{rows}; $row++) { |
435
|
0
|
|
|
|
|
|
for(my $col = 0; $col < $self->{columns}; $col++) { |
436
|
0
|
0
|
0
|
|
|
|
if( $across_start_squares{$counter} || $down_start_squares{$counter} ) { |
437
|
|
|
|
|
|
|
# Hash - Square number |
438
|
0
|
|
|
|
|
|
$clue_numbers{$counter} = $clue_num; |
439
|
|
|
|
|
|
|
# Array - row/col |
440
|
0
|
|
|
|
|
|
$clue_numbers[$row][$col] = $clue_num; |
441
|
0
|
|
|
|
|
|
$clue_num++; |
442
|
|
|
|
|
|
|
} |
443
|
0
|
|
|
|
|
|
$counter++; |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
} |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
# Now get data and set hash of hashes |
448
|
|
|
|
|
|
|
# Down |
449
|
0
|
|
|
|
|
|
my $start_row = 0; # Square number - row |
450
|
0
|
|
|
|
|
|
my $start_col = 0; # Square number - col |
451
|
0
|
|
|
|
|
|
my $start_square = 0; |
452
|
0
|
|
|
|
|
|
my $length = 0; # Length of current solution word |
453
|
0
|
|
|
|
|
|
my $square = 0; # Grid square |
454
|
0
|
|
|
|
|
|
my $sol_word = ''; # Solution word |
455
|
0
|
|
|
|
|
|
my $last_square = ''; # Contents of last square visted |
456
|
0
|
|
|
|
|
|
my $last_square_two = ''; # Contents of two squares back |
457
|
0
|
|
|
|
|
|
for(my $col = 0; $col < $self->{columns}; $col++) { |
458
|
0
|
|
|
|
|
|
for(my $row = 0; $row < $self->{rows}; $row++) { |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
# If $sol_two[$row][$col] eq '.', then we *probably* want to |
461
|
|
|
|
|
|
|
# save our data...if $row == $self->{rows} - 1, then we |
462
|
|
|
|
|
|
|
# definitely want to. |
463
|
0
|
0
|
0
|
|
|
|
if ( $sol_two[$row][$col] eq '.' || $row >= $self->{rows} - 1 ) { |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
# If this is the first square and it contains '.', |
466
|
|
|
|
|
|
|
# don't save our data. Just set a few vars and move on. |
467
|
0
|
0
|
0
|
|
|
|
if( $sol_two[$row][$col] eq '.' && $row == 0 ) { |
468
|
0
|
|
|
|
|
|
$square++; |
469
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
470
|
0
|
|
|
|
|
|
$last_square = '.'; |
471
|
0
|
|
|
|
|
|
$start_row++; |
472
|
0
|
|
|
|
|
|
next; |
473
|
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
# If this isn't first square in row, this square contains '.' |
476
|
|
|
|
|
|
|
# and the previous square contains '.', don't save data. |
477
|
|
|
|
|
|
|
# Just set a few vars and move on. |
478
|
0
|
0
|
0
|
|
|
|
if( $sol_two[$row][$col] eq '.' && $row != 0 && $last_square eq '.' ) { |
|
|
|
0
|
|
|
|
|
479
|
0
|
0
|
|
|
|
|
if( $row == $self->{rows} - 1 ) { |
480
|
0
|
|
|
|
|
|
$start_col++; |
481
|
0
|
|
|
|
|
|
$start_row = 0; |
482
|
|
|
|
|
|
|
} else { |
483
|
0
|
|
|
|
|
|
$start_row++; |
484
|
|
|
|
|
|
|
} |
485
|
0
|
|
|
|
|
|
$square++; |
486
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
487
|
0
|
|
|
|
|
|
$last_square = '.'; |
488
|
0
|
|
|
|
|
|
next; |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
# Get last square of each column |
492
|
0
|
0
|
|
|
|
|
if( $row == $self->{rows} - 1 ) { |
493
|
|
|
|
|
|
|
# If last square is '.', don't add to solution |
494
|
0
|
0
|
|
|
|
|
unless( $sol_two[$row][$col] eq '.' ) { |
495
|
0
|
|
|
|
|
|
$sol_word .= $sol_two[$row][$col]; |
496
|
0
|
|
|
|
|
|
$length++; |
497
|
|
|
|
|
|
|
} |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
# NOTE: US crossword rules state clue must be at least three letters |
501
|
|
|
|
|
|
|
# We're checking the length is at least two, since some crosswords |
502
|
|
|
|
|
|
|
# are built that way... |
503
|
0
|
0
|
|
|
|
|
if( $length < 2 ) { |
504
|
|
|
|
|
|
|
# Reset variables |
505
|
0
|
|
|
|
|
|
$length = 0; |
506
|
0
|
|
|
|
|
|
$sol_word = ''; |
507
|
0
|
0
|
|
|
|
|
if($row >= $self->{rows} - 1 ) { |
508
|
0
|
|
|
|
|
|
$start_col++; |
509
|
0
|
|
|
|
|
|
$start_row = 0; |
510
|
|
|
|
|
|
|
} else { |
511
|
0
|
|
|
|
|
|
$start_row = $row + 1; |
512
|
|
|
|
|
|
|
} |
513
|
0
|
|
|
|
|
|
next; |
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
# Get key and clue nem |
517
|
0
|
|
|
|
|
|
my $clue_num = $clue_numbers[$start_row][$start_col]; |
518
|
0
|
0
|
|
|
|
|
next unless defined $clue_num; |
519
|
0
|
|
|
|
|
|
my $key = $clue_num; |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
# Store info into puzzle hash |
522
|
0
|
|
|
|
|
|
$self->{down}{$key}{length} = $length; |
523
|
0
|
|
|
|
|
|
$self->{down}{$key}{solution} = $sol_word; |
524
|
0
|
|
|
|
|
|
$self->{down}{$key}{row} = $start_row + 1; |
525
|
0
|
|
|
|
|
|
$self->{down}{$key}{column} = $start_col + 1; |
526
|
0
|
|
|
|
|
|
$self->{down}{$key}{clue_number} = $clue_num; |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
# Reset variables |
529
|
0
|
|
|
|
|
|
$length = 0; |
530
|
0
|
|
|
|
|
|
$sol_word = ''; |
531
|
0
|
0
|
|
|
|
|
if($row >= $self->{rows} - 1 ) { |
532
|
0
|
|
|
|
|
|
$start_col++; |
533
|
0
|
|
|
|
|
|
$start_row = 0; |
534
|
|
|
|
|
|
|
} else { |
535
|
0
|
|
|
|
|
|
$start_row = $row + 1; |
536
|
|
|
|
|
|
|
} |
537
|
|
|
|
|
|
|
} else { |
538
|
0
|
|
|
|
|
|
$sol_word .= $sol_two[$row][$col]; |
539
|
0
|
|
|
|
|
|
$length++; |
540
|
|
|
|
|
|
|
} |
541
|
0
|
|
|
|
|
|
$square++; |
542
|
0
|
|
|
|
|
|
$last_square_two = $last_square; |
543
|
0
|
|
|
|
|
|
$last_square = $sol_two[$row][$col]; |
544
|
|
|
|
|
|
|
} |
545
|
|
|
|
|
|
|
} |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
# Return down hash |
548
|
0
|
|
|
|
|
|
return ($self->{down}); |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
sub parse_file { |
553
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
554
|
0
|
|
|
|
|
|
_parse_file($self); |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
sub _parse_file { |
558
|
0
|
|
|
0
|
|
|
my($self) = shift; |
559
|
0
|
|
|
|
|
|
my($buf, $parse_word, $oe); |
560
|
0
|
|
|
|
|
|
my($aclues, $dclues); |
561
|
|
|
|
|
|
|
|
562
|
0
|
|
|
|
|
|
my $PUZ_IN = $self->{in_file}; |
563
|
|
|
|
|
|
|
|
564
|
0
|
0
|
|
|
|
|
open FH, $PUZ_IN or croak "Can't open $PUZ_IN: $!"; |
565
|
0
|
|
|
|
|
|
binmode(FH); # Be nice to windoz |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
# Skip unneeded data |
568
|
0
|
|
|
|
|
|
seek(FH, 44, 0); |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
# Width and Height |
571
|
0
|
|
|
|
|
|
read(FH, $buf, 2); |
572
|
0
|
|
|
|
|
|
my ($width, $height) = unpack "C C", $buf; |
573
|
0
|
|
|
|
|
|
$self->{rows} = $height; |
574
|
0
|
|
|
|
|
|
$self->{columns} = $width; |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
# Skip more unneeded data |
577
|
0
|
|
|
|
|
|
read(FH, $buf, 6); |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
# Solution |
580
|
0
|
|
|
|
|
|
my @solution; |
581
|
|
|
|
|
|
|
my @solution_two; # two-dimensional array |
582
|
0
|
|
|
|
|
|
for(my $j=0; $j<$height; $j++) { |
583
|
0
|
|
|
|
|
|
my $twodim_col = 0; |
584
|
0
|
|
|
|
|
|
read(FH, $solution[$j], $width); |
585
|
0
|
|
|
|
|
|
my @letters = split(//,$solution[$j]); |
586
|
0
|
|
|
|
|
|
foreach my $letter (@letters) { |
587
|
0
|
|
|
|
|
|
$solution_two[$j][$twodim_col] = $letter; |
588
|
0
|
|
|
|
|
|
$twodim_col++; |
589
|
|
|
|
|
|
|
} |
590
|
|
|
|
|
|
|
} |
591
|
0
|
|
|
|
|
|
$self->{solution} = \@solution; |
592
|
0
|
|
|
|
|
|
$self->{solution_two} = \@solution_two; |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
# Diagram |
595
|
0
|
|
|
|
|
|
my @diagram; |
596
|
0
|
|
|
|
|
|
for(my $j=0;$j<$height;$j++) { |
597
|
0
|
|
|
|
|
|
read(FH, $diagram[$j], $width); |
598
|
|
|
|
|
|
|
} |
599
|
0
|
|
|
|
|
|
$self->{diagram} = \@diagram; |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
# Title |
602
|
0
|
|
|
|
|
|
$oe = 0; |
603
|
0
|
|
|
|
|
|
while(1) { |
604
|
0
|
0
|
|
|
|
|
read(FH, $buf, 1) or last; |
605
|
0
|
|
|
|
|
|
my ($char) = unpack "C", $buf; |
606
|
0
|
0
|
|
|
|
|
last if $char == 0; |
607
|
0
|
|
|
|
|
|
$parse_word .= $buf; |
608
|
|
|
|
|
|
|
} |
609
|
0
|
0
|
|
|
|
|
if( defined $parse_word ) { |
610
|
0
|
|
|
|
|
|
$parse_word =~ s/^\s+//; |
611
|
0
|
|
|
|
|
|
$parse_word =~ s/\s+$//; |
612
|
0
|
|
|
|
|
|
$self->{title} = $parse_word; |
613
|
|
|
|
|
|
|
} else { |
614
|
0
|
|
|
|
|
|
$self->{title} = ''; |
615
|
|
|
|
|
|
|
} |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
# Author |
618
|
0
|
|
|
|
|
|
$parse_word = ''; |
619
|
0
|
|
|
|
|
|
$oe = 0; |
620
|
0
|
|
|
|
|
|
while(1) { |
621
|
0
|
0
|
|
|
|
|
read(FH, $buf, 1) or last; |
622
|
0
|
|
|
|
|
|
my ($char) = unpack "C", $buf; |
623
|
0
|
0
|
|
|
|
|
last if $char == 0; |
624
|
0
|
|
|
|
|
|
$parse_word .= $buf; |
625
|
|
|
|
|
|
|
} |
626
|
0
|
0
|
|
|
|
|
if( defined $parse_word ) { |
627
|
0
|
|
|
|
|
|
$parse_word =~ s/^\s+//; |
628
|
0
|
|
|
|
|
|
$parse_word =~ s/\s+$//; |
629
|
0
|
|
|
|
|
|
$self->{author} = $parse_word; |
630
|
|
|
|
|
|
|
} else { |
631
|
0
|
|
|
|
|
|
$self->{author} = ''; |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
# Copyright |
635
|
0
|
|
|
|
|
|
$parse_word = ''; |
636
|
0
|
|
|
|
|
|
$oe = 0; |
637
|
0
|
|
|
|
|
|
while(1) { |
638
|
0
|
0
|
|
|
|
|
read(FH, $buf, 1) or last; |
639
|
0
|
|
|
|
|
|
my ($char) = unpack "C", $buf; |
640
|
0
|
0
|
|
|
|
|
last if $char == 0; |
641
|
0
|
|
|
|
|
|
$parse_word .= $buf; |
642
|
|
|
|
|
|
|
} |
643
|
0
|
0
|
|
|
|
|
if( defined $parse_word ) { |
644
|
0
|
|
|
|
|
|
$parse_word =~ s/^\s+//; |
645
|
0
|
|
|
|
|
|
$parse_word =~ s/\s+$//; |
646
|
0
|
|
|
|
|
|
$self->{copyright} = $parse_word; |
647
|
|
|
|
|
|
|
} else { |
648
|
0
|
|
|
|
|
|
$self->{copyright} = ''; |
649
|
|
|
|
|
|
|
} |
650
|
|
|
|
|
|
|
|
651
|
0
|
|
|
|
|
|
my $ccount = 0; |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
# Check position for across number |
654
|
0
|
|
|
|
|
|
for (my $j=0;$j<$height;$j++) { |
655
|
0
|
|
|
|
|
|
my $rowtext; |
656
|
0
|
|
|
|
|
|
for(my $k=0;$k<$width;$k++) { |
657
|
|
|
|
|
|
|
# Check position for across number |
658
|
|
|
|
|
|
|
# Left edge non-black followed by non-black |
659
|
0
|
|
|
|
|
|
my $anum = 0; # across number |
660
|
0
|
0
|
0
|
|
|
|
if( ($k == 0 && |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
661
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && # Row $j, Col 0 (k) |
662
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.') || # Row $j, Col 1 (k+1) |
663
|
|
|
|
|
|
|
# Previous black - nonblack - nonblack |
664
|
|
|
|
|
|
|
( ($k+1)<$width && # Not last col |
665
|
|
|
|
|
|
|
($k-1)>=0 && # Not first col |
666
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) ne '.' && |
667
|
|
|
|
|
|
|
substr($diagram[$j],$k-1,1) eq '.' && |
668
|
|
|
|
|
|
|
substr($diagram[$j],$k+1,1) ne '.' ) ) { |
669
|
|
|
|
|
|
|
|
670
|
0
|
|
|
|
|
|
$ccount++; |
671
|
0
|
|
|
|
|
|
$anum = $ccount; |
672
|
|
|
|
|
|
|
} |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
# Check position for down number |
675
|
0
|
|
|
|
|
|
my $dnum = 0; |
676
|
0
|
0
|
0
|
|
|
|
if( ($j == 0 && # Row 0 |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
677
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) eq '-' && |
678
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) eq '-') || |
679
|
|
|
|
|
|
|
# Black above - nonblack - nonblack below |
680
|
|
|
|
|
|
|
( ($j-1)>=0 && # Not first row |
681
|
|
|
|
|
|
|
($j+1)<$height && # Not last row |
682
|
|
|
|
|
|
|
substr($diagram[$j],$k,1) eq '-' && |
683
|
|
|
|
|
|
|
substr($diagram[$j-1],$k,1) eq '.' && |
684
|
|
|
|
|
|
|
substr($diagram[$j+1],$k,1) eq '-' ) ) { |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
# Don't double number the same space |
687
|
0
|
0
|
|
|
|
|
if( $anum == 0 ) { |
688
|
0
|
|
|
|
|
|
$ccount++; |
689
|
|
|
|
|
|
|
} |
690
|
0
|
|
|
|
|
|
$dnum = $ccount; |
691
|
|
|
|
|
|
|
} |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
# Get clues |
694
|
|
|
|
|
|
|
# Across |
695
|
0
|
0
|
|
|
|
|
if( $anum != 0 ) { |
696
|
0
|
|
|
|
|
|
my $tmp; |
697
|
0
|
|
|
|
|
|
$parse_word = ''; |
698
|
0
|
|
|
|
|
|
$oe = 0; |
699
|
0
|
|
|
|
|
|
while(1) { |
700
|
0
|
0
|
|
|
|
|
read(FH, $buf, 1) or last; |
701
|
0
|
|
|
|
|
|
my ($char) = unpack "C", $buf; |
702
|
0
|
0
|
|
|
|
|
last if $char == 0; |
703
|
0
|
|
|
|
|
|
$parse_word .= $buf; |
704
|
|
|
|
|
|
|
} |
705
|
0
|
|
|
|
|
|
$parse_word =~ s/^\s+//; |
706
|
0
|
|
|
|
|
|
$parse_word =~ s/\s+$//; |
707
|
0
|
|
|
|
|
|
$tmp = $parse_word; |
708
|
0
|
|
|
|
|
|
$aclues .= "$anum - $tmp\n"; |
709
|
|
|
|
|
|
|
|
710
|
0
|
|
|
|
|
|
my $key = "$anum"; |
711
|
0
|
|
|
|
|
|
$self->{across}{$key}{direction} = 'across'; |
712
|
0
|
|
|
|
|
|
$self->{across}{$key}{clue_number} = $anum; |
713
|
0
|
|
|
|
|
|
$self->{across}{$key}{clue} = $tmp; |
714
|
|
|
|
|
|
|
} |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
# Down |
717
|
0
|
0
|
|
|
|
|
if( $dnum != 0 ) { |
718
|
0
|
|
|
|
|
|
my $tmp; |
719
|
0
|
|
|
|
|
|
$parse_word = ''; |
720
|
0
|
|
|
|
|
|
$oe = 0; |
721
|
0
|
|
|
|
|
|
while(1) { |
722
|
0
|
0
|
|
|
|
|
read(FH, $buf, 1) or last; |
723
|
0
|
|
|
|
|
|
my ($char) = unpack "C", $buf; |
724
|
0
|
0
|
|
|
|
|
last if $char == 0; |
725
|
0
|
|
|
|
|
|
$parse_word .= $buf; |
726
|
|
|
|
|
|
|
} |
727
|
0
|
|
|
|
|
|
$parse_word =~ s/^\s+//; |
728
|
0
|
|
|
|
|
|
$parse_word =~ s/\s+$//; |
729
|
0
|
|
|
|
|
|
$tmp = $parse_word; |
730
|
0
|
|
|
|
|
|
$dclues .= "$dnum - $tmp\n"; |
731
|
|
|
|
|
|
|
|
732
|
0
|
|
|
|
|
|
my $key = "$dnum"; |
733
|
0
|
|
|
|
|
|
$self->{down}{$key}{direction} = 'down'; |
734
|
0
|
|
|
|
|
|
$self->{down}{$key}{clue_number} = $dnum; |
735
|
0
|
|
|
|
|
|
$self->{down}{$key}{clue} = $tmp; |
736
|
|
|
|
|
|
|
} |
737
|
|
|
|
|
|
|
} |
738
|
|
|
|
|
|
|
} |
739
|
|
|
|
|
|
|
|
740
|
0
|
|
|
|
|
|
close FH; |
741
|
0
|
|
|
|
|
|
$self->{aclues} = $aclues; |
742
|
0
|
|
|
|
|
|
$self->{dclues} = $dclues; |
743
|
0
|
|
|
|
|
|
$self->{is_parsed} = 1; |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
} |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
sub is_parsed { |
748
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
749
|
0
|
|
|
|
|
|
return $self->{is_parsed}; |
750
|
|
|
|
|
|
|
} |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
sub get_rows { |
753
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
754
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
755
|
0
|
|
|
|
|
|
return $self->{rows}; |
756
|
|
|
|
|
|
|
} |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
sub get_columns { |
759
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
760
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
761
|
0
|
|
|
|
|
|
return $self->{columns}; |
762
|
|
|
|
|
|
|
} |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
sub get_solution { |
765
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
766
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
767
|
0
|
|
|
|
|
|
my $solref = $self->{solution}; |
768
|
0
|
|
|
|
|
|
my @sol = @$solref; |
769
|
0
|
|
|
|
|
|
return @sol; |
770
|
|
|
|
|
|
|
} |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
sub get_diagram { |
773
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
774
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
775
|
0
|
|
|
|
|
|
my $diagref = $self->{diagram}; |
776
|
0
|
|
|
|
|
|
my @diag = @$diagref; |
777
|
0
|
|
|
|
|
|
return @diag; |
778
|
|
|
|
|
|
|
} |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
sub get_title { |
781
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
782
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
783
|
0
|
|
|
|
|
|
return $self->{title}; |
784
|
|
|
|
|
|
|
} |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
sub get_author { |
787
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
788
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
789
|
0
|
|
|
|
|
|
return $self->{author}; |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
sub get_copyright { |
793
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
794
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
795
|
0
|
|
|
|
|
|
return $self->{copyright}; |
796
|
|
|
|
|
|
|
} |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
sub get_across_clues { |
799
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
800
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
801
|
0
|
|
|
|
|
|
return $self->{aclues}; |
802
|
|
|
|
|
|
|
} |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
sub get_down_clues { |
805
|
0
|
|
|
0
|
1
|
|
my($self) = shift; |
806
|
0
|
0
|
|
|
|
|
_parse_file($self) unless $self->{is_parsed}; |
807
|
0
|
|
|
|
|
|
return $self->{dclues}; |
808
|
|
|
|
|
|
|
} |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
1; |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
__END__ |