Class: Binary_Puzzle_Solver::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_puzzle_solver/base.rb

Direct Known Subclasses

Board_View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Board

Returns a new instance of Board.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/binary_puzzle_solver/base.rb', line 168

def initialize (params)
    @dim_limits = {:x => params[:x], :y => params[:y]}
    @cells = dim_range(:y).map {
        dim_range(:x).map{ Cell.new }
    }
    @row_summaries = {
        :x => dim_range(:x).map { RowSummary.new(limit(:y)); },
        :y => dim_range(:y).map { RowSummary.new(limit(:x)); }
    }
    @old_moves = []
    @new_moves = []

    @iters_quota = 0
    @num_iters_done = 0

    @state = {:method_idx => 0, :view_idx => 0, :row_idx => 0, };

    return
end

Instance Attribute Details

#iters_quotaObject (readonly)

Returns the value of attribute iters_quota.



167
168
169
# File 'lib/binary_puzzle_solver/base.rb', line 167

def iters_quota
  @iters_quota
end

#num_iters_doneObject (readonly)

Returns the value of attribute num_iters_done.



167
168
169
# File 'lib/binary_puzzle_solver/base.rb', line 167

def num_iters_done
  @num_iters_done
end

Instance Method Details

#_get_cell(coord) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/binary_puzzle_solver/base.rb', line 237

def _get_cell(coord)

    x = coord.x
    y = coord.y

    if (y < 0)
        raise RuntimeError, "y cannot be lower than 0."
    end

    if (x < 0)
        raise RuntimeError, "x cannot be lower than 0."
    end

    if (y > max_idx(:y))
        raise RuntimeError, "y cannot be higher than max_idx."
    end

    if (x > max_idx(:x))
        raise RuntimeError, "x cannot be higher than max_idx."
    end

    return @cells[y][x]
end

#add_move(m) ⇒ Object



219
220
221
222
223
# File 'lib/binary_puzzle_solver/base.rb', line 219

def add_move(m)
    @new_moves.push(m)

    return
end

#add_to_iters_quota(delta) ⇒ Object



192
193
194
# File 'lib/binary_puzzle_solver/base.rb', line 192

def add_to_iters_quota(delta)
    @iters_quota += delta
end

#as_stringObject



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/binary_puzzle_solver/base.rb', line 338

def as_string()
    return dim_range(:y).map { |y|
        ['|'] +
            dim_range(:x).map { |x|
            s = get_cell_state(
                Coord.new(:y => y, :x => x)
            )
            ((s == Cell::UNKNOWN) ? ' ' : s.to_s())
        } +
        ["|\n"]
    }.inject([]) { |a,e| a+e }.join('')
end

#dim_range(dim) ⇒ Object



188
189
190
# File 'lib/binary_puzzle_solver/base.rb', line 188

def dim_range(dim)
    return (0 .. max_idx(dim))
end

#flush_movesObject



208
209
210
211
212
213
# File 'lib/binary_puzzle_solver/base.rb', line 208

def flush_moves()
    @old_moves += @new_moves
    @new_moves = []

    return
end

#get_cell_state(coord) ⇒ Object



268
269
270
# File 'lib/binary_puzzle_solver/base.rb', line 268

def get_cell_state(coord)
    return _get_cell(coord).state
end

#get_movesObject



215
216
217
# File 'lib/binary_puzzle_solver/base.rb', line 215

def get_moves
    return @old_moves
end

#get_new_move(idx) ⇒ Object



225
226
227
# File 'lib/binary_puzzle_solver/base.rb', line 225

def get_new_move(idx)
    return @new_moves[idx]
end

#get_row_summary(params) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/binary_puzzle_solver/base.rb', line 278

def get_row_summary(params)
    idx = params[:idx]
    dim = params[:dim]

    if (idx < 0)
        raise RuntimeError, "idx cannot be lower than 0."
    end
    if (idx > max_idx(dim))
        raise RuntimeError, "idx cannot be higher than max_idx."
    end
    return @row_summaries[dim][idx]
end

#get_view(params) ⇒ Object

There is an equivalence between the dimensions, so a view allows us to view the board rotated.



274
275
276
# File 'lib/binary_puzzle_solver/base.rb', line 274

def get_view(params)
    return Board_View.new(self, params[:rotate])
end

#limit(dim) ⇒ Object



229
230
231
# File 'lib/binary_puzzle_solver/base.rb', line 229

def limit(dim)
    return @dim_limits[dim]
end

#max_idx(dim) ⇒ Object



233
234
235
# File 'lib/binary_puzzle_solver/base.rb', line 233

def max_idx(dim)
    return limit(dim) - 1
end

#num_moves_doneObject



204
205
206
# File 'lib/binary_puzzle_solver/base.rb', line 204

def num_moves_done()
    return @new_moves.length
end

#opposite_value(val) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/binary_puzzle_solver/base.rb', line 291

def opposite_value(val)
    if (val == Cell::ZERO)
        return Cell::ONE
    elsif (val == Cell::ONE)
        return Cell::ZERO
    else
        raise RuntimeError, "'#{val}' must be zero or one."
    end
end

#rotate_dir(dir) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/binary_puzzle_solver/base.rb', line 196

def rotate_dir(dir)
    if dir == :x
        return :y
    else
        return :x
    end
end

#set_cell_state(coord, state) ⇒ Object



261
262
263
264
265
266
# File 'lib/binary_puzzle_solver/base.rb', line 261

def set_cell_state(coord, state)
    _get_cell(coord).set_state(state)
    get_row_summary(:dim => :x, :idx => coord.x).inc_count(state)
    get_row_summary(:dim => :y, :idx => coord.y).inc_count(state)
    return
end

#try_to_solve_using(params) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/binary_puzzle_solver/base.rb', line 301

def try_to_solve_using (params)
    methods_list = params[:methods]
    views = [get_view(:rotate => false), get_view(:rotate => true), ]

    catch :out_of_iters do
        first_iter = true

        while first_iter or num_moves_done() > 0
            first_iter = false
            flush_moves()
            while (@state[:method_idx] < methods_list.length)
                m = methods_list[@state[:method_idx]]
                while (@state[:view_idx] < views.length)
                    v = views[@state[:view_idx]]
                    while (@state[:row_idx] <= v.max_idx(v.row_dim()))
                        row_idx = @state[:row_idx]
                        @state[:row_idx] += 1
                        v.method(m).call(:idx => row_idx)
                        @iters_quota -= 1
                        @num_iters_done += 1
                        if iters_quota == 0
                            throw :out_of_iters
                        end
                    end
                    @state[:view_idx] += 1
                    @state[:row_idx] = 0
                end
                @state[:method_idx] += 1
                @state[:view_idx] = 0
            end
            @state[:method_idx] = 0
        end
    end

    return
end

#validateObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/binary_puzzle_solver/base.rb', line 351

def validate()
    views = [get_view(:rotate => false), get_view(:rotate => true), ]

    is_final = true

    views.each do |v|
        view_final = v.validate_rows()
        is_final &&= view_final
    end

    if is_final
        return :final
    else
        return :non_final
    end
end