86. Life

In 1970, British mathematician John Horton Conway devised a simulation known as the Game of Life or simply Life. The rules are slightly more complicated than those used by Langton's Ant, although they are still quite modest.

The universe consists of an infinite grid of square cells. Each cell can be alive or dead (or occupied or unoccupied, if you prefer). Starting from some initial configuration, the cells obey the following rules:

  • If a live cell has fewer than two live neighbors, it dies from underpopulation
  • If a live cell has more than three live neighbors, it dies from overpopulation
  • If a dead cell has exactly three neighbors, it becomes a live cell by reproduction

A cell's neighbors are the cells that are adjacent horizontally, vertically, or diagonally.

For this problem, write a Life program similar to the one shown in the following screenshot:

Let the user click cells to toggle their states and then start the program to see how it evolves. Also, let the user determine whether the program wraps cells at the grid's edges or treats locations outside of the grid as dead cells.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset