Class: Group

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

Instance Method Summary collapse

Constructor Details

#initialize(board, stone) ⇒ Group

Returns a new instance of Group.



3
4
5
6
7
# File 'lib/gorb/group.rb', line 3

def initialize(board, stone)
  @board = board
  @board.groups << self
  self << stone
end

Instance Method Details

#include?(point) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/gorb/group.rb', line 20

def include?(point)
  self.any? {|stone| stone.point == point}
end

#libertiesObject

Check the liberties of the Group.



25
26
27
28
29
30
31
32
33
34
# File 'lib/gorb/group.rb', line 25

def liberties
  libs = []
  self.each do |stone|
    stone.liberties.each do |liberty|
      libs << liberty
    end
  end
  libs.uniq!
  return libs.size
end

#liberties!Object

Destructive version of the former. Note that this will remove the Group from the board if it has no liberties, so all the existing groups should have their liberties checked before checking the liberties of a new group in order to allow kills by filling dame.



40
41
42
43
44
# File 'lib/gorb/group.rb', line 40

def liberties!
  libs = self.liberties
  @board.groups.delete(self) if libs == 0
  return libs
end

#merge(groups) ⇒ Object



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

def merge(groups)
  groups.each do |group|
    group.each {|stone| self << stone}
    @board.groups.delete group
  end
  # Group references need to be updated for each stone after a merge.
  self.each do |stone|
    stone.group = self
  end
end