Skip to content

Commit

Permalink
Merge pull request #5 from JoelQ/minor-improvements
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
Joel Quenneville committed Sep 21, 2012
2 parents 92b1588 + a5ef7fa commit 1dc3281
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -21,11 +21,15 @@ This will create a 3x4 game that runs for 5 generations. The live cells in the s

## Pre-built configurations
### Random
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.
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.

```
$ life random -w 10 -h 10 -g 5
```
or
```
$ life random
```

## Contributing

Expand Down
35 changes: 23 additions & 12 deletions lib/life/cli.rb
Expand Up @@ -17,25 +17,35 @@ class Base < Thor
def new
seed = build_seed(options[:seed])
max_gen = options[:generations]
world = World.new(options[:height], options[:width], seed)
display_simulation max_gen, options[:height], world
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", :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 :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[:height], options[:width], seed)
display_simulation max_gen, options[:height], world
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-1) + "\r")
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)
Expand All @@ -46,15 +56,16 @@ def build_random_seed(width, height)
seed = []
seed_count = rand(1..(width*height/2))
seed_count.times do
seed << [rand(height-1), rand(width-1)]
seed << [rand(width-1), rand(height-1)]
end
seed
end

def display_simulation(max_gen, height, world)
def display_simulation(max_gen, width, height, world)
(1..max_gen).each do |gen|
print world.to_s("@", "_") + eol(height, gen, max_gen)
sleep(1)
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
Expand Down
3 changes: 2 additions & 1 deletion lib/life/grid.rb
Expand Up @@ -24,7 +24,8 @@ def live_neighbor_count_for(x, y)
end

def to_s(live, dead)
rows = map do |row|
array_of_rows = self.transpose
rows = array_of_rows.map do |row|
row.map { |cell| cell.live? ? live : dead }.join(" ")
end
rows.join("\n")
Expand Down
2 changes: 1 addition & 1 deletion lib/life/version.rb
@@ -1,3 +1,3 @@
module Life
VERSION = "1.1.0"
VERSION = "1.2.0"
end
4 changes: 2 additions & 2 deletions spec/life/grid_spec.rb
Expand Up @@ -16,9 +16,9 @@
end

describe "#to_s" do
let(:grid) { World.new(3,3, [[0,0], [1,2], [2,1]])}
let(:grid) { World.new(3,4, [[0,0], [1,2], [2,1]])}
it "should output the correct string" do
grid.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _"
grid.to_s("@", "_").should eq "@ _ _\n_ _ @\n_ @ _\n_ _ _"
end
end

Expand Down

0 comments on commit 1dc3281

Please sign in to comment.