Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created February 22, 2013 04:36
Show Gist options
  • Save mfpiccolo/5010756 to your computer and use it in GitHub Desktop.
Save mfpiccolo/5010756 to your computer and use it in GitHub Desktop.
The game of life
class LifeGrid
def initialize(width, height, seeds)
matrix = Array.new(height).fill(0..(height - 1))
@matrix = matrix
new_matrix = @matrix
@new_matrix = new_matrix
@height = height
@width = width
@matrix.map! { |x| x = Array.new(width).fill(0..width -1) { rand(2) } }
@matrix.each {|row| p row}
puts
end
def evolve
#x is the array that is each row of the matrix
#y is the value which we are checking if it is alive
#xi is the x axis place holder
#yi is the y axis place holder
#example [0,1] == 1 if x == [0,1,0,0,1]
#example [xi,yi] == y if x == [0,1,0,0,1]
@matrix.each_with_index do |x, xi|
x.each_with_index do |y, yi|
#checking if the top left box is alive then checking the ammount of alive surrounding boxes
if xi == 0 and yi == 0 and y == 1
surrounding = @matrix[0][@width - 1] + @matrix[0][1] + @matrix[@height - 1][0] + @matrix[1][0] + @matrix[@height - 1][@width - 1] + @matrix[@height - 1][1] + @matrix[1][@width - 1] + @matrix[1][1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#check top right alive and surrounding
elsif xi == 0 and yi == (@width - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][0] + @matrix[@height - 1][yi] + @matrix[xi + 1][yi] + @matrix[@height - 1][yi - 1] + @matrix[@height - 1][0] + @matrix[xi + 1][yi - 1] + @matrix[xi + 1][0]
@matrix.each {|row| p row}
p " "
@new_matrix.each {|row| p row}
p "#{xi}, #{yi}"
p "left #{@matrix[xi][yi - 1]}"
p "right #{@matrix[xi][0]}"
p "up #{@matrix[xi -1][yi]}"
p "down #{@matrix[xi + 1][yi]}"
p "top-left #{@matrix[xi - 1][yi - 1]}"
p "top_right #{@matrix[xi - 1][0]}"
p "bottom-left #{@matrix[xi + 1][yi - 1]}"
p "bottom-right #{@matrix[xi +1][0]}"
p "total #{surrounding}"
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#check top row alive and surrounding
elsif xi == 0 and yi != 0 and yi != (@width - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][yi + 1] + @matrix[@height - 1][yi] + @matrix[xi + 1][yi] + @matrix[@height - 1][yi - 1] + @matrix[@height - 1][yi + 1] + @matrix[xi + 1][yi - 1] + @matrix[xi + 1][yi +1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#checking the bottom left
elsif xi == (@height - 1) and yi == 0 and y == 1
surrounding = @matrix[xi][@width - 1] + @matrix[xi][yi + 1] + @matrix[xi - 1][yi] + @matrix[0][yi] + @matrix[xi - 1][@width - 1] + @matrix[xi - 1][yi + 1] + @matrix[0][@width - 1] + @matrix[0][yi + 1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#checking bottom right
elsif xi == (@height - 1) and yi == (@width - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][0] + @matrix[xi - 1][yi] + @matrix[0][yi] + @matrix[xi - 1][yi - 1] + @matrix[xi - 1][0] + @matrix[0][yi - 1] + @matrix[0][0]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#checking the bottom middle
elsif xi == (@height - 1) and yi != 0 and yi != (@width - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][yi + 1] + @matrix[xi - 1][yi] + @matrix[0][yi] + @matrix[xi -1][yi - 1] + @matrix[xi - 1][yi + 1] + @matrix[0][yi -1] + @matrix[0][yi + 1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#checking the left side
elsif yi == 0 and xi != 0 and xi != (@height - 1) and y == 1
surrounding = @matrix[xi][@height - 1] + @matrix[xi][yi + 1] + @matrix[xi - 1][yi] + @matrix[xi + 1][yi] + @matrix[xi - 1][@width - 1] + @matrix[xi - 1][yi +1] + @matrix[xi + 1][@width -1] + @matrix[xi + 1][yi + 1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
#checking the right side
elsif yi == (@width - 1) and xi != 0 and xi != (@height - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][yi - yi] + @matrix[xi -1][yi] + @matrix[xi + 1][yi] + @matrix[xi - 1][yi - 1] + @matrix[xi - 1][0] + @matrix[xi + 1][yi - 1] + @matrix[xi +1][0]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
elsif xi != 0 and xi != (@height -1) and yi != 0 and yi != (@width - 1) and y == 1
surrounding = @matrix[xi][yi - 1] + @matrix[xi][yi + 1] + @matrix[xi - 1][yi] + @matrix[xi + 1][yi] + @matrix[xi - 1][yi -1] + @matrix[xi -1][yi + 1] + @matrix[xi + 1][yi - 1] + @matrix[xi + 1][yi + 1]
if surrounding < 2 || surrounding > 3
@new_matrix[xi][yi] = 0
end
end
end
end
@new_matrix.each {|row| p row}
end
end
my_lifegrid = LifeGrid.new(4,4,3)
my_lifegrid.evolve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment