Write a program to implement the simulation of life as described in Scientific A
ID: 3812474 • 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) An initial set of cells are marked as “alive” by the user. This is generation 0. 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. 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.
3) The new generation becomes the current generation and is displayed.
4) After displaying each new generation, ask the user if they wish to continue to the next generation or stop at this point.
#include kiostream> using namespace std; 5 #include memory.h> need this to use memset in code below #include time. h> need this to work with the clock #includeExplanation / Answer
#include <iostream>
using namespace std;
#include <memory.h>
#include <time.h>
#include <stdlib.h>
int main()
{
const int MaxCols (60);
const int MaxRows (60);
const time_t WaitTime (2);
bool Board[MaxRows+2][MaxCols+2];
int Col;
bool FutureBoard [MaxRows+2] [MaxCols+2];
int Generation;
int Row;
time_t stopTime;
memset(Board, false, (MaxRows+2)*(MaxCols+2)*sizeof(bool));
//for(Row =1 ; Row<= MaxRows; Row++)
// Board[Row][Row] = true;
// Fetching the input from the user using option one mentioned in the answer format
int count;
cout << "enter the num of active cells";
cin >> count;
int i;
for(i =0; i< count; i++)
{
int row, col;
cout << "Enter the Active cell row and column numbers one by one";
cin >> row;
cin >> col;
if((row <= MaxRows) && (col <= MaxCols))
{
Board[row][col] = true;
}
else
{
// For invalid input case
cout << "Invalid Row or column. Please reenter";
i--;
}
}
for (Generation = 0; ;Generation++)
{
cout << " Generation " << Generation << endl;
for(Row = 1; Row <=MaxRows; Row++)
{
for(Col = 1; Col <= MaxCols; Col++)
cout << (Board[Row][Col] ? '*' : ' ');
cout << endl;
}
// Logic to stop next generation creations
int input;
cout << "Enter -1 to stop next genaration or Enter 0 to continue";
cin >> input;
if(input == -1)
break;
//applying rules
for(Row = 1; Row <=MaxRows; Row++)
{
for(Col = 1; Col <= MaxCols; Col++)
{
//first fetch the alive neighbours count
int aliveCells = 0;
int i;
int j;
// neighbors are row-1, col-1, row-1,col, row-1,col+1
// row, col-1; row,col+1
// row+1,col-1; row+1,col; row+1,col+1
// total 8 cells
for(i = Row -1; i <= Row+1 ; i++)
for(j = Col-1; j <= Col+1; j++)
if( (i != Row) || (j != Col))
aliveCells += (Board[i][j] ? 1 : 0);
// Set the default value
FutureBoard[Row][Col] = Board[Row][Col];
// For living cell
// If the active neighbours are more than or equal to 4
// or less than 1,
// kill the cell in future Generation
if(Board[Row][Col])
{
if((aliveCells >= 4) || (aliveCells <= 1))
FutureBoard[Row][Col] = false;
}
else if (aliveCells == 3) // for dead cell and 3 active neighbours
{
FutureBoard[Row][Col] = true;
}
}
}
memcpy(Board, FutureBoard, (MaxRows+2)*(MaxCols+2)*sizeof(bool));
stopTime = time(0) + WaitTime;
while(stopTime > time(0));
system("cls");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.