I'm trying to write the code for the Game of Life program, using arrays and for loops. I based most of my code on what my teacher did in class and modified it to fit the requirements of my assignment. I've hit a wall, though. When I execute the program, I'm able to input what how many cells and what cells I want occupied. The program displays my input correctly for the first Generation, but every Generation after that has all the cells filled with astericks.
#include <iostream>
using namespace std;
#include <memory.h>
#include <stdlib.h>
#include <time.h>
void main ()
{
const long NumCols (60);
const long NumRows (60);
bool Board [NumRows + 2] [NumCols + 2];
bool NextBoard [NumRows + 2] [NumCols + 2];
long Col;
time_t CurrTime;
long Generation;
long i;
long NumNeighbors;
long NumOccupied;
long Row;
time_t StartTime;
const time_t WaitTime (3);
memset (Board, false, (NumRows + 2) * (NumCols + 2) * sizeof (bool));
cout << "How many cells do you want occupied? ";
cin >> NumOccupied;
for (i = 0; i < NumOccupied; i++)
{
cout << "Which row and column position is to be occupied: (1 to 60) ";
cin >> Row >> Col;
if(Row > NumRows, Col > NumCols)
{
cout << "Not a valid position, reenter" << endl;
i--;
}
else
Board [Row] [Col] = true;
}
for (Generation = 0; ; Generation++)
{
system ("cls"); // "cls" needs to be changed if you are on UNIX
cout << "Generation " << Generation << endl;
for (Row = 1; Row <= NumRows; Row++)
{
for (Col = 1; Col <= NumCols; Col++)
cout << (Board [Row] [Col] ? '*' : ' ');
if (Board [Row] [Col])
cout << '*';
else
cout << ' ';
cout << endl;
}
for (Row = 1; Row <= NumRows; Row++)
for (Col = 1; Col <= NumCols; Col++)
{
// count the number of occupied neighbors
NumNeighbors = 0;
if (Board [Row - 1] [Col - 1])
NumNeighbors++;
else;
if (Board [Row - 1] [Col])
NumNeighbors++;
else;
if (Board [Row + 1] [Col - 1])
NumNeighbors++;
else;
if (Board [Row + 1] [Col])
NumNeighbors++;
else;
if (Board [Row + 1] [Col + 1])
NumNeighbors++;
else;
if (Board [Row + 1] [Col])
NumNeighbors++;
else;
if (Board [Row - 1] [Col + 1])
NumNeighbors++;
else;
// do the same for the other cells around this one
// now apply the rules
if (NumNeighbors >= 4)
NextBoard [Row] [Col] = ' ';
if (NumNeighbors <= 1)
NextBoard [Row] [Col] = ' ';
if (NumNeighbors = 3)
NextBoard [Row] [Col] = '*';
else;
}
memcpy (Board, NextBoard, (NumRows + 2) * (NumCols + 2) * sizeof (bool));
StartTime = time (0); // gets current time in seconds since Jan 1, 1970
do {
CurrTime = time (0);
} while ((CurrTime - StartTime) < WaitTime);
}
}
Any help would would be appreciated.