Class: Sudoku::CellCoordinates

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

Overview

Represents a two dimensional coordinates in the Sudoku grid.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ CellCoordinates

Will initialize this cell coordinate.

  • x horizontal coordinate.

  • y vertical coordinate.



12
13
14
15
# File 'lib/sudoku/model/cell_coordinates.rb', line 12

def initialize(x, y)
  @coordinate_x = Sudoku::Coordinate.new(x)
  @coordinate_y = Sudoku::Coordinate.new(y)
end

Instance Attribute Details

#coordinate_xObject (readonly)

Returns the value of attribute coordinate_x.



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

def coordinate_x
  @coordinate_x
end

#coordinate_yObject (readonly)

Returns the value of attribute coordinate_y.



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

def coordinate_y
  @coordinate_y
end

Class Method Details

.randomObject

Will return randomly generated coordinate.



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

def self.random
  CellCoordinates.new(Random.rand(0..8), Random.rand(0..8))
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  return @coordinate_y <=> other.coordinate_y if
    @coordinate_y != other.coordinate_y
  @coordinate_x <=> other.coordinate_x
end

#==(other) ⇒ Object Also known as: eql?



17
18
19
20
21
# File 'lib/sudoku/model/cell_coordinates.rb', line 17

def ==(other)
  self.class == other.class &&
    @coordinate_x == other.coordinate_x &&
    @coordinate_y == other.coordinate_y
end

#hashObject



25
26
27
# File 'lib/sudoku/model/cell_coordinates.rb', line 25

def hash
  @coordinate_x.coordinate ^ @coordinate_y.coordinate # XOR, from the doc
end

#in_area(x, y) ⇒ Object

Will return true, if this coordinate belongs to area given by two ranges of coordinates.

  • x range of x coordinates to accept.

  • y range of y coordinates to accept.



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

def in_area(x, y)
  return true if x.include?(@coordinate_x) && y.include?(@coordinate_y)
  false
end

#to_sObject

Will return this coordinate in the format [x, y]



36
37
38
# File 'lib/sudoku/model/cell_coordinates.rb', line 36

def to_s
  "[#{coordinate_x}, #{coordinate_y}]"
end