Question: need some help with my reproduce function. Not sure if im using my count function right. Also do I need another array to this? If

need some help with my reproduce function. Not sure if im using my count function right. Also do I need another array to this? If you could show me with explaination that would be nice.

Its the game of life, I put the rules in comments for reproduce function

#include

using namespace std;

// GLOBAL CONSTANTS

const int MAXGEN = 3; const int n = 3; // rows const int m = 4; // columns

void initialize(int grid[][m]){ cout << "Enter 0 for dead, 1 for alive" << endl; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){

cin >> grid[i][j]; }

}

} void initialize2(){

}

int countNeighbours(int grid[][m],int x, int y){

//grid[x][y] would represent current cell. Need to check all round that cell. Using modular for wrap.

int counter = 0;

//(x+n)%n and (y+m)%m to get wrap around

// y> if (grid[((x+n)%n)][(((y-1)+m)%m)] == 1) //one to left // 0 0 1 2 counter++; // 1 ^x 3 4 5 if (grid[(((x-1)+n)%n)][(((y-1)+m)%m)] == 1) //up one to left // 2 6 7 8 counter++; if (grid[(((x-1)+n)%n)][((y+m)%m)] == 1) // up one counter++; if (grid[(((x-1)+n)%n)][((y+1)+m)%m]==1) //up one to right x= 1 y = 2 counter++; if (grid[((x+n)%n)][(((y+1)+m)%m)]==1) // one to right counter++; if (grid[(((x+1)+n)%n)][(((y+1)+m)%m)]==1) // down one to right counter++; if (grid[(((x+1)+n)%n)][((y+m)%m)]==1) // down one counter++; if (grid[(((x+1)+n)%n)][(((y-1)+m)%m)]==1) // down one to left counter++;

return counter; // counter represents total number of Alive cells }

bool allDead(int grid[][m]){ bool allDead = true; int x, y; int tempNeighbours = 0;

for(int i=0; i for (int j=0; j tempNeighbours += countNeighbours(grid,x,y); }

}

if (tempNeighbours == 0){

return allDead; }

else return !allDead;

}

void reproduce(int grid[][m]){ int x,y;

while(!allDead(grid)){

for(int i=0; i for (int j=0; j int myNeighbours = countNeighbours(grid,x,y);

if (myNeighbours < 2 || myNeighbours > 3 && grid[i][j] == 1) // live with < 2 or > 3 neighbours dies grid[i][j] == 0; if (myNeighbours == 2 || myNeighbours == 3 && grid[i][j] == 1) // live cell with 2 or 3 neighbours lives on grid[i][j] == 1; if (myNeighbours == 3 && grid[i][j]== 0) //dead cell with 3 neighbours becomes alive grid[i][j] == 1;

} }

} }

void print (int grid[][m]){

for(int i=0; i for (int j=0; j

cout << grid[i][j]; } cout << endl;

} cout << endl; }

int main(){

int grid[n][m]; initialize(grid); cout << "Initial population = "; print(grid); int gen = 1;

while (gen <= MAXGEN && !allDead(grid)){ cout << "gen = " << gen << ": " << endl; reproduce(grid); //will call the function countNeighbours for each cell print(grid); gen++; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!