Class: Go::GTP::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/go/gtp/board.rb

Constant Summary collapse

STONES =
{"X" => "black", "O" => "white"}

Instance Method Summary collapse

Constructor Details

#initialize(board_string) ⇒ Board

Returns a new instance of Board.



8
9
10
11
# File 'lib/go/gtp/board.rb', line 8

def initialize(board_string)
  @string = board_string
  @array  = nil
end

Instance Method Details

#[](*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/go/gtp/board.rb', line 13

def [](*args)
  point = if args.size == 1 and args.first.is_a? Point
            args.first
          elsif args.size == 1 and
                args.first =~ /\A([A-HJ-T])(\d{1,2})\z/i
            Point.new(*args, board_size: size)
          else
            Point.new(*args)
          end
  to_a[point.y][point.x]
end

#captures(color) ⇒ Object



25
26
27
28
# File 'lib/go/gtp/board.rb', line 25

def captures(color)
  @string[/#{Regexp.escape(color)}(?: \([XO])?\) has captured (\d+)/i, 1]
  .to_i
end

#sizeObject



41
42
43
# File 'lib/go/gtp/board.rb', line 41

def size
  to_a.size
end

#to_aObject



34
35
36
37
38
39
# File 'lib/go/gtp/board.rb', line 34

def to_a
  @array ||= @string.scan(/^\s*(\d+)((?:.[.+XO])+).\1\b/)
                    .map { |_, row| row.chars
                                       .each_slice(2)
                                       .map { |_, stone| STONES[stone] } }
end

#to_sObject



30
31
32
# File 'lib/go/gtp/board.rb', line 30

def to_s
  @string
end