Class: Gobstones::Runner::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/gobstones/runner/board.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows_num, cols_num, matrix = []) ⇒ Board

Returns a new instance of Board.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gobstones/runner/board.rb', line 8

def initialize(rows_num, cols_num, matrix = [])
  @rows = rows_num
  @columns = cols_num
  if matrix.empty?
    @matrix = []
    rows_num.times do
      @matrix << []
      cols_num.times { @matrix.last << Cell.new }
    end
  else
    @matrix = matrix
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/gobstones/runner/board.rb', line 6

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/gobstones/runner/board.rb', line 6

def rows
  @rows
end

Instance Method Details

#are_there_balls?(x, y, color) ⇒ Boolean

Returns:



40
41
42
# File 'lib/gobstones/runner/board.rb', line 40

def are_there_balls?(x, y, color)
  cell_at(x, y).are_there_balls?(color)
end

#cell_at(x, y) ⇒ Object

Raises:



22
23
24
25
26
# File 'lib/gobstones/runner/board.rb', line 22

def cell_at(x, y)
  raise OutOfBoardError unless x.between?(0, rows - 1) && y.between?(0, columns - 1)

  @matrix[x][y]
end

#cloneObject



57
58
59
60
# File 'lib/gobstones/runner/board.rb', line 57

def clone
  new_matrix = @matrix.map { |row| row.map(&:clone) }
  self.class.new(rows, columns, new_matrix)
end

#each_cellObject



28
29
30
# File 'lib/gobstones/runner/board.rb', line 28

def each_cell
  @matrix.each { |row| row.each { |cell| yield(cell) } }
end

#empty!Object



48
49
50
# File 'lib/gobstones/runner/board.rb', line 48

def empty!
  each_cell(&:empty!)
end

#empty?Boolean

Returns:



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

def empty?
  each_cell { |cell| return false unless cell.empty? }
  true
end

#number_of_balls(x, y, color) ⇒ Object



44
45
46
# File 'lib/gobstones/runner/board.rb', line 44

def number_of_balls(x, y, color)
  cell_at(x, y).number_of_balls(color)
end

#put(x, y, color) ⇒ Object



32
33
34
# File 'lib/gobstones/runner/board.rb', line 32

def put(x, y, color)
  cell_at(x, y).put(color)
end

#take_out(x, y, color) ⇒ Object



36
37
38
# File 'lib/gobstones/runner/board.rb', line 36

def take_out(x, y, color)
  cell_at(x, y).take_out(color)
end