Class: Rubykon::GTPCoordinateConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubykon/gtp_coordinate_converter.rb

Constant Summary collapse

X_CHARS =
('A'..'Z').reject { |c| c == 'I'.freeze }

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ GTPCoordinateConverter

Returns a new instance of GTPCoordinateConverter.



6
7
8
# File 'lib/rubykon/gtp_coordinate_converter.rb', line 6

def initialize(board)
  @board = board
end

Instance Method Details

#from(string) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rubykon/gtp_coordinate_converter.rb', line 10

def from(string)
  x = string[0]
  y = string[1..-1]
  x_index = X_CHARS.index(x) + 1
  y_index = @board.size - y.to_i + 1
  @board.identifier_for(x_index, y_index)
end

#to(index) ⇒ Object



18
19
20
21
22
23
# File 'lib/rubykon/gtp_coordinate_converter.rb', line 18

def to(index)
  x, y = @board.x_y_from(index)
  x_char = X_CHARS[x - 1]
  y_index = @board.size - y + 1
  "#{x_char}#{y_index}"
end