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.



580
581
582
583
# File 'lib/binary_puzzle_solver/base.rb', line 580

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

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



579
580
581
# File 'lib/binary_puzzle_solver/base.rb', line 579

def idx
  @idx
end

#viewObject (readonly)

Returns the value of attribute view.



579
580
581
# File 'lib/binary_puzzle_solver/base.rb', line 579

def view
  @view
end

Instance Method Details

#check_for_duplicated(complete_rows_map) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/binary_puzzle_solver/base.rb', line 619

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



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/binary_puzzle_solver/base.rb', line 641

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().each do |x, cell|
        cell_state = cell.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



593
594
595
# File 'lib/binary_puzzle_solver/base.rb', line 593

def col_dim()
    return view.col_dim()
end

#get_coord(x) ⇒ Object



605
606
607
# File 'lib/binary_puzzle_solver/base.rb', line 605

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

#get_state(x) ⇒ Object



609
610
611
# File 'lib/binary_puzzle_solver/base.rb', line 609

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

#get_stringObject



589
590
591
# File 'lib/binary_puzzle_solver/base.rb', line 589

def get_string()
    return iter().map { |x, cell| cell.get_char() }.join('')
end

#get_summaryObject



585
586
587
# File 'lib/binary_puzzle_solver/base.rb', line 585

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

#iterObject



613
614
615
616
617
# File 'lib/binary_puzzle_solver/base.rb', line 613

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

#max_idxObject



601
602
603
# File 'lib/binary_puzzle_solver/base.rb', line 601

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

#row_dimObject



597
598
599
# File 'lib/binary_puzzle_solver/base.rb', line 597

def row_dim()
    return view.row_dim()
end

#validate(params) ⇒ Object



671
672
673
674
675
676
677
# File 'lib/binary_puzzle_solver/base.rb', line 671

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