Write a program to implement the simulation of life as described in Scientific A
ID: 3799784 • Letter: W
Question
Write a program to implement the simulation of life as described in Scientific American by Martin Gardner. The program will be implemented on a two dimensional surface of size 60 by 60 visible elements. The rules of the simulation are as follows: 1) Aninitialsetofcellsaremarkedas“alive”bytheuser.Thisisgeneration0.Your program will ask the user to input a set of row and column values to let the user determine which cells are “alive”. Display this generation. 2) Cells change for each succeeding generation by the following rules: A living cell dies of overcrowding in the next generation if it currently has 4 or more living neighbors. A living cell dies of loneliness in the next generation if it currently has only 0 or 1 living neighbors. An empty cell becomes a “birth” cell (becomes alive) in the next generation if it has exactly 3 living neighbors. All other cells remain unchanged. 3) The new generation becomes the current generation and is displayed. 4) Afterdisplayingeachnewgeneration,asktheuseriftheywishtocontinuetothe next generation or stop at this point.
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <memory.h>
#include <time.h>
using namespace std;
void Ask(int, int);
void F_Gen(int, int);
void Cell(int, int);
void Start_Again(int);
const int M_Row (60);
const int M_Col (60);
int Gen = 0;
bool I_Cell [M_Row + 2] [M_Col + 2];
bool L_Cell [M_Row + 2] [M_Col + 2];
int main()
{
int Row = 0;
int Col = 0;
time_t StopTime;
do
{
do
{
Ask(Row, Col);
F_Gen(Row, Col);
I_Cell [Row][Col] = L_Cell [Row][Col];
}
while (1);
memcpy (I_Cell, L_Cell, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
StopTime = time (0) + 3;
while (time (0) < StopTime);
} while (!_kbhit ());
cout << "Bye" << endl;
}
void Ask (int X, int Y)
{
cout << " Game Of Life " << endl;
cout << "Enter the Number of Row (1 to 60): ";
cin >> X;
if ((X < 0) || (X > 60))
{
cout << "Invalid Row" << endl;
exit (0);
}
else if ((Y >=0 ) || (Y <= 60))
{
cout << "Enter the Number of Column (1 to 60): ";
cin >> Y;
}
else if ((Y < 0) || (Y > 60))
{
cout << "Invalid Column" << endl;
exit (0);
}
else if ((X < 0) || (X > 60) || (Y < 0) || (Y > 60))
{
cout << "Invalid Number" << endl;
exit (0);
}
else;
}
void F_Gen(int XY, int YZ)
{
int SN;
memset (I_Cell, false, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
memset (L_Cell, false, (M_Row + 2) * (M_Col + 2) * sizeof (bool));
do
{
for (XY = 1; XY <= M_Row; XY++)
{
for (YZ = 1; YZ <= M_Col; YZ++)
cout << (I_Cell [XY] [YZ] ? '*' : ' ') << endl;
}
++Gen;
for (XY = 1; XY <= M_Row; XY++)
for (YZ = 1; YZ <= M_Col; YZ++)
{
Cell(XY, YZ);
}
cout << "Would You like to Start the next generation (Y/N): ";
cin >> SN;
}
while (SN == 'Y' && SN == 'y');
}
void Cell(int W, int Z)
{
if (I_Cell [W][Z] == 1)
{
if ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1))
I_Cell [W][Z] == 0;
else if ((I_Cell [W][Z - 1] <= 1) && (I_Cell [W][Z + 1] <= 1) && (I_Cell [W - 1][Z] <= 1) && (I_Cell [W + 1][Z] <= 1))
I_Cell [W][Z] == 0;
else if (((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 0)) || ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 0) &&
(I_Cell [W + 1][Z] == 1)) || ((I_Cell [W][Z - 1] == 0) && (I_Cell [W][Z + 1] == 1) && (I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1)) || ((I_Cell [W][Z - 1] == 1) && (I_Cell [W][Z + 1] == 0) &&
(I_Cell [W - 1][Z] == 1) && (I_Cell [W + 1][Z] == 1)))
I_Cell [W][Z] == 1;
else
I_Cell [W][Z] == 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.