Class: Binary_Puzzle_Solver::RowHandle

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init_view, init_idx) ⇒ RowHandle

Returns a new instance of RowHandle.



808
809
810
811
# File 'lib/binary_puzzle_solver/base.rb', line 808

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

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



807
808
809
# File 'lib/binary_puzzle_solver/base.rb', line 807

def idx
  @idx
end

#viewObject (readonly)

Returns the value of attribute view.



807
808
809
# File 'lib/binary_puzzle_solver/base.rb', line 807

def view
  @view
end

Class Method Details

.calc_iter_of_states_str(iter) ⇒ Object



817
818
819
# File 'lib/binary_puzzle_solver/base.rb', line 817

def self.calc_iter_of_states_str(iter)
    return iter.map { |v| Cell.get_state_char(v) }.join('')
end

Instance Method Details

#check_for_duplicatedObject



874
875
876
877
878
879
880
881
882
883
884
# File 'lib/binary_puzzle_solver/base.rb', line 874

def check_for_duplicated()
    summary = get_summary()

    if not summary.are_both_not_exceeded() then
        raise GameIntegrityException, "Value exceeded"
    elsif summary.are_both_full() then
        return true
    else
        return false
    end
end

#check_for_too_many_consecutiveObject



887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'lib/binary_puzzle_solver/base.rb', line 887

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



827
828
829
# File 'lib/binary_puzzle_solver/base.rb', line 827

def col_dim()
    return view.col_dim()
end

#get_cell(x) ⇒ Object



847
848
849
# File 'lib/binary_puzzle_solver/base.rb', line 847

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

#get_cell_handle(x) ⇒ Object



857
858
859
# File 'lib/binary_puzzle_solver/base.rb', line 857

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

#get_coord(x) ⇒ Object



839
840
841
# File 'lib/binary_puzzle_solver/base.rb', line 839

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

#get_state(x) ⇒ Object



843
844
845
# File 'lib/binary_puzzle_solver/base.rb', line 843

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

#get_stringObject



821
822
823
824
825
# File 'lib/binary_puzzle_solver/base.rb', line 821

def get_string()
    return RowHandle.calc_iter_of_states_str(
        iter_of_handles().map { |cell_h| cell_h.get_state() }
    )
end

#get_summaryObject



813
814
815
# File 'lib/binary_puzzle_solver/base.rb', line 813

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

#iterObject



851
852
853
854
855
# File 'lib/binary_puzzle_solver/base.rb', line 851

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

#iter_of_handlesObject



861
862
863
# File 'lib/binary_puzzle_solver/base.rb', line 861

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

#iter_of_statesObject



865
866
867
# File 'lib/binary_puzzle_solver/base.rb', line 865

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

#max_idxObject



835
836
837
# File 'lib/binary_puzzle_solver/base.rb', line 835

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

#row_dimObject



831
832
833
# File 'lib/binary_puzzle_solver/base.rb', line 831

def row_dim()
    return view.row_dim()
end

#validate(params) ⇒ Object



917
918
919
920
921
# File 'lib/binary_puzzle_solver/base.rb', line 917

def validate(params)
    check_for_too_many_consecutive()

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

#where_values_are(v) ⇒ Object



869
870
871
872
# File 'lib/binary_puzzle_solver/base.rb', line 869

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