# 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 class << self # 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 [GameOfLife::Universe] populated with the parsed input # @see ::GameOfLife::Generators::Input.populate def 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:]]/) } } populate(parsed_input: raw, width: options["width"].to_i, height: options["height"].to_i) end # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False) # @param parsed_input [Array>] # @param widht [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:) # 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) } universe[i][j] = ::GameOfLife::Cell.new(cell) end end universe end end end end end