Class: Binary_Puzzle_Solver::RowHandle

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClass

#opposite_value

Constructor Details

#initialize(init_view, init_idx) ⇒ RowHandle

Returns a new instance of RowHandle.



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

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

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



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

def idx
  @idx
end

#viewObject (readonly)

Returns the value of attribute view.



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

def view
  @view
end

Class Method Details

.calc_iter_of_states_str(iter) ⇒ Object



928
929
930
# File 'lib/binary_puzzle_solver/base.rb', line 928

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

Instance Method Details

#calc_gapsObject



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/binary_puzzle_solver/base.rb', line 1034

def calc_gaps
    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
    }

    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()

    return gaps
end

#calc_gaps_implicit_counts(gaps) ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'lib/binary_puzzle_solver/base.rb', line 1063

def calc_gaps_implicit_counts(gaps)
    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] < max_idx())
            x_s << gap[-1]+1
        end

        bordering_values = {Cell::ZERO => 0, Cell::ONE => 0,}
        x_s.each do |x|
            bordering_values[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

    return implicit_counts
end

#check_for_duplicatedObject



985
986
987
988
989
990
991
992
993
994
995
# File 'lib/binary_puzzle_solver/base.rb', line 985

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



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/binary_puzzle_solver/base.rb', line 998

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



938
939
940
# File 'lib/binary_puzzle_solver/base.rb', line 938

def col_dim()
    return view.col_dim()
end

#get_cell(x) ⇒ Object



958
959
960
# File 'lib/binary_puzzle_solver/base.rb', line 958

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

#get_cell_handle(x) ⇒ Object



968
969
970
# File 'lib/binary_puzzle_solver/base.rb', line 968

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

#get_coord(x) ⇒ Object



950
951
952
# File 'lib/binary_puzzle_solver/base.rb', line 950

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

#get_state(x) ⇒ Object



954
955
956
# File 'lib/binary_puzzle_solver/base.rb', line 954

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

#get_stringObject



932
933
934
935
936
# File 'lib/binary_puzzle_solver/base.rb', line 932

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

#get_summaryObject



924
925
926
# File 'lib/binary_puzzle_solver/base.rb', line 924

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

#iterObject



962
963
964
965
966
# File 'lib/binary_puzzle_solver/base.rb', line 962

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

#iter_of_handlesObject



972
973
974
# File 'lib/binary_puzzle_solver/base.rb', line 972

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

#iter_of_statesObject



976
977
978
# File 'lib/binary_puzzle_solver/base.rb', line 976

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

#max_idxObject



946
947
948
# File 'lib/binary_puzzle_solver/base.rb', line 946

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

#row_dimObject



942
943
944
# File 'lib/binary_puzzle_solver/base.rb', line 942

def row_dim()
    return view.row_dim()
end

#validate(params) ⇒ Object



1028
1029
1030
1031
1032
# File 'lib/binary_puzzle_solver/base.rb', line 1028

def validate(params)
    check_for_too_many_consecutive()

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

#where_values_are(v) ⇒ Object



980
981
982
983
# File 'lib/binary_puzzle_solver/base.rb', line 980

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