Skip to content

Instantly share code, notes, and snippets.

@eqdw
Created January 9, 2010 23:51
Show Gist options
  • Save eqdw/273211 to your computer and use it in GitHub Desktop.
Save eqdw/273211 to your computer and use it in GitHub Desktop.
infile = ARGV[0]
@gridsize = ARGV[1].to_i
@world = Array.new(@gridsize+2){|i| Array.new(@gridsize+2){"."}}
File.open(infile, "r") do |f|
(1..@gridsize).each do |i|
row = f.gets
rowarr = row.split(" ")
(0..@gridsize-1).each do |j|
@world[i][j+1] = rowarr[j]
end
end
end
def num_neighbours(i, j)
count = 0
(i-1..i+1).each do |a|
(j-1..j+1).each do |b|
unless (i == a) && (j == b)
count += 1 if @world[a][b] == "X"
end
end
end
count
end
def render()
line = ""
(1..@world.length-2).each do |i|
line = ""
(1..@world[i].length-2).each do |j|
line += "#{@world[i][j]} "
end
puts line
end
end
def tick()
new_world = Array.new(@gridsize+2){|i| Array.new(@gridsize+2){"."}}
(1..@gridsize).each do |i|
(1..@gridsize).each do |j|
tmp = num_neighbours(i,j)
#puts "DEBUG: tmp = #{tmp}"
if(tmp < 2)
new_world[i][j] = "."
elsif(tmp > 3)
new_world[i][j] = "."
elsif(tmp == 3)
new_world[i][j] = "X"
else #if tmp == 2
if @world[i][j] == "X"
new_world[i][j] = "X"
else
new_world[i][j] = "."
end
end
end
end
@world = new_world.clone
render
end
render
10.times do
puts "-------------------"
tick
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment