-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainscript.rb
executable file
·69 lines (53 loc) · 1.42 KB
/
mainscript.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require_relative 'lib/cellularautomata.rb'
require 'optparse'
#Simple script using Ruby's built in command line option parser.
options = {}
banner = ""
optparse = OptionParser.new do |opts|
File.open "usage" do |f|
opts.banner = banner = f.read
end
options[:generations] = 0
opts.on( '-g', '--generations N', Integer, 'Number of generations to be computed' ) do |g|
options[:generations] = g
end
opts.on( '-o', '--output [FILE]', String, 'Output file to which final generation is written' ) do |o|
options[:output] = o
end
options[:delay] = 0.8
opts.on( '-d', '--delay N', Float, 'Delay between printed generations in seconds -- try 0 < N < 1' ) do |d|
options[:delay] = d
end
opts.on( '-i', '--input [FILE]', String, 'Input .cel file' ) do |i|
options[:input] = i
end
options[:terse] = false
opts.on( '-t', '--terse', 'Do not print any output. For use with -o' ) do
options[:terse] = true
end
end
optparse.parse!
abort banner unless options[:input]
game = CellularAutomata.new
game.load_board options[:input]
if !options[:terse]
print game
else
puts "Working..."
time = Time.now
end
1.upto options[:generations] do
game.next_gen!
if !options[:terse]
sleep options[:delay]
print "\n"
print game
end
end
if options[:terse]
puts "Completed in #{Time.now - time} seconds."
end
if options[:output]
game.write_board options[:output]
end