Game of Life Write the Program in C++. Task : Simulate the growth of a biologica
ID: 3826573 • Letter: G
Question
Game of Life
Write the Program in C++.
Task: Simulate the growth of a biological population. The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. Our implementation of the game will use a 2-dimensional array with 12 rows and 12 columns. Each cell of the array will hold either a 1 (an organism is present) or a 0 (no organism there). You will calculate 4 generations of the organisms’ birth, survival, and death.
An organism’s neighbors affect its survival. Each organism has 8 adjoining cells where its neighbors may live, as shown in this grid:
0
0
1
1
X
0
0
1
0
The neighbors for cell X would be located in the 8 shaded cells surrounding it. In the situation illustrated, X has 3 neighbors.
Rules for the game:
Birth: An organism will be born in each empty location that has exactly 3 neighbors.
Death: An organism with 4 or more organisms as neighbors will die from overcrowding. An organism with 1 or 0 neighbors will die of loneliness.
Survival: An organism with 2 or 3 neighbors will survive to the next generation.
You will not have to process the border cells (i.e., rows 0 and 11, and columns 0 and 11)
of the game, but the border cells’ contents will affect the internal cells. Assume that border cells are infertile regions where organisms can neither survive nor be born.
The population configuration as shown below will be the initial contents of one array. For each generation, calculate the results of the first array’s births, deaths, and survivals, and store those results in a second array. After printing the results, copy the second array’s data back into the first one (replacing the original data), and repeat the process to calculate the next generation.
Input: The first array will be initialized as follows:
int life[12][12] =
{ {0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0} };
Output: You will print out the initial situation and the results of each of 4 generations of growth. Send all the print output to a single external file. Provide header information (including name and date) in your printed version, and include row numbers and column numbers. Line up the columns. The initial generation would look something like this:
Game of Life
---------------
Original Configuration:
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 0 1 0 0 0
2 0 0 0 1 0 1 0 1 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 1 0 1 0 1 0 0 0
5 0 0 0 1 0 1 0 1 0 0 0 0
6 0 1 1 1 1 1 1 1 1 1 1 0
7 0 0 0 0 1 1 1 1 0 0 0 0
8 0 0 0 0 1 0 1 0 1 0 0 0
9 0 0 0 1 0 1 0 1 0 0 0 0
10 0 0 0 0 1 0 1 0 1 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0
0
0
1
1
X
0
0
1
0
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
// to print the grid
void print(int cells[12][12], std::ostream &myfile) {
myfile << " 0 1 2 3 4 5 6 7 8 9 10 11" << endl;
for (int i = 0; i < 12; i++) {
myfile << i << " ";
for (int j = 0; j < 12; j++) {
myfile << cells[i][j] << " ";
}
myfile << endl;
}
}
// check if i, j is valid indices for grid
bool isValid(int i, int j) {
if (i >= 0 && i < 12 && j >= 0 && j < 12)
return true;
else
return false;
}
// find no of live neighbors
int findActiveNeighbours(int cells[12][12], int i, int j) {
int activeNeighbours = 0;
if (isValid(i - 1, j - 1) && cells[i - 1][j - 1] == 1)
activeNeighbours++;
if (isValid(i - 1, j) && cells[i - 1][j] == 1)
activeNeighbours++;
if (isValid(i - 1, j + 1) && cells[i - 1][j + 1] == 1)
activeNeighbours++;
if (isValid(i, j - 1) && cells[i][j - 1] == 1)
activeNeighbours++;
if (isValid(i, j + 1) && cells[i][j + 1] == 1)
activeNeighbours++;
if (isValid(i + 1, j - 1) && cells[i + 1][j - 1] == 1)
activeNeighbours++;
if (isValid(i + 1, j) && cells[i + 1][j] == 1)
activeNeighbours++;
if (isValid(i + 1, j + 1) && cells[i + 1][j + 1] == 1)
activeNeighbours++;
return activeNeighbours;
}
// this method simulates a round
// it changes the original array
void simulate(int cells[12][12]) {
int newCells[12][12];
// not processing border cells
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 11; j++) {
// find no of live neighbors
int n = findActiveNeighbours(cells, i, j);
if (cells[i][j] == 1) { // if cell is live
if (n == 0 || n == 1) {
newCells[i][j] = 0; // mark for deletion
} else if (n >= 4) {
newCells[i][j] = 0; // mark for deletion
} else {
newCells[i][j] = cells[i][j];
}
} else { // if cell is dead
if (n == 3) {
newCells[i][j] = 1; // mark for generation
} else {
newCells[i][j] = cells[i][j];
}
}
}
}
// restoring values into original array
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 11; j++) {
cells[i][j] = newCells[i][j];
}
}
}
// driver function
int main()
{
ofstream myfile;
myfile.open ("output.txt");
int life[12][12] =
{{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}};
myfile << " Game of Life --------------" << endl;
int round = 1;
myfile << " Original configuration" << endl;
print(life, myfile);
while(round <= 4) {
myfile << " Round" << round << endl;
simulate(life);
print(life, myfile);
round++;
}
}
Sample Output:
Game of Life
--------------
Original configuration
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 0 1 0 0 0
2 0 0 0 1 0 1 0 1 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 1 0 1 0 1 0 0 0
5 0 0 0 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