Class: Sudoku::Cell
- Inherits:
-
Object
- Object
- Sudoku::Cell
- Includes:
- Comparable
- Defined in:
- lib/sudoku/model/cell.rb
Overview
Represents single Cell in the Sudoku grid.
Instance Attribute Summary collapse
-
#coordinates ⇒ Object
Returns the value of attribute coordinates.
-
#fixed ⇒ Object
Returns the value of attribute fixed.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Same as eql?.
-
#as_string ⇒ Object
Will return value of this cell in the correct string format.
-
#decrement ⇒ Object
Will decrement cell value, if the cell is not fixed.
-
#eql?(other) ⇒ Boolean
The cells are equal, if they have the same coordinate, no matter the value and fixed tag.
-
#in_area(x, y) ⇒ Object
Will return true, if coordinates of this cell belongs to area given by two coordinate ranges.
-
#increment ⇒ Object
Will increment cell value, if the cell is not fixed.
-
#initialize(coordinates, fixed = false, value = 0) ⇒ Cell
constructor
Initializes this cell.
- #to_s ⇒ Object
Constructor Details
#initialize(coordinates, fixed = false, value = 0) ⇒ Cell
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.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#coordinates ⇒ Object
Returns the value of attribute coordinates.
5 6 7 |
# File 'lib/sudoku/model/cell.rb', line 5 def coordinates @coordinates end |
#fixed ⇒ Object
Returns the value of attribute fixed.
5 6 7 |
# File 'lib/sudoku/model/cell.rb', line 5 def fixed @fixed end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/sudoku/model/cell.rb', line 5 def value @value end |
Instance Method Details
#<=>(other) ⇒ Object
77 78 79 |
# File 'lib/sudoku/model/cell.rb', line 77 def <=>(other) @coordinates <=> other.coordinates end |
#==(other) ⇒ Object
Same as eql?
69 70 71 |
# File 'lib/sudoku/model/cell.rb', line 69 def ==(other) eql?(other) end |
#as_string ⇒ Object
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.
30 31 32 33 |
# File 'lib/sudoku/model/cell.rb', line 30 def as_string return '' unless @value != 0 @value.to_s end |
#decrement ⇒ Object
Will decrement cell value, if the cell is not fixed. Will reset on underflow. Returns new value.
54 55 56 57 58 59 |
# 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) ⇒ Boolean
The cells are equal, if they have the same coordinate, no matter the value and fixed tag.
63 64 65 66 |
# 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) ⇒ Object
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
48 49 50 |
# File 'lib/sudoku/model/cell.rb', line 48 def in_area(x, y) @coordinates.in_area(x, y) end |
#increment ⇒ Object
Will increment cell value, if the cell is not fixed. Will reset on overflow. Returns new value.
37 38 39 40 41 42 |
# 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 ⇒ Object
73 74 75 |
# File 'lib/sudoku/model/cell.rb', line 73 def to_s "#{@coordinates}, #{@value}, #{fixed}" end |