Class: Binary_Puzzle_Solver::Board_View
- Inherits:
-
Board
- Object
- Board
- Binary_Puzzle_Solver::Board_View
show all
- Defined in:
- lib/binary_puzzle_solver/base.rb
Instance Attribute Summary
Attributes inherited from Board
#iters_quota, #num_iters_done
Instance Method Summary
collapse
Methods inherited from Board
#add_move, #add_to_iters_quota, #as_string, #dim_range, #flush_moves, #get_cell_state, #get_moves, #get_new_move, #get_view, #max_idx, #num_moves_done, #opposite_value, #rotate_dir, #set_cell_state, #try_to_solve_using, #validate
Constructor Details
#initialize(board, rotation) ⇒ Board_View
Returns a new instance of Board_View.
371
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/binary_puzzle_solver/base.rb', line 371
def initialize (board, rotation)
@board = board
@rotation = rotation
if rotation
@dims_map = {:x => :y, :y => :x}
else
@dims_map = {:x => :x, :y => :y}
end
return
end
|
Instance Method Details
#_append_move(params) ⇒ Object
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
# File 'lib/binary_puzzle_solver/base.rb', line 430
def _append_move(params)
coord = params[:coord]
dir = params[:dir]
@board.add_move(
Move.new(
:coord => _calc_mapped_coord(coord),
:val => params[:val],
:reason => params[:reason],
:dir => _calc_mapped_dir(dir)
)
)
return
end
|
#_calc_mapped_coord(coord) ⇒ Object
395
396
397
|
# File 'lib/binary_puzzle_solver/base.rb', line 395
def _calc_mapped_coord(coord)
return _calc_mapped_item(coord, :rotate_coord)
end
|
#_calc_mapped_dir(dir) ⇒ Object
399
400
401
|
# File 'lib/binary_puzzle_solver/base.rb', line 399
def _calc_mapped_dir(dir)
return _calc_mapped_item(dir, :rotate_dir)
end
|
#_calc_mapped_item(item, rotation_method) ⇒ Object
387
388
389
390
391
392
393
|
# File 'lib/binary_puzzle_solver/base.rb', line 387
def _calc_mapped_item(item, rotation_method)
if @rotation then
return method(rotation_method).call(item)
else
return item
end
end
|
#_get_cell(coord) ⇒ Object
403
404
405
|
# File 'lib/binary_puzzle_solver/base.rb', line 403
def _get_cell(coord)
return @board._get_cell(_calc_mapped_coord(coord))
end
|
#check_and_handle_cells_of_one_value_in_row_were_all_found(params) ⇒ Object
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
# File 'lib/binary_puzzle_solver/base.rb', line 540
def check_and_handle_cells_of_one_value_in_row_were_all_found(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
full_val = row.get_summary().find_full_value()
if (full_val)
opposite_val = opposite_value(full_val)
row.iter_of_handles().each do |cell_h|
x = cell_h.x
cell = cell_h.get_cell
cell_state = cell.state
if cell_state == Cell::UNKNOWN
perform_and_append_move(
:coord => row.get_coord(x),
:val => opposite_val,
:reason => "Filling unknowns in row with an exceeded value with the other value",
:dir => col_dim()
)
end
end
end
return
end
|
#check_and_handle_known_unknown_sameknown_in_row(params) ⇒ Object
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
# File 'lib/binary_puzzle_solver/base.rb', line 508
def check_and_handle_known_unknown_sameknown_in_row(params)
row_idx = params[:idx]
prev_cell_states = []
max_in_a_row = 2
row = get_row_handle(row_idx)
(1 .. (row.max_idx - 1)).each do |x|
get_coord = lambda { |offset| row.get_coord(x+offset); }
get_state = lambda { |offset| row.get_state(x+offset); }
if (get_state.call(-1) != Cell::UNKNOWN and
get_state.call(0) == Cell::UNKNOWN and
get_state.call(1) == get_state.call(-1))
then
perform_and_append_move(
:coord => get_coord.call(0),
:val => opposite_value(get_state.call(-1)),
:reason => "In between two identical cells",
:dir => col_dim()
)
end
end
return
end
|
#check_and_handle_sequences_in_row(params) ⇒ Object
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
# File 'lib/binary_puzzle_solver/base.rb', line 452
def check_and_handle_sequences_in_row(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
prev_cell_states = []
max_in_a_row = 2
handle_prev_cell_states = lambda { |x|
if prev_cell_states.length > max_in_a_row then
raise GameIntegrityException, "Too many #{prev_cell_states[0]} in a row"
elsif (prev_cell_states.length == max_in_a_row)
coords = Array.new
start_x = x - max_in_a_row - 1;
if (start_x >= 0)
coords << row.get_coord(start_x)
end
if (x <= row.max_idx())
coords << row.get_coord(x)
end
coords.each do |c|
if (get_cell_state(c) == Cell::UNKNOWN)
perform_and_append_move(
:coord => c,
:val => opposite_value(prev_cell_states[0]),
:reason => "Vicinity to two in a row",
:dir => col_dim()
)
end
end
end
return
}
row.iter_of_handles().each do |h|
x = h.x
cell_state = h.get_cell.state
if cell_state == Cell::UNKNOWN
handle_prev_cell_states.call(x)
prev_cell_states = []
elsif ((prev_cell_states.length == 0) or
(prev_cell_states[-1] != cell_state)) then
handle_prev_cell_states.call(x)
prev_cell_states = [cell_state]
else
prev_cell_states << cell_state
end
end
handle_prev_cell_states.call(row.max_idx + 1)
return
end
|
#check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params) ⇒ Object
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
# File 'lib/binary_puzzle_solver/base.rb', line 570
def check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
gaps = {}
next_gap = []
add_gap = lambda {
l = next_gap.length
if (l > 0)
if not gaps[l]
gaps[l] = []
end
gaps[l] << next_gap
next_gap = []
end
}
row.iter_of_handles().each do |cell_h|
if (cell_h.get_state == Cell::UNKNOWN)
next_gap << cell_h.x
else
add_gap.call()
end
end
add_gap.call()
if (gaps.has_key?(2)) then
implicit_counts = {Cell::ZERO => 0, Cell::ONE => 0,}
gaps[2].each do |gap|
x_s = []
if (gap[0] > 0)
x_s << gap[0]-1
end
if (gap[-1] < row.max_idx)
x_s << gap[-1]+1
end
bordering_values = {Cell::ZERO => 0, Cell::ONE => 0,}
x_s.each do |x|
bordering_values[row.get_state(x)] += 1
end
for v in [Cell::ZERO, Cell::ONE] do
if bordering_values[opposite_value(v)] > 0
implicit_counts[v] += 1
end
end
end
summ = row.get_summary()
v = [Cell::ZERO, Cell::ONE].find {
|v| summ.get_count(v) + implicit_counts[v] \
== summ.half_limit()
}
if v then
gap_keys = gaps.keys.select { |x| x != 2 }
opposite_val = opposite_value(v)
gap_keys.each do |k|
gaps[k].each do |gap|
gap.each do |x|
perform_and_append_move(
:coord => row.get_coord(x),
:val => opposite_val,
:reason => \
"Analysis of gaps and their neighboring values",
:dir => row.col_dim()
)
end
end
end
end
end
end
|
#check_try_placing_last_of_certain_digit_in_row(params) ⇒ Object
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
# File 'lib/binary_puzzle_solver/base.rb', line 650
def check_try_placing_last_of_certain_digit_in_row(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
summary = row.get_summary()
v = [Cell::ZERO, Cell::ONE].find {
|v| summary.get_count(v) == summary.half_limit() - 1
}
if not(v) then
return
end
oppose_v = opposite_value(v)
values = row.iter_of_states().to_a
coords = row.where_values_are(Cell::UNKNOWN)
coords_copy = Array.new(coords)
for x in coords do
v_s = Array.new(values)
for x_to_fill in coords_copy do
v_s[x_to_fill] = ((x_to_fill == x) ? v : oppose_v)
end
if ((0 .. (v_s.length - 3)).to_a.index { |i|
(1 .. 2).all? { |offset| v_s[i] == v_s[i+offset] }
}) then
perform_and_append_move(
:coord => row.get_coord(x),
:val => oppose_v,
:reason => "Trying opposite value that is the last remaining results in three-in-a-row",
:dir => col_dim()
)
return
end
end
return
end
|
#col_dim ⇒ Object
422
423
424
|
# File 'lib/binary_puzzle_solver/base.rb', line 422
def col_dim()
return :x
end
|
#get_row_handle(idx) ⇒ Object
426
427
428
|
# File 'lib/binary_puzzle_solver/base.rb', line 426
def get_row_handle(idx)
return RowHandle.new(self, idx)
end
|
#get_row_summary(params) ⇒ Object
411
412
413
414
415
416
|
# File 'lib/binary_puzzle_solver/base.rb', line 411
def get_row_summary(params)
return @board.get_row_summary(
:idx => params[:idx],
:dim => @dims_map[params[:dim]]
)
end
|
#limit(dim) ⇒ Object
407
408
409
|
# File 'lib/binary_puzzle_solver/base.rb', line 407
def limit(dim)
return @board.limit(@dims_map[dim])
end
|
447
448
449
450
|
# File 'lib/binary_puzzle_solver/base.rb', line 447
def perform_and_append_move(params)
set_cell_state(params[:coord], params[:val])
_append_move(params)
end
|
#rotate_coord(coord) ⇒ Object
383
384
385
|
# File 'lib/binary_puzzle_solver/base.rb', line 383
def rotate_coord(coord)
return coord.rotate
end
|
#row_dim ⇒ Object
418
419
420
|
# File 'lib/binary_puzzle_solver/base.rb', line 418
def row_dim()
return :y
end
|
#validate_rows ⇒ Object
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
# File 'lib/binary_puzzle_solver/base.rb', line 695
def validate_rows()
complete_rows_map = Hash.new
is_final = true
dim_range(row_dim()).each do |row_idx|
row = get_row_handle(row_idx)
ret = row.validate( :complete_rows_map => complete_rows_map )
is_final &&= ret[:is_final]
end
return is_final
end
|