# include<iostream>
using namespace std;
# define N 10
main()
{
char life[10][10]={{' ','*',' ','*','*','*','*',' ','*'},
{' ','*',' ','*',' ','*','*','*','*','*'},
{' ','*',' ','*','*',' ','*','*',' ',' '},
{' ',' ',' ','*','*','*','*','*',' ','*'},
{'*','*',' ',' ','*','*','*','*','*','*'},
{' ','*','*','*',' ','*',' ','*','*','*'},
{' ',' ','*','*','*','*','*','*',' ',' '},
{'*','*',' ','*','*','*','*','*','*','*'},
{' ','*',' ','*',' ','*',' ','*',' ','*'},
{'*','*',' ','*','*','*','*','*','*','*'}
};
int i,j,n,m;
int b[10][10];//中间变量,默认值为0,如果为1.则说明该位置在下代出生生物,若为2,则说明该位置的生物下代死亡
cout<<"---------第1代---------\n";
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cout<<"%c",life[i][j];
}
cout<<"\n";
}
cout<<"\n\n";
/*以上语句为给此二维数组赋值*/
for(n=1;n<20;n++){
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
m=0;
if(life[i][j]==' ')
{
if(i+1<N&&life[i+1][j]=='*')
m++;
if(i-1>=0&&life[i-1][j]=='*')
m++;
if(j+1<N&&life[i][j+1]=='*')
m++;
if(j-1>=0&&life[i][j-1]=='*')
m++;
if(j+1<N&&i+1<N&&life[i+1][j+1]=='*')
m++;
if(j+1<N&&i-1>=0&&life[i-1][j+1]=='*')
m++;
if(j-1>=0&&i+1<N&&life[i+1][j-1]=='*')
m++;
if(j-1>=0&&i-1>=0&&life[i-1][j-1]=='*')
m++;
if(m==3)
b[i][j]=1; }
m=0;
if(life[i][j]=='*')
{
if(i+1<N&&life[i+1][j]=='*')
m++;
if(i-1>=0&&life[i-1][j]=='*')
m++;
if(j+1<N&&life[i][j+1]=='*')
m++;
if(j-1>=0&&life[i][j-1]=='*')
m++;
if(j+1<N&&i+1<N&&life[i+1][j+1]=='*')
m++;
if(j+1<N&&i-1>=0&&life[i-1][j+1]=='*')
m++;
if(j-1>=0&&i+1<N&&life[i+1][j-1]=='*')
m++;
if(j-1>=0&&i-1>=0&&life[i-1][j-1]=='*')
m++;
if(m>=4||m<=1)
b[i][j]=2;
}
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(b[i][j]==1)
{
life[i][j]='*';
}
if(b[i][j]==2)
{
life[i][j]=' ';
}
}
}
cout<<"--------第%d代---------\n"<<n+1;
for(i=0;i<N;j++)
{
for(j=0;j<N;j++)
{
cout<<"&c",life[i][j];
}
cout<<"\n";
}
cout<<"\n\n";
}
}
using namespace std;
# define N 10
main()
{
char life[10][10]={{' ','*',' ','*','*','*','*',' ','*'},
{' ','*',' ','*',' ','*','*','*','*','*'},
{' ','*',' ','*','*',' ','*','*',' ',' '},
{' ',' ',' ','*','*','*','*','*',' ','*'},
{'*','*',' ',' ','*','*','*','*','*','*'},
{' ','*','*','*',' ','*',' ','*','*','*'},
{' ',' ','*','*','*','*','*','*',' ',' '},
{'*','*',' ','*','*','*','*','*','*','*'},
{' ','*',' ','*',' ','*',' ','*',' ','*'},
{'*','*',' ','*','*','*','*','*','*','*'}
};
int i,j,n,m;
int b[10][10];//中间变量,默认值为0,如果为1.则说明该位置在下代出生生物,若为2,则说明该位置的生物下代死亡
cout<<"---------第1代---------\n";
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cout<<"%c",life[i][j];
}
cout<<"\n";
}
cout<<"\n\n";
/*以上语句为给此二维数组赋值*/
for(n=1;n<20;n++){
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
m=0;
if(life[i][j]==' ')
{
if(i+1<N&&life[i+1][j]=='*')
m++;
if(i-1>=0&&life[i-1][j]=='*')
m++;
if(j+1<N&&life[i][j+1]=='*')
m++;
if(j-1>=0&&life[i][j-1]=='*')
m++;
if(j+1<N&&i+1<N&&life[i+1][j+1]=='*')
m++;
if(j+1<N&&i-1>=0&&life[i-1][j+1]=='*')
m++;
if(j-1>=0&&i+1<N&&life[i+1][j-1]=='*')
m++;
if(j-1>=0&&i-1>=0&&life[i-1][j-1]=='*')
m++;
if(m==3)
b[i][j]=1; }
m=0;
if(life[i][j]=='*')
{
if(i+1<N&&life[i+1][j]=='*')
m++;
if(i-1>=0&&life[i-1][j]=='*')
m++;
if(j+1<N&&life[i][j+1]=='*')
m++;
if(j-1>=0&&life[i][j-1]=='*')
m++;
if(j+1<N&&i+1<N&&life[i+1][j+1]=='*')
m++;
if(j+1<N&&i-1>=0&&life[i-1][j+1]=='*')
m++;
if(j-1>=0&&i+1<N&&life[i+1][j-1]=='*')
m++;
if(j-1>=0&&i-1>=0&&life[i-1][j-1]=='*')
m++;
if(m>=4||m<=1)
b[i][j]=2;
}
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(b[i][j]==1)
{
life[i][j]='*';
}
if(b[i][j]==2)
{
life[i][j]=' ';
}
}
}
cout<<"--------第%d代---------\n"<<n+1;
for(i=0;i<N;j++)
{
for(j=0;j<N;j++)
{
cout<<"&c",life[i][j];
}
cout<<"\n";
}
cout<<"\n\n";
}
}