Module: GameOfLife::Generators::Seed
- Defined in:
- lib/game_of_life/generators/seed.rb
Overview
Generate a new Universe/Board from a seed number
Class Method Summary collapse
-
.new(options) ⇒ GameOfLife::Universe
Generate a new Universe/Board from a seed number.
Instance Method Summary collapse
-
#generate_seed_data(seed:, width:, height:) ⇒ Array<Array<True|False>>
private
Generate the seeding data for populating the Universe.
-
#populate(input:, width:, height:) ⇒ GameOfLife::Universe
private
Populate the Universe with reference to the parsed input 2D array (True/False).
Class Method Details
.new(options) ⇒ GameOfLife::Universe
Generate a new Universe/Board from a seed number
16 17 18 19 20 21 22 |
# File 'lib/game_of_life/generators/seed.rb', line 16 def new() width = ["width"].to_i height = ["height"].to_i seed = ["seed"].to_i input = generate_seed_data(seed: seed, width: width, height: height) populate(input: input, width: width, height: height) end |
Instance Method Details
#generate_seed_data(seed:, width:, height:) ⇒ Array<Array<True|False>> (private)
Generate the seeding data for populating the Universe
31 32 33 34 35 36 37 |
# File 'lib/game_of_life/generators/seed.rb', line 31 private def generate_seed_data(seed:, width:, height:) # Create a pseudo(stable) random generator from the seed # Generate a sequence of bytes to cover the Game of Life Universe plane # each byte can be unpacked into binary data string (ex. "01101011") raw = Random.new(seed).bytes(width * height).unpack1("B*").chars.each_slice(width) raw.map { |row| row.map { |char| char.eql?("1") } } end |
#populate(input:, width:, height:) ⇒ GameOfLife::Universe (private)
Populate the Universe with reference to the parsed input 2D array (True/False)
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/game_of_life/generators/seed.rb', line 45 private def populate(input:, width:, height:) # Generate a universe based on the parsed raw file and fill it with the cells universe = GameOfLife::Universe.new(height: height, width: width) (0...height).each do |i| (0...width).each do |j| cell = { x: j, y: i, alive: input.fetch(i, nil)&.fetch(j, nil) } universe[i][j] = ::GameOfLife::Cell.new(**cell) end end universe end |