0

I have an assignment for school to make a program called the game of life. This program takes values from a data file and places them in a 2d array. All the values taked are set to true in the array and any empty places default to false. Then it displays it on a grid of *'s for true and blank spaces for false. There are 20 rows and 20 colums in the grid. Then the program follows these rules to find the next generation of values from the grid:

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

I have written the code and from everything I see it should be working and compiles fine but for some reason I keep getting the wrong answer, can somone please let me know what I am doing wrong. For now all I want it to do is print the 2nd generation to the screen with the correct results.
The correct output for each grid can be found here: http://www.santarosa.edu/~dharden/10s08/proj5fullout.html

The data values that are read from the data file can be found at: http://www.santarosa.edu/~dharden/10s08/proj5data.html

I have included my main .cpp file for the code as an attachment. I was going to include the header file and the class member functions file as attachments but it wouldn't allow me to so I am posting them right here

Class code(i'm sorry for the length of this code):

//////////////////////Header//////////////////////
#include <iostream>
#ifndef BOOLMATRIX_H
#define BOOLMATRIX_H


const int rows = 20;
const int cols = 20;

 
class boolMatrix
{
	public: // Available for clients use.

	        boolMatrix();
            bool get(int row, int col);
            void set(int row, int col, bool answer);
            int rowcount(int row);
            int colcount(int col);
            int totalcount();
            void print();
		

	private: // Can only be used within the class, Client cannot call it.
            bool container[rows][cols];
};


#endif // BOOLMATRIX_H

/////////Class Member Functions////////////
#include <iostream>
#include "boolmatrix.h" // class's header file
#include <iomanip>


using namespace std;


// class constructor
boolMatrix::boolMatrix()
{
    for (int row = 0; row < rows; row++){
        for (int col = 0; col < cols; col++){    
             container[row][col] = false;
         }
    }
            
}


bool boolMatrix::get(int row, int col)
{
    return container[row][col];
}


void boolMatrix::set(int row, int col, bool answer)
{
     container[row][col] = answer;
}


int boolMatrix::rowcount(int row)
{
    int rowtotal = 0;
    
    for (int count = 0; count < rows; count++){
        if ( container[row][count] == true ){
           rowtotal++;
        }
    }
    return rowtotal;           
}


int boolMatrix::colcount(int col)
{
    int coltotal = 0;
    
    for (int count = 0; count < cols; count++){
        if ( container[count][col] == true ){
           coltotal++;
        }
    }
    return coltotal;                    
}


int boolMatrix::totalcount()
{
     int total = 0;
     for (int row = 0; row < rows; row++){
         for (int col = 0; col < cols; col++){ 
             if ( container[row][col] == true ){
                total++;
             }
         }     
     }
     return total;
     
}


void boolMatrix::print()
{
    int toptable[9];
    int sidetable[rows];
    int test = 0;
    
    cout << "  ";
    for ( int count = 0; count < 10; count++){
        toptable[count] = count;
    }
    
    for ( int row = 0; row < rows; row++){
        sidetable[row] = row;
    }

    for (int col = 0; col < cols; col++){    
        cout << toptable[test];
        test++;
        if ( test == 10 )
           test = 0;
    }
    cout << endl;

    for (int row = 0; row < rows; row++){
        cout << setw(2) << sidetable[row];
        for (int col = 0; col < cols; col++){    
            if ( container[row][col] == true ){
               cout << "*";
            } else if ( container[row][col] == false ){
              cout << " ";
            }
        }
        cout << endl;
    }      
}
Attachments
#include <iostream>
#include <fstream>
#include "boolmatrix.h"

using namespace std;

void livingcell(boolMatrix generation, boolMatrix &newgeneration, int row, int col);
void deadcell(boolMatrix generation, boolMatrix &newgeneration, int row, int col);
void firstgen(boolMatrix &generation);
void generationswap(boolMatrix &generation, boolMatrix &newgeneration);


int main()
{
    boolMatrix generation;
    boolMatrix newgeneration;
    firstgen(generation);
    int count = 0;
    for (int row = 0; row < rows; row++){
        for (int col = 0; col < cols; col++){
            if ( generation.get(row, col) == true ){
               livingcell(generation, newgeneration, row, col);
            }
            else if ( generation.get(row, col) == false ){ 
               deadcell(generation, newgeneration, row, col);
            }
        }
    }
    
    generationswap(generation, newgeneration);
    generation.print();
    
    system("pause");
}

void generationswap(boolMatrix &generation, boolMatrix &newgeneration)
{ 
     for (int row = 0; row < rows; row++){
        for (int col = 0; col < cols; col++){
            if ( newgeneration.get(row, col) == true )
               generation.set(col, row, true);
            else if ( newgeneration.get(row, col) == false )
               generation.set(col, row, false);
        }
     }
}



void livingcell(boolMatrix generation, boolMatrix &newgeneration, int row, int col)
{
     int truecount = 0;
     
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get( row - 1, col + count ) == true && row > 0 ){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) ){
                 truecount++;
              }
         }
     }
     
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get( row, col + count ) == true && row > 0 ){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) ){
                 truecount++;
              }
         }
     }
     
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get( row + 1, col + count ) == true && row > 0 ){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) ){
                 truecount++;
              }
         }
     }

    if ( truecount == 3 || truecount == 2 ){
         newgeneration.set(row, col, true);
     } else {
         newgeneration.set(row, col, false);
     }
}


void deadcell(boolMatrix generation, boolMatrix &newgeneration, int row, int col)
{
     int truecount = 0;
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get(row - 1, col + count) == true){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) )
                 truecount++;
         }
     }
     
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get( row, col + count ) == true && row > 0 ){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) ){
                 truecount++;
              }
         }
     }
     
     for ( int count = -1; count < 2; count++ ){
         if ( generation.get( row + 1, col + count ) == true && row > 0 ){
              if ( ( (col + count) >= 0 ) && ( (col + count) <= cols) ){
                 truecount++;
              }
         }
     }
     
     if ( truecount == 3 ){
         newgeneration.set(row, col, true);
     } else {
         newgeneration.set(row, col, false);
     }
}



void firstgen(boolMatrix &generation)
{
     ifstream infile("lifeinput.dat");
     int row, col;
    
     infile >> row >> col;
     while (infile) {
           generation.set(row, col, true);
           infile >> row >> col;
     }
     infile.close();
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Salem
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.