/*I have to write code for the Game of Life. I wrote nearly all the code for it but for some reason when I run my second generation it just displays the same one as before. Here is my code any idea on why this is happening would be greatly appreciated!

/*

The game of life is really not a game. Is is a computer simulation that was created by a Cambridge Mathematician named John Conway.

The idea is that during each iteration life will populate, survive, or die based on certain rules.
The Rules:

For a space that is populated:
· Each cell with one or no neighbors dies from loneliness
· Each cell with four or more neighbors dies from overpopulation
· Each cell with two or three neighbors survives.
*/


#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int n=5;

int checkx(const char life_array[][n+2],int row,int col);
//function to determine how many neighbors


int main()
{

int row;
int col;
int x;
cout << "Please enter how many times you wish to play the game of life!" << endl;
cin >> x;

char life_array[n+2][n+2]; //original array
char new_array[n+2][n+2]; //array for next generation
int num_neighbors=0; //Neighbors of each square in generation

for(row=1;row<n+2;row++)
for(col=1;col<n+2;col++)
{
int random_integer=(rand()%2);
if((random_integer)==0)
life_array[row][col]=' ';
else
life_array[row][col]='0';

}
for(int num_gen=0;num_gen<x;num_gen++) //generation loop

{
cout<<setw(10)<<"Generation:"<<num_gen+1<<endl<<endl; //Header


for(row=1;row<n+1;row++) //Print Generation
{
cout<<endl;
for(col=1;col<n+1;col++)
{
cout<<life_array[row][col]<<" ";
}
}
for(row=1;row<n+2;row++) //Loop to determine nieghbors
{
cout<<endl;
for(col=1;col<n+1;col++)
{
if ((life_array[row][col])=='0') //If box has an X
{
num_neighbors=checkx(life_array, row, col);
if (num_neighbors != 3 || num_neighbors !=4 ) //Conditions to determine if X lives or dies
new_array[row][col]=' ';
else
new_array[row][col]='0';
}
else //If no 'X' in box
{
num_neighbors=checkx(life_array, row,col);
if(num_neighbors != 2 || num_neighbors !=3) //Condition to determine if X is born
new_array[row][col]=' ';
else
new_array[row][col]='0';
}
}
}


for(row=1;row<n+1;row++) //Copies new array to original array
for(col=1;col<n+2;col++)
new_array[row][col]=life_array[row][col];


}
return 0;
}


int checkx(const char life_array[][n+2],int row,int col)
//function to determine number of neighbors
{

int num_neighbors;

num_neighbors=0;
if ((life_array[row-1][col-1])=='0')
num_neighbors++;
if ((life_array[row][col-1])=='0')
num_neighbors++;
if ((life_array[row][col+1])=='0')
num_neighbors++;
if ((life_array[row-1][col])=='0')
num_neighbors++;
if ((life_array[row+1][col-1])=='0')
num_neighbors++;
if ((life_array[row+1][col])=='0')
num_neighbors++;
if ((life_array[row+1][col+1])=='0')
num_neighbors++;
return num_neighbors;

}
My problem is that it isnt printing out correctly, If you could help me out i would greatly appreciate it.

Also the other problem is that the different generations should be different not the same as the preceding one. And here is the code.

/*I have to write code for the Game of Life. I wrote nearly all the code for it but for some reason when I run my second generation it just displays the same one as before. Here is my code any idea on why this is happening would be greatly appreciated! 





/*

The game of life is really not a game. Is is a computer simulation that was created by a Cambridge Mathematician named John Conway.

The idea is that during each iteration life will populate, survive, or die based on certain rules.
The Rules:

For a space that is populated:
· Each cell with one or no neighbors dies from loneliness
· Each cell with four or more neighbors dies from overpopulation
· Each cell with two or three neighbors survives.
*/


#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int n=5; 

int checkx(const char life_array[][n+2],int row,int col);
//function to determine how many neighbors 


int main()
{
	int row; 
	int col;
	int x;
	cout << "Please enter how many times you wish to play the game of life!" << endl;
	cin >> x;
	
	char life_array[n+2][n+2]; //original array
	char new_array[n+2][n+2]; //array for next generation
	int num_neighbors=0; //Neighbors of each square in generation

	for(row=1;row<n+2;row++)
	for(col=1;col<n+2;col++)
	{
		int random_integer=(rand()%2);
		if((random_integer)==0)
		life_array[row][col]=' ';
	else
		life_array[row][col]='0';

	}
	for(int num_gen=0;num_gen<x;num_gen++) //generation loop
	{
		cout<<setw(10)<<"Generation:"<<num_gen+1<<endl<<endl; //Header

	for(row=1;row<n+1;row++) //Print Generation
		{
			cout<<endl;
			for(col=1;col<n+1;col++)
				{
					cout<<life_array[row][col]<<" ";
				}
		}
	for(row=1;row<n+2;row++) //Loop to determine nieghbors
		{
			cout<<endl;
	for(col=1;col<n+1;col++)
		{
			if ((life_array[row][col])=='0') //If box has an 0
			{
				num_neighbors=checkx(life_array, row, col);
				if (num_neighbors != 3 || num_neighbors !=4 ) //Conditions to determine if 0 lives or dies
					new_array[row][col]=' ';
				else
					new_array[row][col]='0';
			}
	else //If no '0' in box
		{
			num_neighbors=checkx(life_array, row,col);
			if(num_neighbors != 2 || num_neighbors !=3) //Condition to determine if 0 is born
				new_array[row][col]=' ';
			else
				new_array[row][col]='0';
		}
		}
		}


	for(row=1;row<n+1;row++) //Copies new array to original array
	for(col=1;col<n+2;col++)
		new_array[row][col]=life_array[row][col];


	}
	return 0;
}


int checkx(const char life_array[][n+2],int row,int col)
//function to determine number of neighbors
{

	int num_neighbors;
	num_neighbors=0;
	if ((life_array[row-1][col-1])=='0')
	num_neighbors++;	
	if ((life_array[row][col-1])=='0')
	num_neighbors++;
	if ((life_array[row][col+1])=='0')
	num_neighbors++;
	if ((life_array[row-1][col])=='0')
	num_neighbors++;
	if ((life_array[row+1][col-1])=='0')
	num_neighbors++;
	if ((life_array[row+1][col])=='0')
	num_neighbors++;
	if ((life_array[row+1][col+1])=='0')
	num_neighbors++;	

	return num_neighbors;

}

Line 95: you are copying an original array on top of what you just calculated. You want it other way around.

Your code is not formatted.Formatting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.