Skip to content

Instantly share code, notes, and snippets.

@geowy
Created June 3, 2020 01:38
Show Gist options
  • Save geowy/05105c761e5fe218d282b424ef1dc2dd to your computer and use it in GitHub Desktop.
Save geowy/05105c761e5fe218d282b424ef1dc2dd to your computer and use it in GitHub Desktop.
require 'matrix'
NEIGHBOUR_RELATIVE_COORDS =
[[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]]
# Init matrix
# true = alive, false = dead
matrix = Matrix.build(35, 45) { [true, false].sample }
loop do
# display matrix
system 'clear'
matrix.row_vectors.each do |row|
row.each { |alive| print alive ? '██' : ' ' }
print "\n"
end
sleep(0.2)
# build next matrix
matrix = Matrix.build(35, 45) do |x, y|
neighbour_coords = NEIGHBOUR_RELATIVE_COORDS.map { |nrx, nry| [x + nrx, y + nry] }
alive_neighbours = neighbour_coords.count { |x, y| matrix[x, y] }
alive_neighbours == 3 || (matrix[x, y] && alive_neighbours == 2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment