Skip to content

Instantly share code, notes, and snippets.

@Thermatix
Created February 12, 2016 12:23
Show Gist options
  • Save Thermatix/54bda5fc4d2d92e6a939 to your computer and use it in GitHub Desktop.
Save Thermatix/54bda5fc4d2d92e6a939 to your computer and use it in GitHub Desktop.
#!/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
Copy link

#!/usr/bin/env ruby
require 'curses'
Ticks = 5
Board_Size = 10
Board_Height = 0...Board_Size
Board_Width = 0...(Board_Size * 2)

Curses.init_screen

def print_board board
  board.each_with_index do |row,y|
    Curses.setpos(y, 0)
    rowstr = row.map.with_index do |cell,x|
      cell == 1 ? '*' : ' '
    end
    Curses.addstr(rowstr.join)
  end
  Curses.refresh
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
  sleep(0.3)
end
puts "\nPress any key to exit"
Curses.getch

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