Hi:

I wasn't able to find the exact solution to my problem on Daniweb, although there are several posts regarding the game of life. This situation came the closest: http://www.daniweb.com/software-development/cpp/threads/242338
My situation is a bit different because I HAVE to use a combination of functions and a class in the client program; it's 3 files total: client, specification(header), and the file that holds all the defitions for the secification file. So I'm not sure what I'm doing wrong to end up with blank cells. I think that the problem is with my tally function in the client file/program.

I will first post my assignment, then my specification file, then my class member definitions, and lastly my client file.

...thanks in advance for any help.

Problem

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.

Specifications

The grid of cells is represented by a two-dimensional array. 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.

After your program has created the two-dimensional array 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.
Please make sure that your output matches the correct output exactly.

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. Your client program must use this class rather than declaring its own arrays. You must not use any arrays in your client program.

The size of each dimension of the matrix must be specified near the top of the specification file by two global named constants. You must use a 20 by 20 array to start. These named constants will also be used to limit the loops that process the array in both the member functions and the client code. When processing the array, do not access any memory outside of the array bounds.

Keep in mind as you write the class that it is to be completely unrelated to the game of Life. The class should not mention anything about life, cells, neighbors, etc. The class will have exactly one private data member that is an array of bool. The class will have seven member functions:

default constructor: initialize all of the array elements to false
get: return the current contents of a single array element. Use arguments to indicate the row and column of the array element that should be returned.
set: set the contents of a single array element. Use arguments to indicate the row and column of the array element that should be set, and the value to which it should be set.
rowcount: return the number of true values that exist in any given row
colcount: return the number of true values that exist in any given column
totalcount: return the number of true values that exist in the entire array
print: display the contents of the array, including the row and column indices (see the posted correct output). For each element of the array, a true value must be displayed as an asterisk ("*") and a false value must be displayed as a space. This member function is the only one that displays output.
Use const to indicate const functions and unchangeable reference parameters whenever possible.

In the client code, a minimum of three functions is required in addition to the main function. Remember our discussions about functional cohesion, value-returning vs. void functions, and value parameters vs. reference parameters.

The only documentation required is the initial file documentation for each of the three files. Note that this means you must provide pre/post conditions in your header file.

Hints

Use iterative development.

Keep straight that the array is a data member within the class object and must be accessed accordingly.

I think the most difficult part is counting the neighbors. Try to keep this code as simple as possible.

Turn in

The program code from each of the three files.
Output showing generation 5 for a 20 by 20 grid.
Output showing generation 8 for a grid of 21 rows and 22 columns. The change in the grid size should be accomplished by changing only the two named constants in the specification file.

#include <iostream>
#include "boolmatrix.h"

using namespace std;

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 << "  " << 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;
    }
}

Header file:

#include <iostream>
#ifndef boolmatrix_h
#define boolmatrix_h

const int NUM_ROWS = 20;
const int NUM_COLS = 20;

class boolMatrix
{
public:
    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: 
    bool matrix[NUM_ROWS][NUM_COLS];
};

#endif /* boolmatrix_h */

Client file (this is where the issue is):

///////////////Client File\\\\\\\\\\\\\\\\\\\\\\\\
#include <iostream>
#include <fstream>
#include <cassert>
#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);
void 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();
    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)    {
    ifstream infile("life.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)    {
    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);
            }
        }
    }
}

void 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();
    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;
}

Recommended Answers

All 21 Replies

tallyLiveNeighbors appears to, at best, allow a total of one live neighbor. I imagine you want to change those "else if" lines to "if" lines.

You may have to ask a question. I missed where you asked a question in all this. In fact the question mark only appears in your code.

That's one fine dump you have here but what is the question? Few will dive into 100+ lines of code to find where there's an error.
Try again but tell what's causing you to ask for help and narrow it down to a line of code or at most a function.

Hard to test this without the data file.

Also, it's a good idea to quote the assignment spec (see the quote mark in the box above where you type your post) and leave YOUR thoughts unquoted. It makes it faster for readers to zoom in on what you need help on. I believe your description of the problem is actually way at the top (quoted by me below). It's a tad vague, but it does state a problem and points to a function. However, there's a lot to wade through to grab that line. If you quote the spec, and perhaps bold the description of the problem, it can stand out more. Oh, and use the function's name (tallyLiveNeighbors). It's more precise than "my tally function". Again, anything to make it so the reader can quickly zoom in on the problem(s).

So I'm not sure what I'm doing wrong to end up with blank cells. I think that the problem is with my tally function in the client file/program.

Hard to test this without the data file.

That too.

Also, it's a good idea to quote the assignment spec

Could be copyright issues with that. Probably best to paraphrase.

Hey guys, sorry about that, my question is, why does it not show any output for anything over 0 generations? I need it to show output for 5 generations and it is just blank!

Heres the data file:

0   0
   0   5
   0   9
   0  19
   1   6
   1  14
   1  16
   2   4
   2   7
   2  11
   2  14
   2  15
   2  16
   2  18
   3   2
   3   5
   3   9
   3  10
   3  17
   3  19
   4   0
   4   3
   4   5
   4   9
   5   2
   5   3
   5   5
   5  15
   5  16
   5  17
   6   5
   6   6
   6  10
   6  11
   6  13
   6  14
   6  17
   6  18
   7   6
   7   8
   7  14
   7  16
   8   1
   8   5
   8   8
   8  10
   8  17
   9   3
   9   4
   9   6
   9   7
  10   0
  10   9
  10  19
  11   1
  11   2
  11   3
  11   5
  11   7
  11   8
  11  14
  11  17
  11  19
  12   4
  12   5
  12   6
  12   8
  13   0
  13   1
  13   7
  13  10
  13  11
  13  14
  13  15
  13  16
  13  17
  13  18
  14   2
  14  11
  14  13
  14  15
  14  17
  14  19
  15  11
  15  13
  15  18
  16   1
  16   2
  16   5
  16   8
  16  17
  17   5
  17   6
  18   6
  18  16
  18  18
  19   0
  19   3
  19   4
  19   7

I believe the issue is with tallyLiveNeighbor and checkOutBounds, just need to know what to fix so it works for 5 generations!!!

I mentioned a problem with tallyLiveNeighbors. Change your "else if" statements to "if" statements. Regarding checkOutBounds, if I pass the function the value 19 for row or column, that should be OK, but it appears that your function declares it NOT OK.

EDIT: Actually, scratch that. Look at line 114 very carefully.

it is just blank!

While debugging, you should make it so no matter what, it's not blank. Give false/dead cells a display character other than a space. Make it visible. That makes things far easier to debug. If, after you are done, you want dead cells to be blank, change it back.

I changed the if else statements to if statements, made no difference whatsoever, not sure what you are getting at there. Line 114, ok, what do I need to change those to? Would appreciate straight answers to get this cleaned up ASAP thanks.

if(((row-1) < 0) || ((row+1) > NUM_ROWS) || ((col-1)< 0) || ((col+1) < NUM_COLS))

Try a test case. row = 0, col = 0. Let's go through this. This is a legal value, so the if statement should evaluate to false.

if((-1 < 0) || (1 > 20) || (-1 < 0) || (1 < 20))

changes to...

if(true || false || true || true)

evaluates to true, so there is a problem there.

Try...

if(row < 0 || row >= NUM_ROWS || col < 0 || col >= NUM_COLS)

I changed the if else statements to if statements, made no difference whatsoever, not sure what you are getting at there.

It makes a huge difference. The way you had it, your liveNeighbors count could only be incremented by at most 1. With all "if" statements, it could potentially be incremented by 8. If a cell dies whenever the number of live neighbors is less than 2, your entire matrix will be dead by the second generation with your original code if everything else works right.

Ok, so now its working with all generations however this is the output for generation 5

  01234567890123456789
  0               ***  
  1    **       * *    
  2*** *        ****   
  3    * **     *      
  4 ** * **     *     *
  5    *       *    *  
  6*  * *****  * * *  *
  7*   *     ***  ** * 
  8**   ***         ** 
  9*  * * * **      *  
  10 * *    * *     *  *
  11 **     * * *** **  
  12 **      *  *     **
  13*                 * 
  14       **     **    
  15*     * **   * *   *
  16**   ** **        * 
  17     * *         * *
  18*   *  **       ** *
  19 *  *****       *  *
Total alive in row 10 = 6
Total alive in col 10 = 4
Total alive = 122

And this is what the correct output should be:

  01234567890123456789
 0                    
 1         *        * 
 2       *   * ***  * 
 3       *   **** *   
 4      *  * *    **  
 5    *  *   *****  * 
 6 ** *     *    *** *
 7    * ***       *  *
 8               **** 
 9 *         ******   
10* *          *      
11   *  *      **   * 
12**    ****       ***
13  *         *** ** *
14      *  *   *   ***
15 **   ***      ** **
16      *         * **
17                    
18                    
19                    
Total alive in row 10 = 3
Total alive in col 10 = 1
Total alive = 95

Ok, so now its working with all generations

The fact that it's running to completion and not crashing is just dumb luck. If you put the assert statements back in your get function, it will abort prematurely because you are calling get from your tallyLiveNeighbors function with parameters -1 and -1 for row and column. See line 66 in tallyLiveNeighbors when row and col are both 0. You call get with parameters row - 1 and col - 1, which is -1 and -1. Thus get is trying to access negative array offsets, which is not allowed. Again, it's just dumb luck that the program isn't crashing. It IS, however, returning jibberish from the get function. You are calling get first, THEN calling checkOutBounds to see if the parameters are legal. That's backwards. Try checking the boundaries FIRST. A quick change to checkOutBounds...

// returns true if row and col are within valid range, false otherwise
bool checkOutBounds(int row, int col)
{
    if(row < 0 || row >= NUM_ROWS || col < 0 || col >= NUM_COLS)
    {
        return false;
    }
    return true;
}

Now change the tallyLiveNeighbors function so that get is ONLY called if checkOutBounds has determined that the row and column are legal array indexes.

void tallyLiveNeighbors(boolMatrix& generation,int& liveNeighbors,int row,int col)
{
    // note that I took out the == true part.  It is not needed
    if(checkOutBounds(row-1,col-1) && generation.get(row-1,col-1)){
        liveNeighbors++;
    }
    // do similar adjustment for the other 7 cases.
}

I think you have problems besides this, but this is a big one.

Cool did all that, thanks a bunch! Output is this:

How many generations?: 
5
  01234567890123456789
  0*    *   *         *
  1      *       * *   
  2    *  *   *  *** * 
  3  *  *   **      * *
  4*  * *   *          
  5  ** *         ***  
  6     **   ** **  ** 
  7      * *     * *   
  8 *   *  * *      *  
  9   ** **            
  10*        *         *
  11 *** * **     *  * *
  12    *** *           
  13**     *  **  ***** 
  14  *        * * * * *
  15           * *    * 
  16 **  *  *        *  
  17     **             
  18      *         * * 
  19*  **  *            
Total alive in row 10 = 3
Total alive in col 10 = 4
Total alive = 100

  01234567890123456789
  0                    
  1              * **  
  2          **  * * * 
  3    **   *       ***
  4  *  **             
  5  **          ***** 
  6     ***     *    * 
  7        **          
  8    *** *           
  9   ** ****          
  10     *   *        **
  11       ***        **
  12      *  *     *   *
  13          **** * *  
  14             *  **  
  15                * * 
  16     ***         *  
  17       *         ** 
  18      ***        ** 
  19      *             
Total alive in row 10 = 4
Total alive in col 10 = 2
Total alive = 78

  01234567890123456789
  0                    
  1               ***  
  2          *    *    
  3    ***             
  4  *   *        * ** 
  5  ** * *      **  **
  6    ** *          **
  7        **          
  8   * **             
  9   *   ***          
  10                  **
  11       ***         *
  12         * *  *** **
  13          ** *    **
  14            **  * * 
  15      **        * * 
  16     * **       * **
  17                *  *
  18      **         ** 
  19      **            
Total alive in row 10 = 2
Total alive in col 10 = 2
Total alive = 75

  01234567890123456789
  0                **  
  1               * *  
  2     **             
  3    * **            
  4  * *  **     ******
  5  *  * *      * *   
  6    ** *            
  7       **           
  8      *  *          
  9       ***          
  10                  **
  11        ***    **   
  12        *  *** ** **
  13          *   * *  *
  14                * **
  15      ***      **   
  16      * *       * **
  17      *         *  *
  18     * *         ** 
  19      **            
Total alive in row 10 = 2
Total alive in col 10 = 2
Total alive = 75

  01234567890123456789
  0                **  
  1                **  
  2     ***            
  3   **   *      * *  
  4  * ** **     *  ** 
  5   *   *            
  6    * * *           
  7     *  **          
  8      *             
  9       **           
  10       *            
  11       * ***  * ****
  12        *   **  *  *
  13             ** *  *
  14       **     * * **
  15      * **      *   
  16     ** **     ** **
  17       *       *   *
  18       **           
  19      ***           
Total alive in row 10 = 1
Total alive in col 10 = 1
Total alive = 76

Looks like your output's still off and I believe I know why. Take a simple 2 x 3 matrix, as follows, X is alive, O is dead.

XXX
OOO

The (0,0) element has 1 live neighbor, so it should die. (0,1) has 2 live neighbors, so it should live.

First your loop checks (0,0) and tallies 1 neighbor, so it marks (0,0) dead. So the matrix is now

    OXX
    OOO

Now we calculate (0,1). It has 1 live neighbor, so it dies. But wait, it's supposed to have 2 live neighbors and live!!!

The problem is that you are overwriting your one matrix as you go through your loop. But every cell needs to figure out the numebr of live neighbors from the PREVIOUS generation, so if you change (0,0)'s value, that's going to mess up your live neighbor count because the correct count is calculated from the LAST generation and you have (0,0) written as dead in THIS generation, so the fact that it was alive before is lost by the time you calculate the (0,1) cell.

Solution? Before every new generation, make a copy of the last generation. You DECIDE based on the LAST generation and you CHANGE the values of the CURRENT generation. Thus you need to have two matrices and make a copy constructor and/or assignment operator in your boolMatrix class. You might want to stick some "const" qualifiers in your functions to make it clear what is allowed to change and what is not.

Ok, so according to my code what needs the change, and how exactly? I appreciate all your help.

Last post. I'm going to just give you the file. You can write the copy constructor yourself. I encourage you to study the code. A huge part of programming is the troubleshooting and experimentation part. You have to make intelligent test cases and test them. Think of the algorithm, put it in English words and think of it mathematically. THEN code. Also go through your code line by line on paper and see what's going on. You'll end up a far better coder and you'll be able to debug your own programs better. So here's an updated client file. Note that I got rid of a few functions that were just complicating things, I thought, and some parameters that were passed unnecessarily. Also, to match the printout, I changed the < to <= in line 27 and I made your "first generation" the "zero generation". I did that because it seems to match the output your professor wants. You type in "5" and you'll get 6 generations (generation 0 through generation 5, generation 0 being the generation from input file). Also note I added some "const" to a few of the function parameters. You may or may not need to change your boolMatrix class to add some const qualifiers, but you'll definitely need to add a copy constructor.

///////////////Client File\\\\\\\\\\\\\\\\\\\\\\\\
#include <iostream>
#include <fstream>
#include <cassert>
#include "boolmatrix.h"
using namespace std;

void zerogen(boolMatrix& generation);
void getNextGen(boolMatrix& generation);
bool checkOutBounds(int row, int col);
void printResults(const boolMatrix& generation);
int tallyLiveNeighbors(const boolMatrix& generation,int row,int col);

int main()
{
    boolMatrix generation;
    int numGen;
    cout << "How many generations in total?: " << endl;
    cin >> numGen;
    zerogen(generation);
    generation.print();
    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;

    // changed less than to less than or equal below
    for(int count = 1; count <= numGen; count++)
    {
        getNextGen(generation);
        printResults(generation);
    }

    cin.get(); // put system("pause") back in if you want.
    return 0;
}

// changed from 1st generation to 0 generation
void zerogen(boolMatrix& generation)    {
    ifstream infile("life.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;
    const boolMatrix lastGen(generation); // call to copy constructor
    for(int row = 0; row < NUM_ROWS; row++)
    {
        for(int col = 0; col < NUM_COLS; col++)
        {
            liveNeighbors = tallyLiveNeighbors(lastGen, row, col);
            if(liveNeighbors == 3)
                generation.set(row, col, true);
            else if(liveNeighbors == 2 && lastGen.get(row, col))
                generation.set(row, col, true);
            else
                generation.set(row, col, false);
        }
    }
}

int tallyLiveNeighbors(const boolMatrix& generation,int row,int col)
{
    int liveNeighbors = 0;

    // note that I took out the == true part.  It is not needed
    if(checkOutBounds(row-1,col-1) && generation.get(row-1,col-1))
        liveNeighbors++;
    if(checkOutBounds(row-1,col) && generation.get(row-1,col))
        liveNeighbors++;
    if(checkOutBounds(row-1,col+1) && generation.get(row-1,col+1))
        liveNeighbors++;
    if(checkOutBounds(row,col-1) && generation.get(row,col-1))
        liveNeighbors++;
    if(checkOutBounds(row,col+1) && generation.get(row,col+1))
        liveNeighbors++;
    if(checkOutBounds(row+1,col-1) && generation.get(row+1,col-1))
        liveNeighbors++;
    if(checkOutBounds(row+1,col) && generation.get(row+1,col))
        liveNeighbors++;
    if(checkOutBounds(row+1,col+1) && generation.get(row+1,col+1))
        liveNeighbors++;
    return liveNeighbors;
}

bool checkOutBounds(int row, int col)
{
    if(row < 0 || row >= NUM_ROWS || col < 0 || col >= NUM_COLS)
    {
        return false;
    }
    return true;
}

void printResults(const boolMatrix& generation)
{
    generation.print();
    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;
}

THANK YOU THANK YOU THANK YOU. I LOVE YOU!!!

Purely for interest sake, here is the generator for the game of life in APL

Screenshot_(37).png

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.