Created
February 12, 2016 12:23
-
-
Save Thermatix/54bda5fc4d2d92e6a939 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
#!/usr/bin/env ruby | |
Ticks = 5 | |
Board_Size = 10 | |
Board_Height = 0...Board_Size | |
Board_Width = 0...(Board_Size * 2) | |
def print_board board | |
board.each_with_index do |row,y| | |
row.each_with_index do |cell,x| | |
print cell == 1 ? '*' : ' ' | |
end | |
print "\n" | |
end | |
end | |
def wipe_screen | |
Board_Height.each do |_| | |
# print "\r" | |
# $stdout.write "\e[A\e[2K" | |
end | |
end | |
def count board,row,cell | |
0 + | |
board[row][edge_check cell,:l] + | |
board[row][edge_check cell] + | |
board[edge_check row,:l][cell] + | |
board[edge_check row][cell] | |
end | |
def seed h,w | |
(h).map do |row| | |
(w).map do |cell| | |
rand(0..1) | |
end | |
end | |
end | |
def edge_check val,bit=:r | |
if bit == :l | |
val <= 0 ? 0 : (val - 1) | |
else | |
val >= Board_Size - 1 ? Board_Size - 1 : (val + 1) | |
end | |
end | |
def check board | |
(Board_Height).each do |row| | |
(Board_Width).each do |cell| | |
yield(count(board,row,cell),row,cell) | |
end | |
end | |
end | |
def cycle board | |
check board do |count,row,cell| #check for new life | |
board[row][cell] = 1 if count > 2 | |
end | |
check board do |count,row,cell| #check for overcrowding | |
board[row][cell] = 0 if count > 3 | |
end | |
check board do |count,row,cell| #check for stagnation | |
board[row][cell] = 0 if count < 2 | |
end | |
end | |
board = seed Board_Height, Board_Width | |
while ( (tick ||= 0) < Ticks) | |
print_board board | |
cycle board | |
tick +=1 | |
end |
stulentsev
commented
Feb 12, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment