Sha256: 8f2905ae9078c1ec6444c55c12c753ba5f7838c495713c7905c7338fa0e38d3f

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Conwaymp
  class Game
    attr_accessor :board

    def initialize
      @errors = []

      @options = Options.new(parse_options).validate

      if @options.success?
        @board = Board.new(@options)
      else
        display_errors(@options.errors)
      end
    end

    def play
      loop do
        @board.display
        @board.tick
      end
    end

    def parse_options
      @options = Slop.parse suppress_errors: true do |o|
        o.integer '-r', '--rows', 'Rows. Default: 20', default: 20
        o.integer '-c', '--cols', 'Columns. Default: 20', default: 20
        o.string  '-l', '--load', 'Path to board state to load'
        o.string  '-s', '--seed', 'Load a seed'
        o.on      '-ls', '--seeds', 'List available seeds' do
          puts 'Available Seeds:'
          puts Conwaymp::Game.available_seeds
          exit
        end
        o.on '--version', 'print the version' do
          puts Conwaymp::VERSION
          exit
        end
        o.on '--help' do
          puts o
          exit
        end
      end
    end

    def display_errors errors
      errors = errors.map do |k,e|
        "#{k}: #{e.join(',')}".colorize(:red)
      end
      puts errors
      puts parse_options
      exit
    end

    def self.available_seeds
      Dir.glob(File.expand_path('../../../seeds/*', __FILE__)).map do |f|
        File.basename(f)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conwaymp-0.2.0 lib/conwaymp/game.rb