生命游戏

复制粘贴后运行按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
 }

 

posted @ 2013-01-06 18:16  再见~雨泉  阅读(204)  评论(0编辑  收藏  举报