Class: Go::GTP::Point

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

Overview

This utility class manages points on a Go board using any one of three formats:

  • X and Y Array indices counting from the upper left hand corner

  • SGF letter pairs (“ac”) counting from the upper left hand corner

  • GNU Go letter and number pairs (“A13”) counting from the lower left hand corner

There are two gotchas to stay aware of with these systems. First, the GNU Go format skips over I in columns, but the SGF format does not. Second, the GNU Go format relies on knowing the board size. A 19x19 size is assumed, but you can override this when creating from or converting to this format.

Point instances can be initialized from any format and converted to any format.

Constant Summary collapse

BIG_A =
"A".getbyte(0)
LITTLE_A =
"a".getbyte(0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Point

Returns a new instance of Point.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/go/gtp/point.rb', line 25

def initialize(*args)
  if args.size == 2 and args.all? { |n| n.is_a? Integer }
    @x, @y = args
  elsif (args.size == 1 or (args.size == 2 and args.last.is_a?(Hash))) and
        args.first =~ /\A([A-HJ-T])(\d{1,2})\z/i
    options = args.last.is_a?(Hash) ? args.pop : { }
    letter  = $1.upcase
    @x      = letter.getbyte(0) - BIG_A - (letter > "I" ? 1 : 0)
    @y      = options.fetch(:board_size, 19) - $2.to_i
  elsif args.size == 1 and args.first =~ /\A([a-s])([a-s])\z/i
    @x, @y = $~.captures.map { |l| l.downcase.getbyte(0) - LITTLE_A }
  else
    fail ArgumentError, "unrecognized point format"
  end
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#to_gnugo(board_size = 19) ⇒ Object Also known as: to_s



52
53
54
# File 'lib/go/gtp/point.rb', line 52

def to_gnugo(board_size = 19)
  "#{(BIG_A + @x).chr}#{board_size - @y}"
end

#to_indexesObject Also known as: to_indices



43
44
45
# File 'lib/go/gtp/point.rb', line 43

def to_indexes
  [@x, @y]
end

#to_sgfObject



48
49
50
# File 'lib/go/gtp/point.rb', line 48

def to_sgf
  [@x, @y].map { |n| (LITTLE_A + n).chr }.join
end