game of life

#include<iostream>
#include<cstring>
#include <iomanip>
#include <cstdlib>
//dear all i dont know how to return array can pls help me.
using namespace std;
char new_array[5][5];

char life_array[5][5];
int row;
int col;
int n=5;
int num_neighbors=0;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void initGeneration()

{

int num_gen;

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


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


}

char change= boarder(life_array);
for(row=0;row<n;row++)
{
cout<<endl;
for(col=0;col<n;col++)
{

cout<<life_array[row][col]<<" ";
}



}

}



char boarder(life_array)


{

for(row=0;row<5;row++)
for( col=0;col<5;col++)
{
if((row==0)||(row==4))
life_array[row][col]='-';
if((col==0)||(col==4))
life_array[row][col]='-';
}
return life_array;
}



int checkx()
//function to determine number of 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-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+1])=='X')
num_neighbors++;

return num_neighbors;

}

void nextGeneration()
{

for(int num_gen=0;num_gen<n;num_gen++)

{



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

for(row=1;row<n;row++)
{
cout<<endl;
for(col=1;col<n;col++)
{
if ((life_array[row][col])=='X')
{
num_neighbors=checkx();
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();
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];


}

}



void display()
{

void nextGeneration();

}


int main()
{
int ch;
//again: clrscr();
cout<<"what do u want"<<endl;
cout<<"1...... to initioalize generation"<<endl;
cout<<"2...... to see next generation"<<endl;
cout<<"3...... to display generetion list"<<endl;
cout<<"4...... to exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
initGeneration();
break;
case 2:
nextGeneration();
break;
case 3:
display();
break;
default :
exit(0);
cout<<"do u want to continue"<<endl;
//cin>> res;
//if(res==1)
// goto again;
//else
// exit(0);*/
}
return 0;
}
Last edited on
Topic archived. No new replies allowed.