pangrid 0.7.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README +6 -5
- data/lib/pangrid/clue.rb +137 -0
- data/lib/pangrid/clues.rb +1 -1
- data/lib/pangrid/plugin.rb +6 -9
- data/lib/pangrid/plugins/acrosslite.rb +37 -28
- data/lib/pangrid/plugins/exolve.rb +421 -78
- data/lib/pangrid/plugins/ipuz.rb +417 -0
- data/lib/pangrid/plugins/json.rb +2 -2
- data/lib/pangrid/plugins/png.rb +1 -1
- data/lib/pangrid/plugins/qxw.rb +9 -14
- data/lib/pangrid/version.rb +1 -1
- data/lib/pangrid/xw.rb +211 -42
- metadata +4 -2
|
@@ -2,64 +2,305 @@
|
|
|
2
2
|
# crossword solver that allows you to embed the crossword and solving software
|
|
3
3
|
# in a single HTML file.
|
|
4
4
|
|
|
5
|
+
require 'strscan'
|
|
6
|
+
|
|
5
7
|
module Pangrid
|
|
6
8
|
|
|
9
|
+
# Clue syntax shared by the reader and writer:
|
|
10
|
+
#
|
|
11
|
+
# 1, 5, 2d Clue text (7,3,4) Explanation
|
|
12
|
+
# Hint: a hint
|
|
13
|
+
# Concise: a shorter clue
|
|
14
|
+
#
|
|
15
|
+
# Linked clues list the later parts after the first number; parts in another
|
|
16
|
+
# direction get a suffix (a or d). The explanation ("anno") follows the
|
|
17
|
+
# enumeration; a clue with an explanation but no enumeration separates the two
|
|
18
|
+
# with "[]". The definition can be marked as ~{definition}~, and other parts
|
|
19
|
+
# of the clue as ~{{css-class}text}~. Clues without a direction (exolve-nodir) start with the cells
|
|
20
|
+
# they occupy and a label: "#c3 #c5 [A] Clue (2)".
|
|
21
|
+
module ExolveClues
|
|
22
|
+
# Written when the puzzle has no id; exolve needs one to save progress
|
|
23
|
+
PLACEHOLDER_ID = 'replace-with-unique-id'
|
|
24
|
+
|
|
25
|
+
DIRECTION_SUFFIX = {"a" => :across, "d" => :down}
|
|
26
|
+
|
|
27
|
+
# Tokens of a clue line, in the order they appear. These are matched by a
|
|
28
|
+
# StringScanner, so they are anchored at the scan position.
|
|
29
|
+
#
|
|
30
|
+
# A cell location before a nodir clue: "#c3r1", "#r1c3" or "#a3"
|
|
31
|
+
CELL = /#([a-z]\d+|c\d+r\d+|r\d+c\d+)\s+/
|
|
32
|
+
# One clue reference: a number with an optional direction suffix
|
|
33
|
+
REF = /\d+[adbu]?/
|
|
34
|
+
# The clue number and any linked parts, "1, 5, 2d" or "1 & 5", optionally
|
|
35
|
+
# followed by a period. It must be followed by a space or the end of the
|
|
36
|
+
# line, so that "2nd place" isn't read as clue 2.
|
|
37
|
+
LEAD = /(#{REF}(?:\s*[,&]\s*#{REF})*)\.?(?:\s+|\z)/
|
|
38
|
+
# A label shown instead of the number: "[A]"
|
|
39
|
+
LABEL = /\[([^\]]+)\]\s*/
|
|
40
|
+
# Marks the end of the clue text when there is no enumeration
|
|
41
|
+
END_OF_CLUE = /\[\]/
|
|
42
|
+
# Lines that add to the clue before them
|
|
43
|
+
HINT = /hint:\s*/i
|
|
44
|
+
CONCISE = /concise:\s*/i
|
|
45
|
+
# Separator lines between groups of clues
|
|
46
|
+
SEPARATOR = /---/
|
|
47
|
+
# In-clue annotations: ~{text}~ or ~{{css-class}text}~
|
|
48
|
+
SPAN_OPEN = /~\{/
|
|
49
|
+
SPAN_STYLE = /\{([^{}~]*)\}/
|
|
50
|
+
SPAN_CLOSE = /\}~/
|
|
51
|
+
|
|
52
|
+
# [x, y] -> "c<col>r<row>", rows counted from the bottom
|
|
53
|
+
def format_cell(xw, (x, y))
|
|
54
|
+
"c#{x + 1}r#{xw.height - y}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def parse_cell(height, s)
|
|
58
|
+
case s
|
|
59
|
+
when /\A([a-z])(\d+)\z/ then [$1.ord - 'a'.ord, height - $2.to_i]
|
|
60
|
+
when /\Ac(\d+)r(\d+)\z/ then [$1.to_i - 1, height - $2.to_i]
|
|
61
|
+
when /\Ar(\d+)c(\d+)\z/ then [$2.to_i - 1, height - $1.to_i]
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def parse_ref(s)
|
|
66
|
+
ClueRef.new(s.to_i, DIRECTION_SUFFIX[s[/[a-z]\z/]])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# String -> Clue
|
|
70
|
+
#
|
|
71
|
+
# A clue line is, in order: cell locations, the number and linked parts,
|
|
72
|
+
# a label, the clue text, the enumeration, and the explanation. All are
|
|
73
|
+
# optional except the text.
|
|
74
|
+
def parse_clue(line, height)
|
|
75
|
+
clue = Clue.new
|
|
76
|
+
ss = StringScanner.new(line.strip)
|
|
77
|
+
clue.cells << parse_cell(height, ss[1]) while ss.scan(CELL)
|
|
78
|
+
if ss.scan(LEAD)
|
|
79
|
+
first, *rest = ss[1].scan(REF)
|
|
80
|
+
clue.number = first.to_i
|
|
81
|
+
clue.linked = rest.map {|r| parse_ref(r)}
|
|
82
|
+
end
|
|
83
|
+
clue.label = ss[1] if ss.scan(LABEL)
|
|
84
|
+
text, enum, anno = parse_clue_body(ss)
|
|
85
|
+
clue.text, clue.spans = parse_spans(text.strip)
|
|
86
|
+
clue.enumeration = enum && enum.strip
|
|
87
|
+
clue.explanation = anno.strip if anno && !anno.strip.empty?
|
|
88
|
+
clue
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The rest of a clue line -> [text, enumeration, explanation]. With "[]",
|
|
92
|
+
# the text ends there and may itself end in an enumeration; otherwise the
|
|
93
|
+
# enumeration is found by Clue.find_enumeration's rules.
|
|
94
|
+
def parse_clue_body(ss)
|
|
95
|
+
if (upto = ss.scan_until(END_OF_CLUE))
|
|
96
|
+
text = upto.delete_suffix(ss.matched)
|
|
97
|
+
text, enum = Clue.split_trailing_enumeration(text) || [text, nil]
|
|
98
|
+
[text, enum, ss.rest]
|
|
99
|
+
elsif (m = Clue.find_enumeration(ss.rest))
|
|
100
|
+
[m.pre_match, m[1], m.post_match]
|
|
101
|
+
else
|
|
102
|
+
[ss.rest, nil, nil]
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Clue text with ~{...}~ markup -> [plain text, ClueSpan[]]. A "~{" with
|
|
107
|
+
# no closing "}~" is kept as text.
|
|
108
|
+
def parse_spans(text)
|
|
109
|
+
ss = StringScanner.new(text)
|
|
110
|
+
plain, spans = +"", []
|
|
111
|
+
until ss.eos?
|
|
112
|
+
start = ss.pos
|
|
113
|
+
if ss.scan(SPAN_OPEN)
|
|
114
|
+
style = ss.scan(SPAN_STYLE) && ss[1]
|
|
115
|
+
if (inner = ss.scan_until(SPAN_CLOSE))
|
|
116
|
+
inner = inner.delete_suffix(ss.matched)
|
|
117
|
+
spans << ClueSpan.new(plain.length, inner.length, style)
|
|
118
|
+
plain << inner
|
|
119
|
+
next
|
|
120
|
+
end
|
|
121
|
+
ss.pos = start
|
|
122
|
+
end
|
|
123
|
+
plain << ss.getch
|
|
124
|
+
end
|
|
125
|
+
[plain, spans]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Plain text and ClueSpans -> text with ~{...}~ markup
|
|
129
|
+
def format_spans(text, spans)
|
|
130
|
+
out, pos = +"", 0
|
|
131
|
+
spans.sort_by(&:start).each do |sp|
|
|
132
|
+
next if sp.start < pos || sp.start + sp.length > text.length
|
|
133
|
+
out << text[pos...sp.start] << "~{"
|
|
134
|
+
out << "{#{sp.style}}" if sp.style
|
|
135
|
+
out << text[sp.range] << "}~"
|
|
136
|
+
pos = sp.start + sp.length
|
|
137
|
+
end
|
|
138
|
+
out << text[pos..]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# String[] -> Clue[], attaching Hint: and Concise: lines to their clues
|
|
142
|
+
def parse_clues(lines, height)
|
|
143
|
+
clues = []
|
|
144
|
+
lines.each do |line|
|
|
145
|
+
ss = StringScanner.new(line.strip)
|
|
146
|
+
next if ss.eos? || ss.match?(SEPARATOR)
|
|
147
|
+
if clues.last && ss.scan(HINT)
|
|
148
|
+
clues.last.hints << ss.rest
|
|
149
|
+
elsif clues.last && ss.scan(CONCISE)
|
|
150
|
+
clues.last.concise = ss.rest
|
|
151
|
+
else
|
|
152
|
+
clues << parse_clue(ss.string, height)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
clues
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Light, Clue -> String[]
|
|
159
|
+
#
|
|
160
|
+
# Lights whose square has a label (and so no number) are placed by giving
|
|
161
|
+
# the square's location instead: "#c1r3 [B] Clue"
|
|
162
|
+
def format_clue(xw, light, clue)
|
|
163
|
+
clue ||= Clue.new
|
|
164
|
+
return [] if clue.parent && clue.text.to_s.empty?
|
|
165
|
+
n, label, cells = light && light.number, clue.label, clue.cells
|
|
166
|
+
if n.is_a?(String)
|
|
167
|
+
cells = [[light.x, light.y]]
|
|
168
|
+
label = n unless n.empty?
|
|
169
|
+
n = nil
|
|
170
|
+
end
|
|
171
|
+
lead = cells.map {|c| "#" + format_cell(xw, c)}
|
|
172
|
+
lead << ([n] + clue.linked).compact.map(&:to_s).join(", ") if n
|
|
173
|
+
lead = lead.join(" ")
|
|
174
|
+
lead = lead.rjust(2) if n && cells.empty?
|
|
175
|
+
s = lead.empty? ? [] : [lead]
|
|
176
|
+
s << "[#{label}]" if label
|
|
177
|
+
s << format_spans(clue.text, clue.spans) if clue.text && !clue.text.empty?
|
|
178
|
+
s << "(#{clue.enumeration})" if clue.enumeration
|
|
179
|
+
if clue.explanation
|
|
180
|
+
s << "[]" unless clue.enumeration
|
|
181
|
+
s << clue.explanation
|
|
182
|
+
end
|
|
183
|
+
[s.join(" ").rstrip] +
|
|
184
|
+
clue.hints.map {|h| "Hint: #{h}"} +
|
|
185
|
+
(clue.concise ? ["Concise: #{clue.concise}"] : [])
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
7
189
|
module ExolveWriter
|
|
190
|
+
include ExolveClues
|
|
191
|
+
|
|
8
192
|
def write(xw)
|
|
9
193
|
headers = format_headers(xw)
|
|
10
|
-
across, down = xw.number
|
|
11
194
|
grid = format_grid(xw)
|
|
12
|
-
ac = format_clues(
|
|
13
|
-
dn = format_clues(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
195
|
+
ac = format_clues(xw, xw.across_clues, :across)
|
|
196
|
+
dn = format_clues(xw, xw.down_clues, :down)
|
|
197
|
+
body = headers +
|
|
198
|
+
section("grid", grid) +
|
|
199
|
+
section("across", ac) +
|
|
200
|
+
section("down", dn)
|
|
201
|
+
unless xw.nodir_clues.empty?
|
|
202
|
+
body += section("nodir", xw.nodir_clues.flat_map {|c| format_clue(xw, nil, c)})
|
|
203
|
+
end
|
|
204
|
+
body += format_colours(xw)
|
|
205
|
+
body += format_extras(xw)
|
|
206
|
+
body += header("explanations", xw.explanation, multiline: true) if xw.explanation
|
|
207
|
+
body += header("postscript", xw.postscript, multiline: true) if xw.postscript
|
|
18
208
|
out = ["exolve-begin"] + indent(body) + ["exolve-end"]
|
|
19
209
|
out.join("\n")
|
|
20
210
|
end
|
|
21
211
|
|
|
212
|
+
# Single line headers are written inline, multi-line ones on the following
|
|
213
|
+
# lines
|
|
214
|
+
def header(key, value, multiline: false)
|
|
215
|
+
lines = value.to_s.split("\n")
|
|
216
|
+
if lines.length > 1 || multiline
|
|
217
|
+
section(key, lines)
|
|
218
|
+
else
|
|
219
|
+
["exolve-#{key}: #{value}"]
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def section(key, lines)
|
|
224
|
+
["exolve-#{key}:"] + indent(lines)
|
|
225
|
+
end
|
|
226
|
+
|
|
22
227
|
def format_headers(xw)
|
|
23
228
|
headers = [
|
|
24
|
-
'id',
|
|
25
|
-
'title', xw.title,
|
|
26
|
-
'setter', xw.author,
|
|
27
|
-
'width', xw.width,
|
|
28
|
-
'height', xw.height,
|
|
29
|
-
'copyright', xw.copyright,
|
|
30
|
-
'
|
|
229
|
+
['id', xw.id || PLACEHOLDER_ID],
|
|
230
|
+
['title', xw.title],
|
|
231
|
+
['setter', xw.author],
|
|
232
|
+
['width', xw.width],
|
|
233
|
+
['height', xw.height],
|
|
234
|
+
['copyright', xw.copyright],
|
|
235
|
+
*(xw.credits || []).map {|c| ['credits', c]},
|
|
236
|
+
['email', xw.email],
|
|
237
|
+
['language', xw.language],
|
|
238
|
+
['maker', xw.origin],
|
|
239
|
+
['prelude', xw.preamble]
|
|
31
240
|
]
|
|
32
|
-
headers.
|
|
241
|
+
headers.select {|k, v| v}.flat_map {|k, v| header(k, v)}
|
|
33
242
|
end
|
|
34
243
|
|
|
35
|
-
def format_clues(
|
|
36
|
-
|
|
244
|
+
def format_clues(xw, clues, dir)
|
|
245
|
+
lights = xw.lights.select {|l| l.direction == dir}
|
|
246
|
+
lights.each_with_index.flat_map {|l, i| format_clue(xw, l, clues[i])}
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def format_extras(xw)
|
|
250
|
+
out = (xw.ninas || []).map do |n|
|
|
251
|
+
"exolve-nina: " + ([n.color] + n.cells.map {|c| format_cell(xw, c)}).compact.join(" ")
|
|
252
|
+
end
|
|
253
|
+
out += (xw.questions || []).map do |q|
|
|
254
|
+
# exolve needs an enumeration to check an answer
|
|
255
|
+
enum = q.enumeration || (q.answer && q.answer.split(/\s+/).map(&:length).join(","))
|
|
256
|
+
"exolve-question: " + [q.text, enum && "(#{enum})", q.answer].compact.join(" ")
|
|
257
|
+
end
|
|
258
|
+
out + (xw.alternatives || []).map do |group|
|
|
259
|
+
"exolve-alternatives: " + group.map {|c, l| "#{format_cell(xw, c)}:#{l}"}.join(" ")
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# One exolve-colour line per colour
|
|
264
|
+
def format_colours(xw)
|
|
265
|
+
cells = Hash.new {|h, k| h[k] = []}
|
|
266
|
+
xw.each_cell_with_coords {|x, y, c| cells[c.color] << [x, y] if c.color}
|
|
267
|
+
cells.map do |colour, cs|
|
|
268
|
+
"exolve-colour: #{colour} " + cs.map {|c| format_cell(xw, c)}.join(" ")
|
|
269
|
+
end
|
|
37
270
|
end
|
|
38
271
|
|
|
39
272
|
def indent(lines)
|
|
40
273
|
lines.map {|x| " " + x}
|
|
41
274
|
end
|
|
42
275
|
|
|
43
|
-
# Exolve marks
|
|
44
|
-
# right, _
|
|
45
|
-
#
|
|
276
|
+
# Exolve marks squares with decorators after the letter:
|
|
277
|
+
# | bar on the right, _ bar underneath, + both
|
|
278
|
+
# @ circle, * diagramless, ! letter given, ~ no clue number
|
|
279
|
+
# If any square has decorators, squares are padded so the columns line up.
|
|
46
280
|
def format_grid(xw)
|
|
47
281
|
rows = xw.to_array(:black => '.', :null => '0') {|c| grid_char(c)}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
right, bottom = xw.bar?(x, y, :right), xw.bar?(x, y, :bottom)
|
|
51
|
-
right && bottom ? '+' : right ? '|' : bottom ? '_' : ' '
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
if decorators.flatten.all? {|d| d == ' '}
|
|
55
|
-
rows.map(&:join)
|
|
56
|
-
else
|
|
57
|
-
rows.zip(decorators).map {|r, d| r.zip(d).join.rstrip}
|
|
282
|
+
tokens = rows.each_with_index.map do |row, y|
|
|
283
|
+
row.each_with_index.map {|ch, x| ch.to_s + decorators(xw, x, y)}
|
|
58
284
|
end
|
|
285
|
+
width = tokens.flatten.map(&:length).max
|
|
286
|
+
tokens.map {|row| row.map {|t| t.ljust(width)}.join.rstrip}
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def decorators(xw, x, y)
|
|
290
|
+
cell = xw.solution[y][x]
|
|
291
|
+
right, bottom = xw.bar?(x, y, :right), xw.bar?(x, y, :bottom)
|
|
292
|
+
d = right && bottom ? '+' : right ? '|' : bottom ? '_' : ''
|
|
293
|
+
d += '@' if cell.circle
|
|
294
|
+
d += '*' if cell.diagramless
|
|
295
|
+
d += '!' if cell.given
|
|
296
|
+
d += '~' if cell.label
|
|
297
|
+
d
|
|
59
298
|
end
|
|
60
299
|
end
|
|
61
300
|
|
|
62
301
|
module ExolveReader
|
|
302
|
+
include ExolveClues
|
|
303
|
+
|
|
63
304
|
def read(data)
|
|
64
305
|
s = data.each_line.map(&:rstrip)
|
|
65
306
|
first = s.index('exolve-begin')
|
|
@@ -68,20 +309,37 @@ module ExolveReader
|
|
|
68
309
|
check("exolve-end missing") { last }
|
|
69
310
|
lines = s[(first + 1)...last]
|
|
70
311
|
xw = XWord.new
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
312
|
+
clues = {}
|
|
313
|
+
colours, ninas, grid_bars = [], [], []
|
|
314
|
+
sections(lines).each do |field, value, body|
|
|
315
|
+
text = value.empty? ? body.map(&:strip).join("\n") : value
|
|
316
|
+
next if field == "id" && text == PLACEHOLDER_ID
|
|
317
|
+
if %w(id title copyright email language postscript).include? field
|
|
318
|
+
xw[field] = text
|
|
319
|
+
elsif field == "explanations"
|
|
320
|
+
xw.explanation = text
|
|
321
|
+
elsif field == "credits"
|
|
322
|
+
(xw.credits ||= []) << text
|
|
323
|
+
elsif field == "maker"
|
|
324
|
+
xw.origin = text
|
|
325
|
+
elsif %w(prelude preamble).include? field
|
|
326
|
+
xw.preamble = text
|
|
77
327
|
elsif field == "setter"
|
|
78
|
-
xw.author =
|
|
328
|
+
xw.author = text
|
|
79
329
|
elsif %w(height width).include? field
|
|
80
|
-
xw[field] =
|
|
81
|
-
elsif %w(across down).include? field
|
|
82
|
-
|
|
330
|
+
xw[field] = text.to_i
|
|
331
|
+
elsif %w(across down nodir).include? field
|
|
332
|
+
clues[field] = body
|
|
333
|
+
elsif %w(colour color).include? field
|
|
334
|
+
colours << text
|
|
335
|
+
elsif field == "nina"
|
|
336
|
+
ninas << text
|
|
337
|
+
elsif field == "question"
|
|
338
|
+
(xw.questions ||= []) << parse_question(text)
|
|
339
|
+
elsif field == "alternatives"
|
|
340
|
+
(xw.alternatives ||= []) << parse_alternatives(xw, text)
|
|
83
341
|
elsif field == "grid"
|
|
84
|
-
xw.solution = parse_grid(
|
|
342
|
+
xw.solution, grid_bars = parse_grid(body)
|
|
85
343
|
end
|
|
86
344
|
end
|
|
87
345
|
check("exolve-grid missing") { xw.solution && !xw.solution.empty? }
|
|
@@ -92,28 +350,116 @@ module ExolveReader
|
|
|
92
350
|
xw.solution.length == xw.height &&
|
|
93
351
|
xw.solution.all? {|row| row.length == xw.width}
|
|
94
352
|
}
|
|
353
|
+
grid_bars.each {|x, y, side| xw.set_bar(x, y, side)}
|
|
354
|
+
read_clues(xw, clues)
|
|
355
|
+
colours.each do |line|
|
|
356
|
+
colour, cells = parse_cell_list(xw, line)
|
|
357
|
+
cells.each {|x, y| xw.solution[y][x].color = colour} if colour
|
|
358
|
+
end
|
|
359
|
+
xw.ninas = ninas.map {|line| Nina.new(*parse_cell_list(xw, line).reverse)} unless ninas.empty?
|
|
95
360
|
xw
|
|
96
361
|
end
|
|
97
362
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
363
|
+
# "What links the ninas? (3,4) GET LOST"
|
|
364
|
+
def parse_question(s)
|
|
365
|
+
m = Clue.find_enumeration(s)
|
|
366
|
+
return Question.new(s.strip) unless m
|
|
367
|
+
answer = m.post_match.strip
|
|
368
|
+
Question.new(m.pre_match.strip, m[1].strip, answer.empty? ? nil : answer)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# "r1c6:Z r2c1:A" -> {[5, 2] => "Z", [0, 1] => "A"}
|
|
372
|
+
def parse_alternatives(xw, s)
|
|
373
|
+
s.split.each_with_object({}) do |word, h|
|
|
374
|
+
loc, letter = word.split(":", 2)
|
|
375
|
+
cell = parse_cell(xw.height, loc)
|
|
376
|
+
h[cell] = letter if cell && letter
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
# "palegreen a1 c2r3 A12 16D" -> ["palegreen", [[x, y], ...]], where A12
|
|
381
|
+
# and 16D are all the squares of 12 across and 16 down. The colour (or
|
|
382
|
+
# other non-location word) can be anywhere in the list.
|
|
383
|
+
def parse_cell_list(xw, line)
|
|
384
|
+
other = nil
|
|
385
|
+
cells = line.split.flat_map do |word|
|
|
386
|
+
if (cell = parse_cell(xw.height, word))
|
|
387
|
+
x, y = cell
|
|
388
|
+
(0...xw.width).cover?(x) && (0...xw.height).cover?(y) ? [cell] : []
|
|
389
|
+
elsif (m = word.match(/\A(?:([AD])(\d+)|(\d+)([adAD]))\z/))
|
|
390
|
+
dir = (m[1] || m[4]).downcase == "a" ? :across : :down
|
|
391
|
+
light = xw.light((m[2] || m[3]).to_i, dir)
|
|
392
|
+
light ? xw.light_cells(light.x, light.y, dir) : []
|
|
104
393
|
else
|
|
105
|
-
|
|
394
|
+
other = word
|
|
395
|
+
[]
|
|
106
396
|
end
|
|
107
397
|
end
|
|
398
|
+
[other, cells]
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Lines -> [field, inline value, following lines][]
|
|
402
|
+
def sections(lines)
|
|
403
|
+
headers = lines.each.with_index.map do |l, i|
|
|
404
|
+
m = l.match(/^(\s+)exolve-([\w-]+):(.*)$/)
|
|
405
|
+
m && [i, m[2], m[3].strip]
|
|
406
|
+
end
|
|
108
407
|
headers.compact!
|
|
109
|
-
headers.push([lines.length
|
|
110
|
-
headers.each_cons(2) do |i, j|
|
|
111
|
-
|
|
112
|
-
|
|
408
|
+
headers.push([lines.length])
|
|
409
|
+
headers.each_cons(2).map do |(i, field, value), (j, _)|
|
|
410
|
+
[field, value, lines[(i + 1)...j]]
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def read_clues(xw, sections)
|
|
415
|
+
h = xw.height
|
|
416
|
+
across = parse_clues(sections["across"] || [], h)
|
|
417
|
+
down = parse_clues(sections["down"] || [], h)
|
|
418
|
+
xw.nodir_clues = parse_clues(sections["nodir"] || [], h)
|
|
419
|
+
children = linked_children(across, :across).merge(linked_children(down, :down))
|
|
420
|
+
xw.across_clues = align_clues(xw, across, :across, children)
|
|
421
|
+
xw.down_clues = align_clues(xw, down, :down, children)
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
# {[number, direction] => ClueRef to the first part} for the later parts
|
|
425
|
+
# of linked clues
|
|
426
|
+
def linked_children(clues, dir)
|
|
427
|
+
clues.each_with_object({}) do |c, h|
|
|
428
|
+
c.linked.each do |r|
|
|
429
|
+
h[[r.number, r.direction || dir]] = ClueRef.new(c.number, dir)
|
|
113
430
|
end
|
|
114
431
|
end
|
|
115
|
-
|
|
116
|
-
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# Match clues to the grid, by number or by the location of their first
|
|
435
|
+
# square ("#c1r3 [B] Clue", used for squares marked ~ with no number), and
|
|
436
|
+
# fill in the later parts of linked clues, which exolve does not require to
|
|
437
|
+
# be listed. If a clue can't be placed, keep the clues in the order given.
|
|
438
|
+
def align_clues(xw, clues, dir, children)
|
|
439
|
+
return clues if clues.empty?
|
|
440
|
+
lights = xw.lights.select {|l| l.direction == dir}
|
|
441
|
+
placed = {}
|
|
442
|
+
clues.each do |c|
|
|
443
|
+
light =
|
|
444
|
+
if c.cells.length == 1
|
|
445
|
+
lights.find {|l| [l.x, l.y] == c.cells[0]}
|
|
446
|
+
elsif c.number
|
|
447
|
+
lights.find {|l| l.number == c.number}
|
|
448
|
+
end
|
|
449
|
+
return clues if light.nil? || placed[light]
|
|
450
|
+
placed[light] = c
|
|
451
|
+
end
|
|
452
|
+
lights.map do |l|
|
|
453
|
+
c = placed[l] || Clue.new(number: l.number, parent: children[[l.number, dir]])
|
|
454
|
+
cell = xw.solution[l.y][l.x]
|
|
455
|
+
if c.cells == [[l.x, l.y]]
|
|
456
|
+
# the location just places the clue; a label belongs to the square
|
|
457
|
+
c.cells = []
|
|
458
|
+
cell.label = (c.label || c.number).to_s if cell.label == ""
|
|
459
|
+
c.label = nil
|
|
460
|
+
end
|
|
461
|
+
c
|
|
462
|
+
end
|
|
117
463
|
end
|
|
118
464
|
|
|
119
465
|
def parse_grid_char(char)
|
|
@@ -124,41 +470,37 @@ module ExolveReader
|
|
|
124
470
|
end
|
|
125
471
|
end
|
|
126
472
|
|
|
127
|
-
# Each square is a character followed by optional decorators
|
|
128
|
-
#
|
|
129
|
-
#
|
|
473
|
+
# Each square is a character followed by optional decorators (see
|
|
474
|
+
# ExolveWriter#format_grid)
|
|
475
|
+
# -> [Cell[][], [x, y, side][] bars]
|
|
130
476
|
def parse_grid(lines)
|
|
131
|
-
|
|
132
|
-
|
|
477
|
+
bars = []
|
|
478
|
+
grid = lines.each_with_index.map do |line, y|
|
|
479
|
+
line.scan(/([^\s|_+@*!~])([|_+@*!~]*)/).each_with_index.map do |(c, decorators), x|
|
|
133
480
|
cell = Cell.new(:solution => parse_grid_char(c))
|
|
134
|
-
|
|
135
|
-
|
|
481
|
+
bars << [x, y, :right] if decorators =~ /[|+]/
|
|
482
|
+
bars << [x, y, :bottom] if decorators =~ /[_+]/
|
|
483
|
+
cell.circle = true if decorators.include?("@")
|
|
484
|
+
cell.diagramless = true if decorators.include?("*")
|
|
485
|
+
cell.given = true if decorators.include?("!")
|
|
486
|
+
cell.label = "" if decorators.include?("~")
|
|
136
487
|
cell
|
|
137
488
|
end
|
|
138
489
|
end
|
|
139
|
-
|
|
140
|
-
grid
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
# Record each bar on the squares on both sides of it
|
|
144
|
-
def mirror_bars(grid)
|
|
145
|
-
grid.each_with_index do |row, y|
|
|
146
|
-
row.each_with_index do |cell, x|
|
|
147
|
-
right = row[x + 1]
|
|
148
|
-
below = grid[y + 1] && grid[y + 1][x]
|
|
149
|
-
right.borders |= [:left] if right && cell.has_bar?(:right)
|
|
150
|
-
below.borders |= [:top] if below && cell.has_bar?(:bottom)
|
|
151
|
-
end
|
|
152
|
-
end
|
|
490
|
+
[grid, bars]
|
|
153
491
|
end
|
|
154
492
|
end
|
|
155
493
|
|
|
494
|
+
EXOLVE_FEATURES = %i(bars circles colors given diagramless labels
|
|
495
|
+
explanations hints linked_clues definitions nodir_clues
|
|
496
|
+
ninas questions final_answer alternatives)
|
|
497
|
+
|
|
156
498
|
class ExolveFilled < Plugin
|
|
157
499
|
include ExolveReader
|
|
158
500
|
include ExolveWriter
|
|
159
501
|
|
|
160
502
|
DESCRIPTION = "Exolve writer with solutions"
|
|
161
|
-
FEATURES =
|
|
503
|
+
FEATURES = EXOLVE_FEATURES
|
|
162
504
|
|
|
163
505
|
def grid_char(cell)
|
|
164
506
|
cell.to_char
|
|
@@ -169,10 +511,11 @@ class ExolveBlank < Plugin
|
|
|
169
511
|
include ExolveWriter
|
|
170
512
|
|
|
171
513
|
DESCRIPTION = "Exolve writer without solutions"
|
|
172
|
-
FEATURES =
|
|
514
|
+
FEATURES = EXOLVE_FEATURES
|
|
173
515
|
|
|
516
|
+
# given letters are shown even in a blank grid
|
|
174
517
|
def grid_char(cell)
|
|
175
|
-
'0'
|
|
518
|
+
cell.given ? cell.to_char : '0'
|
|
176
519
|
end
|
|
177
520
|
end
|
|
178
521
|
|