生命游戏

复制粘贴后运行按1 or 0

// game of life.cpp
 //function head
 #include <stdio.h>
 #include <stdlib.h> 
 #include <windows.h>
 #include <time.h> 
 #include <ctype.h> 
 //define the size of the two-dimensional array
 #define SIZE 40 
 void rule(int array[SIZE][SIZE]);
 //function prototype 
 void SetColor(unsigned short ForeColor=5,unsigned short
               BackGroundColor=2)   
 //change color 
 {  
     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
     SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor); 
 }  
 int main()
 {  
     int data[SIZE][SIZE]={0};//0->virus  1->cell  2->boundary 
     int i,j,instruct; 
 srand(time(NULL));//use the current time to seed the random number generator   
 for(i=0;i<=SIZE-1;i++)
 { 
     for(j=0;j<=SIZE-1;j++)
     {    
         if(i*j==0||i==SIZE-1||j==SIZE-1)
         {                
             data[i][j]=22;    
         }//end if   
         else    
             data[i][j]=11*(rand()%2);  
     }//end for  
 }//end for 
 system("mode con cols=100  & color 0f");//to set the size and the color of the window       
 //input a random number except 0 to continue the game  
 while(scanf("%d",&instruct)!=0)
 {   
     system("cls");//clear the screen      
     for(i=0;i<=SIZE-1;i++)
     {    
         for(j=0;j<=SIZE-1;j++)
         {     
             if(data[i][j]%10==1)
             {     
                 SetColor(10);//function call       
                 printf("%c ",6);//display the character    
             }    
             else if(data[i][j]%10==0)
             {
                 SetColor(6);//function call      
                 printf("%c ",4);//display the character     
             }    
             else
             { 
                 SetColor(3);//function call                      
                 printf("%c ",3);//display the character    
             }     
             if(i*j==0||i==SIZE-1||j==SIZE-1)
             {     
                 ;    
             }    
             else       
                 data[i][j]=(data[i][j]%10)*10;   
         }    
         printf("\n");  
     }   
     rule(data);//function call   
     printf("请输入1继续0结束\n"); 
 }//end while 
 return 0; 
 } 
 void rule(int array[SIZE][SIZE]) 
 {     
     int amount,i,j,k,l;  
     for(i=1;i<=SIZE-2;i++)
     {    
         for(j=1;j<=SIZE-2;j++)
         {    
             amount=0;    
             for(k=i-1;k<=i+1;k++)
             {     
                 for(l=j-1;l<=j+1;l++)
                 {       
                     if(array[k][l]/10==1&&(k!=i||l!=j))
                     {       
                         amount++;     
                     }//end if     
                 }//end for    
             }//end for     
             //** the rule of the game of life    
             if(amount==3)
             {     
                 array[i][j]++;   
             }    
             else if(amount==2)
             {     
                 array[i][j]+=array[i][j]/10;   
             }    
             else
                 ; 
         }//end for  
     }//end for
 }

 

转载于:https://www.cnblogs.com/gj-Acit/archive/2013/01/06/2848140.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值