Projects · 19 / 19 Exercise Hard

The game of life

Objective

Conway's game of life is a cellular automaton: each cell is alive or dead and its state in the next generation depends only on its eight neighbors.

Instructions

  • The first line has rows, columns and g (number of generations). Then come rows lines with the board, where # is a live cell and . a dead one.
  • Rules: a live cell with 2 or 3 live neighbors stays alive; a dead one with exactly 3 live neighbors is born; in any other case it is dead. Outside the board everything is dead.
  • Compute each generation from a copy, not from the board you are modifying. Show the final board.
Input
5 5 2
.....
..#..
..#..
..#..
.....
Output
.....
..#..
..#..
..#..
.....

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise