Conway's Game of Life

Assignment overview

In this assignment, you’ll gain practice with arrays by implementing Conway’s Game of Life, a simplified model of the rise and fall of organisms or societies that leads to pretty patterns. (Check out the Wikipedia page!)

Rules of the game

In the Game of Life, you work with a grid of “cells,” each of which is “alive” or “dead.” You will represent this grid as an array of Booleans (alive is true, dead is false). You may choose whether you want to use a 1D array or a 2D array. If you use a 1D array, that is, a simple array of Booleans, you will think of index i as representing the ith cell, counting from the top left, all the way across a row, then across the second row, then the third, and so on. If you use a 2D array, then index [i][j] can represent the ith row and jth column.

The grid is filled with an initial pattern of on-and-off cells. After this, the “game” procedes in generations, or time steps. Each generation, every cell in the grid updates itself simultaneously, according to the following rules (taken from Wikipedia):

  1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.

  2. Any live cell with two or three live neighbors lives on to the next generation.

  3. Any live cell with more than three live neighbors dies, as if by overpopulation.

  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The word “neighbors” here refers to the eight cells that surround the current cell. You may realize that some cells, namely the ones at the borders of the grid, do not have eight neighbors. You should think of the grid as “wrapping around” horizontally and vertically, so that for example if I am in the furthest left column, and walk one step left, I wrap around to the furthest right column.

You will implement a loop that clears the screen, prints the current generation’s grid to the Terminal, using symbols of your choice to represent live and dead cells (e.g. * and .), then uses the current generation to compute the next generation, and sleeps for a bit before looping back and continuing the animation.

Instructions

1A type for the game of life

Declare a grid type with whatever underlying type you’d like to use to represent the grid (likely an array of Booleans or an array of arrays of Booleans). The grid should be 50x50 in size, so altogether you will be storing 2,500 Booleans. (Note: if you’d like to use a different size, that’s all right! Anything much taller than 50 will be hard, because your Terminal window doesn’t get that tall; you might want to make it shorter or wider, though.) Implement the following useful methods on the type:

2An initial state

In your main function, create a new grid and set some of its entries to true. You may want to look at Wikipedia’s Game of Life page for some good ideas on initial configurations!

3The main loop

Now, loop infinitely (using for {...}), and in each iteration, do the following:

  1. Clear the screen, using clear.WipeScreen() from the package you used in your animations assignment.

  2. Use the current grid’s Draw method to print it to the screen.

  3. Compute the new grid: currentGrid = currentGrid.NextGeneration().

  4. Sleep for a small amount of time: time.Sleep(300 * time.Millisecond) is reasonable.

Enjoy your animation! You can now experiment with different starting configurations if you’d like.