Skip to content

Commit

Permalink
added some tests for the cell and for the conways class also, put eve…
Browse files Browse the repository at this point in the history
…rything in namespace etc
  • Loading branch information
skorks committed Dec 22, 2010
1 parent 6caaf9b commit 995030a
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 169 deletions.
19 changes: 10 additions & 9 deletions bin/conways
Expand Up @@ -23,13 +23,14 @@ dead cells, i.e. all the edges of the grid should be made
up of zeros (dead cells) up of zeros (dead cells)
EOS EOS
end end
Conways::Life.new(ARGV.join("")).live


begin #begin
input = eval(ARGV[0]) #input = eval(ARGV[0])
life = Conways::Life.new(input) #life = Conways::Life.new(input)
life.live #life.live
rescue StandardError => e #rescue StandardError => e
puts "Your input was invalid somehow: #{e}" #puts "Your input was invalid somehow: #{e}"
rescue SyntaxError => e #rescue SyntaxError => e
puts "Your input didn't have correct syntax: #{e}" #puts "Your input didn't have correct syntax: #{e}"
end #end
65 changes: 57 additions & 8 deletions lib/conways.rb
@@ -1,17 +1,72 @@
require 'conways/cell'
require 'conways/grid' require 'conways/grid'


module Conways module Conways
class Life class Life
#some possible inputs
#[[0,0,0,0,0,0], [0,0,1,1,1,0], [0,1,1,1,0,0], [0,0,0,0,0,0]]
#[[0,0,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,1,1,1,0], [0,0,0,0,0]]
#[[0,0,0,0,0,0,0], [0,1,0,0,1,0,0],[0,0,0,0,0,1,0],[0,1,0,0,0,1,0],[0,0,1,1,1,1,0],[0,0,0,0,0,0,0]]
CROSS = [[0,0,0,0,0], [0,1,1,1,0], [0,0,0,0,0]]
ACORN = [[0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0],[0,0,0,0,1,0,0,0,0],[0,1,1,0,0,1,1,1,0],[0,0,0,0,0,0,0,0,0]]

def initialize(input) def initialize(input)
@input = input input = input.gsub(/\s+/,"")
begin
@input = (input.class == String ? eval(input) : input)
rescue StandardError => e
raise "Your input was invalid somehow: #{e}"
rescue SyntaxError => e
raise "Your input didn't have correct syntax: #{e}"
end
raise "Input must be a two dimensional array" unless two_dimensional? @input
raise "Input must only contain 0 or 1 as values" unless cell_values_correct? @input
raise "Input rows must all be of the same size" unless even_rows? @input

@exit = false @exit = false
trap("SIGINT") do trap("SIGINT") do
@exit = true @exit = true
end end
end end


def two_dimensional?(input)
input.each do |row|
return false unless row.class == Array
end
true
end

def cell_values_correct?(input)
input.each do |row|
row.each do |cell|
return false unless Conways::Cell::LIVE || Conways::Cell::DEAD
end
end
true
end

def even_rows?(input)
current_row_size = nil
input.each do |row|
return false if current_row_size && current_row_size != row.size
current_row_size = row.size
end
true
end

#def live
#grid = InputToGrid.convert @input
#loop do
#@output_writer.write grid
#grid.tick
#sleep(1)
#exit if @exit
#end
#@output_writer.write grid
#end

def live def live
grid = CellGrid.build_from(@input) grid = Grid.from_input @input
loop do loop do
puts grid.to_s puts grid.to_s
puts puts
Expand All @@ -25,9 +80,3 @@ def live
end end
end end


#some possible inputs
#[[0,0,0,0,0], [0,1,1,1,0], [0,0,0,0,0]]
#[[0,0,0,0,0,0], [0,0,1,1,1,0], [0,1,1,1,0,0], [0,0,0,0,0,0]]
#[[0,0,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,1,1,1,0], [0,0,0,0,0]]
#[[0,0,0,0,0,0,0], [0,1,0,0,1,0,0],[0,0,0,0,0,1,0],[0,1,0,0,0,1,0],[0,0,1,1,1,1,0],[0,0,0,0,0,0,0]]
#[[0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0],[0,0,0,0,1,0,0,0,0],[0,1,1,0,0,1,1,1,0],[0,0,0,0,0,0,0,0,0]] #acorn
53 changes: 29 additions & 24 deletions lib/conways/cell.rb
@@ -1,30 +1,35 @@
class Cell module Conways
class << self class Cell
def live LIVE = 1
1 DEAD = 0
end class << self
def live_one
Cell.new Cell::LIVE
end


def dead def dead_one
0 Cell.new Cell::DEAD
end end
end end


attr_accessor :neighbours, :state, :next_state, :previous_state attr_accessor :state, :next_state
def initialize(state) def initialize(state)
@state = state raise "Cell must be live or dead" unless state == Cell::LIVE || state == Cell::DEAD
@next_state = nil @state = state
end @next_state = nil
end


def live? def live?
@state == Cell.live @state == Cell::LIVE
end end


def dead? def dead?
@state == Cell.dead @state == Cell::DEAD
end end


def update_to_next_generation_state def move_to_next_state
@state = @next_state @state = @next_state
@next_state = nil @next_state = nil
end end
end
end end

0 comments on commit 995030a

Please sign in to comment.