my assignment is to write the program The Game of Life which was invented by Cam
ID: 3635244 • Letter: M
Question
my assignment is to write the program The Game of Life which was invented by Cambridge mathematician John H. Conway to model the process of birth, survival and death. By the rules of the game, individuals require others to survive, but die as the result of overcrowding.a. Birth Rule: an organism is born into an empty cell that has exactly three living neighbors.
b. Survival Rule: An organism survives from one generation to the next if it has either 2 or 3 living neighbor.
c. Death Rule: An organism dies from loneliness if it has fewer than 2 neighbors. It dies from overcrowding if it has 4 or more neighbors.
I have a function that calculate the neighbor and Im trying to calculate the next generation which is called by the calculate the neighbor
here is my function of calculate the neighbor
int countNeighborCell(int row, int col, char array2d[][40])
{
int i =0;
int j = 0;
int row_checkpoint;
int col_checkpoint;
int arrayNeighbor[20][40] = {0,0};
char cell[20][40] = {0,0};
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
row_checkpoint = i - 1;
col_checkpoint = j -1;
if(row_checkpoint == -1)
arrayNeighbor[i][j] = 0;
if (col_checkpoint == -1)
arrayNeighbor[i][j] = 0;
if(array2d[i ][j] == '*')
{
if(array2d[i - 1][j] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i + 1][j] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i + 1][j - 1] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i + 1][j + 1] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i - 1][j - 1] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i -1][j] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i][j + 1] == '*')
{
arrayNeighbor[i][j]++;
}
if(array2d[i][j - 1] == '*')
{
arrayNeighbor[i][j]++;
}
return arrayNeighbor[i][j];
}
}
}
}
here is my function to calculate the next generation, it not working properly, how do i return the next element in 2d array in the calculateNextGen
void calculateNextGen(int row, int col, char array2d[][40])
{
int i,j;
char nextGen[20][40] ={0,0};
int countedCell[20][40];
i = countNeighborCell(row, col, array2d);
return;
}
please help
Explanation / Answer
#include 002 #include 003 using namespace std; 004 const int r = 22, c = 80; 005 006 void display(char size_2[][80]); 007 void display2(char size_2[][80]); 008 void neighbors(char size_2[][80], char size_2hold[][80]); 009 void neighbors2(char size_2[][80], char size_2hold[][80]); 010 011 int main() 012 { 013 char a, size_2[r][c], size_2hold[r][c]; 014 015 coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.