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.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/binary_puzzle_solver/base.rb', line 173

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.



171
172
173
# File 'lib/binary_puzzle_solver/base.rb', line 171

def iters_quota
  @iters_quota
end

#num_iters_doneObject (readonly)

Returns the value of attribute num_iters_done.



171
172
173
# File 'lib/binary_puzzle_solver/base.rb', line 171

def num_iters_done
  @num_iters_done
end

Instance Method Details

#_get_cell(coord) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/binary_puzzle_solver/base.rb', line 246

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



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

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

    return
end

#add_to_iters_quota(delta) ⇒ Object



201
202
203
# File 'lib/binary_puzzle_solver/base.rb', line 201

def add_to_iters_quota(delta)
    @iters_quota += delta
end

#as_stringObject



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/binary_puzzle_solver/base.rb', line 374

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



197
198
199
# File 'lib/binary_puzzle_solver/base.rb', line 197

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

#flush_movesObject



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

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

    return
end

#get_cell_state(coord) ⇒ Object



304
305
306
# File 'lib/binary_puzzle_solver/base.rb', line 304

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

#get_complete_map(dir) ⇒ Object



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

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

#get_movesObject



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

def get_moves
    return @old_moves
end

#get_new_move(idx) ⇒ Object



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

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

#get_row_summary(params) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/binary_puzzle_solver/base.rb', line 314

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.



310
311
312
# File 'lib/binary_puzzle_solver/base.rb', line 310

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

#limit(dim) ⇒ Object



238
239
240
# File 'lib/binary_puzzle_solver/base.rb', line 238

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

#list_viewsObject



270
271
272
# File 'lib/binary_puzzle_solver/base.rb', line 270

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

#max_idx(dim) ⇒ Object



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

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

#num_moves_doneObject



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

def num_moves_done()
    return @new_moves.length
end

#opposite_value(val) ⇒ Object



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

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



205
206
207
208
209
210
211
# File 'lib/binary_puzzle_solver/base.rb', line 205

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

#set_cell_state(coord, state) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/binary_puzzle_solver/base.rb', line 278

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



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/binary_puzzle_solver/base.rb', line 337

def try_to_solve_using (params)
    methods_list = params[:methods]
    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



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/binary_puzzle_solver/base.rb', line 387

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