class Sudoku::Cell
Attributes
Public Class Methods
Initializes this cell. It is not possible to initialize fixed cell with value 0.
-
fixed
boolean value, which indicates if this cell is fixed -
coordinates
X Y coordinates of this cell. -
value
numerical value of this cell. Default set to 0.
# File lib/sudoku/model/cell.rb, line 12 def initialize(coordinates, fixed = false, value = 0) fail ArgumentError, 'Cant initialize fixed cell with value 0' if fixed && value == 0 fail ArgumentError, 'Wrong coordinate parameter.' unless coordinates.respond_to?(:coordinate_x) && coordinates.respond_to?(:coordinate_y) self.value = value @fixed = fixed @coordinates = CellCoordinates.new(coordinates.coordinate_x, coordinates.coordinate_y) end
Public Instance Methods
# File lib/sudoku/model/cell.rb, line 77 def <=>(other) @coordinates <=> other.coordinates end
Same as eql?
# File lib/sudoku/model/cell.rb, line 69 def ==(other) eql?(other) end
Will return value of this cell in the correct string format. As an empty string, if the value is 0, or as value string represetation.
# File lib/sudoku/model/cell.rb, line 30 def as_string return '' unless @value != 0 @value.to_s end
Will decrement cell value, if the cell is not fixed. Will reset on underflow. Returns new value.
# File lib/sudoku/model/cell.rb, line 54 def decrement return @value if @fixed @value -= 1 @value = 9 if @value < 0 @value end
The cells are equal, if they have the same coordinate, no matter the value and fixed tag.
# File lib/sudoku/model/cell.rb, line 63 def eql?(other) return false unless other.respond_to?(:coordinates) @coordinates.eql?(other.coordinates) end
Will return true, if coordinates of this cell belongs to area given by two coordinate ranges.
-
x
range of x coordinates -
y
range of y coordinates
# File lib/sudoku/model/cell.rb, line 48 def in_area(x, y) @coordinates.in_area(x, y) end
Will increment cell value, if the cell is not fixed. Will reset on overflow. Returns new value.
# File lib/sudoku/model/cell.rb, line 37 def increment return @value if @fixed @value += 1 @value = 0 if @value > 9 @value end
# File lib/sudoku/model/cell.rb, line 73 def to_s "#{@coordinates}, #{@value}, #{fixed}" end
Will set the value of this cell. New value must be in the interval of 0 - 9, where 0 means, that the cell is not filled yet. This method will fail, if the attribute fixed is set to true.
-
value
new value to set to this cell
# File lib/sudoku/model/cell.rb, line 86 def value=(value) fail ArgumentError, 'Cant assign new value to fixed value cell.' if @fixed fail ArgumentError, 'Cell initialization value out of interval 0-9' if value < 0 || value > 9 @value = value end