Skip to content

Instantly share code, notes, and snippets.

@stulentsev
Last active May 12, 2017 07:17
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 stulentsev/0e25ae7b079466412a87de26fc4f11be to your computer and use it in GitHub Desktop.
Save stulentsev/0e25ae7b079466412a87de26fc4f11be to your computer and use it in GitHub Desktop.
class Cell
attr_accessor :alive
attr_accessor :coordinates
attr_accessor :neighbours
attr_accessor :live_neighbours
def initialize(live_cell_ratio)
@alive = ([true] + Array.new(live_cell_ratio, false)).sample
@coordinates = []
@neighbours = []
@live_neighbours = 0
end
def live_neighbour_count
@live_neighbours = 0
neighbours.each do |neighbour|
if neighbour.alive
@live_neighbours += 1
end
end
@live_neighbours
end
def set_future_state
if @live_neighbours < 2 and @alive == true
@alive = false
elsif (@live_neighbours == 2 or @live_neighbours == 3) and @alive == true
@alive = true
elsif @live_neighbours > 3 and @alive == true
@alive = false
elsif @live_neighbours == 3 and @alive == false
@alive = true
end
@alive
end
def x
coordinates[0]
end
def y
coordinates[1]
end
end
class World
HEIGHT = 30
WIDTH = 60
attr_accessor :grid
def initialize(live_cell_ratio)
@grid = Array.new(HEIGHT) { Array.new(WIDTH) { Cell.new(live_cell_ratio) } }
end
def set_cell_coordinates
@grid.each_with_index do |row, row_index|
row.each_with_index do |cell, cell_index|
cell.coordinates = [row_index, cell_index]
end
end
end
def collect_cell_neighbours
@grid.each do |row|
row.each do |cell|
cell.neighbours << @grid[cell.x - 1][cell.y] unless cell.x - 1 == -1
cell.neighbours << @grid[cell.x - 1][cell.y - 1] unless (cell.x - 1 == -1) or (cell.y - 1 == -1)
cell.neighbours << @grid[cell.x - 1][cell.y + 1] unless cell.x - 1 == -1 or (cell.y + 1 == WIDTH)
cell.neighbours << @grid[cell.x][cell.y - 1] unless cell.y - 1 == -1
cell.neighbours << @grid[cell.x][cell.y + 1] unless cell.y + 1 == WIDTH
cell.neighbours << @grid[cell.x + 1][cell.y] unless cell.x + 1 == HEIGHT
cell.neighbours << @grid[cell.x + 1][cell.y - 1] unless cell.x + 1 == HEIGHT or (cell.y - 1 == -1)
cell.neighbours << @grid[cell.x + 1][cell.y + 1] unless cell.x + 1 == HEIGHT or (cell.y + 1 == WIDTH)
end
end
end
def set_live_neighbours
@grid.each_with_index do |row, row_index|
row.each_with_index do |cell, cell_index|
cell.live_neighbour_count
end
end
end
def update_to_new_state
@grid.each do |row|
row.each do |cell|
cell.set_future_state
end
end
end
def put_grid
@grid.each do |row|
row.each do |cell|
if cell.alive
print '0'
else
print ' '
end
end
puts ' '
end
end
end
class Game
def play
puts "What would you like the seeding ratio of live cells to be?"
puts "The higher, the less live cells will be initially placed on the grid"
puts "Please enter a number between 1 and 10"
live_cell_ratio = gets.chomp.to_i
until (1..10).include?(live_cell_ratio) do
puts "Please enter a number between 1 and 10"
live_cell_ratio = gets.chomp.to_i
end
world = World.new(live_cell_ratio)
world.set_cell_coordinates
world.collect_cell_neighbours
generation = 0
loop do
system("clear")
world.put_grid
world.set_live_neighbours
world.update_to_new_state
generation += 1
puts "Generation: #{generation}"
sleep 0.2
end
end
end
Game.new.play
@irhen
Copy link

irhen commented May 12, 2017

does is ever stop? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment