class Sudoku::Cell

Represents single Cell in the Sudoku grid.

Attributes

coordinates[RW]
fixed[RW]
value[RW]

Public Class Methods

new(coordinates, fixed = false, value = 0) click to toggle source

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

<=>(other) click to toggle source
# File lib/sudoku/model/cell.rb, line 77
def <=>(other)
  @coordinates <=> other.coordinates
end
==(other) click to toggle source

Same as eql?

# File lib/sudoku/model/cell.rb, line 69
def ==(other)
  eql?(other)
end
as_string() click to toggle source

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
decrement() click to toggle source

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
eql?(other) click to toggle source

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
in_area(x, y) click to toggle source

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
increment() click to toggle source

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
to_s() click to toggle source
# File lib/sudoku/model/cell.rb, line 73
def to_s
  "#{@coordinates}, #{@value}, #{fixed}"
end
value=(value) click to toggle source

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