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:



16
17
18
# File 'lib/gorb/group.rb', line 16

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

#libertiesObject

Check the liberties of the Group.



21
22
23
24
25
26
27
28
29
30
# File 'lib/gorb/group.rb', line 21

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.



36
37
38
39
40
# File 'lib/gorb/group.rb', line 36

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

#merge(groups) ⇒ Object



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

def merge(groups)
  groups.each do |group|
    group.each {|stone| self << stone}
    @board.groups.delete group
  end
end