Class: Gobgems::Board

Inherits:
Object
  • Object
show all
Includes:
WithColorOps, WithMovementOps
Defined in:
lib/gobgems/board.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithColorOps

#count, #exist?, #pop, #push

Methods included from WithMovementOps

#__move_to__, #can_move?, #move

Constructor Details

#initialize(cells, position) ⇒ Board

Returns a new instance of Board.



52
53
54
55
# File 'lib/gobgems/board.rb', line 52

def initialize(cells, position)
  @cells = cells
  @head_position = position
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



50
51
52
# File 'lib/gobgems/board.rb', line 50

def cells
  @cells
end

#head_positionObject (readonly)

Returns the value of attribute head_position.



50
51
52
# File 'lib/gobgems/board.rb', line 50

def head_position
  @head_position
end

Class Method Details

.empty(x, y, position = [0, 0]) ⇒ Object



71
72
73
# File 'lib/gobgems/board.rb', line 71

def self.empty(x, y, position=[0, 0])
  self.new((1..x).map { (1..y).map { empty_cell } }, position)
end

.from(cells, position = [0, 0]) ⇒ Object



75
76
77
# File 'lib/gobgems/board.rb', line 75

def self.from(cells, position=[0, 0])
  self.new(cells.map { |row| row.map { |cell| empty_cell.merge(cell) } }, position)
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
# File 'lib/gobgems/board.rb', line 61

def ==(other)
  self.class == other.class &&
      self.cells == other.cells &&
      self.head_position == other.head_position
end

#__cell_at__(position) ⇒ Object

Raises:



83
84
85
86
# File 'lib/gobgems/board.rb', line 83

def __cell_at__(position)
  raise OutOfBoardError unless within_bounds? position
  cells[position[0]][position[1]]
end

#__set_cell__(position, cell) ⇒ Object



79
80
81
# File 'lib/gobgems/board.rb', line 79

def __set_cell__(position, cell)
  __cell_at__(position).merge! cell
end

#hashObject



67
68
69
# File 'lib/gobgems/board.rb', line 67

def hash
  self.cells.hash ^ self.head_position.hash
end

#sizeObject



57
58
59
# File 'lib/gobgems/board.rb', line 57

def size
  [cells.size, cells[0].size]
end