Class: RubySGF::SGF::Coordinates
- Inherits:
-
Object
- Object
- RubySGF::SGF::Coordinates
- Defined in:
- lib/sgf/coordinates.rb
Constant Summary collapse
- VERTICAL =
HORIZONTAL = [*"a".."z"] + [*"A".."Z"]
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #==(second_coordinates) ⇒ Object
- #down ⇒ Object
-
#initialize(x, y) ⇒ Coordinates
constructor
A new instance of Coordinates.
- #inside?(size) ⇒ Boolean
- #left ⇒ Object
- #neighbours(size) ⇒ Object
- #outside?(size) ⇒ Boolean
- #right ⇒ Object
- #to_s ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Coordinates
Returns a new instance of Coordinates.
12 13 14 15 |
# File 'lib/sgf/coordinates.rb', line 12 def initialize(x, y) @x = x @y = y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
11 12 13 |
# File 'lib/sgf/coordinates.rb', line 11 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
11 12 13 |
# File 'lib/sgf/coordinates.rb', line 11 def y @y end |
Instance Method Details
#==(second_coordinates) ⇒ Object
17 18 19 |
# File 'lib/sgf/coordinates.rb', line 17 def ==(second_coordinates) @x == second_coordinates.x && @y == second_coordinates.y end |
#down ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/sgf/coordinates.rb', line 53 def down idx = VERTICAL.index(y) + 1 return nil unless idx >= 0 new_y = VERTICAL[idx] return nil unless new_y Coordinates.new(x, new_y) end |
#inside?(size) ⇒ Boolean
27 28 29 30 31 |
# File 'lib/sgf/coordinates.rb', line 27 def inside?(size) vert = VERTICAL[0,size] horiz = HORIZONTAL[0,size] horiz.include?(@x) && vert.include?(@y) end |
#left ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/sgf/coordinates.rb', line 61 def left idx = HORIZONTAL.index(x) - 1 return nil unless idx >= 0 new_x = HORIZONTAL[idx] return nil unless new_x Coordinates.new(new_x, y) end |
#neighbours(size) ⇒ Object
21 22 23 24 25 |
# File 'lib/sgf/coordinates.rb', line 21 def neighbours(size) [up, right, down, left].compact.map do |c| c.inside?(size) ? c : nil end end |
#outside?(size) ⇒ Boolean
33 34 35 |
# File 'lib/sgf/coordinates.rb', line 33 def outside?(size) !inside?(size) end |
#right ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/sgf/coordinates.rb', line 45 def right idx = HORIZONTAL.index(x) + 1 return nil unless idx >= 0 new_x = HORIZONTAL[idx] return nil unless new_x Coordinates.new(new_x, y) end |
#to_s ⇒ Object
69 70 71 |
# File 'lib/sgf/coordinates.rb', line 69 def to_s @x + @y end |
#up ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/sgf/coordinates.rb', line 37 def up idx = VERTICAL.index(y) - 1 return nil unless idx >= 0 new_y = VERTICAL[idx] return nil unless new_y Coordinates.new(x, new_y) end |