Class: Conwaymp::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/conwaymp/game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/conwaymp/game.rb', line 7

def initialize
  @errors = []

  @options = Options.new(parse_options).validate

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

Instance Attribute Details

#boardObject

Returns the value of attribute board.



5
6
7
# File 'lib/conwaymp/game.rb', line 5

def board
  @board
end

Class Method Details

.available_seedsObject



57
58
59
60
61
# File 'lib/conwaymp/game.rb', line 57

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

Instance Method Details

#display_errors(errors) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/conwaymp/game.rb', line 48

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

#parse_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/conwaymp/game.rb', line 26

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

#playObject



19
20
21
22
23
24
# File 'lib/conwaymp/game.rb', line 19

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