Skip to content

Instantly share code, notes, and snippets.

@igorgue
Created December 1, 2015 22:30
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 igorgue/2ad9eff1cacb62df0ff7 to your computer and use it in GitHub Desktop.
Save igorgue/2ad9eff1cacb62df0ff7 to your computer and use it in GitHub Desktop.
# Conway's Game of Life in Ruby
# http://en.wikipedia.org/wiki/Conway's_Game_of_Life
class Cell
attr_writer :neighbors
def initialize(seed_probability)
@alive = seed_probability > rand
end
def next!
@alive = @alive ? (2..3) === @neighbors : 3 == @neighbors
end
def to_i
@alive ? 1 : 0
end
def to_s
@alive ? 'o' : ' '
end
end
class Game
def initialize(width, height, seed_probability, steps)
@width, @height, @steps = width, height, steps
@cells = Array.new(height) {
Array.new(width) { Cell.new(seed_probability) } }
end
def play!
(1..@steps).each do
next!
system('clear')
puts self
end
end
def next!
@cells.each_with_index do |row, y|
row.each_with_index do |cell, x|
cell.neighbors = alive_neighbours(y, x)
end
end
@cells.each { |row| row.each { |cell| cell.next! } }
end
def alive_neighbours(y, x)
[[-1, 0], [1, 0], # sides
[-1, 1], [0, 1], [1, 1], # over
[-1, -1], [0, -1], [1, -1] # under
].inject(0) do |sum, pos|
sum + @cells[(y + pos[0]) % @height][(x + pos[1]) % @width].to_i
end
end
def to_s
@cells.map { |row| row.join }.join("\n")
end
end
Game.new(100, 50, 0.1, 100).play!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment