生命游戏代码

生命游戏

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <time.h>
  4 #include <conio.h>
  5 
  6 #define ROWLEN 10
  7 #define COLLEN 10
  8 #define DEAD 10
  9 #define ALIVE 1
 10 int cell[ROWLEN][COLLEN];
 11 int celltemp[ROWLEN][COLLEN];
 12 
 13 void initcell()
 14 {
 15     int row,col;
 16     
 17     for(row = 0 ;row < ROWLEN;row++)
 18     {
 19         for(col = 0;col < COLLEN;col++)
 20         {
 21             cell[row][col] = DEAD;
 22         }
 23     }
 24     printf("请先输入一组活细胞的坐标位置,输入(-1-1)结束:\n");
 25     while(1)
 26     {
 27         printf("请输入一个活细胞的坐标位置:");
 28         scanf("%d%d",&row,&col);
 29         if(0 <= row && row < ROWLEN && 0 <= col && col < COLLEN)
 30         {
 31             cell[row][col] = ALIVE;
 32         }
 33         else if(row == -1 || col == -1)
 34         {
 35             break;
 36         }
 37         else
 38         {
 39             printf("输入坐标超过范围。\n");
 40         }
 41     }
 42 }
 43 
 44 int LinSum(int row,int col)
 45 {
 46     int count = 0,c,r;
 47     
 48     for(r = row - 1;r <= row + 1;r++)
 49     {
 50         for(c = col - 1; c <= col + 1;c++)
 51         {
 52             if(r < 0 || r >= ROWLEN || c < 0 || c >= COLLEN)
 53             {
 54                 continue;
 55             }
 56             if(cell[r][c] == ALIVE)
 57             {
 58                 count++;
 59             }
 60         }
 61         
 62     }
 63     if(cell[row][col] == ALIVE)
 64     {
 65         count--;
 66     }
 67     return count;
 68 }
 69 
 70 void OutCell()
 71 {
 72     int row,col;
 73     
 74     printf("\n细胞状态\n");
 75     printf("");
 76     for(col = 0;col < COLLEN - 1;col++)
 77     {
 78         printf("──┬");
 79     }
 80     printf("──┐\n");
 81     for(row = 0;row < ROWLEN;row++)
 82     {
 83         printf("");
 84         for(col = 0;col < COLLEN;col++)
 85         {
 86             switch(cell[row][col])
 87             {
 88                 case ALIVE: printf("●│");
 89                             break;
 90                 case DEAD: printf("○│");
 91                             break;
 92                 default:
 93                         ;
 94             }
 95         }
 96         printf("\n");
 97         
 98         if(row < ROWLEN - 1)
 99         {
100             printf("");
101             for(col = 0;col < COLLEN - 1;col++)
102             {
103                 printf("──┼");
104             }
105             printf("──┤\n");
106         }
107     }
108     printf("");
109     for(col = 0;col < COLLEN - 1;col++)
110     {
111         printf("──┴");
112     }
113     printf("──┘\n");
114 }
115 
116 void cellfun()
117 {
118     int row,col,sum;
119     int count = 0;
120     
121     for(row = 0; row < ROWLEN;row ++)
122     {
123         for(col = 0;col < COLLEN;col++)
124         {
125             switch(LinSum(row,col))
126             {
127                 case 2: celltemp[row][col] = cell[row][col];
128                         break;
129                 case 3: celltemp[row][col] = ALIVE;
130                         break;
131                 default: celltemp[row][col] = DEAD;
132             }
133         }
134     }
135     for(row = 0;row < ROWLEN;row++)
136     {
137         for(col = 0;col < COLLEN;col++)
138         {
139             cell[row][col] = celltemp[row][col];
140         }
141     }
142     for(row = 0;row < ROWLEN;row++)
143     {
144         for(col = 0;col < COLLEN;col++)
145         {
146             if(cell[row][col] == ALIVE)
147             {
148                 count++;
149             }
150         }
151     }
152     sum = count;
153     OutCell();
154     printf("当前状态下,共有%d个活细胞。\n",sum);
155 }
156 
157 int main()
158 {
159     char again;
160     printf("生命游戏!\n");
161     initcell();
162     OutCell();
163     printf("按任意键开始游戏,进行细胞转换。\n");
164     getch();
165     S1:  cellfun();
166     S2:  printf("\n继续生产下一次细胞的状态(y/n)?");
167     fflush(stdin);
168     scanf("%c",&again);
169     if(again == 'Y' || again == 'y')
170     {
171         goto S1;
172     }
173     else if(again == 'n' || again == 'N')
174     {
175         goto S3;
176     }
177     else
178     {
179         printf("输入错误,请重新输入!\n");
180         goto S2;
181     }
182     S3:  printf("游戏结束!\n");
183 
184     return 0;
185 }

 

posted @ 2021-01-03 15:06  互联星空  阅读(617)  评论(0编辑  收藏  举报