Class: Grid
- Inherits:
-
Object
- Object
- Grid
- Defined in:
- lib/ruby-life.rb
Instance Attribute Summary collapse
-
#grid_size ⇒ Object
Returns the value of attribute grid_size.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #cell_separator ⇒ Object
- #dead_row ⇒ Object
- #display ⇒ Object
- #find_neighbors(i, j) ⇒ Object
- #index_array ⇒ Object
-
#initialize(input_file = nil, input_state = []) ⇒ Grid
constructor
A new instance of Grid.
- #load(input_file) ⇒ Object
- #make_cells(input_hash) ⇒ Object
- #modular_indices(i, j) ⇒ Object
- #next(timestep) ⇒ Object
- #pad_boundaries! ⇒ Object
- #save(output_file) ⇒ Object
Constructor Details
#initialize(input_file = nil, input_state = []) ⇒ Grid
Returns a new instance of Grid.
44 45 46 47 48 |
# File 'lib/ruby-life.rb', line 44 def initialize(input_file = nil, input_state = []) @state = input_state load input_file if input_file @grid_size = state.size end |
Instance Attribute Details
#grid_size ⇒ Object
Returns the value of attribute grid_size.
42 43 44 |
# File 'lib/ruby-life.rb', line 42 def grid_size @grid_size end |
#state ⇒ Object
Returns the value of attribute state.
42 43 44 |
# File 'lib/ruby-life.rb', line 42 def state @state end |
Instance Method Details
#cell_separator ⇒ Object
126 127 128 |
# File 'lib/ruby-life.rb', line 126 def cell_separator ' ' end |
#dead_row ⇒ Object
86 87 88 |
# File 'lib/ruby-life.rb', line 86 def dead_row Array.new(grid_size) { Cell.new('dead') } end |
#display ⇒ Object
121 122 123 124 |
# File 'lib/ruby-life.rb', line 121 def display state.each { |row| puts row.join cell_separator } puts end |
#find_neighbors(i, j) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/ruby-life.rb', line 101 def find_neighbors(i, j) neighbors = index_array.map do |k,l| k_index, l_index = modular_indices (i+k), (j+l) state[k_index][l_index] end neighbors.flatten.map(&:to_i).inject(:+) end |
#index_array ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/ruby-life.rb', line 109 def index_array [ [-1,-1], [-1,0], [-1, 1], # cells above self [0, -1], [0, 1], # cells left and right of self [1, -1], [1, 0], [1,1] # cells below self ] end |
#load(input_file) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/ruby-life.rb', line 50 def load(input_file) input_hash = File.open(input_file) do |f| input_json = f.read JSON.parse input_json end make_cells input_hash end |
#make_cells(input_hash) ⇒ Object
70 71 72 73 74 |
# File 'lib/ruby-life.rb', line 70 def make_cells(input_hash) input_hash.each do |col, row| state << row.map { |cell| Cell.new cell } end end |
#modular_indices(i, j) ⇒ Object
117 118 119 |
# File 'lib/ruby-life.rb', line 117 def modular_indices(i, j) [i, j].map { |index| index % grid_size } end |
#next(timestep) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby-life.rb', line 90 def next(timestep) timestep.times do state.each.with_index do |row, i| row.each.with_index do |cell, j| cell.neighbors = find_neighbors i, j end end state.flatten.each { |cell| cell.update } end end |
#pad_boundaries! ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby-life.rb', line 76 def pad_boundaries! state.map do |row| row.unshift Cell.new('dead') row.push Cell.new('dead') self.grid_size = row.size end self.state = state.unshift dead_row self.state = state.push dead_row end |
#save(output_file) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby-life.rb', line 58 def save(output_file) output_hash = {} state.each.with_index(1) do |row, index| output_hash["row_#{index}"] = row.map do |cell| cell.to_sym end end File.open(output_file, 'w') do |f| f.write output_hash.to_json end end |