class Stones::Board

Attributes

cells[R]
head_position[R]

Public Class Methods

empty(x, y, position=[0, 0]) click to toggle source
# File lib/stones/board.rb, line 41
def self.empty(x, y, position=[0, 0])
  self.new(empty_cells(x, y), position)
end
from(cells, position=[0, 0]) click to toggle source
# File lib/stones/board.rb, line 45
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/stones/board.rb, line 14
def initialize(cells, position)
  @cells = cells
  @head_position = position
end

Private Class Methods

empty_cell() click to toggle source
# File lib/stones/board.rb, line 78
def self.empty_cell
  {red: 0, black: 0, green: 0, blue: 0}
end
empty_cells(x, y) click to toggle source
# File lib/stones/board.rb, line 82
def self.empty_cells(x, y)
  (1..y).map { (1..x).map { empty_cell } }
end

Public Instance Methods

==(other) click to toggle source
# File lib/stones/board.rb, line 23
def ==(other)
  self.class == other.class &&
      cells_equal?(other) &&
      head_position_equal?(other)
end
cell_at(position) click to toggle source
# File lib/stones/board.rb, line 57
def cell_at(position)
  raise OutOfBoardError unless within_bounds? position
  cells[-(position[1]+1)][position[0]]
end
cells_equal?(other) click to toggle source
# File lib/stones/board.rb, line 37
def cells_equal?(other)
  self.cells == other.cells
end
each_cell() { |cell_at([x, y]), x, y| ... } click to toggle source
# File lib/stones/board.rb, line 49
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
hash() click to toggle source
# File lib/stones/board.rb, line 29
def hash
  self.cells.hash ^ self.head_position.hash
end
head_cell() click to toggle source
# File lib/stones/board.rb, line 62
def head_cell
  cell_at(head_position)
end
head_position_equal?(other) click to toggle source
# File lib/stones/board.rb, line 33
def head_position_equal?(other)
  self.head_position == other.head_position
end
size() click to toggle source
# File lib/stones/board.rb, line 19
def size
  [cells[0].size, cells.size]
end

Private Instance Methods

set_cell(position, cell) click to toggle source
# File lib/stones/board.rb, line 68
def set_cell(position, cell)
  cell_at(position).merge! cell
end
within_bounds?(position) click to toggle source
# File lib/stones/board.rb, line 72
def within_bounds?(position)
  (x, y) = size
  position[0] >= 0 && position[1] >= 0 &&
      position[0] < x && position[1] < y
end