康威生命游戏规则:
若一格细胞周围有2或3个细胞,则这个细胞在下一代中存活,否则死亡
若一个空格周围有3个细胞,则这个空格在下一代变成细胞
源码:
super c:
#include <stdio.h>
#define SIZE 10
#define TIME 20
#define START 0
int main(void){
printf("康威生命游戏nia~\n");
int current[SIZE][SIZE];
int next[SIZE][SIZE];
//初始化
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
current[i][j] = 0;
next[i][j] = 0;
}
}
//设定初始细胞
current[1][1] = 1;
current[1][2] = 1;
current[1][3] = 1;
current[2][1] = 1;
current[2][3] = 1;
current[3][1] = 1;
current[3][2] = 1;
current[3][3] = 1;
current[3][4] = 1;
current[1][6] = 1;
current[1][8] = 1;
current[2][6] = 1;
current[2][7] = 1;
current[3][6] = 1;
current[3][8] = 1;
current[5][1] = 1;
current[5][3] = 1;
current[6][2] = 1;
current[7][2] = 1;
current[5][6] = 1;
current[5][8] = 1;
current[6][6] = 1;
current[6][8] = 1;
current[7][6] = 1;
current[7][7] = 1;
current[7][8] = 1;
//拷贝
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
next[i][j] = current[i][j];
}
}
//打印
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
if(current[i][j]) {
printf("■ ");
}
else{
printf("□ ");
}
}
printf("\n");
}
//演化
for(int time=0; time<TIME + START;time++){
printf("第%d代演化:\n", time+1);
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
//周围细胞为2或3存活
int around = 0;
//最边上的不算外面的
//左
if(j > 0 && current[i][j-1]) around++;
//上
if(i > 0 && current[i-1][j]) around++;
//右
if(j < SIZE-1 && current[i][j+1]) around++;
//下
if(i < SIZE-1 && current[i+1][j]) around++;
//左上
if(j > 0 && i > 0 && current[i-1][j-1]) around++;
//左下
if(j > 0 && i <SIZE-1 && current[i+1][j-1]) around++;
//右上
if(j < SIZE-1 && i > 0 && current[i-1][j+1]) around++;
//右下
if(j < SIZE-1 && i < SIZE-1 && current[i+1][j+1]) around++;
//若这格有细胞
若一格细胞周围有2或3个细胞,则这个细胞在下一代中存活,否则死亡
若一个空格周围有3个细胞,则这个空格在下一代变成细胞
源码:
super c:
#include <stdio.h>
#define SIZE 10
#define TIME 20
#define START 0
int main(void){
printf("康威生命游戏nia~\n");
int current[SIZE][SIZE];
int next[SIZE][SIZE];
//初始化
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
current[i][j] = 0;
next[i][j] = 0;
}
}
//设定初始细胞
current[1][1] = 1;
current[1][2] = 1;
current[1][3] = 1;
current[2][1] = 1;
current[2][3] = 1;
current[3][1] = 1;
current[3][2] = 1;
current[3][3] = 1;
current[3][4] = 1;
current[1][6] = 1;
current[1][8] = 1;
current[2][6] = 1;
current[2][7] = 1;
current[3][6] = 1;
current[3][8] = 1;
current[5][1] = 1;
current[5][3] = 1;
current[6][2] = 1;
current[7][2] = 1;
current[5][6] = 1;
current[5][8] = 1;
current[6][6] = 1;
current[6][8] = 1;
current[7][6] = 1;
current[7][7] = 1;
current[7][8] = 1;
//拷贝
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
next[i][j] = current[i][j];
}
}
//打印
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
if(current[i][j]) {
printf("■ ");
}
else{
printf("□ ");
}
}
printf("\n");
}
//演化
for(int time=0; time<TIME + START;time++){
printf("第%d代演化:\n", time+1);
for(int i=0; i<SIZE;i++){
for(int j=0;j<SIZE;j++){
//周围细胞为2或3存活
int around = 0;
//最边上的不算外面的
//左
if(j > 0 && current[i][j-1]) around++;
//上
if(i > 0 && current[i-1][j]) around++;
//右
if(j < SIZE-1 && current[i][j+1]) around++;
//下
if(i < SIZE-1 && current[i+1][j]) around++;
//左上
if(j > 0 && i > 0 && current[i-1][j-1]) around++;
//左下
if(j > 0 && i <SIZE-1 && current[i+1][j-1]) around++;
//右上
if(j < SIZE-1 && i > 0 && current[i-1][j+1]) around++;
//右下
if(j < SIZE-1 && i < SIZE-1 && current[i+1][j+1]) around++;
//若这格有细胞