-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.rb
76 lines (64 loc) · 2.67 KB
/
cli.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
70
71
72
73
74
75
76
require 'thor'
require 'life'
module Life
module CLI
class Base < Thor
include Thor::Actions
default_task :help
desc :new, "Create new game"
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", :required => true
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", :required => true
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", :required => true
method_option :seed, :type => :array, :aliases => '-s', :desc => "Initial Pattern", :required => true
def new
seed = build_seed(options[:seed])
max_gen = options[:generations]
world = World.new(options[:width], options[:height], seed)
display_simulation max_gen, options[:width], options[:height], world
rescue Interrupt
quit(options[:height])
end
desc :random, "Create new game with random starting pattern"
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", default: 30
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", default: 30
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", default: 100
def random
seed = build_random_seed options[:width], options[:height]
max_gen = options[:generations]
world = World.new(options[:width], options[:height], seed)
display_simulation max_gen, options[:width], options[:height], world
rescue Interrupt
quit(options[:height])
end
no_tasks do
def eol(height, curr_gen, max_gen)
curr_gen == max_gen ? "\n" : format("\e[1A" * (height+3) + "\r")
end
def quit(height)
puts ("\n"*(height+3))
puts "Exiting..."
puts "Thanks for trying out Conway's game of life!"
end
def build_seed(pattern)
pattern.map {|pair| pair.split(":").map { |coord| coord.to_i - 1 } }
end
def build_random_seed(width, height)
seed = []
seed_count = rand(1..(width*height/2))
seed_count.times do
seed << [rand(width-1), rand(height-1)]
end
seed
end
def display_simulation(max_gen, width, height, world)
(1..max_gen).each do |gen|
world_data = "==============\nGeneration:#{gen}/#{max_gen}\nWidth:#{width}\nHeight: #{height}\n"
print world_data + world.to_s("@", " ") + eol(height, gen, max_gen)
sleep(0.1)
world.tick
end
end
end
end
end
end