Class: RubySGF::SGF::Coordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/sgf/coordinates.rb

Constant Summary collapse

VERTICAL =
HORIZONTAL = [*"a".."z"] + [*"A".."Z"]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#xObject (readonly)

Returns the value of attribute x.



11
12
13
# File 'lib/sgf/coordinates.rb', line 11

def x
  @x
end

#yObject (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

#downObject



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

Returns:

  • (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

#leftObject



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

Returns:

  • (Boolean)


33
34
35
# File 'lib/sgf/coordinates.rb', line 33

def outside?(size)
  !inside?(size)
end

#rightObject



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_sObject



69
70
71
# File 'lib/sgf/coordinates.rb', line 69

def to_s
  @x + @y
end

#upObject



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