Created
June 16, 2015 17:38
-
-
Save simonszu/21bf5b504fa18824eecf to your computer and use it in GitHub Desktop.
Conway's game of life with ASCII output
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 | |
STDOUT.sync = true | |
# Create the grid | |
class Grid | |
def initialize(height, width) | |
@width = width | |
@height = height | |
@grid = Array.new(@height) { Array.new(@width)} | |
@height.times do |row| | |
@width.times do |col| | |
@grid[row][col] = " " | |
end | |
end | |
end | |
def show() | |
puts "\e[H\e[2J" | |
@height.times do |row| | |
print "\r" | |
@width.times do |col| | |
print @grid[row][col] | |
end | |
print "\n" | |
end | |
end | |
def toggle(x, y) | |
if @grid[x-1][y-1] == " " | |
@grid[x-1][y-1] = "X" | |
elsif @grid[x-1][y-1] == "X" | |
@grid[x-1][y-1] = " " | |
end | |
end | |
def reset | |
@height.times do |row| | |
@width.times do |col| | |
@grid[row][col] = " " | |
end | |
end | |
end | |
def toggleRandCoords | |
xrand = rand(@height) + 1 | |
yrand = rand(@width) + 1 | |
toggle(xrand, yrand) | |
end | |
# spawn the universe | |
def spawn(count) | |
count.times do | |
toggleRandCoords | |
end | |
end | |
# calculate the next generation | |
def nextGen | |
nextGrid = Array.new(@height) { Array.new(@width)} | |
@height.times do |row| | |
@width.times do |col| | |
nextGrid[row][col] = " " | |
end | |
end | |
@height.times do |row| | |
@width.times do |col| | |
upperRow = row-1 | |
lowerRow = row+1 | |
leftColumn = col-1 | |
rightColumn = col+1 | |
# Check the number of living neighbours | |
livingNeighbours = 0 | |
# check the upperleft neighbour | |
if ((upperRow >= 0) && (leftColumn >= 0) && (@grid[upperRow][leftColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the above neighbour | |
if ((upperRow >= 0) && (@grid[upperRow][col].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the upperright neighbour | |
if ((upperRow >= 0) && (rightColumn < @width) && (@grid[upperRow][rightColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the left neighbour | |
if ((leftColumn >= 0) && (@grid[row][leftColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the right neighbour | |
if ((rightColumn < @width) && (@grid[row][rightColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the lowerleft neighbour | |
if ((lowerRow < @height) && (leftColumn >= 0) && (@grid[lowerRow][leftColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the under neighbour | |
if ((lowerRow < @height) && (@grid[lowerRow][col].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# check the lowerright neighbour | |
if ((lowerRow < @height) && (rightColumn < @width) && (@grid[lowerRow][rightColumn].eql? "X")) | |
livingNeighbours = livingNeighbours + 1 | |
end | |
# Set the new state of the cell according to its current state and the neighbours | |
if @grid[row][col] == " " | |
if livingNeighbours == 3 | |
nextGrid[row][col] = "X" | |
end | |
elsif @grid[row][col] == "X" | |
if (livingNeighbours == 1) | |
nextGrid[row][col] = " " | |
elsif ((livingNeighbours == 2) || (livingNeighbours == 3)) | |
nextGrid[row][col] = "X" | |
else | |
nextGrid[row][col] = " " | |
end | |
end | |
end | |
end | |
@grid = nextGrid | |
end | |
end | |
ROWS = 30 | |
COLS = 200 | |
SPAWN = 4000 | |
SLEEP = 0.1 | |
game = Grid.new(ROWS, COLS) | |
game.spawn (SPAWN) | |
game.show | |
sleep(SLEEP) | |
while true | |
game.nextGen | |
game.show | |
sleep(SLEEP) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment