Class: Sudoku::Cell

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/sudoku/model/cell.rb

Overview

Represents single Cell in the Sudoku grid.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#coordinatesObject

Returns the value of attribute coordinates.


5
6
7
# File 'lib/sudoku/model/cell.rb', line 5

def coordinates
  @coordinates
end

#fixedObject

Returns the value of attribute fixed.


5
6
7
# File 'lib/sudoku/model/cell.rb', line 5

def fixed
  @fixed
end

#valueObject

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_stringObject

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

#decrementObject

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.

Returns:

  • (Boolean)

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

#incrementObject

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_sObject


73
74
75
# File 'lib/sudoku/model/cell.rb', line 73

def to_s
  "#{@coordinates}, #{@value}, #{fixed}"
end