Hi everyone,
I am having trouble with a prgram that simulates Conway's Game of Life. I cannot get the program to run past a few generations. I think I am having trouble with the countNeighbors() or the genNextGrid() functions that is causing the game not to function properly. Anyone have any ideas on what is going on? Thank you in advance.
Nick

// This program simulates conways game of life.


/****************************************************************************/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

/****************************************************************************/
const int gridSize = 18;                // specify the size of the grid

/****************************************************************************/
void doLife      (int grid[][gridSize], const int gens, const char start);
void setup       (int grid[][gridSize], char);
void displayGrid (int grid[][gridSize]);

/****************************************************************************/
void resetGrid   (int grid[][gridSize]);
void randLife    (int grid[][gridSize]);
void genNextGrid (int grid[][gridSize]);
int  countNeighbors (const int grid[][gridSize], const int row, const int col);

/****************************************************************************/
int main ()
{
       int generations;
       char init;

       int life [gridSize][gridSize];

       do
       {
               cout << "How many generations would you like to see (at least 20)? " << flush;
               cin >> generations;
       } while ((generations < 20));

       do
       {
               cout << "Start with a (r)andom board, the (q)ueen "
            << "bee shuttle or the (g)lider pattern? " << flush;
               cin >> init;
       } while (init != 'r' && init != 'q' && init != 'g');

       doLife (life, generations, init);

}

/****************************************************************************/
void doLife (int grid[][gridSize], const int gens, const char start)
{
       resetGrid(grid);

       if (start == 'r')
               randLife(grid);
       else
               setup(grid,start);

       cin.get();
       for (int x = 0; x < gens; x ++)
       {
           system ("clear");                   // clears the screen; not C++ ... Unix
               cout << "Viewing generation #" << x + 1 << "/" << gens << ":" << endl << endl;
               displayGrid(grid);
               genNextGrid(grid);
               cout << endl << "Press enter to see next iteration: ";
               cin.get();
       }
}

/****************************************************************************/
void displayGrid (int grid[][gridSize])
{
       cout << "   ";
       for (int x = 1; x <= gridSize; x++)
       {
               if ((x / 10) != 0)
                       cout << x / 10;
               else
                       cout << " ";
       }
       cout << endl;
       cout << "   ";
       for (int x = 1; x <= gridSize; x++)
       {
               cout << x % 10;
       }
       cout << endl ;

       for (int r = 0; r < gridSize; r++)
       {
               cout << r + 1;
               if (r + 1 < 10)
                       cout << "  ";
               else
                       cout << " ";
               for (int c = 0; c < gridSize; c++)
               {
                       if (grid[r][c] == 1)
                               cout << "*" << flush;
                       else
                               cout << " " << flush;
               }
               cout << endl;
       }
       return;
}

/****************************************************************************/
void setup (int grid[][gridSize], char which)
{

       resetGrid (grid);

   if (which == 'q')
   {
       // Set up the Queen Bee Shuttle pattern
       grid[5][1] = 1; grid[5][2] = 1; grid[6][3] = 1; grid[7][4] = 1;
       grid[8][4] = 1; grid[9][4] = 1; grid[10][3] = 1;grid[11][2] = 1;
       grid[11][1] = 1;
   }
   else
   {
       // Set up a Glider
       grid [17][0] = 1; grid[16][1] = 1; grid[15][1] = 1; grid[16][2] = 1;
       grid [17][2] = 1;
   }

       return;
}




/* FUNCTION resetGrid() should go here */
void resetGrid(int grid[][gridSize])
{
  for (int r=0; r<gridSize; r++)
     for (int c=0; c<gridSize; c++)
         grid[r][c]= 0;
}

/* FUNCTION randLife() should go here */
void randLife(int grid[][gridSize])
{
   srand(time(0));

  for (int r=0; r<gridSize; r++)
      for (int c=0; c<gridSize; c++)
          grid[r][c] = (rand() % 2);
}

/* FUNCTION genNextGrid() should go here */
void genNextGrid(int grid[][gridSize])
{
   int tmp[gridSize][gridSize] = {{0}};

  /* for (int r=0; r<gridSize; r++)
       for (int c=0; c<gridSize; c++)
           tmp[r][c] = grid[r][c];
           */

   for (int r=0; r<gridSize; r++)
   {
       for (int c=0; c<gridSize; c++)
       {
           int num = countNeighbors(grid,r,c);
           cout << num << endl;
           if (grid[r][c] == 0)
           {
               if (num == 3)
                   tmp[r][c] = 1;
           }
           else if (grid[r][c] == 1)
           {
               if (num == 3 || num == 2)
                   tmp[r][c] = 1;
               /*
                  else if (num > 3 || num < 2)
                   tmp[r][c] = 0;
                */
           }
           else
               tmp[r][c] == 0;
           /*
           if (grid[r][c] == 1 && (countNeighbors(grid, r, c) == 2 || countNeighbors(grid, r,c) == 3) )
               tmp[r][c] = 1;
           if(countNeighbors(grid, r,c) < 3 || countNeighbors(grid,r,c) > 3)
               tmp[r][c] = 0;
           if (countNeighbors(grid,r,c) == 3)
               tmp[r][c] = 1;
               */

           for(int r=0; r<gridSize; r++)
               for(int c=0;c<gridSize;c++)
                   grid[r][c] = tmp[r][c];
       }
   }
}

/* FUNCTION countNeighbors() should go here */
int countNeighbors(const int grid[][gridSize], const int row, const int col)
{
   int neighbor = 0;
/*
   if ((row-1) >= 0 && (col-1) >= 0 && grid[row-1][col-1] == 1)
       neighbor++;
   if ((row -1) >= 0 && grid[row-1][col] == 1)
       neighbor++;
   if ((row+1) < gridSize && (col-1) < gridSize && grid[row-1][col+1] == 1)
       neighbor++;
   if (row >= 0 && (col-1) >= 0 && grid[row][col-1] == 1)
       neighbor++;
   if (row < gridSize && (col+1) < gridSize && grid[row][col+1] == 1)
       neighbor++;
   if ((row-1) >= 0 && (col-1) >= 0 && grid[row-1][col-1] == 1)
       neighbor++;
   if((row+1) < gridSize && grid[row+1][col] == 1)
       neighbor++;
   if ((row+1) < gridSize && (col+1) < gridSize && grid[row+1][col+1] == 1)
       neighbor++;
   return neighbor;
   */

   for (int r=-1; r<2;r++)
       for(int c=-1;c<2;c++)
       {
           if (row+r<0 || row+r>=gridSize);
           else if(col+c<0 || col+c>=gridSize);
           else if(r==0 && c==0);
           else if(grid[r][c] == 0);
           else
               neighbor++;

       }
   return neighbor;

}
tmp[r][c] == 0;

The above is probably wrong. My compiler, VC++ 6.0, produced a warning -- your should have too.

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.