Skip to content

Instantly share code, notes, and snippets.

@deep-spaced
Created June 29, 2016 11:43
Show Gist options
  • Save deep-spaced/01c3b5d1e66fb70ac1d5912ef53aed05 to your computer and use it in GitHub Desktop.
Save deep-spaced/01c3b5d1e66fb70ac1d5912ef53aed05 to your computer and use it in GitHub Desktop.
Run it with `ruby game_of_life.rb`.
class GameOfLife
def initialize(size, iterations)
@square_size = size
@max_iterations = iterations
end
def run
# Set up the initial array.
@board = Array.new(@square_size)
# Initialize the grid with zeroes.
Range.new(0, @square_size-1).each do |row|
@board[row] = Array.new(@square_size, 0)
end
# Seed particular parts of the grid.
spaceship
# Print the initial state.
print_board
# Play the game!
iterate_game(1)
end
private
def iterate_game(iteration)
return false if iteration > @max_iterations
# Sleep for a moment so people can watch.
sleep 0.3
print_board(iteration)
# Duplicate the board for the next iteration.
new_board = @board.map { |row| row.dup }
# Process each cell.
board_size = @board.size - 1
Range.new(0, board_size).each do |row|
Range.new(0, board_size).each do |col|
new_board[row][col] = process_cell(@board[row][col], row, col)
end
end
# Iterate!
@board = new_board
iterate_game(iteration+1)
end
def process_cell(cell_status, row, col)
living_neighbors = 0
# Check neighbors
living_neighbors += 1 if check_neighbor(row-1, col-1) > 0
living_neighbors += 1 if check_neighbor(row-1, col) > 0
living_neighbors += 1 if check_neighbor(row-1, col+1) > 0
living_neighbors += 1 if check_neighbor(row, col-1) > 0
living_neighbors += 1 if check_neighbor(row, col+1) > 0
living_neighbors += 1 if check_neighbor(row+1, col-1) > 0
living_neighbors += 1 if check_neighbor(row+1, col) > 0
living_neighbors += 1 if check_neighbor(row+1, col+1) > 0
# Actually check the rules!
if living_neighbors < 2
0
elsif living_neighbors > 3
0
elsif living_neighbors === 3
1
else
cell_status
end
end
def check_neighbor(row, col)
return 0 if !in_bounds(@board.size, row) ||
!in_bounds(@board.size, col)
@board[row][col]
end
def in_bounds(max, num)
(num >= 0 and num < max - 1) ? true : false
end
def duplicate_board
end
def print_board(iteration = 0)
puts "\e[H\e[2J"
puts "Iteration: #{iteration}"
@board.each do |row|
puts '| ' + row.map { |cell| cell == 0 ? ' ' : "\u{1F60B}" }.join(' | ') + ' |'
end
puts ""
end
# This is the toad configuration.
def toad
# Requires a 6 square.
@board[3][3] = 1
@board[3][4] = 1
@board[4][3] = 1
@board[4][4] = 1
@board[5][5] = 1
@board[5][6] = 1
@board[6][5] = 1
@board[6][6] = 1
end
def blinker
# This is the blinker:
# Requires a 5 square.
@board [2][1] = 1
@board [2][2] = 1
@board [2][3] = 1
end
def spaceship
@board[11][13] = 1
@board[11][16] = 1
@board[12][12] = 1
@board[13][12] = 1
@board[13][16] = 1
@board[14][12] = 1
@board[14][13] = 1
@board[14][14] = 1
@board[14][15] = 1
end
end
game = GameOfLife.new(30, 30)
game.run
@deep-spaced
Copy link
Author

deep-spaced commented Jun 29, 2016

At this point, iterations and board size can be defined on line #131. Switch out the different types of seeds on line #17.

Written with Ruby 2.2.4. Make sure you use a terminal that supports Unicode (iTerm2 on OS X, for instance).

Fun stuff!

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