Class: Rubylife::Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows = 3, cols = 3) ⇒ Board

Returns a new instance of Board.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rubylife/board.rb', line 5

def initialize(rows=3, cols=3)
  @rows = rows
  @cols = cols
  @cells = []
  @grid = Array.new(rows) do |row|
    Array.new(cols) do |col|
      cell = Cell.new(row, col)
      @cells.push(cell)
      cell
    end
  end
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



3
4
5
# File 'lib/rubylife/board.rb', line 3

def cells
  @cells
end

#colsObject

Returns the value of attribute cols.



3
4
5
# File 'lib/rubylife/board.rb', line 3

def cols
  @cols
end

#gridObject

Returns the value of attribute grid.



3
4
5
# File 'lib/rubylife/board.rb', line 3

def grid
  @grid
end

#rowsObject

Returns the value of attribute rows.



3
4
5
# File 'lib/rubylife/board.rb', line 3

def rows
  @rows
end

Instance Method Details

#live_neighbours(cell) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubylife/board.rb', line 18

def live_neighbours(cell)
  neighbours = []

  @grid.each_with_index do |row, i|
  	row.each_with_index do |col,j|
  		if (grid[i][j] != cell)
  			if(grid[i][j].alive? && (((cell.x - i).abs < 2) && ((cell.y - j).abs < 2)))
  				neighbours.push(grid[i][j])
  			end
  		end
  	end
  end

  neighbours
end