diff options
| author | a14m <[email protected]> | 2020-11-29 21:19:31 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-11-29 23:29:41 +0100 |
| commit | 8f88de4e158681f6321ecb2348005424108e2bd9 (patch) | |
| tree | 3d898de350328438eeacea3bc2d9a1869f95b8c6 /CLI/ruby/lib/game_of_life/generators | |
| parent | 8436fb52f30718b595f8f39a88f063780f7deda9 (diff) | |
Add the basics for input generator
Diffstat (limited to 'CLI/ruby/lib/game_of_life/generators')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/generators/input.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb new file mode 100644 index 0000000..3965131 --- /dev/null +++ b/CLI/ruby/lib/game_of_life/generators/input.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "open-uri" + +module GameOfLife + module Generators + # Generate a new Universe/Board with a parsed local file or remote URL/file + module Input + # Generate a new Universe/Board with a parsed local file or remote URL/file + # @param options [Hash] CLI options to generate initial conditions + # @option options [String] "input" path to a local file or URL to open + # @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 [TODO] + def self.new(options) + # 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 + + # 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:]]/) } } + + # Generate a universe based on the parsed raw file and fill it with the cells + universe = GameOfLife::Universe.new(height: options["height"], width: options["width"]) + (0...options["height"].to_i).each do |i| + (0...options["width"].to_i).each do |j| + cell = ::GameOfLife::Cell.new(alive: raw.fetch(i, nil)&.fetch(j, nil)) + universe[i][j] = cell + end + end + return universe + end + end + end +end |
