life 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -5
- data/lib/life/cli.rb +25 -29
- data/lib/life/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -15,14 +15,17 @@ To use Life, run the `life new` command. It can be used with the following optio
|
|
15
15
|
* '--seed' or '-s' is the initial pattern of the game. Pass coordinate pairs using the following pattern: `x:y x:y x:y ...`
|
16
16
|
|
17
17
|
```
|
18
|
-
$ life new -w 3 -h 4 -g 5
|
18
|
+
$ life new -w 3 -h 4 -g 5 -s 3:2 1:1
|
19
19
|
```
|
20
|
-
This will create a 3x4 game that runs for 5 generations
|
20
|
+
This will create a 3x4 game that runs for 5 generations. The live cells in the starting pattern are `3,2` and `1,1`
|
21
21
|
|
22
|
-
|
22
|
+
## Pre-built configurations
|
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.
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
+
```
|
27
|
+
$ life random -w 10 -h 10 -g 5
|
28
|
+
```
|
26
29
|
|
27
30
|
## Contributing
|
28
31
|
|
data/lib/life/cli.rb
CHANGED
@@ -12,18 +12,25 @@ module Life
|
|
12
12
|
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", :required => true
|
13
13
|
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", :required => true
|
14
14
|
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", :required => true
|
15
|
-
method_option :seed, :type => :array, :aliases => '-s', :desc => "Initial Pattern"
|
15
|
+
method_option :seed, :type => :array, :aliases => '-s', :desc => "Initial Pattern", :required => true
|
16
16
|
|
17
17
|
def new
|
18
18
|
seed = build_seed(options[:seed])
|
19
19
|
max_gen = options[:generations]
|
20
|
-
world =
|
20
|
+
world = World.new(options[:height], options[:width], seed)
|
21
|
+
display_simulation max_gen, options[:height], world
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
desc :random, "Create new game with random starting pattern"
|
25
|
+
method_option :width, :type => :numeric, :aliases => '-w', :desc => "Width of board", :required => true
|
26
|
+
method_option :height, :type => :numeric,:aliases => '-h', :desc => "Height of board", :required => true
|
27
|
+
method_option :generations, :type => :numeric,:aliases => '-g', :desc => "How many generations to display", :required => true
|
28
|
+
|
29
|
+
def random
|
30
|
+
seed = build_random_seed options[:width], options[:height]
|
31
|
+
max_gen = options[:generations]
|
32
|
+
world = World.new(options[:height], options[:width], seed)
|
33
|
+
display_simulation max_gen, options[:height], world
|
27
34
|
end
|
28
35
|
|
29
36
|
no_tasks do
|
@@ -35,33 +42,22 @@ module Life
|
|
35
42
|
pattern.map {|pair| pair.split(":").map { |coord| coord.to_i - 1 } }
|
36
43
|
end
|
37
44
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
seed << [(
|
45
|
+
def build_random_seed(width, height)
|
46
|
+
seed = []
|
47
|
+
seed_count = rand(1..(width*height/2))
|
48
|
+
seed_count.times do
|
49
|
+
seed << [rand(height-1), rand(width-1)]
|
43
50
|
end
|
44
|
-
|
51
|
+
seed
|
45
52
|
end
|
46
53
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
54
|
+
def display_simulation(max_gen, height, world)
|
55
|
+
(1..max_gen).each do |gen|
|
56
|
+
print world.to_s("@", "_") + eol(height, gen, max_gen)
|
57
|
+
sleep(1)
|
58
|
+
world.tick
|
52
59
|
end
|
53
|
-
return x
|
54
60
|
end
|
55
|
-
|
56
|
-
def get_y(height)
|
57
|
-
y = ask("Please enter y coordinate").to_i
|
58
|
-
while y > height
|
59
|
-
puts "This number is too big"
|
60
|
-
y = ask("Please enter y coordinate").to_i
|
61
|
-
end
|
62
|
-
return y
|
63
|
-
end
|
64
|
-
|
65
61
|
end
|
66
62
|
|
67
63
|
end
|
data/lib/life/version.rb
CHANGED