Class: Stones::GbbReader

Inherits:
Object
  • Object
show all
Defined in:
lib/stones/gbb/gbb_reader.rb

Constant Summary collapse

COLORS =
{'Azul' => :blue, 'Negro' => :black, 'Rojo' => :red, 'Verde' => :green}

Instance Method Summary collapse

Instance Method Details

#from_string(gbb_string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stones/gbb/gbb_reader.rb', line 14

def from_string(gbb_string)
  board = nil

  lines = gbb_string.lines.reject { |it| it.start_with? 'GBB', '%%' }

  /size (\d+) (\d+)/.match(lines[0]) { |match| board = Board.empty(match[1].to_i, match[2].to_i) }
  /head (\d+) (\d+)/.match(lines.last) { |match| board.send :move_to!, match.to_position }

  lines.drop(1).init.each do |cell_line|
    position = get_position_from cell_line
    cell = create_cell_from cell_line

    board.send :set_cell, position, cell
  end

  board
end