Thread: Game of Life pros - help me

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Unhappy Game of Life pros - help me

    Help me . I am close on, just need help. The ouput for this game of life program is incorrect. I know the problem lies with the "occ" function.

    Code:
    #include <stdio.h>
    #define ROW 12
    #define COLUMN 17
    /*Function prototypes */
    int initconfig(int [ROW][COLUMN]); /*initial configuration*/
    void printgrid (int[ROW][COLUMN]); /* prints grid */
    int occ (int [ROW] [COLUMN], int, int); /*returns # of occupied cells around cells */
    void generate (int [ROW] [COLUMN],int [ROW] [COLUMN]); /*creates/prints new generation*/
    
    int main()
    {
    	int curgen [ROW] [COLUMN]={0}; /* initialize array elements to 0 */
    	int nextgen [ROW] [COLUMN]={0};
      int generation;
      int i; /* Initialize generation counter */
      generation = initconfig (curgen);
      for (i=1; i<= generation; i++)
      {
       printf("Generation #%d \n", i);
       generate(curgen, nextgen); /* generate next genaration using function while saving current */
      }
    
      return 0;
    }
    
     int initconfig(int curgen [ROW] [COLUMN]) /* function to generate initial configuration */
     {
       int rowcoord=0;
       int colcoord=0;
       int numofgen=0;
    
       printf("\n Enter the number of generations: \n"); /* prompt user for number of generations */
       scanf("%d", &numofgen);
    
        while ((rowcoord != -1)||(colcoord != -1)) /* while loop to prompt user for coordinates until -1 entered */
    	{
    	  printf("Enter coordinates(in row then column format): \n");
          printf("Press -1 when done \n");
    	  scanf("%d%d", &rowcoord, &colcoord);
    	   if ((colcoord< -1)|| (colcoord==0)||(colcoord>15))
    	   {
    		 printf("Invalid row value entered, please reenter.\n");
    		 break;
    	   }
    
    	   if ((rowcoord< -1)|| (rowcoord==0)|| (rowcoord >10))
           {
    		 printf("Invalid column value entered, please reenter both column and row values.\n");
    		 break;
           }
    	   curgen[rowcoord] [colcoord]=1;
    	}
    
    	printf("\n Initial State \n\n");
    	printgrid(curgen);
    	return numofgen;
    	}
    
     void printgrid(int P[ROW][COLUMN])/* function to print a grid */
     {
       int X;
       int Y;
    
       for (X=1; X<ROW-1; X++)
       {
    	for (Y=1; Y<COLUMN-1; Y++)
    	 if ( P[X][Y]==1)
    	  printf("%c",'*');
    	 else 
    	  printf("%c", '-');
    	  printf("\n");
       }
     }
    
     int occ (int curgen[ROW][COLUMN], int R,int C)
     {
       int occupied=0; /* initialized occupied counter to 0 */
    
       if ( curgen[R-1][C-1]==1) /* if cell is occupied add one to occupied counter */
    	  ++occupied;
       if ( curgen[R-1][C]==1)
          ++occupied;
       if ( curgen[R][C+1]==1)
          ++occupied;
       if ( curgen[R+1][C+1]==1)
          ++occupied;
       if ( curgen[R+1][C]==1)
          ++occupied;
       if ( curgen[R+1][C-1]==1)
          ++occupied;
       if ( curgen[R][C-1]==1)
          ++occupied;
       return occupied;
     }
    
     void generate(int curgen[ROW][COLUMN], int nextgen[ROW][COLUMN])
     {
       int r;
       int c;
       int occupant;
       int born=0;
       int died=0;
    
       for (r=1; r <= ROW-2; r++)
       { 
    	 for (c=1; c<= COLUMN-2; c++)
         {
    	  occupant= occ(curgen, r, c);
    	 if (curgen[r][c]==1 &&(2<=occupant && occupant <=3))
    		nextgen[r][c]=1;
    	   else if (curgen[r][c]==1)
    	   {
    		 nextgen[r][c]=0;
    		 died ++;
    	   }
    	   else if (curgen[r][c]==0 && occupant==3)
    	   {
    		 nextgen[r][c]=1;
    		 born++;
    	   }
    	   else 
    		nextgen[r][c]=0;
    	 }
       }
       printf("Number born = %d \t Number died= %d \n \n", born, died);
         printgrid(nextgen);
    
    	 for (r=1; r<= ROW-2;r++)
    	 {
    	   for (c=1; c<= COLUMN-2; c++)
    		curgen[r][c] = nextgen[r][c];
    	 }
     }
    Putting to use what this newbie knows so far, to correct the output I can figure out some of what i need:
    Code:
    if (y==0)
    {  if (x==0)
       (3 conditions/checks ??)
         if(x==1) ??
        (3 conditions/checks??)
        else 
         (5 conditions/checks??)
        if(y==14)
            ....
    
             if ( curgen[R-1][C-1]==1) /* if cell is occupied add one to occupied counter */
    	  ++occupied;
       if ( curgen[R-1][C]==1)
          ++occupied;
       if ( curgen[R][C+1]==1)
          ++occupied;
       if ( curgen[R+1][C+1]==1)
          ++occupied;
       if ( curgen[R+1][C]==1)
          ++occupied;
       if ( curgen[R+1][C-1]==1)
          ++occupied;
       if ( curgen[R][C-1]==1)
          ++occupied;
       return occupied;
     }
    Is the above heading in the right direction?? If so, can anyone help me with the coding and conditions (brain drain has me perplexed). Or is there an easier way I am missing or have not learned yet?? Any help would be appreciated. Thanks

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    There's no curgen[R-1][C+1].
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 10:36 PM
  4. Game of Life - HELP!
    By Folklord* in forum C Programming
    Replies: 1
    Last Post: 01-14-2004, 04:21 AM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM