Skip to content

Instantly share code, notes, and snippets.

@hannahherbig
Created April 20, 2012 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannahherbig/2426408 to your computer and use it in GitHub Desktop.
Save hannahherbig/2426408 to your computer and use it in GitHub Desktop.
conway's game of life
# conway's game of life
col = 50
row = 50
# set this up however you want, i like randomness.
env = col.times.map { row.times.map { 0 == rand(2) } }
loop do
puts "-" * col
puts env.transpose.map { |c| c.map { |i| i ? "X" : " " }.join }.join "\n"
nxt = col.times.map { row.times.map { false } }
col.times do |x|
row.times do |y|
c = 0
(-1 .. 1).each do |a|
(-1 .. 1).each do |b|
c += 1 if not (a == 0 and b == 0) and env[(x + a) % col][(y + b) % row]
end
end
if (env[x][y] and (c == 2 or c == 3)) or (not env[x][y] and c == 3)
nxt[x][y] = true
else
nxt[x][y] = false
end
end
end
env = nxt
sleep 0.1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment