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.
data/lib/pangrid/xw.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'clue'
2
+
1
3
  module Pangrid
2
4
 
3
5
  class PuzzleFormatError < StandardError
@@ -25,27 +27,41 @@ class Rebus
25
27
  end
26
28
  end
27
29
 
28
- # solution = :black | :null | char | Rebus
29
- # number = int
30
- # borders = [:left, :right, :top, :bottom]
30
+ # solution = :black | :null | :void | char | Rebus
31
+ # :null is an empty light (a square whose letter isn't known); :void is a
32
+ # square that is not part of the grid at all (ipuz null, qxw cutouts)
33
+ # number = int, filled in by XWord#number
34
+ # circle, highlight, given, diagramless = bool
35
+ # given: the letter is shown to the solver (exolve "!", ipuz "value")
36
+ # color = string, as given in the source file (e.g. "FF0000", "palegreen")
37
+ # label = string shown instead of the clue number; labelled squares don't
38
+ # use up a number, and "" means no number at all (exolve "~")
39
+ # marks = { "TL" | "T" | "TR" | "L" | "C" | "R" | "BL" | "B" | "BR" => text }
40
+ # small text in the corners or sides of the square (ipuz mark)
31
41
  class Cell
32
- attr_accessor :solution, :number, :borders
42
+ attr_accessor :solution, :number, :circle, :color, :highlight,
43
+ :given, :diagramless, :label, :marks
33
44
 
34
45
  def initialize(**args)
35
- @borders = []
46
+ @marks = {}
36
47
  args.each {|k,v| self.send :"#{k}=", v}
37
48
  end
38
49
 
39
- def borders=(b)
40
- @borders = b || []
50
+ def marks=(m)
51
+ @marks = m || {}
41
52
  end
42
53
 
43
54
  def black?
44
55
  solution == :black
45
56
  end
46
57
 
47
- def has_bar?(s)
48
- borders.include? s
58
+ def void?
59
+ solution == :void
60
+ end
61
+
62
+ # Part of a word: not a block or a void square
63
+ def light?
64
+ !black? && !void?
49
65
  end
50
66
 
51
67
  def rebus?
@@ -60,6 +76,7 @@ class Cell
60
76
  case solution
61
77
  when :black; '#'
62
78
  when :null; '.'
79
+ when :void; '_'
63
80
  when Rebus; solution.inspect
64
81
  else; solution
65
82
  end
@@ -68,15 +85,65 @@ class Cell
68
85
  alias_method :inspect, :to_w
69
86
  end
70
87
 
88
+ # A nina (a hidden message in the grid): cells = [x, y][], color = string
89
+ Nina = Struct.new(:cells, :color)
90
+
91
+ # A question to answer after solving the grid, e.g. the theme of a meta
92
+ # puzzle. text may be nil for a puzzle's final answer (ipuz answer).
93
+ Question = Struct.new(:text, :enumeration, :answer)
94
+
95
+ # A light (an across or down word): number is the clue number, or the
96
+ # square's label if it has one
97
+ Light = Struct.new(:number, :direction, :x, :y)
98
+
71
99
  # solution = Cell[][]
72
100
  # width, height = int
73
- # across_clues = string[]
74
- # down_clues = string[]
101
+ # across_clues = Clue[], one per across light, in numbering order
102
+ # down_clues = Clue[]
103
+ # nodir_clues = Clue[], clues without a direction (exolve nodir, ipuz
104
+ # "Clues"), typically with a label and a list of cells
75
105
  # rebus = { solution => [int, rebus_char] }
106
+ # bars = { [x, y, :right | :bottom] => :bar }, the bars between squares. Each
107
+ # bar is stored once, as the right or bottom edge of the square to its
108
+ # left or above it; use bar? and set_bar rather than editing the hash.
109
+ # The value is the style of the edge, leaving room for others (e.g.
110
+ # ipuz dotted or dashed edges).
111
+ #
112
+ # The clue lists also accept strings, which are converted to Clues.
113
+ #
114
+ # Metadata (all strings unless noted):
115
+ # title, author, editor, copyright, publisher, publication, url
116
+ # date: as given in the source file (ipuz uses mm/dd/yyyy)
117
+ # difficulty
118
+ # id: a unique id for the puzzle (ipuz uniqueid, exolve-id)
119
+ # preamble: instructions shown before solving (ipuz intro, exolve-prelude)
120
+ # notes: notes shown alongside the puzzle (AcrossLite notepad, ipuz notes)
121
+ # explanation: shown after solving (ipuz explanation, exolve-explanations)
122
+ # postscript: text shown below the puzzle (exolve-postscript)
123
+ # annotation: never shown to the solver (ipuz annotation)
124
+ # credits: string[] (exolve-credits)
125
+ # email: contact address(es) for the setter (exolve-email)
126
+ # origin: the program that made the file (ipuz origin, exolve-maker)
127
+ # language: language and script, e.g. "hi Devanagari" (exolve-language)
128
+ # charset: the characters that can be entered (ipuz charset)
129
+ #
130
+ # Puzzle-wide extras:
131
+ # ninas: Nina[]
132
+ # questions: Question[]
133
+ # misses: { wrong answer => hint } shown when the solver enters a common
134
+ # wrong answer (ipuz misses)
135
+ # alternatives: { [x, y] => letter }[], groups of squares that may all be
136
+ # filled with these letters instead (exolve-alternatives)
76
137
  class XWord
77
- # Metadata shared by all formats
78
- COMMON = %i(width height solution across_clues down_clues
79
- title author copyright notes preamble rebus)
138
+ METADATA = %i(title author editor copyright publisher publication url date
139
+ difficulty id preamble notes explanation postscript
140
+ annotation credits email origin language charset)
141
+
142
+ EXTRAS = %i(ninas questions misses alternatives)
143
+
144
+ # Fields shared by all formats
145
+ COMMON = %i(width height solution bars across_clues down_clues nodir_clues
146
+ rebus) + METADATA + EXTRAS
80
147
 
81
148
  # AcrossLite binary format fields
82
149
  # clues: across and down clues interleaved in numerical order
@@ -94,16 +161,24 @@ class XWord
94
161
  def initialize(**args)
95
162
  @across_clues = []
96
163
  @down_clues = []
164
+ @nodir_clues = []
165
+ @bars = {}
97
166
  args.each {|k, v| self[k] = v}
98
167
  end
99
168
 
100
- # Writers can rely on the clue lists being arrays
101
- def across_clues=(clues)
102
- @across_clues = clues || []
169
+ # Writers can rely on the clue lists being arrays of Clues
170
+ %i(across_clues down_clues nodir_clues).each do |list|
171
+ define_method(:"#{list}=") do |clues|
172
+ instance_variable_set(:"@#{list}", (clues || []).map {|c| Clue.coerce(c)})
173
+ end
103
174
  end
104
175
 
105
- def down_clues=(clues)
106
- @down_clues = clues || []
176
+ def bars=(b)
177
+ @bars = b || {}
178
+ end
179
+
180
+ def all_clues
181
+ (across_clues + down_clues + nodir_clues).compact
107
182
  end
108
183
 
109
184
  def [](k)
@@ -117,19 +192,58 @@ class XWord
117
192
  end
118
193
 
119
194
  def to_h
120
- ATTRIBUTES.to_h {|a| [a, public_send(a)]}.compact
195
+ ATTRIBUTES.to_h {|a| [a, public_send(a)]}.compact.reject {|k, v|
196
+ (k == :nodir_clues || k == :bars) && v.empty?
197
+ }
121
198
  end
122
199
 
123
200
  # Grid features that not every format can represent. Plugins list the ones
124
201
  # they support in Plugin::FEATURES.
125
202
  FEATURES = {
126
- bars: "bars between squares"
203
+ bars: "bars between squares",
204
+ circles: "circled squares",
205
+ colors: "coloured squares",
206
+ highlights: "highlighted squares",
207
+ given: "letters given in the grid",
208
+ diagramless: "diagramless squares",
209
+ labels: "non-standard clue numbers",
210
+ marks: "text in the corners of squares",
211
+ void: "squares outside the grid",
212
+ explanations: "clue explanations",
213
+ hints: "clue hints",
214
+ linked_clues: "linked clues",
215
+ definitions: "definitions marked in clues",
216
+ nodir_clues: "clues without a direction",
217
+ ninas: "ninas",
218
+ questions: "extra questions",
219
+ final_answer: "a final answer for the puzzle",
220
+ misses: "hints for wrong answers",
221
+ alternatives: "alternative answers"
127
222
  }
128
223
 
129
224
  # The features this crossword uses
130
225
  def features
226
+ cells = solution ? solution.flatten.compact : []
227
+ clues = all_clues
131
228
  used = []
132
- used << :bars if solution && solution.flatten.any? {|c| c && !c.borders.empty?}
229
+ used << :bars if bars.value?(:bar)
230
+ used << :circles if cells.any?(&:circle)
231
+ used << :colors if cells.any?(&:color)
232
+ used << :highlights if cells.any?(&:highlight)
233
+ used << :given if cells.any?(&:given)
234
+ used << :diagramless if cells.any?(&:diagramless)
235
+ used << :labels if cells.any?(&:label)
236
+ used << :marks if cells.any? {|c| !c.marks.empty?}
237
+ used << :void if cells.any?(&:void?)
238
+ used << :explanations if clues.any?(&:explanation)
239
+ used << :hints if clues.any? {|c| !c.hints.empty?}
240
+ used << :linked_clues if clues.any? {|c| !c.linked.empty? || c.parent}
241
+ used << :definitions if clues.any? {|c| !c.spans.empty?}
242
+ used << :nodir_clues if !nodir_clues.empty?
243
+ (EXTRAS - [:questions]).each {|f| used << f if self[f] && !self[f].empty?}
244
+ questions = self.questions || []
245
+ used << :questions if questions.any?(&:text)
246
+ used << :final_answer if questions.any? {|q| !q.text}
133
247
  used
134
248
  end
135
249
 
@@ -139,20 +253,46 @@ class XWord
139
253
  end
140
254
 
141
255
  def boundary?(x, y)
142
- (x < 0) || (y < 0) || (x >= width) || (y >= height) || black?(x, y)
256
+ (x < 0) || (y < 0) || (x >= width) || (y >= height) || !solution[y][x].light?
143
257
  end
144
258
 
145
- OPPOSITE = {left: :right, right: :left, top: :bottom, bottom: :top}
146
259
  NEIGHBOUR = {left: [-1, 0], right: [1, 0], top: [0, -1], bottom: [0, 1]}
147
260
 
148
- # Is there a bar on the given side of square (x, y)? A bar may be recorded
149
- # on either of the two squares it separates.
261
+ # The key for the bar on the given side of square (x, y) in the bars hash,
262
+ # or nil if that side is the edge of the grid
263
+ def bar_key(x, y, side)
264
+ dx, dy = NEIGHBOUR.fetch(side)
265
+ nx, ny = x + dx, y + dy
266
+ return nil unless in_grid?(x, y) && in_grid?(nx, ny)
267
+ case side
268
+ when :right, :bottom then [x, y, side]
269
+ when :left then [nx, ny, :right]
270
+ when :top then [nx, ny, :bottom]
271
+ end
272
+ end
273
+
274
+ # Is there a bar on the given side of light (x, y)?
150
275
  def bar?(x, y, side)
151
276
  return false if boundary?(x, y)
152
- return true if solution[y][x].has_bar?(side)
153
- dx, dy = NEIGHBOUR[side]
154
- nx, ny = x + dx, y + dy
155
- !boundary?(nx, ny) && solution[ny][nx].has_bar?(OPPOSITE[side])
277
+ key = bar_key(x, y, side)
278
+ !!key && bars[key] == :bar
279
+ end
280
+
281
+ # Add (or with on = false, remove) the bar on the given side of square
282
+ # (x, y). on can also be another edge style, which bar? ignores. Bars on the edge of the grid are ignored, since the edge already
283
+ # ends a word.
284
+ def set_bar(x, y, side, on = true)
285
+ key = bar_key(x, y, side)
286
+ return unless key
287
+ if on
288
+ bars[key] = on == true ? :bar : on
289
+ else
290
+ bars.delete(key)
291
+ end
292
+ end
293
+
294
+ def in_grid?(x, y)
295
+ x >= 0 && y >= 0 && x < width && y < height
156
296
  end
157
297
 
158
298
  # Does a word in direction dir end after square (x, y)?
@@ -174,11 +314,11 @@ class XWord
174
314
  !boundary?(x, y) && !word_end?(x, y, :down)
175
315
  end
176
316
 
177
- # Word starting from a square
178
- def word_from(x, y, dir)
179
- s = ""
317
+ # [x, y] of the squares in the word starting from a square
318
+ def light_cells(x, y, dir)
319
+ cells = []
180
320
  while !boundary?(x, y) do
181
- s += solution[y][x].to_w
321
+ cells << [x, y]
182
322
  break if word_end?(x, y, dir)
183
323
  if dir == :across
184
324
  x += 1
@@ -186,25 +326,40 @@ class XWord
186
326
  y += 1
187
327
  end
188
328
  end
189
- s
329
+ cells
190
330
  end
191
331
 
332
+ # Word starting from a square
333
+ def word_from(x, y, dir)
334
+ light_cells(x, y, dir).map {|cx, cy| solution[cy][cx].to_w}.join
335
+ end
336
+
337
+ # Number the grid. Returns the across and down clue numbers in order (and,
338
+ # if words is true, the words in the grid). Squares with a label don't use
339
+ # up a number, and their label takes the place of the number.
192
340
  def number(words = false)
193
341
  n, across, down = 1, [], []
194
342
  words_a, words_d = [], []
343
+ @lights = []
195
344
  each_cell {|c| c.number = nil}
196
345
  (0 ... height).each do |y|
197
346
  (0 ... width).each do |x|
198
- if across? x, y
199
- across << n
347
+ a, d = across?(x, y), down?(x, y)
348
+ next unless a || d
349
+ cell = solution[y][x]
350
+ id = cell.label || n
351
+ if a
352
+ across << id
200
353
  words_a << word_from(x, y, :across)
354
+ @lights << Light.new(id, :across, x, y)
201
355
  end
202
- if down? x, y
203
- down << n
356
+ if d
357
+ down << id
204
358
  words_d << word_from(x, y, :down)
359
+ @lights << Light.new(id, :down, x, y)
205
360
  end
206
- if across.last == n || down.last == n
207
- solution[y][x].number = n
361
+ unless cell.label
362
+ cell.number = n
208
363
  n += 1
209
364
  end
210
365
  end
@@ -212,6 +367,17 @@ class XWord
212
367
  words ? [across, down, words_a, words_d] : [across, down]
213
368
  end
214
369
 
370
+ # All the lights in grid order, across before down for each square
371
+ def lights
372
+ number
373
+ @lights
374
+ end
375
+
376
+ # The light with the given number (or label) and direction
377
+ def light(number, dir)
378
+ lights.find {|l| l.number == number && l.direction == dir}
379
+ end
380
+
215
381
  def each_cell
216
382
  (0 ... height).each do |y|
217
383
  (0 ... width).each do |x|
@@ -229,13 +395,16 @@ class XWord
229
395
  end
230
396
 
231
397
  # {:black => char, :null => char} -> Any[][]
398
+ #
399
+ # Void squares are shown as black ones unless opts[:void] is given.
232
400
  def to_array(opts = {})
233
401
  opts = {:black => '#', :null => ' '}.merge(opts)
402
+ opts[:void] = opts[:black] unless opts.key?(:void)
234
403
  solution.map {|row|
235
404
  row.map {|c|
236
405
  s = c.solution
237
406
  case s
238
- when :black, :null
407
+ when :black, :null, :void
239
408
  opts[s]
240
409
  when String, Rebus
241
410
  block_given? ? (yield c) : c.to_char
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pangrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin DeMello
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-16 00:00:00.000000000 Z
11
+ date: 2026-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -50,12 +50,14 @@ files:
50
50
  - README
51
51
  - bin/pangrid
52
52
  - lib/pangrid.rb
53
+ - lib/pangrid/clue.rb
53
54
  - lib/pangrid/clues.rb
54
55
  - lib/pangrid/plugin.rb
55
56
  - lib/pangrid/plugins/acrosslite.rb
56
57
  - lib/pangrid/plugins/csv.rb
57
58
  - lib/pangrid/plugins/excel.rb
58
59
  - lib/pangrid/plugins/exolve.rb
60
+ - lib/pangrid/plugins/ipuz.rb
59
61
  - lib/pangrid/plugins/json.rb
60
62
  - lib/pangrid/plugins/png.rb
61
63
  - lib/pangrid/plugins/qxw.rb