class Rubykon::Group

Constants

NOT_SET

Attributes

identifier[R]
liberties[R]
liberty_count[R]
stones[R]

Public Class Methods

new(id, stones = [id], liberties = {}, liberty_count = 0) click to toggle source
# 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

Public Instance Methods

add_enemy_group_at(enemy_identifier) click to toggle source
# File lib/rubykon/group.rb, line 54
def add_enemy_group_at(enemy_identifier)
  liberties[enemy_identifier] = enemy_identifier
end
add_liberty(identifier) click to toggle source
# 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?() click to toggle source
# File lib/rubykon/group.rb, line 50
def caught?
  @liberty_count <= 0
end
connect(stone_identifier, stone_group, group_tracker) click to toggle source
# 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
dup() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

add_stone(identifier, group_tracker) click to toggle source
# File lib/rubykon/group.rb, line 81
def add_stone(identifier, group_tracker)
  group_tracker.stone_joins_group(identifier, @identifier)
  @stones << identifier
end
already_counted_as_liberty?(identifier, value) click to toggle source
# File lib/rubykon/group.rb, line 95
def already_counted_as_liberty?(identifier, value)
  @liberties.fetch(identifier, NOT_SET) == value
end
merge(other_group, group_tracker) click to toggle source
# File lib/rubykon/group.rb, line 60
def merge(other_group, group_tracker)
  merge_stones(other_group, group_tracker)
  merge_liberties(other_group)
end
merge_liberties(other_group) click to toggle source
# File lib/rubykon/group.rb, line 71
def merge_liberties(other_group)
  @liberty_count += other_group.liberty_count
  @liberties.merge!(other_group.liberties) do |_key, my_identifier, other_identifier|
    if shared_liberty?(my_identifier, other_identifier)
      @liberty_count -= 1
    end
    my_identifier
  end
end
merge_stones(other_group, group_tracker) click to toggle source
# File lib/rubykon/group.rb, line 65
def merge_stones(other_group, group_tracker)
  other_group.stones.each do |identifier|
    add_stone(identifier, group_tracker)
  end
end
remove_connector_liberty(identifier) click to toggle source
# File lib/rubykon/group.rb, line 90
def remove_connector_liberty(identifier)
  @liberties.delete(identifier)
  @liberty_count -= 1
end
shared_liberty?(my_identifier, other_identifier) click to toggle source
# File lib/rubykon/group.rb, line 86
def shared_liberty?(my_identifier, other_identifier)
  my_identifier == Board::EMPTY || other_identifier == Board::EMPTY
end