Class: Pangrid::XWord

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/pangrid/xw.rb

Overview

solution = Cell[] width, height = int across_clues = string[] down_clues = string[] rebus = { solution => [int, rebus_char] }

Instance Method Summary collapse

Instance Method Details

#across?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/pangrid/xw.rb', line 90

def across?(x, y)
  boundary?(x - 1, y) && !boundary?(x, y) && !boundary?(x + 1, y)
end

#black?(x, y) ⇒ Boolean

Clue numbering

Returns:

  • (Boolean)


82
83
84
# File 'lib/pangrid/xw.rb', line 82

def black?(x, y)
  solution[y][x].black?
end

#boundary?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/pangrid/xw.rb', line 86

def boundary?(x, y)
  (x < 0) || (y < 0) || (x >= width) || (y >= height) || black?(x, y)
end

#down?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/pangrid/xw.rb', line 94

def down?(x, y)
  boundary?(x, y - 1) && !boundary?(x, y) && !boundary?(x, y + 1)
end

#each_cellObject



134
135
136
137
138
139
140
# File 'lib/pangrid/xw.rb', line 134

def each_cell
  (0 ... height).each do |y|
    (0 ... width).each do |x|
      yield solution[y][x]
    end
  end
end

#each_cell_with_coordsObject



142
143
144
145
146
147
148
# File 'lib/pangrid/xw.rb', line 142

def each_cell_with_coords
  (0 ... height).each do |y|
    (0 ... width).each do |x|
      yield [x, y, solution[y][x]]
    end
  end
end

#encode_rebus!Object

Collect a hash of rebus solutions, each mapped to an integer.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/pangrid/xw.rb', line 171

def encode_rebus!
  k = 0
  self.rebus = {}
  each_cell do |c|
    if c.rebus?
      r = c.solution
      if self.rebus[s]
        sym, _ = self.rebus[s]
        r.symbol = sym.to_s
      else
        k += 1
        self.rebus[r.solution] = [k, r.display_char]
        r.symbol = k.to_s
      end
    end
  end
end

#inspect_gridObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/pangrid/xw.rb', line 189

def inspect_grid
  number
  solution.map {|row|
    row.map {|c|
      s = c.solution
      o = case s
      when :black
        "#"
      when :null
        "."
      when String
        c.to_char
      when Rebus
        c.to_char
      else
        raise PuzzleFormatError, "Unrecognised cell #{c}"
      end
      if c.number && c.number > 0
        o = "#{c.number} #{o}"
      end
      o = o.rjust(4)
    }.join("|")
  }.join("\n")
end

#number(words = false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pangrid/xw.rb', line 112

def number(words = false)
  n, across, down = 1, [], []
  words_a, words_d = [], []
  (0 ... height).each do |y|
    (0 ... width).each do |x|
      if across? x, y
        across << n
        words_a << word_from(x, y, :across)
      end
      if down? x, y
        down << n
        words_d << word_from(x, y, :down)
      end
      if across.last == n || down.last == n
        solution[y][x].number = n
        n += 1
      end
    end
  end
  words ? [across, down, words_a, words_d] : [across, down]
end

#to_array(opts = {}) ⇒ Object

=> char, :null => char -> Any[]



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/pangrid/xw.rb', line 151

def to_array(opts = {})
  opts = {:black => '#', :null => ' '}.merge(opts)
  solution.map {|row|
    row.map {|c|
      s = c.solution
      case s
      when :black, :null
        opts[s]
      when String
        block_given? ? (yield c) : c.to_char
      when Rebus
        block_given? ? (yield c) : c.to_char
      else
        raise PuzzleFormatError, "Unrecognised cell #{c}"
      end
    }
  }
end

#word_from(x, y, dir) ⇒ Object

Word starting from a square



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pangrid/xw.rb', line 99

def word_from(x, y, dir)
  s = ""
  while !boundary?(x, y) do
    s += solution[y][x].to_w
    if dir == :across
      x += 1
    else
      y += 1
    end
  end
  s
end