Class: Rubykon::Group

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

Constant Summary collapse

NOT_SET =
:not_set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, stones = [id], liberties = {}, liberty_count = 0) ⇒ Group

Returns a new instance of Group.



8
9
10
11
12
13
# File 'lib/rubykon/group.rb', line 8

def initialize(id, stones = [id], liberties = {}, liberty_count = 0)
  @identifier    = id
  @stones        = stones
  @liberties     = liberties
  @liberty_count = liberty_count
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/rubykon/group.rb', line 4

def identifier
  @identifier
end

#libertiesObject (readonly)

Returns the value of attribute liberties.



4
5
6
# File 'lib/rubykon/group.rb', line 4

def liberties
  @liberties
end

#liberty_countObject (readonly)

Returns the value of attribute liberty_count.



4
5
6
# File 'lib/rubykon/group.rb', line 4

def liberty_count
  @liberty_count
end

#stonesObject (readonly)

Returns the value of attribute stones.



4
5
6
# File 'lib/rubykon/group.rb', line 4

def stones
  @stones
end

Instance Method Details

#add_enemy_group_at(enemy_identifier) ⇒ Object



54
55
56
# File 'lib/rubykon/group.rb', line 54

def add_enemy_group_at(enemy_identifier)
  liberties[enemy_identifier] = enemy_identifier
end

#add_liberty(identifier) ⇒ Object



38
39
40
41
42
# File 'lib/rubykon/group.rb', line 38

def add_liberty(identifier)
  return if already_counted_as_liberty?(identifier, Board::EMPTY)
  @liberties[identifier] = Board::EMPTY
  @liberty_count += 1
end

#caught?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubykon/group.rb', line 50

def caught?
  @liberty_count <= 0
end

#connect(stone_identifier, stone_group, group_tracker) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/rubykon/group.rb', line 15

def connect(stone_identifier, stone_group, group_tracker)
  return if stone_group == self
  if stone_group
    merge(stone_group, group_tracker)
  else
    add_stone(stone_identifier, group_tracker)
  end
  remove_connector_liberty(stone_identifier)
end

#dupObject



34
35
36
# File 'lib/rubykon/group.rb', line 34

def dup
  self.class.new @identifier, @stones.dup, @liberties.dup, @liberty_count
end

#gain_liberties_from_capture_of(captured_group, group_tracker) ⇒ Object



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

def gain_liberties_from_capture_of(captured_group, group_tracker)
  new_liberties = @liberties.select do |_identifier, stone_identifier|
    group_tracker.group_id_of(stone_identifier) == captured_group.identifier
  end
  new_liberties.each do |identifier, _group_id|
    add_liberty(identifier)
  end
end

#remove_liberty(identifier) ⇒ Object



44
45
46
47
48
# File 'lib/rubykon/group.rb', line 44

def remove_liberty(identifier)
  return if already_counted_as_liberty?(identifier, identifier)
  @liberties[identifier] = identifier
  @liberty_count -= 1
end