diff options
| author | a14m <[email protected]> | 2020-12-10 21:31:16 +0000 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-12-10 21:31:16 +0000 |
| commit | fe3c74d73728de8c732af6982aad2d19e3f61373 (patch) | |
| tree | 35f07cdcf823d03316e803d79bd25d8a7272a378 /CLI/ruby/lib | |
| parent | b4e9a712549dba1326aa680157574e946e1faa85 (diff) | |
| parent | 88ec97f698f5ba68ad2a4be0f45cefa24a3f8934 (diff) | |
Merge branch 'feature/ruby/seed' into 'master'
Feature/ruby/seed
See merge request a14m/game-of-life!6
Diffstat (limited to 'CLI/ruby/lib')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/generators/input.rb | 23 | ||||
| -rw-r--r-- | CLI/ruby/lib/game_of_life/generators/seed.rb | 41 | ||||
| -rw-r--r-- | CLI/ruby/lib/game_of_life/version.rb | 1 |
3 files changed, 59 insertions, 6 deletions
diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb index 79626d4..bb43e3d 100644 --- a/CLI/ruby/lib/game_of_life/generators/input.rb +++ b/CLI/ruby/lib/game_of_life/generators/input.rb @@ -14,15 +14,27 @@ module GameOfLife # @option options [Numeric] "width" (floored) and used as the width of the universe # @option options [Numeric] "height" (floored) and used as the height of the universe # @return [GameOfLife::Universe] populated with the parsed input + # @see ::GameOfLife::Generators::Input.generate_input_data # @see ::GameOfLife::Generators::Input.populate def new(options) + width = options["width"].to_i + height = options["height"].to_i + uri = options["input"] + input = generate_input_data(uri: uri) + populate(input: input, width: width, height: height) + end + + # Generate the seeding data for populating the {GameOfLife::Universe} from input URI + # @param uri [String] inputh path to local file or URL to open + # @return [Array<Array<True|False>>] covering the defined size of universe + # @see ::GameOfLife::Generators::Input.populate + private def generate_input_data(uri:) # By design, it's intentional that we use URI.open to to allow seamless file and url access - raw = URI.open(options["input"]).read.split("\n") # rubocop:disable Security/Open + raw = URI.open(uri).read.split("\n") # rubocop:disable Security/Open # Parse the file and convert it to a boolean 2D array # where any alpha numeric character is considered a living cell (true) raw.map! { |row| row.chars.map { |char| char.match?(/[[:alnum:]]/) } } - populate(parsed_input: raw, width: options["width"].to_i, height: options["height"].to_i) rescue ::OpenURI::HTTPError, ::SocketError raise GameOfLife::Error, "URL isn't avaialable, please check it again and make sure the URL is correct" rescue ::Errno::ENOENT @@ -31,16 +43,17 @@ module GameOfLife end # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False) - # @param parsed_input [Array<Array<True|False>>] + # @param input [Array<Array<True|False>>] # @param width [Integer] width of the generated cyclic universe # @param height [Integer] height of the generated cyclic universe # @return [GameOfLife::Universe] populated with the parsed input - private def populate(parsed_input:, width:, height:) + # @see ::GameOfLife::Generators::Input.generate_input_data + 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: parsed_input.fetch(i, nil)&.fetch(j, nil) } + cell = { x: j, y: i, alive: input.fetch(i, nil)&.fetch(j, nil) } universe[i][j] = ::GameOfLife::Cell.new(cell) end end diff --git a/CLI/ruby/lib/game_of_life/generators/seed.rb b/CLI/ruby/lib/game_of_life/generators/seed.rb index e64a28f..d991efe 100644 --- a/CLI/ruby/lib/game_of_life/generators/seed.rb +++ b/CLI/ruby/lib/game_of_life/generators/seed.rb @@ -11,8 +11,47 @@ module GameOfLife # @option options [Numeric] "width" (floored) and used as the width of the universe # @option options [Numeric] "height" (floored) and used as the height of the universe # @return [GameOfLife::Universe] populated with the parsed input + # @see ::GameOfLife::Generators::Seed.generate_seed_data + # @see ::GameOfLife::Generators::Seed.populate def new(options) - fail ::NotImplementedError, "Seed option isn't implemented yet, please use --input option" + width = options["width"].to_i + height = options["height"].to_i + seed = options["seed"].to_i + input = generate_seed_data(seed: seed, width: width, height: height) + populate(input: input, width: width, height: height) + end + + # Generate the seeding data for populating the {GameOfLife::Universe} + # @param seed [Integer] used to generate {Array<"0"|"1">} using Std ruby Random + # @param width [Integer] width of the generated cyclic universe + # @param height [Integer] height of the generated cyclic universe + # @return [Array<Array<True|False>>] covering the defined size of universe + # @see ::GameOfLife::Generators::Seed.populate + # @see https://ruby-doc.org/core-2.4.0/Random.html + 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*").split("").each_slice(width) + raw.map { |row| row.map { |char| char.eql?("1") } } + end + + # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False) + # @param input [Array<Array<True|False>>] + # @param width [Integer] width of the generated cyclic universe + # @param height [Integer] height of the generated cyclic universe + # @return [GameOfLife::Universe] populated with the parsed input + # @see ::GameOfLife::Generators::Seed.generate_seed_data + 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 end end diff --git a/CLI/ruby/lib/game_of_life/version.rb b/CLI/ruby/lib/game_of_life/version.rb index 5fd800a..1c9f528 100644 --- a/CLI/ruby/lib/game_of_life/version.rb +++ b/CLI/ruby/lib/game_of_life/version.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true module GameOfLife + # Game of life version number VERSION = "0.1.2" end |
