Class: Rubylife::Board
- Inherits:
-
Object
- Object
- Rubylife::Board
- Defined in:
- lib/rubylife/board.rb
Instance Attribute Summary collapse
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
-
#initialize(rows = 3, cols = 3) ⇒ Board
constructor
A new instance of Board.
- #live_neighbours(cell) ⇒ Object
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
#cells ⇒ Object
Returns the value of attribute cells.
3 4 5 |
# File 'lib/rubylife/board.rb', line 3 def cells @cells end |
#cols ⇒ Object
Returns the value of attribute cols.
3 4 5 |
# File 'lib/rubylife/board.rb', line 3 def cols @cols end |
#grid ⇒ Object
Returns the value of attribute grid.
3 4 5 |
# File 'lib/rubylife/board.rb', line 3 def grid @grid end |
#rows ⇒ Object
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 |