Created
June 3, 2020 01:38
-
-
Save geowy/05105c761e5fe218d282b424ef1dc2dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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