Class: Binary_Puzzle_Solver::RowHandle

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_view, init_idx) ⇒ RowHandle

Returns a new instance of RowHandle.



713
714
715
716
# File 'lib/binary_puzzle_solver/base.rb', line 713

def initialize (init_view, init_idx)
    @view = init_view
    @idx = init_idx
end

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



712
713
714
# File 'lib/binary_puzzle_solver/base.rb', line 712

def idx
  @idx
end

#viewObject (readonly)

Returns the value of attribute view.



712
713
714
# File 'lib/binary_puzzle_solver/base.rb', line 712

def view
  @view
end

Instance Method Details

#check_for_duplicated(complete_rows_map) ⇒ Object



773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/binary_puzzle_solver/base.rb', line 773

def check_for_duplicated(complete_rows_map)
    summary = get_summary()

    if not summary.are_both_not_exceeded() then
        raise GameIntegrityException, "Value exceeded"
    elsif summary.are_both_full() then
        s = get_string()
        complete_rows_map[s] ||= []
        dups = complete_rows_map[s]
        dups << idx
        if (dups.length > 1)
            i, j = dups[0], dups[1]
            raise GameIntegrityException, \
                "Duplicate Rows - #{i} and #{j}"
        end
        return true
    else
        return false
    end
end

#check_for_too_many_consecutiveObject



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
822
823
# File 'lib/binary_puzzle_solver/base.rb', line 795

def check_for_too_many_consecutive()
    count = 0
    prev_cell_state = Cell::UNKNOWN

    handle_seq = lambda {
        if ((prev_cell_state == Cell::ZERO) || \
            (prev_cell_state == Cell::ONE)) then
            if count > 2 then
                raise GameIntegrityException, \
                    "Too many #{prev_cell_state} in a row"
            end
        end
    }

    iter_of_handles().each do |cell_h|
        cell_state = cell_h.get_state
        if cell_state == prev_cell_state then
            count += 1
        else
            handle_seq.call()
            count = 1
            prev_cell_state = cell_state
        end
    end

    handle_seq.call()

    return
end

#col_dimObject



726
727
728
# File 'lib/binary_puzzle_solver/base.rb', line 726

def col_dim()
    return view.col_dim()
end

#get_cell(x) ⇒ Object



746
747
748
# File 'lib/binary_puzzle_solver/base.rb', line 746

def get_cell(x)
    return view._get_cell(get_coord(x))
end

#get_cell_handle(x) ⇒ Object



756
757
758
# File 'lib/binary_puzzle_solver/base.rb', line 756

def get_cell_handle(x)
    return CellHandle.new(self, x)
end

#get_coord(x) ⇒ Object



738
739
740
# File 'lib/binary_puzzle_solver/base.rb', line 738

def get_coord(x)
    return Coord.new(col_dim() => x, row_dim() => idx)
end

#get_state(x) ⇒ Object



742
743
744
# File 'lib/binary_puzzle_solver/base.rb', line 742

def get_state(x)
    return view.get_cell_state(get_coord(x))
end

#get_stringObject



722
723
724
# File 'lib/binary_puzzle_solver/base.rb', line 722

def get_string()
    return iter_of_handles().map { |cell_h| cell_h.get_char() }.join('')
end

#get_summaryObject



718
719
720
# File 'lib/binary_puzzle_solver/base.rb', line 718

def get_summary()
    return view.get_row_summary(:idx => idx, :dim => row_dim());
end

#iterObject



750
751
752
753
754
# File 'lib/binary_puzzle_solver/base.rb', line 750

def iter
    return view.dim_range(col_dim()).map { |x|
        [x, get_cell(x)]
    }
end

#iter_of_handlesObject



760
761
762
# File 'lib/binary_puzzle_solver/base.rb', line 760

def iter_of_handles
    return view.dim_range(col_dim()).map { |x| get_cell_handle(x) }
end

#iter_of_statesObject



764
765
766
# File 'lib/binary_puzzle_solver/base.rb', line 764

def iter_of_states
    return iter_of_handles.map { |x| x.get_state() }
end

#max_idxObject



734
735
736
# File 'lib/binary_puzzle_solver/base.rb', line 734

def max_idx()
    return view.max_idx(view.col_dim())
end

#row_dimObject



730
731
732
# File 'lib/binary_puzzle_solver/base.rb', line 730

def row_dim()
    return view.row_dim()
end

#validate(params) ⇒ Object



825
826
827
828
829
830
831
# File 'lib/binary_puzzle_solver/base.rb', line 825

def validate(params)
    complete_rows_map = params[:complete_rows_map]

    check_for_too_many_consecutive()

    return { :is_final => check_for_duplicated(complete_rows_map), };
end

#where_values_are(v) ⇒ Object



768
769
770
771
# File 'lib/binary_puzzle_solver/base.rb', line 768

def where_values_are(v)
    return iter_of_handles.select { |x|
        x.get_state() == v }.map { |h| h.x }
end