Simulating the Game of Life

In 1970, mathematician John H. Conway proposed a simulation that he called the Game of Life. Martin Gardner wrote a column about Life in the October 1970 issue of Scientific American that brought widespread attention to the Game of Life. It’s not what most people think of as a game; there are no players and there’s no way to win or lose the game. Instead, Life is more like a model or simulation in which you can play and experiment.

Life takes place on a two-dimensional grid of square cells. Each square cell can be either alive or dead (full or empty).

The simulation is carried out at fixed time steps; every time step, all the cells on the grid can switch from dead to alive, or alive to dead, depending on four simple rules that only depend on a given cell’s eight immediate neighbours.

If the cell is dead:

  • Birth: if exactly three of its neighbours are alive, the cell will become alive at the next step.
If the cell is already alive:
  • Survival: if the cell has two or three live neighbours, the cell remains alive.
Otherwise, the cell will die:
  • Death by loneliness: if the cell has only zero or one live neighbours, the cell will become dead at the next step.
  • Death by overcrowding: if the cell alive and has more than three live neighbours, the cell also dies.
The changes for each cell happen at the same time throughout one time step.

Task:
Find how many cells are alive after given time steps.

Requirements:
Finish writing a function gameOfLife that has these parameters:

  • Steps – an integer that states how many time steps does the game have to go through.
  • Board – a two-dimensional array that contains all alive and dead cells.
The function must return
  • A number of cells (as an integer) that are alive after given time steps.

Constraints:
Given the board size as N x M and steps as C:

1 ≤ N, M, C ≤ 100

Example input 0:
Steps = 2,
Board =

[ [ 1, 0, 1 ],
  [ 0, 1, 0 ],
  [ 1, 0, 1 ] ]

Example output 0:

gameOfLife( 2, [ [ 1, 0, 1 ], [ 0, 1, 0 ], [ 1, 0, 1 ] ] ) = 4

Explanation:

101	010	010
010 =>	101 =>	101 => alive cells = 4
101	010	010

Example input 1:
Steps = 1,
Board =

[ [ 1, 0, 1 ],
  [ 1, 0, 0 ],
  [ 0, 0, 1 ] ]

Example output 1:

gameOfLife( 1, [ [ 1, 0, 1 ], [ 1, 0, 0 ], [ 1, 0, 0 ] ] ) = 1

Explanation:

101	010
100 =>	000 => alive cells = 1
001	000

You can download the eclipse project file here.

Please send your solution to

Starts on Mon, 14 November 2016 18:30

Ends on Tue, 15 November 2016 00:00

Points you can get: 50