Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Last active June 19, 2017 01:35
Show Gist options
  • Save jbrechtel/4746030 to your computer and use it in GitHub Desktop.
Save jbrechtel/4746030 to your computer and use it in GitHub Desktop.
Conway's Game of Life

Conway's Game of Life

Conway's Game of Life is a set of rules which define a cellular automata. Read more about it in Wikipedia. Your task is to write a Conway's Game of Life (GOL) simulation.

GOL consists of a two-dimensional grid of cells. Each cell is either alive or dead. Your simulation should calculate the grid over a user-specified number of generations. To calculate the next generation you apply the following rules to each cell in the grid and the resulting grid is the next generation.

Rules

  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

A cell's neighbors are the cells that are adjacent (either horizontally, vertically, or diagonally) to it.

Above is a 3x10 grid.

Your program should take two arguments. The first is a filename which contains the grid and the second is the number of generations to calculate. Your program should print out the grid at the specified generation.

Each line in the file will represent one row in the grid. An example file looks like this:

***XXX**X*
X*XX*X*X*X
****XX***X

* designates a dead cell
X designates a live cell

Here's an example of a simple grid over three generations

Generation 1

*******
**XXX**
*******

Generation 2

***X***
***X***
***X***

Generation 3

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