C语言 细胞自动机(生命游戏)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include <windows.h>
  4 
  5 #define ROWS 60
  6 #define COLS 60
  7 
  8 int board[ROWS][COLS];
  9 int temp[ROWS][COLS];
 10 
 11 int system(const char *string);
 12 void randomize_board();
 13 void show_board();
 14 int num_neighbours(int x,int y);
 15 void update_board();
 16 
 17 int main(){
 18     randomize_board();
 19     while(1){
 20         update_board();
 21     }
 22     return 0;
 23 }
 24 
 25 //初始化
 26 void randomize_board(){
 27     for(int y=0;y<ROWS;y++){
 28         for(int x=0;x<COLS;x++){
 29             if(rand()%5==0){
 30                 board[x][y]=1;
 31             }
 32             temp[x][y] = board[x][y];
 33         }
 34     }
 35 }
 36 
 37 //显示
 38 void show_board(){
 39     system("cls");
 40     for(int y=0;y<ROWS;y++){
 41         for(int x=0;x<COLS;x++){
 42             if(board[x][y]){
 43                 printf("");
 44             }else{
 45                 printf("");
 46             }
 47         }
 48         printf("
");
 49     }
 50     Sleep(800);
 51 }
 52 
 53 //计算邻居数
 54 int num_neighbours(int x,int y){
 55     int num_adj = 0; //附近存活的细胞数
 56     int tmpy = y;
 57     int tmpx = x;
 58 
 59     //8号位
 60     if(y-1<0)
 61         tmpy = ROWS -1;
 62     else
 63         tmpy = y - 1;
 64     if(board[x][tmpy] == 1) num_adj++; 
 65 
 66     //2号位
 67     if(y+1>ROWS)
 68         tmpy = 0;
 69     else
 70         tmpy = y+1;
 71     if(board[x][tmpy]==1) num_adj++;
 72 
 73     //4号位
 74     if(x-1<0)
 75         tmpx = COLS-1;
 76     else
 77         tmpx = x-1;
 78     if(board[tmpx][y]==1)num_adj++;
 79 
 80     //6号位
 81     if(x+1>=COLS)
 82         tmpx = 0;
 83     else
 84         tmpx = x+1;
 85     if(board[tmpx][y]==1) num_adj++;
 86 
 87     //9号位
 88     if (y + 1 >= ROWS)
 89         tmpy = 0;
 90     else
 91         tmpy = y + 1;
 92     if (x + 1 >= COLS)
 93         tmpx = 0;
 94     else
 95         tmpx = x + 1;
 96     if (board[tmpx][tmpy] == 1) num_adj++;
 97 
 98     //1号位
 99     if (y - 1 <0)
100         tmpy = ROWS-1;
101     else
102         tmpy = y-1;
103     if (x - 1 < 0)
104         tmpx = COLS-1;
105     else
106         tmpx = x - 1;
107     if (board[tmpx][tmpy] == 1) num_adj++;
108 
109     //3号位
110     if(x+1>=COLS)
111         tmpx = 0;
112     else
113         tmpx = x+1;
114     if(y-1<0)
115         tmpy = ROWS-1;
116     else
117         tmpy = y-1;
118     if(board[tmpx][tmpy]==1) num_adj++;
119 
120     //7号位 y+ x-
121     if (y + 1 >= ROWS)
122         tmpy = 0;
123     else
124         tmpy = y + 1;
125     if(x-1<0)
126         tmpx = COLS-1;
127     else
128         tmpx = x - 1;
129     if(board[tmpx][tmpy]==1) num_adj++;
130 
131     return num_adj;
132 
133 }
134 
135 //更新
136 void update_board(){
137     int neighbours = 0;
138 
139     for(int y=0;y<ROWS;y++){
140         for(int x=0;x<COLS;x++){
141             neighbours = num_neighbours(x,y);
142             if(neighbours<2&&board[x][y]==1){
143                 temp[x][y]=0;
144             }
145             else if(neighbours>3&&board[x][y]==1){
146                 temp[x][y]=0;
147             }else if(neighbours==3&&board[x][y]==0){
148                 temp[x][y]=1;
149             }
150         }
151     }
152 
153     for(int i=0;i<ROWS;i++){
154         for(int j=0;j<COLS;j++){
155             board[j][i]=temp[j][i];
156         }
157     }
158     show_board();
159 }

 

原文地址:https://www.cnblogs.com/blzm742624643/p/9348942.html