class Gobgems::Board

Attributes

cells[R]
head_position[R]

Public Class Methods

empty(x, y, position=[0, 0]) click to toggle source
# File lib/gobgems/board.rb, line 75
def self.empty(x, y, position=[0, 0])
  self.new((1..y).map { (1..x).map { empty_cell } }, position)
end
from(cells, position=[0, 0]) click to toggle source
# File lib/gobgems/board.rb, line 79
def self.from(cells, position=[0, 0])
  self.new(cells.map { |row| row.map { |cell| empty_cell.merge(cell) } }, position)
end
new(cells, position) click to toggle source
# File lib/gobgems/board.rb, line 56
def initialize(cells, position)
  @cells = cells
  @head_position = position
end

Private Class Methods

empty_cell() click to toggle source
# File lib/gobgems/board.rb, line 111
def self.empty_cell
  {red: 0, black: 0, green: 0, blue: 0}
end

Public Instance Methods

==(other) click to toggle source
# File lib/gobgems/board.rb, line 65
def ==(other)
  self.class == other.class &&
      self.cells == other.cells &&
      self.head_position == other.head_position
end
__cell_at__(position) click to toggle source
# File lib/gobgems/board.rb, line 87
def __cell_at__(position)
  raise OutOfBoardError unless within_bounds? position
  cells[-(position[1]+1)][position[0]]
end
__each_cell__() { |__cell_at__([x, y]), x, y| ... } click to toggle source
# File lib/gobgems/board.rb, line 92
def __each_cell__
  (0..(size[0]-1)).each do |x|
    (0..(size[1]-1)).each do |y|
      yield __cell_at__([x, y]), x, y
    end
  end
end
__set_cell__(position, cell) click to toggle source
# File lib/gobgems/board.rb, line 83
def __set_cell__(position, cell)
  __cell_at__(position).merge! cell
end
hash() click to toggle source
# File lib/gobgems/board.rb, line 71
def hash
  self.cells.hash ^ self.head_position.hash
end
size() click to toggle source
# File lib/gobgems/board.rb, line 61
def size
  [cells[0].size, cells.size]
end

Private Instance Methods

head_cell() click to toggle source
# File lib/gobgems/board.rb, line 107
def head_cell
  __cell_at__(head_position)
end
within_bounds?(position) click to toggle source
# File lib/gobgems/board.rb, line 101
def within_bounds?(position)
  (x, y) = size
  position[0] >= 0 && position[1] >= 0 &&
      position[0] < x && position[1] < y
end