Class: Stones::Board

Inherits:
Object
  • Object
show all
Includes:
WithHead, WithStones
Defined in:
lib/stones/board.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithStones

#clear!, #count, #exist?, #pop!, #push!

Methods included from WithHead

#can_move?, #move!, #move_to_edge!

Constructor Details

#initialize(cells, position) ⇒ Board

Returns a new instance of Board.



14
15
16
17
# File 'lib/stones/board.rb', line 14

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

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



12
13
14
# File 'lib/stones/board.rb', line 12

def cells
  @cells
end

#head_positionObject (readonly)

Returns the value of attribute head_position.



12
13
14
# File 'lib/stones/board.rb', line 12

def head_position
  @head_position
end

Class Method Details

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



41
42
43
# 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]) ⇒ Object



45
46
47
# 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

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
# File 'lib/stones/board.rb', line 23

def ==(other)
  self.class == other.class &&
      cells_equal?(other) &&
      head_position_equal?(other)
end

#cells_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/stones/board.rb', line 37

def cells_equal?(other)
  self.cells == other.cells
end

#hashObject



29
30
31
# File 'lib/stones/board.rb', line 29

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

#head_position_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/stones/board.rb', line 33

def head_position_equal?(other)
  self.head_position == other.head_position
end

#sizeObject



19
20
21
# File 'lib/stones/board.rb', line 19

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