Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago