Class: Pangrid::Qxw

Inherits:
Plugin show all
Defined in:
lib/pangrid/plugins/qxw.rb

Constant Summary

Constants inherited from Plugin

Plugin::FAILED, Plugin::MISSING_DEPS, Plugin::REGISTRY

Instance Method Summary collapse

Methods inherited from Plugin

class_to_name, get, inherited, list_all, load_all, load_plugin

Methods included from PluginUtils

#check

Instance Method Details

#read(data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pangrid/plugins/qxw.rb', line 8

def read(data)
  xw = XWord.new
  lines = data.lines.map(&:chomp)
  gp = lines.find {|x| x =~ /^GP( \d+){6}$/}
  check("Could not read grid size from .qxw file") { gp }
  type, w, h, _ = gp.scan(/\d+/).map(&:to_i)
  check("Only rectangular grids are supported") { type == 0 }
  xw.width = w
  xw.height = h
  xw.solution = []
  grid = lines.select {|x| x =~ /^SQ /}
  grid.each do |line|
    parts = line.scan(/\w+/)
    check(QXW_GRID_ERROR) { parts.length == 6 || parts.length == 7 }
    col, row, b, c = parts[1].to_i, parts[2].to_i, parts[5].to_i, parts[6]
    cell = Cell.new
    if b == 1
      cell.solution = :black
    elsif c == nil
      cell.solution = :null
    else
      cell.solution = c
    end
    xw.solution[row] ||= []
    xw.solution[row][col] = cell
  end
  check(QXW_GRID_ERROR) { xw.solution.length == xw.height }
  check(QXW_GRID_ERROR) { xw.solution.all? {|i| i.compact.length == xw.width} }

  # Placeholder clues
  _, _, words_a, words_d = xw.number(true)
  xw.across_clues = words_a.map {|i| "[#{i}]" }
  xw.down_clues = words_d.map {|i| "[#{i}]" }

  xw
end