Question

Task: Simulate the growth of a biological population. The Game of Life, invented by John H....

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

---------------------------------------------------------------------------------------------------------------------------------------------

So here is code and I want the output to send to the data file, not on the console, Also, it should run 4 generations.

Please, help me!

#include <iostream>
#include <fstream>


using namespace std;

const int SIZE = 12;
const int ALIVE = 1;
const int DEAD = 0;


void printWorld(int x[SIZE][SIZE], fstream &fout)
{
int i, j;
cout<<"\n";
fout<<"\n";
for(i = 0; i < SIZE; i++)
{
for(j = 0; j < SIZE; j++)
if(x[i][j] == ALIVE)
{
cout << " 1 ";
fout << " 1 ";
}  
else
{
cout << " 0 ";
fout << " 0 ";
}  
cout << "\n";
fout << "\n";
}
}

int evolve(int x[][SIZE], int row, int col)
{
int i = row;
int j = col;
return x[i-1][j-1] + x[i-1][j] + x[i-1][j+1] + x[i][j-1] + x[i][j+1] + x[i+1][j-1] + x[i+1][j] +x[i+1][j+1];
}

void copyWorld(int x[][SIZE], int y[][SIZE])
{
int i, j;
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
y[i][j] = x[i][j];
}

int extinct(int x[][SIZE])
{
int i, j, flag = 0;
for(i = 0; i < SIZE; i++)
for(j = 0; j < SIZE; j++)
if(x[i][j] == 1)
return 1;
return flag;
}

int main()
{
int i, j;
int nextGeneration[SIZE][SIZE];
int currentGeneration[SIZE][SIZE] = { {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} };

char choice;
fstream fout;

fout.open("ConwayGenerations.txt", fstream::app);
while(1)
{
copyWorld(currentGeneration, nextGeneration);

for(i = 1; i < SIZE-1; i++)
for(j = 1; j < SIZE-1; j++)
if(evolve(currentGeneration, i, j) < 2 || evolve(currentGeneration, i, j) > 3)

nextGeneration[i][j] = DEAD;
else if(evolve(currentGeneration, i, j) == 3)
nextGeneration[i][j] = ALIVE;

cout << "OldWorld:\n";
printWorld(currentGeneration, fout);
cout << endl;

cout <<"NewWorld:\n";
printWorld(nextGeneration, fout);
cout << endl;

cout << "Do you want to evolve into further generation?(Y/N): ";
cin >>choice;

copyWorld(nextGeneration, currentGeneration);
if(choice == 'N' || choice == 'n')
return 0;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to simulate Game of life for the given original configuration and write the output for 4 generations in a file

#include <iostream>

#include <fstream>

using namespace std;

const int SIZE = 12;

const int ALIVE = 1;

const int DEAD = 0;

void printWorld(int x[SIZE][SIZE], fstream &fout);

int evolve(int x[][SIZE], int row, int col);

void copyWorld(int x[][SIZE], int y[][SIZE]);

int extinct(int x[][SIZE]);

int main() {

               int i, j;

               int nextGeneration[SIZE][SIZE];

               int currentGeneration[SIZE][SIZE] = { {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} };

               // open the file in append mode

               fstream fout;

               fout.open("ConwayGenerations.txt", fstream::app);

               int num_generation = 0;

               fout<<"\t\tGame of life\n";

               fout<<"\t\t---------------------\n\n";

               fout<<"Original Configuration:\n";

               printWorld(currentGeneration, fout);

               // loop to simulate for 4 generation of growth

               while(num_generation < 4)

               {

                              copyWorld(currentGeneration, nextGeneration);

                              for(i = 1; i < SIZE-1; i++)

                                             for(j = 1; j < SIZE-1; j++)

                                                            if(evolve(currentGeneration, i, j) < 2 || evolve(currentGeneration, i, j) > 3)

                                                                           nextGeneration[i][j] = DEAD;

                                                            else if(evolve(currentGeneration, i, j) == 3)

                                                                           nextGeneration[i][j] = ALIVE;

                              fout <<"Configuration "<<(num_generation+1)<<"\n";

                              printWorld(nextGeneration, fout);

                              copyWorld(nextGeneration, currentGeneration);

                              num_generation++;

               }

               fout.close();

               return 0;

}

void printWorld(int x[SIZE][SIZE], fstream &fout)

{

               int i, j;

               fout<<"\n\t";

               for(i=0;i<SIZE;i++)

               {

                              fout<<i<<"\t";

               }

               fout<<"\n\n";

               for(i = 0; i < SIZE; i++)

               {

                              fout<<i<<"\t";

                              for(j = 0; j < SIZE; j++)

                                             {

                                                            fout<<x[i][j]<<"\t";

                                             }

                              fout << "\n";

               }

               fout<<"\n";

}

int evolve(int x[][SIZE], int row, int col)

{

               int i = row;

               int j = col;

               return x[i-1][j-1] + x[i-1][j] + x[i-1][j+1] + x[i][j-1] + x[i][j+1] + x[i+1][j-1] + x[i+1][j] +x[i+1][j+1];

}

void copyWorld(int x[][SIZE], int y[][SIZE])

{

               int i, j;

               for(i = 0; i < SIZE; i++)

                              for(j = 0; j < SIZE; j++)

                                             y[i][j] = x[i][j];

}

int extinct(int x[][SIZE])

{

               int i, j, flag = 0;

               for(i = 0; i < SIZE; i++)

               for(j = 0; j < SIZE; j++)

               if(x[i][j] == 1)

                              return 1;

               return flag;

}

// end of program

Output:

Game of life original Configuration: 0 123 4 5 678 9 10 11 1 0 00 0 1 01 0 10 0 0 2 0 001 01 0 1000 0 3 0 00 000 0 000 0 0 4

Configuration <2 0 1 23 45678910 11 1 0 00 0 1 0 0 100 0 0 2 0 001 000 0 1 00 0 3 0 00 1 00 0 0 10 0 0 4 0 00 0 1 0 0 100 0 0

1 000000000000 1 000000000000 9 000000000000 8 001010000000 7 001001000000 6 000000000000 5 000000000000 4 001001000000 3 001

Add a comment
Know the answer?
Add Answer to:
Task: Simulate the growth of a biological population. The Game of Life, invented by John H....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • Write a program that consists of a 10 by 10 game board, with 10 generations. Project...

    Write a program that consists of a 10 by 10 game board, with 10 generations. Project The Game of Life The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell. Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has...

  • Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the...

    Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has more than 3 neighbors- dies of crowding. Its cell will be empty in the next...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT