class RubyGo::Board

Attributes

rows[R]
size[R]

Public Class Methods

new(size) click to toggle source
# File lib/ruby-go/board.rb, line 5
def initialize(size)
  @rows = Array.new(size) { Array.new(size) { Liberty.new } }
  @size = size
end

Public Instance Methods

around(x, y) click to toggle source
# File lib/ruby-go/board.rb, line 18
def around(x, y)
  intersections = []

  intersections << at(x-1, y) unless x == 0
  intersections << at(x+1, y) unless x == (size - 1)
  intersections << at(x, y-1) unless y == 0
  intersections << at(x, y+1) unless y == (size - 1)
  intersections
end
at(x, y) click to toggle source
# File lib/ruby-go/board.rb, line 14
def at(x, y)
  rows[y][x]
end
empty?() click to toggle source
# File lib/ruby-go/board.rb, line 10
def empty?
  rows.flatten.all?(&:empty?)
end
group_of(stone, stones = []) click to toggle source
# File lib/ruby-go/board.rb, line 52
def group_of(stone, stones = [])
  return stones if stones.include?(stone)

  stones << stone

  around(*stone.to_coord).each do |intersection|
    next if intersection.empty?

    group_of(intersection, stones) if intersection.color == stone.color
  end

  stones
end
liberties(stone) click to toggle source
# File lib/ruby-go/board.rb, line 42
def liberties(stone)
  libs = []

  group_of(stone).each do |stn|
    libs += around(*stn.to_coord).select(&:empty?)
  end

  libs.uniq.length
end
place(stone) click to toggle source
# File lib/ruby-go/board.rb, line 36
def place(stone)
  x, y = stone.to_coord

  rows[y][x] = stone
end
remove(stone) click to toggle source
# File lib/ruby-go/board.rb, line 28
def remove(stone)
  return if stone.empty?

  x, y = stone.to_coord

  rows[y][x] = Liberty.new
end