Class: Binary_Puzzle_Solver::RowSummary

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

Overview

A summary for a row or column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ RowSummary

Returns a new instance of RowSummary.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/binary_puzzle_solver/base.rb', line 97

def initialize (limit)
    @limit = limit

    if (limit % 2 != 0)
        raise RuntimeError, "Limit must be even"
    end

    @half_limit = limit / 2

    @counts = {
        Cell::ZERO => 0,
        Cell::ONE => 0,
    }

    return
end

Instance Attribute Details

#half_limitObject (readonly)

Returns the value of attribute half_limit.



96
97
98
# File 'lib/binary_puzzle_solver/base.rb', line 96

def half_limit
  @half_limit
end

#limitObject (readonly)

Returns the value of attribute limit.



96
97
98
# File 'lib/binary_puzzle_solver/base.rb', line 96

def limit
  @limit
end

Instance Method Details

#are_both_fullObject



137
138
139
# File 'lib/binary_puzzle_solver/base.rb', line 137

def are_both_full()
    return (is_full(Cell::ZERO) and is_full(Cell::ONE))
end

#are_both_not_exceededObject



132
133
134
135
# File 'lib/binary_puzzle_solver/base.rb', line 132

def are_both_not_exceeded()
    return (get_count(Cell::ZERO) <= half_limit() and
            get_count(Cell::ONE) <= half_limit())
end

#find_full_valueObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/binary_puzzle_solver/base.rb', line 141

def find_full_value()
    if are_both_full()
        return nil
    elsif (is_full(Cell::ZERO))
        return Cell::ZERO
    elsif (is_full(Cell::ONE))
        return Cell::ONE
    else
        return nil
    end
end

#get_count(value) ⇒ Object



114
115
116
# File 'lib/binary_puzzle_solver/base.rb', line 114

def get_count (value)
    return @counts[value]
end

#inc_count(value) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/binary_puzzle_solver/base.rb', line 122

def inc_count (value)
    new_val = (@counts[value] += 1)

    if (new_val > half_limit())
        raise GameIntegrityException, "Too many #{value}"
    end

    return
end

#is_full(value) ⇒ Object



118
119
120
# File 'lib/binary_puzzle_solver/base.rb', line 118

def is_full(value)
    return (get_count(value) == half_limit())
end