Module: SGF::SGFHelper

Included in:
Model::EventListener, Model::Game, Model::Node
Defined in:
lib/sgf/sgf_helper.rb

Instance Method Summary collapse

Instance Method Details

#move_to_sgf(color, x, y) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/sgf/sgf_helper.rb', line 7

def move_to_sgf color, x, y
  return "" unless [SGF::Model::Constants::BLACK, SGF::Model::Constants::WHITE].include?(color)

  sgf = ";"
  sgf << (color == SGF::Model::Constants::WHITE ? "W" : "B")
  sgf << "["
  sgf << xy_to_sgf_pos(x, y)
  sgf << "]"
end

#to_label(input) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File 'lib/sgf/sgf_helper.rb', line 48

def to_label input
  raise ArgumentError.new(input) if input.nil? or input !~ /^\s*\w\w:\w+\s*$/
  
  position, text = input.split(':', 2)
  SGF::Model::Label.new(to_position(position), text.strip)
end

#to_position(input) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
# File 'lib/sgf/sgf_helper.rb', line 39

def to_position input
  raise ArgumentError.new(input) if input.nil? or input.strip.length != 2
  
  input.strip!
  input.downcase!
  
  [input[0] - ?a, input[1] - ?a]
end

#to_position_array(input) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sgf/sgf_helper.rb', line 17

def to_position_array input
  raise ArgumentError.new(input) if input.nil? or input !~ /^\s*\w\w(:\w\w)?\s*$/

  input.strip!
  input.downcase!
  
  if input.include?(':')
    parts = input.split(':', 2)
    position1 = to_position(parts[0])
    position2 = to_position(parts[1])
    positions = []
    (position1[0]..position2[0]).each do |i|
      (position1[1]..position2[1]).each do |j|
        positions << [i, j]
      end
    end
    positions
  else
    [to_position(input)]
  end
end

#xy_to_sgf_pos(x, y) ⇒ Object



3
4
5
# File 'lib/sgf/sgf_helper.rb', line 3

def xy_to_sgf_pos x, y
  SGF::Model::Constants::POSITIONS[x, 1] + SGF::Model::Constants::POSITIONS[y, 1]
end