Class: Binary_Puzzle_Solver::Board

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

Direct Known Subclasses

Board_View

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClass

#opposite_value

Constructor Details

#initialize(params) ⇒ Board

Returns a new instance of Board.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/binary_puzzle_solver/base.rb', line 185

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)); }
    }
    @complete_rows_map = {
        :x => Hash.new,
        :y => Hash.new
    }
    @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.



183
184
185
# File 'lib/binary_puzzle_solver/base.rb', line 183

def iters_quota
  @iters_quota
end

#num_iters_doneObject (readonly)

Returns the value of attribute num_iters_done.



183
184
185
# File 'lib/binary_puzzle_solver/base.rb', line 183

def num_iters_done
  @num_iters_done
end

Instance Method Details

#_get_cell(coord) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/binary_puzzle_solver/base.rb', line 258

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

#_validate_method_list(methods_list) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/binary_puzzle_solver/base.rb', line 339

def _validate_method_list (methods_list)
    valid_methods_arr = [
        :check_and_handle_sequences_in_row,
        :check_and_handle_known_unknown_sameknown_in_row,
        :check_and_handle_cells_of_one_value_in_row_were_all_found,
        :check_exceeded_numbers_while_accounting_for_two_unknown_gaps,
        :check_try_placing_last_of_certain_digit_in_row,
        :check_try_placing_last_of_certain_digit_in_row_to_avoid_dups,
        :check_remaining_gap_of_three_with_implicits,
        :check_exceeded_digits_taking_large_gaps_into_account,
    ]

    valid_mathods_hash = valid_methods_arr.inject({}) {
        |h,v| h[v] = true; h
    }

    methods_list.each do |m|
        if not valid_mathods_hash.has_key?(m) then
            raise RuntimeError, "Method #{m} is not valid to be used."
        end
    end
end

#add_move(m) ⇒ Object



240
241
242
243
244
# File 'lib/binary_puzzle_solver/base.rb', line 240

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

    return
end

#add_to_iters_quota(delta) ⇒ Object



213
214
215
# File 'lib/binary_puzzle_solver/base.rb', line 213

def add_to_iters_quota(delta)
    @iters_quota += delta
end

#as_stringObject



402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/binary_puzzle_solver/base.rb', line 402

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



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

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

#flush_movesObject



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

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

    return
end

#get_cell_state(coord) ⇒ Object



316
317
318
# File 'lib/binary_puzzle_solver/base.rb', line 316

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

#get_complete_map(dir) ⇒ Object



286
287
288
# File 'lib/binary_puzzle_solver/base.rb', line 286

def get_complete_map(dir)
    return @complete_rows_map[dir]
end

#get_movesObject



236
237
238
# File 'lib/binary_puzzle_solver/base.rb', line 236

def get_moves
    return @old_moves
end

#get_new_move(idx) ⇒ Object



246
247
248
# File 'lib/binary_puzzle_solver/base.rb', line 246

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

#get_row_summary(params) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/binary_puzzle_solver/base.rb', line 326

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.



322
323
324
# File 'lib/binary_puzzle_solver/base.rb', line 322

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

#limit(dim) ⇒ Object



250
251
252
# File 'lib/binary_puzzle_solver/base.rb', line 250

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

#list_viewsObject



282
283
284
# File 'lib/binary_puzzle_solver/base.rb', line 282

def list_views
    return [false, true].map { |v| get_view(:rotate => v) }
end

#max_idx(dim) ⇒ Object



254
255
256
# File 'lib/binary_puzzle_solver/base.rb', line 254

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

#num_moves_doneObject



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

def num_moves_done()
    return @new_moves.length
end

#rotate_dir(dir) ⇒ Object



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

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

#set_cell_state(coord, state) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/binary_puzzle_solver/base.rb', line 290

def set_cell_state(coord, state)
    _get_cell(coord).set_state(state)

    mymap = @complete_rows_map
    list_views().each do |v|
        row_dim = v.row_dim()
        real_row_dim = v.mapped_row_dim()
        row_idx = coord.method(real_row_dim).call()
        summary = v.get_row_summary(:dim => row_dim, :idx => row_idx)

        # puts "Coord = (x=#{coord.x},y=#{coord.y}) row_dim = #{row_dim} RowIdx = #{row_idx} mapped_row_dim = #{v.mapped_row_dim}"
        summary.inc_count(state)

        if summary.are_both_full() then
            str = v.get_row_handle(row_idx).get_string()
            mymap[real_row_dim][str] ||= []
            arr = mymap[real_row_dim][str]
            arr << row_idx
            if (arr.length > 1) then
                raise GameIntegrityException, "Duplicate rows at dim #{row_dim} #{arr.join(',')}"
            end
        end
    end
    return
end

#try_to_solve_using(params) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/binary_puzzle_solver/base.rb', line 363

def try_to_solve_using (params)
    methods_list = params[:methods]
    _validate_method_list(methods_list)

    views = list_views()

    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



415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/binary_puzzle_solver/base.rb', line 415

def validate()
    is_final = true

    list_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