#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define DEAD 0
#define ALIVE 1

typedef unsigned char unchar;

unchar  **allocation     (                int rows,int cols);
int initialization       (unchar **curr  ,int rows,int cols);
int countNeighbors       (unchar **curr  ,int rows,int cols);
int countAlives          (unchar **curr  ,int rows,int cols);
void nextGeneration      (unchar **curr  ,unchar **next,int rows,int cols);
void copyArrays          (unchar **curr  ,unchar **next,int rows,int cols);
void display             (unchar **arr  ,int rows,int cols);

int main()
{unchar **curr={DEAD}; //Keeping current generation
 unchar **next={DEAD}; //Keeping next generation

int i,j,pos;
int **Count_Neighbors,numStillAlives;
int numAlives; //A number of alive cells in cur[]
int numLoops=0; //A number of loops
int rows;
int cols;
printf("Enter Number of  Rows = ");scanf("%d",&rows);
printf("Enter Number of  Cols = ");scanf("%d",&cols);
curr = allocation(rows,cols);
next = allocation(rows,cols);
numAlives = initialization(curr,rows,cols);



 while(numStillAlives != 0 && numLoops !=1000)  ///if numAlive =0 && numLoop = 1000 it will End!!!
 {       numStillAlives = countAlives(curr,rows,cols);
         nextGeneration(curr,next,rows,cols);
         copyArrays    (curr,next,rows,cols);
         display       (curr,rows,cols);
         numLoops++;        
}
getchar();
getchar();
return 0;
}
///////////////////////////////////////////////////////////////////////////////
unchar  **allocation(int rows, int cols)                                       
{   unchar **allo;                                    
    int i,j;                                          
 allo = (unchar **) malloc (sizeof(unchar *)*rows);   
  if (allo == NULL)  
  {printf("Unale to allocate memory");
   return 0;	}
 for(i=0;i<rows;i++)                                     
   allo[i] = (unchar *) malloc (sizeof(unchar)*cols); 
  if (p[i] == NULL)
  {printf("Unale to allocate memory");
  return 0;	}                                                    
  return allo;                                        
}
///////////////////////////////////////////////////////////////////////////////
int initialization (unchar **curr,int rows,int cols)
{ int i,j;
  int numAlive =0;
 
    srand(time(NULL));
                                           ////////////////////////////////////////
 for(i=0;i<rows;i++)                       ////                                ////     
  for(j=0;j<cols;j++)                      ////  Random 0 , 1 to curr[i][j]    //// 
   curr[i][j] = rand()%2 ;                 ////                                ////      
 for(i=0;i<rows;i++)                       ////////////////////////////////////////
  for(j=0;j<cols;j++)                      ////                                ////    
   if(curr[i][j] == ALIVE )                ////        Count NumAlive          ////
       numAlive++;                         ////                                ////
                                           ////////////////////////////////////////
  for(i=0;i<rows;i++)                      ////////////////////////////////////////                       
   { for(j=0;j<cols;j++)                   ////                                ////                       
      printf("%3d",curr[i][j]);            ////                                ////                       
      if(i==2)                             ////  Display the first generation  ////
      printf("\tNumStillAlive = %d",       ////                                ////
              countAlives(curr,rows,cols));////             and                ////
      printf("\n");                        ////                                ////
                                           ////       Display NumAlive         ////
   }                                       ////                                ////
                                           ////                                ////    
    return numAlive;                       ////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
int countNeighbors (unchar **curr,int rows,int cols)
{  int i,j;
   unchar **sum;
    sum = allocation(rows,cols);                       /////////////////////////                        
                                                       ////   Count Neibor  ////                  
    printf("\n");                                      ////    in 4 corners ////                  
                                                       ////*****************////                 
  sum[0][0] = 0;                                       ////                 ////                                             
  if(curr[0][1]==ALIVE)sum[0][0]++;                    ////   Count Neibor  ////
  if(curr[1][0]==ALIVE)sum[0][0]++;                    ////    in Top-Left  ////
  if(curr[1][1]==ALIVE)sum[0][0]++;                    ////                 ////
  sum[0][cols-1]=0;                                    ////*****************////
  if(curr[0][cols-2]==ALIVE)sum[0][cols-1]++;          ////   Count Neibor  ////    
  if(curr[1][cols-2]==ALIVE)sum[0][cols-1]++;          ////                 //// 
  if(curr[1][cols-1]==ALIVE)sum[0][cols-1]++;          ////   in Top-Right  ////
  sum[rows-1][0]=0;                                    ////*****************////
  if(curr[rows-2][0]==ALIVE)sum[rows-1][0]++;          ////  Count Neibor   ////
  if(curr[rows-2][1]==ALIVE)sum[rows-1][0]++;          ////                 ////
  if(curr[rows-1][1]==ALIVE)sum[rows-1][0]++;          ////  in Bottom-Left ////  
  sum[rows-1][cols-1]=0;                               ////*****************////
  if(curr[rows-2][cols-2]==ALIVE)sum[rows-1][cols-1]++;////  Count Neibor   ////   
  if(curr[rows-2][cols-1]==ALIVE)sum[rows-1][cols-1]++;////   in Top-Left   ////
  if(curr[rows-1][cols-2]==ALIVE)sum[rows-1][cols-1]++;////                 ////
                                                       /////////////////////////    
   for(i=1;i<rows-1;i++)                               /////////////////////////             
   {   for(j=1;j<cols-1;j++)                           ////                 ////                 
     {    sum[i][j] = 0;                               ////                 ////                 
          if(curr[i-1][j-1]==ALIVE)sum[i][j]++;        ////                 ////
          if(curr[i-1][j]  ==ALIVE)sum[i][j]++;        ////  Count Neibor   ////
          if(curr[i-1][j+1]==ALIVE)sum[i][j]++;        ////                 ////
                                                       ////                 ////
          if(curr[i][j-1]==ALIVE)sum[i][j]++;          ////        in       ////
          if(curr[i][j+1]==ALIVE)sum[i][j]++;          ////                 ////
                                                       ////                 ////
          if(curr[i+1][j-1]==ALIVE)sum[i][j]++;        //// area of Center  ////
          if(curr[i+1][j]  ==ALIVE)sum[i][j]++;        ////                 ////
          if(curr[i+1][j+1]==ALIVE)sum[i][j]++;        ////                 ////
                                                       ////                 ////
     }///end for                                       ////                 ////
   }                                                   /////////////////////////
   for(j=1;j<cols-1;j++)                               /////////////////////////    
    {    sum[0][j]=0;                                  ////                 ////
        if(curr[0][j-1]==ALIVE)sum[0][j]++;            ////  Count Neibor   ////
        if(curr[0][j+1]==ALIVE)sum[0][j]++;            ////                 ////
                                                       ////       of        ////
        if(curr[1][j-1]==ALIVE)sum[0][j]++;            ////                 ////
        if(curr[1][j]  ==ALIVE)sum[0][j]++;            ////    Top Edge     ////
        if(curr[1][j+1]==ALIVE)sum[0][j]++;            ////                 ////
    }                                                  /////////////////////////
    for(j=1;j<cols-1;j++)                              /////////////////////////                         
    {    sum[rows-1][j]=0;                             ////                 ////
        if(curr[rows-1][j-1]==ALIVE)sum[rows-1][j]++;  ////  Count Neibor   ////
        if(curr[rows-1][j+1]==ALIVE)sum[rows-1][j]++;  ////                 ////
                                                       ////       of        //// 
        if(curr[rows-2][j-1]==ALIVE)sum[rows-1][j]++;  ////                 ////
        if(curr[rows-2][j]  ==ALIVE)sum[rows-1][j]++;  ////   Bottom Edge   ////
        if(curr[rows-2][j+1]==ALIVE)sum[rows-1][j]++;  ////                 ////
    }                                                  /////////////////////////
    for(i=1;i<rows-1;i++)                              /////////////////////////        
    {    sum[i][0]=0;                                  ////                 ////        
        if(curr[i-1][0]==ALIVE)sum[i][0]++;            ////  Count Neibor   ////            
        if(curr[i+1][0]==ALIVE)sum[i][0]++;            ////                 ////
                                                       ////       of        ////              
        if(curr[i-1][1]==ALIVE)sum[i][0]++;            ////                 ////
        if(curr[i][1]  ==ALIVE)sum[i][0]++;            ////    Left Edge    ////           
        if(curr[i+1][1]==ALIVE)sum[i][0]++;            ////                 ////
    }                                                  /////////////////////////
     for(i=1;i<rows-1;i++)                             /////////////////////////                         
    {    sum[i][cols-1]=0;                             ////                 ////  
        if(curr[i-1][cols-1]==ALIVE)sum[i][cols-1]++;  ////  Count Neibor   ////          
        if(curr[i+1][cols-1]==ALIVE)sum[i][cols-1]++;  ////                 ////              
                                                       ////       of        ////           
        if(curr[i-1][cols-2]==ALIVE)sum[i][cols-1]++;  ////                 ////   
        if(curr[i][cols-2]  ==ALIVE)sum[i][cols-1]++;  ////   Right Edge    ////         
        if(curr[i+1][cols-2]==ALIVE)sum[i][cols-1]++;  ////                 ////   
    }                                                  /////////////////////////
    
    
  
  return sum;  
}
//////////////////////////////////////////////////////////////////////////////
int countAlives(unchar **curr  ,int rows,int cols)
{ int i,j;
  int numstillalive=0;         
  
for(i=0;i<rows;i++)          ///////////////////////////////////////
  for(j=0;j<cols;j++)        ////                               ////              
    if(curr[i][j]==ALIVE)    //// Count Alive in Current state  ////
       numstillalive++;      ////                               ////
                             ///////////////////////////////////////
  
  return numstillalive;
}
//////////////////////////////////////////////////////////////////////////////
void nextGeneration(unchar **curr ,unchar **next,int rows,int cols)
{ int i,j;
  unchar **sum1;
  sum1 = allocation(rows,cols);
  sum1 = countNeighbors(curr,rows,cols);
                                               /////////////////////////////////      
  for(i=0;i<rows;i++)                          ////                         ////         
    for(j=0;j<cols;j++)                        ////   if curr[i][j] = ALIVE ////
     { if(curr[i][j]==ALIVE)                   ////      if Neibor = 2,3    ////
        { if((sum1[i][j]==2)||(sum1[i][j]==3)) ////    Next Gen still Alive ////
           next[i][j] = ALIVE ;                ////       else Neibor =     ////
          else                                 ////       0,1,4,5,6,7,8     ////
           next[i][j] = DEAD  ;                ////    Next Gen will  Dead  ////
        }                                      ////                         ////
                                               /////////////////////////////////
     else                                      ////  if curr[i][j] = DEAD   ////    
       { if(sum1[i][j]==3)                     ////     if Neibor = 3       ////
          next[i][j] = ALIVE;                  ////   Next Gen will ALIVE   ////
         else                                  ////    else Still DEAD      ////                  
          next[i][j] = DEAD;                   ////                         ////
       }                                       ////                         ////
     }/// end for                              /////////////////////////////////
    
 }
//////////////////////////////////////////////////////////////////////////////
void copyArrays(unchar **curr  ,unchar **next,int rows,int cols)
{ int i,j;
  for(i=0;i<rows;i++)            /////////////////////////
    for(j=0;j<cols;j++)          ////   Copy Array    //// 
      curr[i][j] = next[i][j];   /////////////////////////
 }
//////////////////////////////////////////////////////////////////////////////
void display(unchar **arr,int rows,int cols)
{ int i,j;
                              //////////////////////////////////////////////////
for(i=0;i<rows;i++)           ////                                          ////  
 { for(j=0;j<cols;j++)        //// This Fuction print and Display  NumAlive ////
    printf("%3d",arr[i][j]);  ////                                          ////
    if(i==(rows/2)-1)         //////////////////////////////////////////////////
    printf("\tNumStillAlive = %d",countAlives(arr,rows,cols));
     printf("\n");
  }
}
//////////////////////////////////////////////////////////////////////////////













