summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authora14m <[email protected]>2020-11-29 21:19:31 +0100
committera14m <[email protected]>2020-11-29 23:29:41 +0100
commit8f88de4e158681f6321ecb2348005424108e2bd9 (patch)
tree3d898de350328438eeacea3bc2d9a1869f95b8c6
parent8436fb52f30718b595f8f39a88f063780f7deda9 (diff)
Add the basics for input generator
-rwxr-xr-xCLI/ruby/bin/game-of-life3
-rw-r--r--CLI/ruby/lib/game_of_life.rb1
-rw-r--r--CLI/ruby/lib/game_of_life/generators/input.rb35
3 files changed, 38 insertions, 1 deletions
diff --git a/CLI/ruby/bin/game-of-life b/CLI/ruby/bin/game-of-life
index 8abd1e0..4403598 100755
--- a/CLI/ruby/bin/game-of-life
+++ b/CLI/ruby/bin/game-of-life
@@ -32,7 +32,8 @@ class CLI < Thor
"seed" => rand(100_000),
}.merge(options)
- GameOfLife::Universe.new(options_with_defaults).run
+ universe = GameOfLife::Generators::Input.new(options_with_defaults)
+ p universe
end
class << self
diff --git a/CLI/ruby/lib/game_of_life.rb b/CLI/ruby/lib/game_of_life.rb
index a50a50f..c2e6845 100644
--- a/CLI/ruby/lib/game_of_life.rb
+++ b/CLI/ruby/lib/game_of_life.rb
@@ -6,6 +6,7 @@ require "game_of_life/universe"
module GameOfLife
class Error < StandardError; end
+ # Generators module for generating the universe initial state
module Generators
autoload :Input, "game_of_life/generators/input.rb"
autoload :Seed, "game_of_life/generators/seed.rb"
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