# Thread: C++ Game of Life Program

1. ## C++ Game of Life Program

Hey guys, have any of you written the code for or are familiar with the game of life, invented by John H. Conway? I have the layout of what is expected in the abstract, however, I have no idea how to begin.

The game is played on a board consisting of n squares in horizontal and vertical directions whre n is alwasy between 10-25. Each square can be empty or contain an X indicating the presence of an organism. Every square (except the border squares) has eight neighbors.

The program should read in number n. Then in a loop read initial configureation of organisms and print the orig. game array. It should calculate the next array into the original game aarry, and repeat for cycle of 8 generations. A function should be written to calculate the number of neighbors for each organism.

this is the formula to calculate the next generation (the whole point of the game):
1. If an empty square has exactly 2 neighbors an organism is born there
2. If an organism has less than 3 neighbors then it dies of loneliness.
3. If an organism has more then 3 neighbors then it dies of overcrowding

also teacher gave hint of using an array of size n+2 thus forming a border around the actual array. Assume that these borders of the game array are infertile regions where organisms can neither survive nor be born; you will not have to process the border squares. Thus the array will be declared of size (27,27)

ok thats the basic layout of the game, here is my thoughts so far. I dont need anyone to write the code, as I am really trying to learn this language and how it works. I would love someone to work with me through the problem and help me understand this.

The board can be randomly generated or controlled by manually assigning the x's (ie. (row 1, col 3 = x)

This pgm will need 2 arrays, an new one and an old one.

Should my first step be how to assign the X's?; writing the function?; ? I always get stuck at the beginning of problems like this

Anyone help describe this game/program in more easy to understand lingo to help me grip what the program really does, etc?

2. > A function should be written to calculate the number of neighbors for each organism.
Seems like a good place to start to me

To calculate the next generation, you call that function in a loop.

The first randomly populates the array. Loop through the array and use a random number generator to calculate if the spot should have life, something like if a random number is less than 30 out of a hundred.

The second function loops through the array, calculates how many neighbors each spot has (could be a third function if you want to do this) and either kills the lifeform at the spot, adds a lifeform to ths spot, or leaves it alone. Make sure you don't change the original array though, create a new array with the updated info.

Once you have those two working properly, then worry about the small specifics as set out by your teacher.

I'm can't tell if you know how the game of life works or not. Do a search online, theres lots of info on it. Heres a version I did in Windows two years ago. Use the mouse to populate the board, or press "x" to randomly populate it. Either can be done at any time. The press "Enter" to see the next generation or hold on to "Enter" to see many generations quickly. Be warned though, this was written when I first started Windows programming and I didn't fully understand how to repaint the screen when the window was mucked with. So if you cover up the window or resize the window, it'll dissappear! When I finally did understand how to fix this, I never went back and rewrote the code for this program

4. Either create a new array or, as I have done with this particular problem, save previous state information in your arrary as well (an array of structs).

5. Hey guys, this program is really giving me problems. I have done code, cleared it, done something else, cleared it, etc...

This is what I have so far, can anyone help direct me along the right path ,keeping with the instructions set?(using two dimensional array)

Code:
```#include <iostream>
#include <iomanip>

using namespace std;

int checkx(char life_array[][],int row,int col,int num_neighbors);
const int n=12;

int main()
{

int row;
int col;
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(int num_gen=0;num_gen<8;num_gen++)        //generation loop
//for 8 generations
{

for(row=1;row<n+2;row++)        //Print Generation
{
cout<<endl;
for(col=1;col<n+2;col++)
{
cout<<life_array[row][col]<<" ";
}
}
for(row=1;row<n+2;row++)
{
cout<<endl;
for(col=1;col<n+2;col++)
{
if (life_array[row][col]='X')
{
num_neighbors=checkx(life_array[][],row,col,num_neighbors);
if (num_neighbors != 3)
new_array[row][col]=' ';
else
new_array[row][col]='X';
}
else
{
num_neighbors=checkx(life_array[][],row,col,num_neighbors);
if(num_neighbors != 2)
new_array[row][col]=' ';
else
new_array[row][col]='X';
}
}
}

for(row=1;row<n+2;row++)
for(col=1;col<n+2;col++)
new_array[row][col]=life_array[row][col];

return 0;
}
}

int checkx(char life_array[][n+2],int row,int col,int 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++;
if life_array[row][col-1]='X';
num_neighbors++;
if life_array[row][col+1]='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;

}```

6. Code:
```if life_array[row-1][col-1]='X';
num_neighbors++;```
I believe you want =='X'; in this statement and all the go below it.

oh, and same thing here:
Code:
```if (life_array[row][col]=='X')
{
num_neighbors=checkx(life_array[][],row,col,num_neighbors);
if (num_neighbors != 3)
new_array[row][col]=' ';
else
new_array[row][col]='X';
}```
sorry, can't help you much more as I've enver played the game...but I would have a seperate function to populate you array.

You're outputing elements here, yet they have never been set...
Code:
```for(col=1;col<n+2;col++)
{
cout<<life_array[row][col]<<" ";
}```
...and then you pass that same empty array to checkx.

actually this looks like just some random code slapped around to evoke more help from the community. I'm pretty sure this has been done before on these boards, and PJY also attached something: do a search, work a little harder, and come back with new questions.

7. Originally Posted by rayrayj52
The game is played on a board consisting of n squares in horizontal and vertical directions whre n is alwasy between 10-25.
Actually the plane of game of life is assumed to be infinite in every direction. For practical reasons, the board is usually a little bit smaller than that though.
It could be played on a 25x25 board, but there exist patterns which requires boards with millions of squares.

8. post your game when it's done cause I want to play

9. What class is the homework for?

Kuphryn

10. Axon pointed out a big error that probably gave you problems with you only using one equal sign. Your other big problem is for some reason you pass num_neighbors to your checkx function. Because of this, num_neigbors never gets resetted to zero which it needs to be with every new cell checked. If you insist of sending it, then add the line "num_neighbors=0" at the beginning of the function.

Other than that, just create the random populate function and actually ask questions next time instead of just posting code and have us guess what you want.

11. thanks for all the help guys, you are really helpfull!! I am still having problems with this.

The code is listed below.

My problem is I am getting an error saying, "checkx cannot convert paramater 1 from 'char' to 'char[][14]'

I have no idea why I am getting this error, can you please help describe what would be causing this.

Also, I added the random generator function to randomly populate the "x's", does this look right to you guys? Would this have anything to do with the problem?

Code:
```#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int n=12; //Size of two dimension array (12x12)

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

int main()
{

int row;
int col;
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(int num_gen=0;num_gen<8;num_gen++)      //generation loop
//for 8 generations
{

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(row=1;row<n+2;row++)        //Print Generation
{
cout<<endl;
for(col=1;col<n+2;col++)
{
cout<<life_array[row][col]<<" ";
}
}
for(row=1;row<n+2;row++)     //Loop to determine nieghbors
{
cout<<endl;
for(col=1;col<n+2;col++)
{
if ((life_array[row][col])=='X') //If box has an X
{
num_neighbors=checkx(life_array[row][col],row,col);
if (num_neighbors != 3)  //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],row,col);
if(num_neighbors != 2)   //Condition to determine if X is born
new_array[row][col]=' ';
else
new_array[row][col]='X';
}
}
}

for(row=1;row<n+2;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(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-1][col])=='X')
num_neighbors++;
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-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;

}```

12. Code:
```             if ((life_array[row][col])=='X') //If box has an X
{
num_neighbors=checkx(life_array[row][col],row,col);
if (num_neighbors != 3)  //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],row,col);                  if(num_neighbors != 2)   //Condition to determine if X is born
new_array[row][col]=' ';
else
new_array[row][col]='X';```
the problem is in red. checkx() is expecting you to pass an array, but u have passed an array element. I didnt check what you are suppose to pass though

13. ok, great, this solved my first problem, the code now executes, however, I dont think it is working quite yet. I believe it still needs tweaking because, it doesn't seem to work right, does anyone notice anything wrong in the source code? I get the 8 generations to populate and randomly as well, however, it doesnt seem to be following the rules I set, and I think I have small changes that need to be made, does anything stand out to anyone with the new code posted below?

I think it could be the assignment of new array and life array

Also the "X's" show up in 13X13 and I need to get them to show up in 12X12, any suggestions on how to alter the code to display in 12X12?

Thanks!!!

Code:
```#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int n=12; //Size of two dimension array (12x12)

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

int main()
{

int row;
int col;
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<8;num_gen++)      //generation loop
//for 8 generations
{

for(row=1;row<n+2;row++)        //Print Generation
{
cout<<endl;
for(col=1;col<n+2;col++)
{
cout<<life_array[row][col]<<" ";
}
}
for(row=1;row<n+2;row++)     //Loop to determine nieghbors
{
cout<<endl;
for(col=1;col<n+2;col++)
{
if ((life_array[row][col])=='X') //If box has an X
{
num_neighbors=checkx(life_array,row,col);
if (num_neighbors != 3)  //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)   //Condition to determine if X is born
new_array[row][col]=' ';
else
new_array[row][col]='X';
}
}
}

for(row=1;row<n+2;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-1][col])=='X')
num_neighbors++;
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-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;

}```

14. Code:
```if ((life_array[row-1][col-1])=='X')
num_neighbors++;
if ((life_array[row-1][col])=='X')
num_neighbors++;

//not right, i think
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-1])=='X')
num_neighbors++;
if ((life_array[row+1][col])=='X')
num_neighbors++;
if ((life_array[row+1][col+1])=='X')
num_neighbors++;```
this is my conjecture cos i dont know how the game works

15. Yeah, Raison pointed out your mistake, you have [row-1][col-1] twice in your check neighbor function. As for printing out 12x12, only go to n+1 in your loops.