Hacker Challenge: Conway's Game of Life
Learn how to make a game of life in C++.
We'll cover the following
Conway’s Game of Life
The Game of Life is a two-dimensional orthogonal grid of cells. Look at the illustration of one of the patterns that we’ve created.
Each cell has two states:
- Alive
- Dead
Each cell has potentially eight neighbor cells:
- 2 Vertical
- 2 Horizontal
- 4 Diagonal
Note: The corner cell will have fewer than 8 neighbors.
Understanding the Game of Life map (in terms of neighbors)
Consider Conway’s Game of Life on a 10x10 grid and answer the following questions:
The coordinate (0,0) has how many legal neighbors?
8
6
5
3
Rules of Conway’s Game of Life
There are some rules for each transition:
- A cell with fewer than two neighbors will die due to underpopulation.
- A cell with more than three live neighbors will die due to overpopulation.
- A cell with two or three neighbors will stay alive.
- A dead cell that has three neighbors will become alive (mimicking a birth of a new cell).
Let’s take a moment to make sure you’ve correctly understood the game. The quiz below helps us to check if you’re solving the correct game:
Game of life
Choose the next state according to the rules.
Given the 3x3 window, we’re only computing the next state of the middle cell and ignoring the rest as we’re not showing their neighbor cells.
. . . . .
. - * - .
. - * - .
. - - - .
. . . . .
. . . . .
. - * - .
. - - - .
. - - - .
. . . . .
. . . . .
. - * - .
. - * - .
. - * - .
. . . . .
Your implementation
Write your implementation here in the playground. We have provided you with all the input test files on which you need to test your work.
40 64 * ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- -------------------------------------------------*-------------- -----------------------------------------------*-*-------------- -------------------------------------**------**----------------- ------------------------------------*---*----**------------**--- -**--------------------------------*-----*---**------------**--- -**--------------------------------*---*-**----*-*-------------- -----------------------------------*-----*-------*-------------- ------------------------------------*---*----------------------- -------------------------------------**------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ----------------------------------------------------------------
Main flow of Conway’s Game of Life
Now let’s come to the implementation.
Before writing a solution, look at the game’s main flow.
- Memory required for a two-dimensional map:
char World[rows][cols];
int r,c;
char lifeSym='*';
- Read the map from the text file and initialize the two-dimensional array.
init(World, r, c, lifeSym);
- To print the complete two-dimensional world, call the display function.
display(World, r, c);
- The repopulate function will update the state of the two-dimensional world, based on the rules discussed before.
repopulate(World, r, c, lifeSym);
Let’s discuss the implementation of each function separately:
Step 1: Initialization
Let’s define a function init()
. This function has four parameters:
- The two-dimensional array
World
- Number of rows
R
- Number of columns
C
- Symbol
LS
Inside the function, we will read the number of rows in R
, the number of columns in C
, and the symbol in LS
from the text file. We will also write a nested loop that reads the complete map from the text file and replace each dash with a space because the dashes in the text file represent dead cells.
Step 2: Displaying the Game of Life
You need to define the display()
function. This function has three parameters:
- The two-dimensional array
World
- Number of rows
R
- Number of columns
C
The display()
function first clears the console and prints the complete two-dimensional map with a nested loop.
Step 3: Computing the neighbor count
We will define the neighbourhoodLivesCount()
function. This function has six parameters:
- The two-dimensional array
World
- Number of rows
R
- Number of columns
C
- Row index
ri
- Column index
ci
- Symbol
LS
This function will count the neighbors of the life symbol around the cell (ri
,ci
).
To avoid the boundaries of the current cell in both dimensions row and column we used a condition. We don’t want to count the current cell with its neighbors.
We need to add these conditions to avoid the row boundaries:
if (r < 0 || r == R)
continue;
To avoid the column boundaries:
if (c < 0 || c == C)
continue;
To avoid the count of cells itself:
if (r == ri && c == ci)
continue;
Step 4: Repopulating the next stage of the Game of Life map
This is the most essential step where we recompute the next stage of life given the current state of life. Let’s define the repopulate()
function. This function has four parameters:
- The two-dimensional array
World
- Number of rows
R
- Number of columns
C
- Symbol
Sym
To repopulate the two-dimensional World
, we need to declare a 2-D array DWorld
that will be used to perform a task because we don’t want to change the original map World
.
- First, we will count the neighbor’s lives with the function
NeighbourhoodLivesCount()
, check the conditions according to the game rules, and update theDWorld
.
Rule 1: A cell with fewer than two neighbors will die due to underpopulation.
if (World[ri][ci] == Sym && count < 2)
{
DWorld[ri][ci] = ' ';
}
Rule 2: A cell with more than three neighbors will die due to overpopulation.
else if (World[ri][ci] == Sym && count > 3)
{
DWorld[ri][ci] = ' ';
}
Rule 3: A cell with two or three live neighbors will stay alive.
else if (World[ri][ci] == Sym && count >= 2 && count <= 3)
{
DWorld[ri][ci] = World[ri][ci];
}
Rule 4: A dead cell with three live neighbors will become live.
else if (World[ri][ci] == ' ' && count == 3)
{
DWorld[ri][ci] = Sym;
}
- After that, we will assign the
DWorld
changes toWorld
.
for (int ri = 0; ri < R; ri++)
{
for(int ci=0; ci < C; ci++)
World[ri][ci]=DWorld[ri][ci];
}
The complete implementation
Let’s execute the complete code below:
40 64 * ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- -------------------------------------------------*-------------- -----------------------------------------------*-*-------------- -------------------------------------**------**----------------- ------------------------------------*---*----**------------**--- -**--------------------------------*-----*---**------------**--- -**--------------------------------*---*-**----*-*-------------- -----------------------------------*-----*-------*-------------- ------------------------------------*---*----------------------- -------------------------------------**------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ---------------------------------------------------------------- ----------------------------------------------------------------
Instruction: Change the file name and see their animations.
We recommend exploring various patterns from this link (https://conwaylife.appspot.com/library) and modifying the input file to observe how the program responds. This will allow you to fully discover the intriguing patterns of life.