Class: Stone

Inherits:
Object
  • Object
show all
Defined in:
lib/gorb/stone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, point, color) ⇒ Stone

Returns a new instance of Stone.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gorb/stone.rb', line 8

def initialize(board, point, color)
  @board = board
  @point = point
  @color = color

  if (@point[1, 2].to_i > @board.size.split('x')[0].to_i or
      not @board.letters.index(@point[0]))
    raise ArgumentError, "Invalid point"
  end

  @group = self.find_group
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



6
7
8
# File 'lib/gorb/stone.rb', line 6

def board
  @board
end

#colorObject (readonly)

Returns the value of attribute color.



6
7
8
# File 'lib/gorb/stone.rb', line 6

def color
  @color
end

#groupObject

Returns the value of attribute group.



5
6
7
# File 'lib/gorb/stone.rb', line 5

def group
  @group
end

#pointObject (readonly)

Returns the value of attribute point.



6
7
8
# File 'lib/gorb/stone.rb', line 6

def point
  @point
end

Instance Method Details

#find_groupObject

Find the Group of the Stone or create a new one. If this Stone connects one or more groups, merge them together to a single Group.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gorb/stone.rb', line 36

def find_group
  groups = []
  stones = @board.search(neighbors)
  stones.each do |stone|
    if stone.color == @color and not groups.include? stone.group
      groups << stone.group
    end
  end
  if groups.empty?
    return Group.new(@board, self)
  else
    group = groups.pop
    group.merge(groups)
    group << self
    return group
  end
end

#libertiesObject

Return the liberties of the Stone.



27
28
29
30
31
32
# File 'lib/gorb/stone.rb', line 27

def liberties
  liberties = neighbors
  stones = @board.search(neighbors)
  stones.each {|stone| liberties.delete(stone.point)}
  return liberties
end

#neighborsObject

Return the neighboring points of the Stone.



22
23
24
# File 'lib/gorb/stone.rb', line 22

def neighbors
  @board.neighbors(@point)
end

#to_sObject



54
55
56
# File 'lib/gorb/stone.rb', line 54

def to_s
  @point
end