Class: Grid
- Inherits:
-
Object
- Object
- Grid
- Defined in:
- lib/grid.rb
Overview
Universe
Constant Summary collapse
- ROWS =
60
- COLS =
60
- DEAD =
' '
- ALIVE =
'█'
- PBACK_SPEED =
0.000
Instance Attribute Summary collapse
-
#grid_struct ⇒ Object
readonly
Returns the value of attribute grid_struct.
Instance Method Summary collapse
- #create_grid_struct(randomize = false) ⇒ Object
- #grid_print ⇒ Object
- #inc_gen ⇒ Object
-
#initialize ⇒ Grid
constructor
A new instance of Grid.
- #load_pattern(pattern, x, y) ⇒ Object
- #map(&block) ⇒ Object
- #neighbor_cells(*coords) ⇒ Object
- #next_generation_grid_struct ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Grid
Returns a new instance of Grid.
16 17 18 |
# File 'lib/grid.rb', line 16 def initialize @grid_struct = create_grid_struct(true) end |
Instance Attribute Details
#grid_struct ⇒ Object (readonly)
Returns the value of attribute grid_struct.
9 10 11 |
# File 'lib/grid.rb', line 9 def grid_struct @grid_struct end |
Instance Method Details
#create_grid_struct(randomize = false) ⇒ Object
28 29 30 31 32 33 34 |
# 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 ⇒ Object
51 52 53 54 55 56 |
# 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 ⇒ Object
40 41 42 |
# File 'lib/grid.rb', line 40 def inc_gen @grid_struct = next_generation_grid_struct end |
#load_pattern(pattern, x, y) ⇒ Object
20 21 22 |
# 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) ⇒ Object
36 37 38 |
# File 'lib/grid.rb', line 36 def map(&block) @grid_struct.map(&block) end |
#neighbor_cells(*coords) ⇒ Object
44 45 46 47 48 49 |
# 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 ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 ⇒ Object
24 25 26 |
# File 'lib/grid.rb', line 24 def reset @grid_struct = create_grid_struct(false) end |