#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define DEAD 0
#define ALIVE 1
#define MAX 40
typedef unsigned char unchar;


int initialization       (unchar curr[]);
int countNeighbors       (unchar curr[]  ,int pos);
int countAlives          (unchar curr[]  ,int len);
void nextGeneration      (unchar curr[]  ,unchar next[],int len);
void copyArrays          (unchar curr[]  ,unchar next[],int len);
void display             (unchar arr[], int len);

int main()
{unchar curr[MAX]={DEAD}; //Keeping current generation
 unchar next[MAX]={DEAD}; //Keeping next generation

int i,pos;
int Count_Neighbors,numStillAlives;
int numAlives; //A number of alive cells in cur[]
int numLoops=0; //A number of loops

numAlives = initialization(curr);   //Initializing the first generation

for(i=0;i<MAX;i++)       ////////////////////////////////
  printf("%d",curr[i]);  //Display the first generation//
			 ////////////////////////////////
printf("        NumAlives = %d\n",numAlives);

while( (numStillAlives != 0)&&(numLoops !=100) )   ////if NumALive = 0 && numLoop = 100 program will end!!!
{      nextGeneration(curr,next,MAX);
       numStillAlives =  countAlives(next,MAX);
       copyArrays(curr,next,MAX);
       display(curr,MAX);
       printf("        NumAlives = %d\n",numStillAlives);
       numLoops++;
}

getchar();
getchar();
return 0;
}

///////////////////////////////////////////////////////////////////////////////
int initialization(unchar curr[])
{int i;
 int numAlives=0;

   srand(time(NULL));    ///////////////////////////
 for(i=0;i<MAX;i++)      ////                   ////
  {                      ////Random 0,1 to Curr ////
   curr[i] = rand()%2;   ////                   ////
  }                      ///////////////////////////
 // curr[0]=1;curr[1]=0;curr[2]=1;curr[3]=1;curr[4]=1;for(i=5;i<MAX;i++)curr[i]=0;
for(i=0;i<MAX;i++)       ///////////////////////////
  if(curr[i] ==ALIVE)    ////  Count Numalives  ////
    numAlives++;         ///////////////////////////

return numAlives;
}
///////////////////////////////////////////////////////////////////////////////
int countNeighbors(unchar curr[],int pos)
{
 int i;

 int CountForward[MAX],CountBackward[MAX],SumCount[MAX];
                                        ///////////////////////////////////                                       
 SumCount[0]=0;                         ////                           ////               
  if(curr[1]==ALIVE)SumCount[0]++;      ////                           ////
  if(curr[2]==ALIVE)SumCount[0]++;      ////                           ////
 SumCount[1]=0;                         ////                           ////                                    
  if(curr[2]==ALIVE)SumCount[1]++;      ////                           ////
  if(curr[3]==ALIVE)SumCount[1]++;      ////                           ////
  if(curr[0]==ALIVE)SumCount[1]++;      ////                           ////              
 SumCount[38]=0;                        ////     Special Case !!!!     ////    
  if(curr[36]==ALIVE)SumCount[38]++;    ////                           ////
  if(curr[37]==ALIVE)SumCount[38]++;    ////                           ////
  if(curr[39]==ALIVE)SumCount[38]++;    ////                           ////
 SumCount[39]=0;                        ////                           ////
  if(curr[37]==ALIVE)SumCount[39]++;    ////                           ////
  if(curr[38]==ALIVE)SumCount[39]++;    ////                           ////
                                        ///////////////////////////////////
 for(i=2;i<MAX-2;i++)
 {
      CountForward[i]= 0;  //////////////////////
      if(curr[i+1]==ALIVE) ////              ////
	    CountForward[i]++; //// CountForward ////
      if(curr[i+2]==ALIVE) ////              ////
	    CountForward[i]++; //////////////////////

      CountBackward[i]=0;  //////////////////////
      if(curr[i-1]==ALIVE) ////              ////
	    CountBackward[i]++;//// CountBackward////
      if(curr[i-2]==ALIVE) ////              ////
	    CountBackward[i]++;//////////////////////
						                              /////////////////////////
 SumCount[i] = CountForward[i] + CountBackward[i] ;  //// Count Neighbors ////
						                            /////////////////////////
 }


return SumCount[pos];
}
///////////////////////////////////////////////////////////////////////////////
int countAlives(unchar curr[]  ,int len)
{ int numStillAlives=0;
  int i;  
  for(i=0;i<MAX;i++)          ////////////////////////////////////
     if(curr[i]==ALIVE)       ////   Count Alive in Current   //// 
        numStillAlives++;     ////////////////////////////////////   
  
 return numStillAlives;   
}
//////////////////////////////////////////////////////////////////////////////
void nextGeneration      (unchar curr[]  ,unchar next[],int len)
{ 
 int i;
 int SumCount[MAX];

 for(i=0;i<len;i++)                              ///////////////////////////////
   {                                             ////     Send curr[i]      ////    
     SumCount[i] =  countNeighbors(curr,i);      ////         and           ////
   }                                             ////  pos to Count Neibors ////
                                                 ///////////////////////////////        
for(i=0;i<len;i++)                             
   {                                            ////////////////////////////////
     if(curr[i] == ALIVE)                       //// if curr[i] = ALIVE     ////                    
      {                                         ////                        //// 
      if (SumCount[i] == 2 || SumCount[i] == 4) ////  if Neibor = 2,4       ////  
         next[i] = ALIVE ;                      ////   Next Gen still ALIVE ////  
      else                                      ////  else Neibor = 0,1,3   ////
         next[i] = DEAD  ;                      ////   Next Gen will DEAD   ////                
      }                                         ////////////////////////////////
                                                ////////////////////////////////
     else                                       //// if curr[i] = DEAD      ////    
      { if(SumCount[i] == 2 || SumCount[i]== 3) ////  if Neibor = 2,3       ////
         next[i] = ALIVE ;                      ////    Next Gen will ALIVE ////
      }                                         ////////////////////////////////
         
   } 
}//end function
//////////////////////////////////////////////////////////////////////////////
void copyArrays(unchar curr[]  ,unchar next[],int len)
{  int i;
     for(i=0;i<len;i++)
       curr[i] = next[i] ;
 }
//////////////////////////////////////////////////////////////////////////////
void display(unchar arr[], int len)
{  int i;
      for(i=0;i<len;i++)
         printf("%d",arr[i]);
    
 }
//////////////////////////////////////////////////////////////////////////////
