[C] 生命游戏 细胞死亡问题 →→→→→进入此内容的聊天室

来自 , 2020-02-23, 写在 C, 查看 110 次.
URL http://www.code666.cn/view/5d6646aa
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4.  
  5. #define MAXROW 10
  6. #define MAXCOL 25
  7. #define DEAD 0
  8. #define ALIVE 1
  9.  
  10.  
  11. /* 生命游戏 */
  12. /*某一细胞的邻居包括上、下、左、右、左上、左下、右上与右下相邻之细胞,游戏规则如下:
  13.   孤单死亡:如果细胞的邻居小于一个,则该细胞在下一次状态将死亡。
  14.   拥挤死亡:如果细胞的邻居在四个以上,则该细胞在下一次状态将死亡。
  15.   稳定:如果细胞的邻居为二个或三个,则下一次状态为稳定存活。
  16.   复活:如果某位置原无细胞存活,而该位置的邻居为三个,则该位置将复活一细胞。 */
  17.  
  18. int map[MAXROW][MAXCOL],newmap[MAXROW][MAXCOL];
  19.  
  20. void init();
  21. int neighbors ( int,int );
  22. void outputMap();
  23. void copyMap();
  24.  
  25. int main()
  26. {
  27.         int row,col;
  28.         char ans;
  29.         init();
  30.         while ( 1 )
  31.         {
  32.                 outputMap();
  33.                 for ( row=0; row<MAXROW; row++ )
  34.                 {
  35.                         for ( col=0; col<MAXCOL; col++ )
  36.                         {
  37.                                 switch ( neighbors ( row,col ) )
  38.                                 {
  39.                                 case 0:
  40.                                 case 1:
  41.                                 case 4:
  42.                                 case 5:
  43.                                 case 6:
  44.                                 case 7:
  45.                                 case 8:
  46.                                         newmap[row][col]=DEAD;
  47.                                         break;
  48.                                 case 2:
  49.                                         newmap[row][col]=map[row][col];
  50.                                         break;
  51.                                 case 3:
  52.                                         newmap[row][col]=ALIVE;
  53.                                         break;
  54.                                 }
  55.                         }
  56.                 }
  57.  
  58.                 copyMap();
  59.                 printf ( "\nContinuenextGeneration?" );
  60.                 getchar();
  61.                 ans=toupper ( getchar() );
  62.                 if ( ans!='Y' )        break;
  63.         }
  64.  
  65.  
  66.         return 0;
  67. }
  68.  
  69. void init()
  70. {
  71.         int row,col;
  72.  
  73.         for ( row=0; row<MAXROW; row++ )
  74.                 for ( col=0; col<MAXCOL; col++ )
  75.                         map[row][col]=DEAD;
  76.  
  77.         puts ( "GameoflifeProgram" );
  78.         puts ( "Enterx,ywherex,yislivingcell" );
  79.         printf ( "0<=x<=%d,0<=y<=%d\n",
  80.                  MAXROW-1,MAXCOL-1 );
  81.         puts ( "Terminatewithx,y=-1,-1" );
  82.  
  83.         while ( 1 )
  84.         {
  85.                 scanf ( "%d%d",&row,&col );
  86.                 if ( 0<=row&&row<MAXROW&&
  87.                         0<=col&&col<MAXCOL )
  88.                         map[row][col]=ALIVE;
  89.                 else if ( row==-1||col==-1 )
  90.                         break;
  91.                 else
  92.                         printf ( "(x,y)exceedsmapranage!" );
  93.         }
  94. }
  95.  
  96. int neighbors ( int row,int col )
  97. {
  98.         int count=0,c,r;
  99.         for ( r=row-1; r<=row+1; r++ )
  100.                 for ( c=col-1; c<=col+1; c++ )
  101.                 {
  102.                         if ( r<0||r>=MAXROW||c<0||c>=MAXCOL )
  103.                                 continue;
  104.                         if ( map[r][c]==ALIVE )
  105.                                 count++;
  106.                 }
  107.  
  108.         if ( map[row][col]==ALIVE )
  109.                 count--;
  110.         return count;
  111.  
  112. }
  113.  
  114. void outputMap()
  115. {
  116.         int row,col;
  117.         printf ( "\n\n%20cGameoflifecellstatus\n" );
  118.         for ( row=0; row<MAXROW; row++ )
  119.         {
  120.                 printf ( "\n%20c",' ' );
  121.                 for ( col=0; col<MAXCOL; col++ )
  122.                         if ( map[row][col]==ALIVE )     putchar ( '#' );
  123.                         else     putchar ( '-' );
  124.         }
  125. }
  126.  
  127. void copyMap()
  128. {
  129.         int row,col;
  130.         for ( row=0; row<MAXROW; row++ )
  131.                 for ( col=0; col<MAXCOL; col++ )
  132.                         map[row][col]=newmap[row][col];
  133. }

回复 "生命游戏 细胞死亡问题"

这儿你可以回复上面这条便签

captcha