life 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,17 +12,17 @@ To use Life, run the `life new` command. It can be used with the following optio
12
12
  * `--width` or `-w` specifies how many cells wide the game will be
13
13
  * `--height` or `-h` specifies how many cells high the game will be
14
14
  * `--generations` or `-g` specifies how many generations the simulation will run
15
+ * '--seed' or '-s' is the initial pattern of the game. Pass coordinate pairs using the following pattern: `x:y x:y x:y ...`
15
16
 
16
17
  ```
17
18
  $ life new -w 3 -h 4 -g 5
18
19
  ```
19
20
  This will create a 3x4 game that runs for 5 generations
20
21
 
21
- When first creating a game, you will be prompted to enter which cells are initially alive.
22
+ When first creating a game, you will be prompted to add cells some live cells.
22
23
 
23
24
  ## TODO
24
- * Ability to pass initial configuration as command-line option
25
- * Animate instead of displaying all the generations
25
+ * Add some default configurations to start the fun
26
26
 
27
27
  ## Contributing
28
28
 
data/lib/life/cli.rb CHANGED
@@ -12,32 +12,56 @@ 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
16
 
16
17
  def new
17
- seed = []
18
- while yes? "Would you like to add a live cell to the grid? [Y/n]"
19
- x = ask "Please enter x coordinate"
20
- y = ask "Please enter y coordinate"
21
- seed << [(x.to_i) - 1, (y.to_i) -1]
22
- end
23
-
24
- world = World.new options[:width], options[:height], seed
18
+ seed = build_seed(options[:seed])
19
+ max_gen = options[:generations]
20
+ world = build_world(options[:width], options[:height], seed)
25
21
 
26
- (1..options[:generations]).each do |gen|
27
- puts "== Generation #{gen} =="
28
- draw(world)
29
- puts ""
22
+ (1..max_gen).each do |gen|
23
+ print world.to_s("@", "_") + eol(options[:height], gen, max_gen)
24
+ sleep(1)
30
25
  world.tick
31
26
  end
32
27
  end
33
28
 
34
- def draw(world)
35
- world.current.each do |row|
36
- processed_row = row.map { |cell| cell.live? ? "@" : "_"}
37
- print "["
38
- print processed_row.join(' ')
39
- puts "]"
29
+ no_tasks do
30
+ def eol(height, curr_gen, max_gen)
31
+ curr_gen == max_gen ? "\n" : format("\e[1A" * (height-1) + "\r")
32
+ end
33
+
34
+ def build_seed(pattern)
35
+ pattern.map {|pair| pair.split(":").map { |coord| coord.to_i - 1 } }
36
+ end
37
+
38
+ def build_world(width, height, seed=[])
39
+ while yes? "Would you like to add a live cell to the grid? [Y/n]"
40
+ x = get_x(width)
41
+ y = get_y(height)
42
+ seed << [(x.to_i) - 1, (y.to_i) -1]
43
+ end
44
+ World.new height, width, seed
40
45
  end
46
+
47
+ def get_x(width)
48
+ x = ask("Please enter x coordinate").to_i
49
+ while x > width
50
+ puts "This number is too big"
51
+ x = ask("Please enter x coordinate").to_i
52
+ end
53
+ return x
54
+ 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
+
41
65
  end
42
66
 
43
67
  end
data/lib/life/grid.rb CHANGED
@@ -23,6 +23,13 @@ class Grid < Array
23
23
  count
24
24
  end
25
25
 
26
+ def to_s(live, dead)
27
+ rows = map do |row|
28
+ row.map { |cell| cell.live? ? live : dead }.join(" ")
29
+ end
30
+ rows.join("\n")
31
+ end
32
+
26
33
  private
27
34
  def x_range(x)
28
35
  west = x - 1 < 0 ? 0 : x - 1
data/lib/life/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Life
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/life/world.rb CHANGED
@@ -25,4 +25,8 @@ class World
25
25
  end
26
26
  @current = @next.deep_copy
27
27
  end
28
+
29
+ def to_s(live, dead)
30
+ @current.to_s(live, dead)
31
+ end
28
32
  end
@@ -15,6 +15,13 @@ describe Grid do
15
15
  end
16
16
  end
17
17
 
18
+ describe "#to_s" do
19
+ let(:grid) { World.new(3,3, [[0,0], [1,2], [2,1]])}
20
+ it "should output the correct string" do
21
+ grid.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _"
22
+ end
23
+ end
24
+
18
25
  describe "#deep_copy" do
19
26
  let(:grid) { Grid.new(3,4) }
20
27
  it "should create a distinct copy" do
@@ -17,6 +17,13 @@ describe World do
17
17
  end
18
18
  end
19
19
 
20
+ describe "to_s" do
21
+ let(:world) { World.new(3,3, [[0,0], [1,2], [2,1]])}
22
+ it "should output the correct string" do
23
+ world.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _"
24
+ end
25
+ end
26
+
20
27
  describe "Rules" do
21
28
  before { world.tick }
22
29
  subject { world.current[1][1] }
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: 0.2.0
4
+ version: 1.0.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-18 00:00:00.000000000 Z
12
+ date: 2012-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor