Class: SampleLifeModel
- Inherits:
-
Object
- Object
- SampleLifeModel
- Defined in:
- lib/life_game_viewer/model/sample_life_model.rb
Overview
Object that contains and serves (stores and retrieves) living/dead states in the matrix.
All public methods here should be responded to in alternate model implementations.
See Wikipedia for more information about Conway’s Game of Life.
Rules distilled:
1) Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2) Any live cell with two or three live neighbours lives on to the next generation. 3) Any live cell with more than three live neighbours dies, as if by overcrowding. 4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.create(row_count, column_count) ⇒ Object
This method will create a model with the specified number of rows and columns.
-
.create_from_string(string) ⇒ Object
Creates a LifeModel instance whose size and values are specified in the passed string.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #alive?(row, col) ⇒ Boolean
- #column_count ⇒ Object
-
#initialize(row_count, column_count) ⇒ SampleLifeModel
constructor
Creates an instance with the specified number of rows and columns.
- #next_generation_model ⇒ Object
- #number_living ⇒ Object
- #row_count ⇒ Object
- #set_living_state(row, col, alive) ⇒ Object
- #set_living_states(array_of_row_col_tuples, alive) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(row_count, column_count) ⇒ SampleLifeModel
Creates an instance with the specified number of rows and columns. All values are initialized to false.
25 26 27 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 25 def initialize(row_count, column_count) @data = create_data(row_count, column_count) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
20 21 22 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 20 def data @data end |
Class Method Details
.create(row_count, column_count) ⇒ Object
This method will create a model with the specified number of rows and columns. If a block is passed it will be used to initialize the alive/dead values; the block should take params row and column number.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 54 def self.create(row_count, column_count) model = new(row_count, column_count) if block_given? (0...row_count).each do |row| (0...column_count).each do |col| model.set_living_state(row, col, yield(row, col)) end end end model end |
.create_from_string(string) ⇒ Object
Creates a LifeModel instance whose size and values are specified in the passed string. Rows must be delimited by “n”. The ‘*’ character represents alive, and the hyphen signifies dead.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 33 def self.create_from_string(string) row_count = string.chomp.count("\n") + 1 lines = string.split("\n") col_count = lines.first.size model = new(row_count, col_count) (0...row_count).each do |row| line = lines[row] (0...(line.size)).each do |col| ch = line[col] alive = (ch == '*') model.set_living_state(row, col, alive) end end model end |
Instance Method Details
#==(other) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 105 def ==(other) other.is_a?(self.class) && other.row_count == row_count && other.column_count == column_count && other.data == data end |
#alive?(row, col) ⇒ Boolean
77 78 79 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 77 def alive?(row, col) @data[row][col] end |
#column_count ⇒ Object
72 73 74 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 72 def column_count @data[0].size end |
#next_generation_model ⇒ Object
95 96 97 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 95 def next_generation_model LifeCalculator.new.next_generation(self) end |
#number_living ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 113 def number_living num = 0 (0...row_count).each do |row| (0...column_count).each do |col| num += 1 if alive?(row, col) end end num end |
#row_count ⇒ Object
67 68 69 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 67 def row_count @data.size end |
#set_living_state(row, col, alive) ⇒ Object
82 83 84 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 82 def set_living_state(row, col, alive) @data[row][col] = alive end |
#set_living_states(array_of_row_col_tuples, alive) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 87 def set_living_states(array_of_row_col_tuples, alive) array_of_row_col_tuples.each do |row_col_tuple| row, col = row_col_tuple set_living_state(row, col, alive) end end |
#to_s ⇒ Object
100 101 102 |
# File 'lib/life_game_viewer/model/sample_life_model.rb', line 100 def to_s super.to_s + ": #{row_count} rows, #{column_count} column_count, #{number_living} alive." end |