Class: Binary_Puzzle_Solver::RowHandle
- Inherits:
-
Object
- Object
- Binary_Puzzle_Solver::RowHandle
- Defined in:
- lib/binary_puzzle_solver/base.rb
Instance Attribute Summary collapse
-
#idx ⇒ Object
readonly
Returns the value of attribute idx.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Class Method Summary collapse
Instance Method Summary collapse
- #check_for_duplicated ⇒ Object
- #check_for_too_many_consecutive ⇒ Object
- #col_dim ⇒ Object
- #get_cell(x) ⇒ Object
- #get_cell_handle(x) ⇒ Object
- #get_coord(x) ⇒ Object
- #get_state(x) ⇒ Object
- #get_string ⇒ Object
- #get_summary ⇒ Object
-
#initialize(init_view, init_idx) ⇒ RowHandle
constructor
A new instance of RowHandle.
- #iter ⇒ Object
- #iter_of_handles ⇒ Object
- #iter_of_states ⇒ Object
- #max_idx ⇒ Object
- #row_dim ⇒ Object
- #validate(params) ⇒ Object
- #where_values_are(v) ⇒ Object
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
#idx ⇒ Object (readonly)
Returns the value of attribute idx.
807 808 809 |
# File 'lib/binary_puzzle_solver/base.rb', line 807 def idx @idx end |
#view ⇒ Object (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_duplicated ⇒ Object
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_consecutive ⇒ Object
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_dim ⇒ Object
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_string ⇒ Object
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_summary ⇒ Object
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 |
#iter ⇒ Object
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_handles ⇒ Object
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_states ⇒ Object
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_idx ⇒ Object
835 836 837 |
# File 'lib/binary_puzzle_solver/base.rb', line 835 def max_idx() return view.max_idx(view.col_dim()) end |
#row_dim ⇒ Object
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 |