Class: Binary_Puzzle_Solver::Board_View
- Inherits:
-
Board
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
#_validate_method_list, #add_move, #add_to_iters_quota, #as_string, #dim_range, #flush_moves, #get_cell_state, #get_complete_map, #get_moves, #get_new_move, #get_view, #list_views, #max_idx, #num_moves_done, #rotate_dir, #try_to_solve_using, #validate
Methods inherited from BaseClass
#opposite_value
Constructor Details
#initialize(board, rotation) ⇒ Board_View
Returns a new instance of Board_View.
433
434
435
436
437
438
439
440
441
442
443
|
# File 'lib/binary_puzzle_solver/base.rb', line 433
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
# File 'lib/binary_puzzle_solver/base.rb', line 500
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
457
458
459
|
# File 'lib/binary_puzzle_solver/base.rb', line 457
def _calc_mapped_coord(coord)
return _calc_mapped_item(coord, :rotate_coord)
end
|
#_calc_mapped_dir(dir) ⇒ Object
461
462
463
|
# File 'lib/binary_puzzle_solver/base.rb', line 461
def _calc_mapped_dir(dir)
return _calc_mapped_item(dir, :rotate_dir)
end
|
#_calc_mapped_item(item, rotation_method) ⇒ Object
449
450
451
452
453
454
455
|
# File 'lib/binary_puzzle_solver/base.rb', line 449
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
465
466
467
|
# File 'lib/binary_puzzle_solver/base.rb', line 465
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
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
|
# File 'lib/binary_puzzle_solver/base.rb', line 616
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
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
|
# File 'lib/binary_puzzle_solver/base.rb', line 584
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
528
529
530
531
532
533
534
535
536
537
538
539
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
# File 'lib/binary_puzzle_solver/base.rb', line 528
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_digits_taking_large_gaps_into_account(params) ⇒ Object
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
|
# File 'lib/binary_puzzle_solver/base.rb', line 823
def check_exceeded_digits_taking_large_gaps_into_account(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
gaps = row.calc_gaps()
summ = row.get_summary()
values_sorted = [Cell::ZERO, Cell::ONE].sort { |a,b|
summ.get_count(a) <=> summ.get_count(b) }
v = values_sorted[-1]
to_add = 0
(3 .. row.max_idx()).each do |len|
if gaps.has_key?(len) then
to_add += gaps[len].length
end
end
if summ.get_count(v) + to_add == summ.half_limit() then
opposite_val = opposite_value(v)
(3 .. row.max_idx()).each do |len|
if gaps.has_key?(len) then
if (len == 3) then
gaps[len].each do |gap_3|
for idx in [0,-1] do
opposite_idx = (idx == 0) ? -1 : 0
edge_offset = (idx == 0) ? (-1) : 1
if (gap_3[idx] > 0 and gap_3[idx] < row.max_idx()) then
if (row.get_state(gap_3[idx]+edge_offset) \
== opposite_val) then
perform_and_append_move(
:coord => row.get_coord(gap_3[opposite_idx]),
:val => opposite_val,
:reason => \
"With gaps of 3, exceeded value cannot be at the opposite edge of the opposite value.",
:dir => row.col_dim()
)
end
end
end
end
elsif (len == 4) then
gaps[len].each do |gap|
[0, -1].each do |i|
perform_and_append_move(
:coord => row.get_coord(gap[i]),
:val => opposite_val,
:reason => \
"With gaps of 4, exceeded value cannot be at edges.",
:dir => row.col_dim()
)
end
end
elsif (len == 5) then
gaps[len].each do |gap|
o = opposite_val
gap.zip([o,o,v,o,o]) do |pos_v|
perform_and_append_move(
:coord => row.get_coord(gap[pos_v[0]]),
:val => pos_v[1],
:reason => \
"With gaps of 5, exceeded value pattern must be 00100.",
:dir => row.col_dim()
)
end
end
else raise GameIntegrityException, "Too large a gap."
end
end
end
end
return
end
|
#check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params) ⇒ Object
646
647
648
649
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
|
# File 'lib/binary_puzzle_solver/base.rb', line 646
def check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
gaps = row.calc_gaps()
if (gaps.has_key?(2)) then
implicit_counts = row.calc_gaps_implicit_counts(gaps)
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_remaining_gap_of_three_with_implicits(params) ⇒ Object
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
# File 'lib/binary_puzzle_solver/base.rb', line 777
def check_remaining_gap_of_three_with_implicits(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
gaps = row.calc_gaps()
if (gaps.has_key?(2) and gaps.has_key?(3)) then
implicit_counts = row.calc_gaps_implicit_counts(gaps)
summ = row.get_summary()
v = [Cell::ZERO, Cell::ONE].find {
|v| summ.get_count(v) + implicit_counts[v] \
== summ.half_limit() - 1
}
if v then
opposite_val = opposite_value(v)
gap_3 = gaps[3][0]
for idx in [0,-1] do
opposite_idx = (idx == 0) ? -1 : 0
edge_offset = (idx == 0) ? (-1) : 1
if (gap_3[idx] > 0 and gap_3[idx] < row.max_idx()) then
if (row.get_state(gap_3[idx]+edge_offset) \
== opposite_val) then
perform_and_append_move(
:coord => row.get_coord(gap_3[opposite_idx]),
:val => opposite_val,
:reason => \
"Gap of 3 with 2 of the value remaining must not form 3 of a kind (with implicits)",
:dir => row.col_dim()
)
end
end
end
end
end
return
end
|
#check_try_placing_last_of_certain_digit_in_row(params) ⇒ Object
720
721
722
723
724
725
726
727
|
# File 'lib/binary_puzzle_solver/base.rb', line 720
def check_try_placing_last_of_certain_digit_in_row(params)
row_idx = params[:idx]
return _generic_check_try_placing_last_digit(
:idx => row_idx,
:callback_method => :_do_values_have_a_three_in_a_row,
)
end
|
#check_try_placing_last_of_certain_digit_in_row_to_avoid_dups(params) ⇒ Object
711
712
713
714
715
716
717
718
|
# File 'lib/binary_puzzle_solver/base.rb', line 711
def check_try_placing_last_of_certain_digit_in_row_to_avoid_dups(params)
row_idx = params[:idx]
return _generic_check_try_placing_last_digit(
:idx => row_idx,
:callback_method => :_are_values_duplicates,
)
end
|
#col_dim ⇒ Object
484
485
486
|
# File 'lib/binary_puzzle_solver/base.rb', line 484
def col_dim()
return :x
end
|
#get_complete_row_map ⇒ Object
492
493
494
|
# File 'lib/binary_puzzle_solver/base.rb', line 492
def get_complete_row_map()
return @board.get_complete_map(mapped_row_dim())
end
|
#get_row_handle(idx) ⇒ Object
496
497
498
|
# File 'lib/binary_puzzle_solver/base.rb', line 496
def get_row_handle(idx)
return RowHandle.new(self, idx)
end
|
#get_row_summary(params) ⇒ Object
473
474
475
476
477
478
|
# File 'lib/binary_puzzle_solver/base.rb', line 473
def get_row_summary(params)
return @board.get_row_summary(
:idx => params[:idx],
:dim => @dims_map[params[:dim]]
)
end
|
#limit(dim) ⇒ Object
469
470
471
|
# File 'lib/binary_puzzle_solver/base.rb', line 469
def limit(dim)
return @board.limit(@dims_map[dim])
end
|
#mapped_row_dim ⇒ Object
488
489
490
|
# File 'lib/binary_puzzle_solver/base.rb', line 488
def mapped_row_dim()
return _calc_mapped_dir(row_dim())
end
|
523
524
525
526
|
# File 'lib/binary_puzzle_solver/base.rb', line 523
def perform_and_append_move(params)
set_cell_state( params[:coord], params[:val] )
_append_move(params)
end
|
#rotate_coord(coord) ⇒ Object
445
446
447
|
# File 'lib/binary_puzzle_solver/base.rb', line 445
def rotate_coord(coord)
return coord.rotate
end
|
#row_dim ⇒ Object
480
481
482
|
# File 'lib/binary_puzzle_solver/base.rb', line 480
def row_dim()
return :y
end
|
#set_cell_state(coord, state) ⇒ Object
517
518
519
520
521
|
# File 'lib/binary_puzzle_solver/base.rb', line 517
def set_cell_state(coord, state)
@board.set_cell_state(
_calc_mapped_coord(coord), state
)
end
|
#validate_rows ⇒ Object
902
903
904
905
906
907
908
909
910
911
912
|
# File 'lib/binary_puzzle_solver/base.rb', line 902
def validate_rows()
is_final = true
dim_range(row_dim()).each do |row_idx|
row = get_row_handle(row_idx)
ret = row.validate(:foo => false)
is_final &&= ret[:is_final]
end
return is_final
end
|