I have this template that the professor provided. But i am stuck at figuring out how to get the user to input numbers into the array and then how to build rules that determine what happens with the array. The rules are
a. A living cell dies of overcrowding in the next generation if it currently has 4 or more living neighbors. b. A living cell dies of loneliness in the next generation if it currently has only 0 or 1 living neighbors. c. An empty cell becomes a “birth” cell (becomes alive) in the next generation if it has exactly 3 living neighbors. d. All other cells remain unchanged.
Here is my code below.
#include <iostream>
#include "ReadNumber.h"
usingnamespace std;
#include <conio.h> // do this so we can use _kbhit in the code below
#include <memory.h> // do this if you want to use memset in the code below
#include <stdlib.h> // do this so we can use system in the code below
#include <time.h> // do this so we can use the clock operations
#include <string.h>
void main()
{
constlong NumCols(10);
constlong NumRows(10);
bool CurrGen[NumRows + 2][NumCols + 2];
bool NextGen[NumRows + 2][NumCols + 2];
long Col;
long Row;
time_t StopTime; // time_t is a short way of writing unsigned long long
const time_t TimeToWait(2);
long CurrentGeneration(0);
memset(CurrGen, false, sizeof (bool)* (NumCols + 2) * (NumRows + 2)); // set all celss in an array (CurrGen) to a value (false), and need to know the size of the array in bytes
memset(NextGen, false, sizeof (bool)* (NumCols + 2) * (NumRows + 2));
for (Col = 1; Col <= NumCols; Col++)
CurrGen[Col][Col] = true;
cout << "Enter row you want alive" << endl;
// find out from the user which cells are initially alive (set up current generation)
// find out from the user which cells are initially alive (set up current generation)
// user enters a row and col for each cell they want occupied
// user will enter -1 and -1 to stop
do { // loop for each generation
system("cls"); // clears the screen in windows, in UNIX change "cls" to "clear"
cout << "\t\tGeneration: " << CurrentGeneration << endl;
for (Row = 1; Row <= NumRows; Row++) // display current generation
{
for (Col = 1; Col <= NumCols; Col++)
cout << (CurrGen[Row][Col] ? '*' : ' ');
// line above is the same as
// if (CurrGen [Row] [Col])
// cout << '*';
// else
// cout << ' ';
cout << endl;
}
// loop for each cell on the board
// get the number of living neighbors
// apply the rules
// save the info on what happens to cell into the other array (NextGen)
// memcpy (CurrGen, NextGen, sizeof (bool) * (NumCols + 2) * (NumRows + 2)); // copies cells from NextGen into CurrGen
// end the loop for this generation
StopTime = time(0) + TimeToWait; // time (0) gives back the current time in seconds since Jan 1, 1970
while (time(0) < StopTime); // keep looking at the clock until it is time to stop
CurrentGeneration++;
} while (!_kbhit()); // _kbhit gives back true if the user has hit a key, false if no key has been hit
cout << "Bye" << endl;
}