Class: Sudoku::Coordinate

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

Overview

Represents a cell coordinate in one dimension of the Sudoku grid.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinate) ⇒ Coordinate

Will initialize this coordinate.

  • coordinate coordinate value, which must be in the 0-8 interval.



11
12
13
14
15
16
17
# File 'lib/sudoku/model/coordinate.rb', line 11

def initialize(coordinate)
  fail ArgumentError,
       "Coordinate value must be 0-8, value #{coordinate} given." unless
        coordinate_valid?(coordinate)

  @coordinate = coordinate.to_i
end

Instance Attribute Details

#coordinateObject (readonly)

Returns the value of attribute coordinate.



7
8
9
# File 'lib/sudoku/model/coordinate.rb', line 7

def coordinate
  @coordinate
end

Class Method Details

.valid?(coordinate) ⇒ Boolean

Will return true, if the given coordinate is between 0-8 interval, inclusive.

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/sudoku/model/coordinate.rb', line 30

def self.valid?(coordinate)
  return true if coordinate >= 0 && coordinate < 9
  false
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this cooridnate with the other one.



50
51
52
53
# File 'lib/sudoku/model/coordinate.rb', line 50

def <=>(other)
  return @coordinate <=> other.coordinate if other.respond_to?(:coordinate)
  @coordinate <=> other.to_i
end

#coordinate_valid?(coordinate) ⇒ Boolean

Will check if given number can be used as the valid coordinate, which means, that it is a number in the interval 0-8 inclusive. True is returned if the coordinate is valid, false otherwise.

  • coordinate coordinate value to check

Returns:

  • (Boolean)


24
25
26
# File 'lib/sudoku/model/coordinate.rb', line 24

def coordinate_valid?(coordinate)
  Coordinate.valid?(coordinate)
end

#eql?(other) ⇒ Boolean

Will check, if the given element represents the same one-dimensional coordiante.

Returns:

  • (Boolean)


57
58
59
# File 'lib/sudoku/model/coordinate.rb', line 57

def eql?(other)
  @coordinate == other.to_i
end

#to_iObject

Will convert this coordinate to integer.



40
41
42
# File 'lib/sudoku/model/coordinate.rb', line 40

def to_i
  @coordinate
end

#to_intObject

Will convert this coordinate to integer.



45
46
47
# File 'lib/sudoku/model/coordinate.rb', line 45

def to_int
  to_i
end

#to_sObject



35
36
37
# File 'lib/sudoku/model/coordinate.rb', line 35

def to_s
  @coordinate.to_s
end