0% found this document useful (0 votes)
34 views

CS 211 - Assignment 3 Due November 27, 2018 The Game of Life

This document provides instructions for an assignment to program Conway's Game of Life. The game simulates the evolution of a two-dimensional grid of cells over generations according to rules of birth, survival, and death. Students are asked to write a program that initializes a grid randomly or by user input, counts neighbors of each cell, generates new generations by applying the rules, and prints outputs. Functions and considerations like parallel updates, border wrapping, and stable states are outlined.

Uploaded by

Lily Rose
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

CS 211 - Assignment 3 Due November 27, 2018 The Game of Life

This document provides instructions for an assignment to program Conway's Game of Life. The game simulates the evolution of a two-dimensional grid of cells over generations according to rules of birth, survival, and death. Students are asked to write a program that initializes a grid randomly or by user input, counts neighbors of each cell, generates new generations by applying the rules, and prints outputs. Functions and considerations like parallel updates, border wrapping, and stable states are outlined.

Uploaded by

Lily Rose
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

You are asked to write a program to simulate the game of life.

There are a couple of tricky things to consider:

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

As an example of a 3x4 array, where dead cells are


represented by blanks and live cells by X,
if the initial population is:

0 1 2 3
0 3 2 4 2
1 3 2 3 2
2 3 3 4 1

The number of neighbours for each cell is:


0 1 2 3
0 X X
1 X X X
2 X X

And the next generation will be:

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).

2. The array’s entries can be either of type int, char or bool.

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.

Include functions such as:


void initialize(..) // initialize the nxm population
void initialize2(.., int density) // initialize given a density
int countNeighbours (.., int x, int y) // counts live neighbours of cell x,y
bool allDead(..) // checks if the population is dead
void reproduce(..) // produce the next generation
void print(..) // print a population

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!

Use the following skeleton for the program

. // 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++;
}
}

You might also like