#include <conio.h> // for _kbhit () and getch
#include <memory.h> // for memset and memcpy
#include <stdlib.h> // for system
#include <time.h> // for time
void main ()
{
const int NumRows (20);
const int NumCols (20);
const time_t WaitTime (3);
bool CurrGen [NumRows + 2] [NumCols + 2];
bool NextGen [NumRows + 2] [NumCols + 2];
int Row;
int Col;
bool ContinueLoop;
int Generation;
int NumOccupied;
time_t CurrTime;
char c;
cout << "Enter row and col for each cell to be living at the start" << endl;
cout << "Rows are numbered 1 thru " << NumRows << endl;
cout << "Columns are numbered 1 thru " << NumCols << endl;
cout << "Enter -1 -1 when you are finished" << endl;
do {
cout << "=> ";
cin >> Row >> Col;
if ((Row == 0) || (Row > NumRows))
{
cout << "Invalid row, type in the pair again" << endl;
ContinueLoop = true;
}
else
{
if ((Col == 0) || (Col > NumCols))
{
cout << "Invalid column, type in the pair again" << endl;
ContinueLoop = true;
}
else
{
if ((Row >= 0) && (Col >= 0))
{
CurrGen [Row] [Col] = true;
ContinueLoop = true;
}
else
ContinueLoop = false;
}
}
} while (ContinueLoop);
cout << "Hit any key to stop the program" << endl;
Generation = 0;
do {
system ("cls"); // erases the screen
cout << " Generation: " << Generation << endl;
for (Row = 1; Row <= NumRows; Row++)
{
for (Col = 1; Col <= NumCols; Col++)
// if (CurrGen [Row] [Col])
// cout << '*';
// else
// cout << '.';
cout << (CurrGen [Row] [Col] ? '*' : '.');
cout << endl;
}
for (Row = 1; Row <= NumRows; Row++)
for (Col = 1; Col <= NumCols; Col++)
{
// figure out what happens to this cell in the next generation
// count how many neighbors are occupied
// if (CurrGen [Row] [Col])
// does it die of overcrowding
// does it die of loneliness
// if not the above it stays occupied
// else
// is it a birth cell
// if not, it stays unoccupied
}
// copy the Next Generation to the Current Generation
memcpy (CurrGen, NextGen, (NumRows + 2) * (NumCols + 2) * sizeof (bool));
// ask the user if they want to continue
// if yes ContinueLoop is true, if not ContinueLoop is false
ContinueLoop = true;
Generation++;
CurrTime = time (0);
// do {
// if (_kbhit ())
// exit (0);
// } while ((CurrTime + WaitTime) > time (0));
while (!_kbhit ()); // hangs until you hit a key
c = _getch (); // reads the key you just hit
if (c == 'x')
exit (0);
else;
} while (true);
}
This is what I ahve so far. Im having trouble making the rules, like if it dies or if its a birth cell. Somebody please help.