class Grid

Universe

Constants

ALIVE
COLS
DEAD
PBACK_SPEED
ROWS

Attributes

grid_struct[R]

Public Class Methods

new() click to toggle source
# File lib/grid.rb, line 16
def initialize
  @grid_struct = create_grid_struct(true)
end

Public Instance Methods

create_grid_struct(randomize = false) click to toggle source
# File lib/grid.rb, line 28
def create_grid_struct(randomize = false)
  ROWS.times.map do |ri|
    COLS.times.map do |ci|
      Cell.new((randomize ? ([ALIVE] + ([DEAD] * 2)).sample : DEAD), ri, ci)
    end
  end
end
grid_print() click to toggle source
# File lib/grid.rb, line 51
def grid_print
  print(@grid_struct.map do |row|
    row.map(&:state).join
  end.join("\n"))
  puts
end
inc_gen() click to toggle source
# File lib/grid.rb, line 40
def inc_gen
  @grid_struct = next_generation_grid_struct
end
load_pattern(pattern, x, y) click to toggle source
# File lib/grid.rb, line 20
def load_pattern(pattern, x, y)
  @grid_struct = Seed.render_to_grid(@grid_struct, pattern, x, y)
end
map(&block) click to toggle source
# File lib/grid.rb, line 36
def map(&block)
  @grid_struct.map(&block)
end
neighbor_cells(*coords) click to toggle source
# File lib/grid.rb, line 44
def neighbor_cells(*coords)
  current_cell = @grid_struct.dig(*coords)
  current_cell.n_indices.map do |neighbor_cell_coords|
    @grid_struct.dig(*neighbor_cell_coords)
  end
end
next_generation_grid_struct() click to toggle source
# File lib/grid.rb, line 58
def next_generation_grid_struct
  next_gen_grid = create_grid_struct
  @grid_struct.map.with_index do |row, ri|
    row.map.with_index do |cell, ci|
      cn_cells = neighbor_cells(ri, ci)
      next_gen_grid[ri][ci] = Cell.new(
        cell.next_gen_fate(cn_cells.count(&:alive?)), ri, ci
      )
    end
  end
  next_gen_grid
end
reset() click to toggle source
# File lib/grid.rb, line 24
def reset
  @grid_struct = create_grid_struct(false)
end