C++ Homework The Game of Life

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]='X';

}
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])=='X') //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]='X';
}
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]='X';
}
}
}


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])=='X')
num_neighbors++;
if ((life_array[row][col-1])=='X')
num_neighbors++;
if ((life_array[row][col+1])=='X')
num_neighbors++;
if ((life_array[row-1][col])=='X')
num_neighbors++;
if ((life_array[row+1][col-1])=='X')
num_neighbors++;
if ((life_array[row+1][col])=='X')
num_neighbors++;
if ((life_array[row+1][col+1])=='X')
num_neighbors++;
return num_neighbors;

}


[code] Your code goes here [/code]
1
2
3
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];
That copies in new_array the content of the original array
Last edited on
So are you saying change it? Because I switched ti life_array first then new_array and then it won't display generation 2. I also tried eliminating it and it just runs the same generation for each iteration.
Your code is in conflict with the comment. Fix the code.
You may want to add a std::cin.get(); just after you print the board, so you can see the changes

Also
1
2
3
for(row=1;row<n+2;row++) //Loop to determine nieghbors
{
cout<<endl; //why? 
The endl; is there to space out the generations. I have tried many things to fix my code I can't find the problem. What exactly do you mean my comment is in conflict with the code?
It is not responsability of a live_die function to space the output. (and you are printing as many as rows you have). Try to do one thing per function.

Check your conditions:
if (num_neighbors != 3 || num_neighbors !=4 ) //Conditions to determine if X lives or dies This is always true.

The count neighbours is wrong, you are just checking 7 cells.

What exactly do you mean my comment is in conflict with the code?
Your code doesn't do what your comments say.
Topic archived. No new replies allowed.