康威生命游戏,详细见百度https://baike.baidu.com/item/%E7%94%9F%E5%91%BD%E6%B8%B8%E6%88%8F/2926434
简而言之就是在一个类似于棋盘的区域内,每个点上可以“生存”或者“死去”一个“棋子”,或者说,“细胞”。其规则如下:
1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。
2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变)
现在,如何用c语言来写一个简单的生命游戏呢?
So show you my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define MAXROW 40
#define MAXCOL 40
#define DEAD 0
#define ALIVE 1
int map[MAXROW][MAXCOL], newmap[MAXROW][MAXCOL];
void init();
int neighbors(int, int);
void outputMap();
void copyMap();
// main
int main()
{
srand(time(0));
int i,j;
int row, col;
char ans;
// random start
for(row = 0; row < MAXROW; row++)
for(col = 0; col < MAXCOL; col++)
map[row][col] = DEAD;
for(j=0;j<=100;j++) // j = # of alive
{
int row = rand() % 40 + 1;
int col = rand() % 40 + 1;
if(0 <= row && row < MAXROW &&
0 <= col && col < MAXCOL)
map[row][col] = ALIVE;
}
printf("\nThe game begin:");
outputMap();
for(i=1;i<=100;i++) // 100 iterations
{
for(row = 0; row < MAXROW; row++)
{
for(col = 0; col < MAXCOL; col++)
{
switch (neighbors(row, col))
{
case 0:
case 1:
case 4: