Class: Sudoku::CellCoordinates
- Inherits:
-
Object
- Object
- Sudoku::CellCoordinates
- Includes:
- Comparable
- Defined in:
- lib/sudoku/model/cell_coordinates.rb
Overview
Represents a two dimensional coordinates in the Sudoku grid.
Instance Attribute Summary collapse
-
#coordinate_x ⇒ Object
readonly
Returns the value of attribute coordinate_x.
-
#coordinate_y ⇒ Object
readonly
Returns the value of attribute coordinate_y.
Class Method Summary collapse
-
.random ⇒ Object
Will return randomly generated coordinate.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#in_area(x, y) ⇒ Object
Will return true, if this coordinate belongs to area given by two ranges of coordinates.
-
#initialize(x, y) ⇒ CellCoordinates
constructor
Will initialize this cell coordinate.
-
#to_s ⇒ Object
Will return this coordinate in the format [x, y].
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_x ⇒ Object (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_y ⇒ Object (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
.random ⇒ Object
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 |
#hash ⇒ Object
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_s ⇒ Object
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 |