Question
C++ Program Part 2 See Part 1 FOR REFRENCE Below ... Your task is to write a program that plays the game of life. This
C++ Program Part 2
See Part 1 FOR REFRENCE Below ...
Your task is to write a program that plays the game of life. This game is a computer simulation of the life and death events in a population of single-cell organisms. Each position in a two-dimensional grid (Petri dish) can support one cell. The program determines whether each position will be able to support life in the next generation. The grid of cells is represented by a boolMatrix object. In other words, you are writing a program that USES the boolMatrix class. No changes should be made to boolmatrix.h or boolmatrix.cpp! The starting grid is generation 0 and is read from a supplied input file. Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each. The rules for the state of each position in the next generation of the grid are as follows: If the cell is currently empty: If the cell has exactly three living neighbors, it will come to life in the next generation. If the cell has any other number of living neighbors, it will remain empty. If the cell is currently living: If the cell has one or zero living neighbors, it will die of loneliness in the next generation. If the cell has four or more living neighbors, it will die of overcrowding in the next generation. If the cell has two or three neighbors, it will remain living. All births and deaths occur simultaneously. This point is critical to the correct result. The following three webpages are also available: the data file, the correct output, and the output including intermediate generations. The last webpage is available to help you debug your code. The data file supplies the data for generation 0. Each line of data gives a pair of coordinates (row# column#) for a living cell in the original grid. Assume that every number in the text file is between 0 and 19. All other grid positions are empty. Please name your file "life.txt". After your program has created the boolMatrix object that represents generation 0, your program must allow life to proceed for the number of generations specified by the user. Start with five generations. Your program should then display a grid on the screen to show the final results. Use a star (*) to represent a live cell and a space to represent a dead cell. After the grid, display the following statistical information: The number of living cells in the entire board. The number of living cells in row 10. The number of living cells in column 10. Additional Requirements Make sure that your output matches the correct output exactly. To repeat, this program must use the boolMatrix class to represent the grid, rather than declaring its own arrays. You must not use any arrays in your client program. In this program, a minimum of four functions is required in addition to main(). In my solution there are five functions besides main(). Remember our discussions about stepwise refinement, value-returning vs. void functions, and value parameters vs. reference parameters. Be sure to document this program thoroughly, as always. The output that you provide with your submission should show generation 5 for a 20 by 20 grid. And also generation 8 for a grid of 21 rows and 22 columns. The change in grid size should be accomplished by changing only the two named constants in the specification file.
These were instructons for Part 1 of the Assignment for refrence, and the code follows that
This assignment will not be graded for style issues. If your code works correctly, you'll get full points. Style will be graded next week.
Create a class named boolMatrix for storing and processing a two-dimensional array of bool values. The class should be saved in a specification file and an implementation file. A client file is not provided. You'll need to write your own client file to test your class. I'll be creating my own client program to test your class after you submit it.
You'll be defining two constants in your class, but you need to know a little bit about "static" members of classes first. With all of the class members we have used so far, each class object that the client declares has its own copy. With a "static" member, there is just one copy of the member that all objects share. Since constants don't change, it makes sense to just have one copy that all objects share. So, when you declare constants, you must make them static.
In the public area of your class you must define two public constants, NUM_ROWS AND NUM_COLS. I'll give you this code for free:
static const int NUM_ROWS = 20; static const int NUM_COLS = 20;
In your client program, if you need to use these constants, you can't access them in the usual way of using a calling object and a dot operator, because they aren't associated with one particular object. So you'll use the scope resolution operator instead. For example:
if (myRow < boolMatrix::NUM_ROWS)
There are a few places in the descriptions of the member functions below where you are required to use "assert()". Here is a quick tutorial on using assert. First, you need to place #include
Part 1 CODE
#include
#ifndef BOOLMATRIX_H
#define BOOLMATRIX_H
const int NUM_ROWS = 20;
const int NUM_COLS = 20;
class boolMatrix
{
public: // Available for clients use.
boolMatrix();
bool get(int row, int col) const;
void set(int row, int col, bool value);
int rowCount(int row) const;
int colCount(int col) const;
int totalCount() const;
void print()const;
private: // Can only be used within the class, Client cannot call it.
bool matrix[NUM_ROWS][NUM_COLS];
};
#endif // BOOLMATRIX_H
/////////Class Member Functions////////////
#include
#include
#include "boolmatrix.h" // class's header file
using namespace std;
// class constructor
boolMatrix::boolMatrix()
{
for (int row = 0; row < NUM_ROWS; row++){
for (int col = 0; col < NUM_COLS; col++){
matrix[row][col] = false;
}
}
}
bool boolMatrix::get(int row, int col) const
{
return matrix[row][col];
}
void boolMatrix::set(int row, int col, bool value)
{
matrix[row][col] = value;
}
int boolMatrix::rowCount(int row) const
{
int rowtotal = 0;
for (int col = 0; col < NUM_COLS; col++){
if ( matrix[row][col] == true ){
rowtotal++;
}
}
return rowtotal;
}
int boolMatrix::colCount(int col) const
{
int colTotal = 0;
for (int row = 0; row < NUM_ROWS; row++){
if ( matrix[row][col] == true ){
colTotal++;
}
}
return colTotal;
}
int boolMatrix::totalCount() const
{
int total = 0;
for (int row = 0; row < NUM_ROWS; row++){
for (int col = 0; col < NUM_COLS; col++){
if ( matrix[row][col] == true ){
total++;
}
}
}
return total;
}
void boolMatrix::print() const
{
cout << " ";
for (int col = 0; col < NUM_COLS; col++)
{
cout << col % 10;
}
cout << endl;
for (int row = 0; row < NUM_ROWS; row++)
{
cout << setw(2) << row % 100;
for (int col = 0; col < NUM_COLS; col++){
if ( matrix[row][col] == true ){
cout << "*";
} else if ( matrix[row][col] == false ){
cout << " ";
}
}
cout << endl;
}
}
///////////////Client File\\\\\\\\\\\\\\\\\\\\\\\\
#include
#include
#include
#include "boolmatrix.h"
using namespace std;
void firstgen(boolMatrix& generation);
void getNextGen(boolMatrix& generation,int liveNeighbors,int row,int col);
void fateDeadCell(boolMatrix& generation,int liveNeighbors,int row,int col);
void fateLiveCell(boolMatrix& generation,int liveNeighbors,int row,int col);
void checkOutBounds(int row, int col, int& liveNeighbors);
void printResults(boolMatrix& generation);
int tallyLiveNeighbors(const boolMatrix& generation, int& liveNeighbors,int row,int col);
int main()
{
boolMatrix generation;
int numGen, liveNeighbors, row, col;
cout << "How many generations in total?: " << endl;
cin >> numGen;
firstgen(generation);
generation.print(); //prints first generations.
cout << "Total alive in row 10 = " << generation.rowCount(10) << endl;
cout << "Total alive in col 10 = " << generation.colCount(10) << endl;
cout << "Total alive = " << generation.totalCount() << endl << endl;
for(int count = 1; count < numGen; count++)
{
getNextGen(generation,liveNeighbors,row,col);
printResults(generation);
}
system("pause");
return 0;
}
void firstgen(boolMatrix& generation) //stores data file info into array.
{
ifstream infile("lifedata.txt");
assert(infile);
int row, col;
infile >> row >> col;
while (infile) {
generation.set(row, col, true);
infile >> row >> col;
}
infile.close();
}
void getNextGen(boolMatrix& generation,int liveNeighbors,int row,int col) //gets all subsequent generations.
{
liveNeighbors = 0;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
if(generation.get(row,col) == false){
fateDeadCell(generation,liveNeighbors,row,col);
}else if(generation.get(row,col) == true){
fateLiveCell(generation,liveNeighbors,row,col);
}
}
}
}
int tallyLiveNeighbors(boolMatrix& generation,int& liveNeighbors,int row,int col)
{
if(generation.get(row-1,col-1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row-1,col) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row-1,col+1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row,col-1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row,col+1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row+1,col-1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row+1,col)== true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}else if(generation.get(row+1,col+1) == true){
liveNeighbors++;
checkOutBounds(row, col, liveNeighbors);
}
}
void fateDeadCell(boolMatrix& generation,int liveNeighbors,int row,int col)
{
tallyLiveNeighbors(generation,liveNeighbors,row,col);
if(liveNeighbors == 3){
generation.set(row,col,true);
}else if ((liveNeighbors < 3) || (liveNeighbors > 3)){
generation.set(row,col,false);
}
}
void fateLiveCell(boolMatrix& generation,int liveNeighbors,int row,int col)
{
tallyLiveNeighbors(generation,liveNeighbors,row,col);
if((liveNeighbors <= 1) || (liveNeighbors >= 4)){
generation.set(row,col,false);
}else if((liveNeighbors == 2) || (liveNeighbors == 3)){
generation.set(row,col,true);
}
}
void checkOutBounds(int row, int col, int& liveNeighbors)
{
if(((row-1) < 0) || ((row+1) > NUM_ROWS) || ((col-1)< 0) || ((col+1) < NUM_COLS))
{
liveNeighbors--;
}
}
void printResults(boolMatrix& generation)
{
generation.print(); //prints subsequent generations
cout << "Total alive in row 10 = " << generation.rowCount(10) << endl;
cout << "Total alive in col 10 = " << generation.colCount(10) << endl;
cout << "Total alive = " << generation.totalCount() << endl << endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started