life 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -1
- data/lib/life/cli.rb +23 -12
- data/lib/life/grid.rb +2 -1
- data/lib/life/version.rb +1 -1
- data/spec/life/grid_spec.rb +2 -2
- metadata +2 -2
data/README.md
CHANGED
@@ -21,11 +21,15 @@ This will create a 3x4 game that runs for 5 generations. The live cells in the s
|
|
21
21
|
|
22
22
|
## Pre-built configurations
|
23
23
|
### Random
|
24
|
-
You can use a random initial pattern if you don't feel like building one yourself. Options are the same as for the new method except that no seed is passed.
|
24
|
+
You can use a random initial pattern if you don't feel like building one yourself. Options are the same as for the new method except that no seed is passed. All of the options are optional. If you don't pass in a value it will default to a 30x30 grid for 100 generations.
|
25
25
|
|
26
26
|
```
|
27
27
|
$ life random -w 10 -h 10 -g 5
|
28
28
|
```
|
29
|
+
or
|
30
|
+
```
|
31
|
+
$ life random
|
32
|
+
```
|
29
33
|
|
30
34
|
## Contributing
|
31
35
|
|
data/lib/life/cli.rb
CHANGED
@@ -17,25 +17,35 @@ module Life
|
|
17
17
|
def new
|
18
18
|
seed = build_seed(options[:seed])
|
19
19
|
max_gen = options[:generations]
|
20
|
-
world = World.new(options[:
|
21
|
-
display_simulation max_gen, options[:height], world
|
20
|
+
world = World.new(options[:width], options[:height], seed)
|
21
|
+
display_simulation max_gen, options[:width], options[:height], world
|
22
|
+
rescue Interrupt
|
23
|
+
quit(options[:height])
|
22
24
|
end
|
23
25
|
|
24
26
|
desc :random, "Create new game with random starting pattern"
|
25
|
-
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", :
|
26
|
-
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", :
|
27
|
-
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", :
|
27
|
+
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", default: 30
|
28
|
+
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", default: 30
|
29
|
+
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", default: 100
|
28
30
|
|
29
31
|
def random
|
30
32
|
seed = build_random_seed options[:width], options[:height]
|
31
33
|
max_gen = options[:generations]
|
32
|
-
world = World.new(options[:
|
33
|
-
display_simulation max_gen, options[:height], world
|
34
|
+
world = World.new(options[:width], options[:height], seed)
|
35
|
+
display_simulation max_gen, options[:width], options[:height], world
|
36
|
+
rescue Interrupt
|
37
|
+
quit(options[:height])
|
34
38
|
end
|
35
39
|
|
36
40
|
no_tasks do
|
37
41
|
def eol(height, curr_gen, max_gen)
|
38
|
-
curr_gen == max_gen ? "\n" : format("\e[1A" * (height
|
42
|
+
curr_gen == max_gen ? "\n" : format("\e[1A" * (height+3) + "\r")
|
43
|
+
end
|
44
|
+
|
45
|
+
def quit(height)
|
46
|
+
puts ("\n"*(height+3))
|
47
|
+
puts "Exiting..."
|
48
|
+
puts "Thanks for trying out Conway's game of life!"
|
39
49
|
end
|
40
50
|
|
41
51
|
def build_seed(pattern)
|
@@ -46,15 +56,16 @@ module Life
|
|
46
56
|
seed = []
|
47
57
|
seed_count = rand(1..(width*height/2))
|
48
58
|
seed_count.times do
|
49
|
-
seed << [rand(
|
59
|
+
seed << [rand(width-1), rand(height-1)]
|
50
60
|
end
|
51
61
|
seed
|
52
62
|
end
|
53
63
|
|
54
|
-
def display_simulation(max_gen, height, world)
|
64
|
+
def display_simulation(max_gen, width, height, world)
|
55
65
|
(1..max_gen).each do |gen|
|
56
|
-
|
57
|
-
|
66
|
+
world_data = "==============\nGeneration:#{gen}/#{max_gen}\nWidth:#{width}\nHeight: #{height}\n"
|
67
|
+
print world_data + world.to_s("@", " ") + eol(height, gen, max_gen)
|
68
|
+
sleep(0.1)
|
58
69
|
world.tick
|
59
70
|
end
|
60
71
|
end
|
data/lib/life/grid.rb
CHANGED
data/lib/life/version.rb
CHANGED
data/spec/life/grid_spec.rb
CHANGED
@@ -16,9 +16,9 @@ describe Grid do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "#to_s" do
|
19
|
-
let(:grid) { World.new(3,
|
19
|
+
let(:grid) { World.new(3,4, [[0,0], [1,2], [2,1]])}
|
20
20
|
it "should output the correct string" do
|
21
|
-
grid.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _"
|
21
|
+
grid.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _\n_ _ _"
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: life
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: thor
|