CS 211 - Assignment 3 Due November 27, 2018 The Game of Life
CS 211 - Assignment 3 Due November 27, 2018 The Game of Life
The “game of life” simulates the behaviour of a population of living cells that evolves from one generation to
the next.
A population is represented by a n x m two-dimensional array of cells, each cell being in one of two different
states: alive or dead.
Each cell has 8 neighbouring cells surrounding it, and the next generation of cells is generated according to
the following rules:
1. Any live cell with fewer than 2 live neighbours dies, as if caused by under-population.
2. Any live cell with more than 3 live neighbours dies, as if by overcrowding.
3. Any live cell with 2 or 3 live neighbours lives on to the next generation.
4. Any dead cell with exactly 3 live neighbours becomes a live cell, as if by reproduction.
1. All changes happen in parallel (i.e. a change of a cell in one generation will only affect the
neighbours in the next generation). So imagine we have two different arrays, one for the current
generation and one for the next generation. When a cell of the current generation is examined, and its
neighbours counted, the new cell is generated and stored “in the next generation array”. At the end of
the process, the next generation array is copied back in the original array.
2. All cells should have 8 neighbours, even the ones on the border. For this purpose, the 2-dim array is
considered as if it was wrapped around, or a sort of sphere.
0 1 2 3
0 X
1 X X
2 X
0 1 2 3
0 3 2 4 2
1 3 2 3 2
2 3 3 4 1
Programming
1. The actual size of the population (n and m), as well as the maximum number of generations, can either be
declared as global constants, or entered by the user and passed as parameters to various functions (in
which case the capacity of the array has to be declared as a global constant).
3. The program has to display the initial population, and the population after each generation (giving the
generation number, starting from 1), until either the whole population dies, or until you reach the max
number of generations.
4. Bonus marks will be added if the execution stops if the population reaches a stable state.
5. Please include two alternative techniques to initialize the population. In the first one, the cells are entered
one after the other by the user, as a sequence of characters (‘0’ for dead and ‘1’ for live). In the second
one, the cells are generated randomly, according to a given density. For example, if the required density is
30%, then for each cell a random number between 1..100 is generated. If the number is <=30, the cell is
alive, otherwise the cell is dead. When marking the assignments, we will choose one or the other of the
initialization techniques.
Make your program simple and elegant!!! And Include comments and use proper indentation
If you check on internet, you will find interesting patterns that make the population go into cycles, or that die
after a number of generations. You can test your program with these patterns! Have fun!
. // Global constants
const int MAXGEN = 3; // maximum no. of generations
const int n = 10 ; // no. of rows
const int m = 10 ; // no. of colums
int main(){
int grid[n][m];
initialize(grid); // you should call either initialize, or initialize2
cout << "Initial population = \n";
print(grid);
gen = 1;
print (grid);
while (gen <= MAXGEN && !allDead(grid)){
cout << "gen = " << gen;
reproduce(grid); // will call the function countNeighbours for each cell
print (grid);
gen++;
}
}