Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

John Conway\'s Game of Life ....Someone Please Help For this project, you will c

ID: 3785754 • Letter: J

Question

John Conway's Game of Life ....Someone Please Help

For this project, you will complete the provided partial C++ program that implements a modified version of John Conway's Game of Life cellular automata simulation of a biological system. In our version, there are two different types of living cells, Type1 and Type2, that may evolve based upon the rules summarized below. The simulation consists of a square grid of cells (hint: 2-D array) where each cell is either dead or alive. The current arrangement of living and dead cells is used to compute the next generation of cells using the following rules that determine the Birth, Survival, and Death of each cell.

To make this simulation reconfigurable, you will be inputting the number of iterations, the birth and survival constraints, and the initial arrangement of living cells (2's or 1's) and dead cells (0's) from a file.

The simulation algorithm implemented by the main()function from the provided file main.cpp is summarized below:

(1) Load the number of iterations, birth rules and survival rules from the input file Hint: First line of file is comment describing file contents. Skip this comment.

(2) Load the initial arrangement of cells from the input file into the current grid

(3) Output initial grid in desired format (Hint:Iteration 0 is the initial grid from the file)

(4) Use birth/survival/death rules to compute next grid of cells from current grid of cells

(5) Copy (overwrite) current grid with next grid contents

(6) Display current grid (which now contains the new arrangement of cells) in desired format. Hint:PrintGrid function provided prints out grid in an easy to read format

(7) Repeat (4) –(7) until desired number of generations have been simulated

To implement the above algorithm, the main()function utilizes a number of support functionsthat you must implement and test.

SupportFunctions

void OpenInputFile(string filename, ifstream& inFile);

// OpenInputFile --opens file whose name is stored in filename

void LoadConstraints(ifstream& inFile, int& num, string& bstring, string& sstring);

// LoadConstraints --loads only simulation constraints from inFile after skippingheader comment. Constraints input in order num, birth string, survival string

void LoadGrid(ifstream& inFile, int grid[][CMAX]);

// LoadGrid --loads the cell grid from inFile

void ComputeNextGrid(int current[][CMAX], int next[][CMAX], int birth[], int survival[]);

// ComputeNextGrid --uses current generation to compute next generation usingconstraints specified in birth and survival arrays

void CopyGrid(const int source[][CMAX], int destination[][CMAX]);

// CopyGrid --copies contents of source array into destination array

intCountType1Neighbors(int grid[][CMAX], int row, int col);

// CountType1Neighbors --counts the total number of LIVING Type1 neighbors for the cellat the grid position specified by row and col.

intCountType2Neighbors(int grid[][CMAX], int row, int col);

// CountType2Neighbors --counts the total number of LIVING Type2 neighbors for the cellat the grid position specified by row and col.

void ParseRequirementsString(string requirements, int reqs[]);

// ParseRequirementsString --takes a birth or survival string and converts it to integer array assumes that the B or S appears as the first character. Order of constraints does not matter.

// For example, B13 should produce same result as B31

//

// (1's in both reqs[1] and reqs[3], zeros elsewhere)

Hint: use string functions to help you scan through each character in therequirements string one at a time and use nested IF or SWITCH to implement logic

Template to be used – Please explain how to implement each part of the program

//

// main.cpp

// Conway's Game of Life with edge wrapping,

// programmable birth and survival rules

// for Type 1 and Type 2 cells

//

#include

#include

#include

using namespace std;

// Global declarations

const int RMAX = 10;       // Maximum number of rows in grid

const int CMAX = 10;       // Maximum number of columns in grid

const int MAXAGE = 8;             // Maximum number of generations any cell can survive

const string BARS = "==========================================================";

// Function prototypes

void OpenInputFile(string filename, ifstream& inFile);

void LoadConstraints(ifstream& inFile, int& num, string& bstring, string& sstring);

void LoadGrid(ifstream& inFile, int grid[][CMAX]);

void PrintGrid(int grid[][CMAX]);

void ComputeNextGrid(int current[][CMAX], int next[][CMAX], int birth[], int survival[]);

void CopyGrid(const int source[][CMAX], int destination[][CMAX]);

int CountType1Neighbors(int grid[][CMAX], int row, int col);

int CountType2Neighbors(int grid[][CMAX], int row, int col);

void ParseRequirementsString(string requirements, int reqs[]);

int main(int argc, char* argv[])

{

ifstream inFile;                    // Input stream for reading grid file

string    filename;                              // Name of grid file

string    bstring;                   // Birth requirements as C++ string

string    sstring;                   // Survival requirement as C++ string

int       currentgrid[RMAX][CMAX];   // Current cell grid

int       nextgrid[RMAX][CMAX];      // Next cell grid

int       num;                       // Number of iterations

int      birth[9], survival[9];     // Birth and survival look up arrays

if (argc != 2)

{

    cout << "Usage: project01 " << endl;

    return 0;

}

else

    filename = argv[1];

      

OpenInputFile(filename, inFile);     // Attempt to open grid file

if (!inFile)

{

    cout << " Error: unable to open '" << filename << "' for input Terminating now... ";

    return 0;

}

else

{

    cout << " File '" << filename << "' opened for input..." << endl;

}

LoadConstraints(inFile, num, bstring, sstring); // Load number of iterations, birth and survival strings

cout << " Iterations = " << num << endl;

      

// Exit if birth or survival requirements not specified, otherwise parse birth and survival strings

if ((bstring[0] != 'B') || (sstring[0] != 'S'))

{

    cout << "Error: incorrect file formatting" << endl;

    return 0;

}

else

{

    // Initialize birth and survival requirements to zero

    for(int k=0; k<9; k++)

    {

      birth[k] = 0;

      survival[k] = 0;

    }               

   

       // Convert bstring and sstring representations into birth and survival look up tables

    ParseRequirementsString(bstring, birth);

    ParseRequirementsString(sstring, survival);

       cout << " Simulation Birth/Survival Configuration ";

    for(int k=0; k<9; k++)

    {

      cout << "birth[" << k << "] = " << birth[k] << "      survival["

           << k << "] = " << survival[k] << " ";

       }                   

}

    

LoadGrid(inFile, currentgrid);              // Populate grid

cout << " Grid loaded from file. ";     

cout << BARS << endl;

cout << "Iteration = 0" << endl << endl;

PrintGrid(currentgrid);

cout << BARS << endl;

for(int iteration = 1; iteration <= num; iteration++)

{

    ComputeNextGrid(currentgrid, nextgrid, birth, survival);

    CopyGrid(nextgrid, currentgrid);

    cout << BARS << endl;

    cout << "Iteration = " << iteration << endl << endl;

    PrintGrid(currentgrid);

    cout << BARS << endl;

} // End iterations loop

return 0;                            // Done!!

} // End main()

void PrintGrid(int grid[][CMAX])

// Outputs grid in desired format

{

for(int r = 0; r < RMAX; r++)

{

    for(int c = 0; c < CMAX; c++)

    {

      switch (grid[r][c])

      {

        case 0: cout << ' ' << '-';   break;

        default: cout << ' ' << grid[r][c]; break;

      }

    }

    cout << endl;

}

} // End PrintGrid()

/***********************************************************************/

/***********************************************************************/

#include "project01.cpp"

/***********************************************************************/

/***********************************************************************/

Explanation / Answer

#include <iostream>

#include <string>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

void copy(int array1[52][102], int array2[52][102])

{

for(int j = 0; j < 52; j++)

{

                                for(int i = 0; i < 102; i++)                                                

                                                array2[j][i] = array1[j][i];

                }

}

void life(int array[52][102], char choice)

{

int temp[52][102];

copy(array, temp);

                for(int j = 1; j < 51; j++)

                {

                                for(int i = 1; i < 101; i++)                                                

                                {             

                                                if(choice == 'm')

                                                {

                                                                int count = 0;

                                                                count = array[j-1][i] +

                                                                                array[j-1][i-1] +

                                                                                array[j][i-1] +

                                                                                array[j+1][i-1] +

                                                                                array[j+1][i] +

                                                                                array[j+1][i+1] +

                                                                                array[j][i+1] +

                                                                                array[j-1][i+1];

        if(count < 2 || count > 3)

                                                                                temp[j][i] = 0;

        if(count == 2)

                                                                                temp[j][i] = array[j][i];

        if(count == 3)

                                                                                temp[j][i] = 1;

                                                }

                                                else if(choice == 'v')

                                                {

                                                                int count = 0;

                                                                count = array[j-1][i] +

                                                                                array[j][i-1] +

                                                                                array[j+1][i] +

                                                                                array[j][i+1];     

                                                                if(count < 2 || count > 3)

                                                                                temp[j][i] = 0;

        if(count == 2)

                                                                                temp[j][i] = array[j][i];

        if(count == 3)

                                                                                temp[j][i] = 1;

                                                }                                                             

                                }

                }

//Copies the completed temp array back to the main array.

copy(temp, array);

}

bool compare(int array1[52][102], int array2[52][102])

{

                int count = 0;

                for(int j = 0; j < 52; j++)

                {

                                for(int i = 0; i < 102; i++)

                                {

                                                if(array1[j][i]==array2[j][i])

                                                                count++;             

                                }

                }

                if(count == 52*102)

                return true;

                else

                return false;

}

void print(int array[52][102])

{

                for(int j = 1; j < 51; j++)

                {

                                for(int i = 1; i < 101; i++)                                                

                                {             

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

                                                                cout << '*';

                                                else

                                                                cout << ' ';

                                }

                                cout << endl;

                }

}

int main()

{

                int gen0[52][102];

                int todo[52][102];

                int backup[52][102];

                char neighborhood;

                char again;

char cont;

bool comparison;

                string decoration;

                cout << endl << "This program is a C++ implementation of John Conway's Game of Life."

                     << endl << "With it, you can simulate how "cells" interact with each other." << endl

                     << endl << "There are two types of neighborhoods you can choose, the"

                     << endl << "Moore, and the Von Neumann. The Moore neighborhood checks"

       << endl << "all 8 surrounding cells, whereas the Von Neumann checks"

       << endl << "only the 4 cardinal directions: (N, S, E, and W)." << endl

       << endl << "The rules of the "Game of Life" are as follows:" << endl

       << endl << "1. Any live cell with fewer than two live neighbors dies, as if caused by under-population."

       << endl << "2. Any live cell with two or three live neighbors lives on to the next generation."

       << endl << "3. Any live cell with more than three live neighbors dies, as if by overcrowding."

       << endl << "4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction." << endl

       << endl << "The initial configuration (Generation 0) of the board is determined randomly."

       << endl << "Every hundred Generations you will get the option to end or continue the simulation."

       << endl << "If a system becomes "stable" (meaning the system does not change from one"

       << endl << "generation to the next), the simulation will end automatically." << endl << endl;

do

                {             

    do

                                {

      cout << "Which neighborhood would you like to use (m or v): ";

                                cin >> neighborhood;

                                }while(neighborhood != 'm' && neighborhood != 'v');

    system("clear");

    int i = 0;

    do

                                {

                                                srand(time(NULL));

                                                for(int j = 1; j < 51; j++)

                                                {

                                                                for (int i = 1; i < 101; i++)

                                                                                gen0[j][i] = rand() % 2;

                                                }

                                                if(i < 10)

                                                                decoration = "#############";

                                                else if(i >= 10 && i < 100)

                                                                decoration = "##############";

                                                else if(i >= 100 && i < 1000)

                                                                decoration = "###############";

                                                else if(i >= 1000 && i < 10000)

                                                                decoration = "################";

                                                else

                                                                decoration = "#################";

                                                cout << decoration << endl << "Generation " << i

                                                     << ":" << endl << decoration << endl << endl;

      if(i == 0)

                                                                copy(gen0, todo);

                                                copy(todo, backup);                                      

                                                print(todo);                       

                                life(todo, neighborhood);

                                i++;

      system("sleep .1");

      if(i % 100 == 1 && i != 1)

      {

        cout << endl;

        do

        {

          cout << "Would you like to continue this simulation? (y/n): ";

          cin >> cont;

        }while(cont != 'y' && cont != 'n');

        if(cont == 'n')

          break;

      }

      comparison = compare(todo, backup);

      if(comparison == false)

        system("clear");

      if(comparison == true)

        cout << endl;

                                }while(comparison == false);

    do

    {

      cout << "Would you like to run another simulation? (y/n): ";

                                cin >> again;

    }while(again != 'y' && again != 'n');

                }while(again == 'y');

                return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote