Thread: A Simple Life Game Code - Near the end ?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    A Simple Life Game Code - Near the end ?

    With a lot of help of people of this forum, Codecall forum, and also Daniweb forum, i made this code. What it is purpose ? It's an adaptation of life, using only basic concepts of C (it's for a introductory C course, so i cannot use stacks, queues, dynamic allocation, ... well i didn't learned that yet, also. ). So what the code is supposed to do:

    1 - It has the main function and two others (count_neighbors and generation)
    2 - The function count_neighbours as name says count the neighbours of a alive cell of a char matrix predefined with fixed size.
    3 - The function generation use the function count_neighbours applyed in each i, and each j (with a for loop) and then let this value in alive_neighbours variable. Well, then , there exists 4 tests depending of the value of alive_neighbours. The purpose here is create the next generation predefined char matrix C.
    4 - In the function main, we create a matrix of random numbers, give it the chance of 40% cells to be alive using
    Code:
    A[i][j] = rand() % 10;  // Aij value is between 0 and 9
    and
    Code:
    if (A[i][j]<=3) B[i][j] = 'X';   //X for alive cells
    and creating the char matrix B (based on previous one) in a size 2 rows and columns larger than the integer matrix, so all cells can count its neighbours properly.
    5 - That's where i'm stopped right now. It seems that generation function somehow isnt working. Could you give me a direction ? Thanks for the help until now.
    (P.S.: I know I should print only the part where supposed there's an error, but I like to share the code also, cause i think its one of the less harder implementation.)

    Code:
     #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #include <time.h> 
    
    //Global variables
        int i,j=0; // Initialize the variables i,j (row and column)
        int x,y=0; // Initialize the variables x,y (used in function count_neighbours)
        int alive_neighbours; //
        int alive,count; // Initialize the variable that control the alive cells 
        int A[40][40]; // Define the 40x40 integer matrix 
        char B[42][42]; // Define the 42x42 initial char matrix 
        char C[42][42]; // Define the 42x42 transitory char  matrix
    
    
    // Function count neighbours
    int count_neighbours(int i, int j)
    {
         int count=0;
    	for (x = -1; x <= +1; x++)          
       	   {
    	      for (y = -1; y <= +1; y++)
    		    {
    			   if (B[i + x][j + y] == 'X' && (x!=0 || y!= 0))
    			  	 count++;
    		    }
           }
      	return count;
    }
    
    //Function generation - apply the necessary conditions.
    int generation()  
    {
    int alive_neighbours=0;
       for (i=1; i<41; i++)
       {
          for (j=1; j<41; j++)
          {
            alive_neighbours = count_neighbours(i,j);
            if(alive_neighbours < 2)
            {
                  C[i][j] = '.'; //make C[i][j] a dead cell
            }            
            if (alive_neighbours >= 2  &&  alive_neighbours <= 3 )
            {
                  C[i][j] = B[i][j]; //copy the alive cell B[i][j] to C[i][j]
            }           
            if(alive_neighbours > 3)
            {
                  C[i][j] = '.'; // make C[i][j] a dead cell.
            }           
            if(alive_neighbours == 3 && B[i][j]=='.')
            {
                  C[i][j] = 'X'; //make C[i][j] alive
            }          
          }printf("%1c", C[i][j]); // Print . or X of new matrix
                       if(j==40) printf("\n"); // Organize the matrix
       }
     //  return 0; 
    }
    
    int main()                                     
    {
        printf("Este codigo trabalha apenas com matrizes LIFE quadradas de 40x40:\n");        
        //This code only works with square matrix dimension 40.                       
    
        printf("\nMatriz LIFE:\n\n");       
        srand(time(NULL));  //Activate function srand
        for(i = 0; i < 40; i++)  //loop - i to 40
        {
             for(j = 0; j < 40; j++) //loop - j to 40
                  {
                       A[i][j] = rand() % 10;  // Aij value is between 0 and 9
                       printf("%1d", A[i][j]);  // Print each Aij
                       if(j==39) printf("\n"); // Organize the matrix         
                  }
                       
        }
        printf("\n");
        printf("\n");
        
        for(i = 0; i < 40; i++)  //loop - i to 40
        {
             for(j = 0; j < 40; j++) //loop - j to 40
                  {
                       if (A[i][j]<=3) B[i][j] = 'X';   //X for alive cells
                       else B[i][j] = '.'; // . for dead cells
                       printf("%1c", B[i][j]); // Print . or X 
                       if(j==39) printf("\n"); // Organize the matrix
                  }
                       
        }    
    printf("\n");
    
    generation();   
    //printmatrix(); 
    getch();
    return 0;
    }
    Last edited by apolochaves; 08-04-2010 at 02:56 AM.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    wait.. WHA?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    The first evident error is that, in function
    count_neighbours() you use the variable count that is not reset to zero.
    It's better to define it in the function
    Code:
    count_neighbours()
    {
    int count=0;
    .
    .
    .
    }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    "The first evident error is that, in function
    count_neighbours() you use the variable count that is not reset to zero.
    It's better to define it in the function"

    OK! Corrected!

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    I believe this version works as expected. Included the function repeatroutine to iterate many times as wished.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #include <time.h> 
    
    //Global variables
        int i,j=0; // Initialize the variables i,j (row and column)
        int x,y=0; // Initialize the variables x,y (used in function count_neighbours)
        int alive_neighbours=0; //
        int alive,count=0; // Initialize the variable that control the alive cells 
        int A[40][40]; // Define the 40x40 integer matrix 
        char B[42][42]; // Define the 42x42 initial char matrix 
        char C[42][42]; // Define the 42x42 transitory char  matrix
        char no;
    
    // Function count neighbours
    int count_neighbours(int i, int j)
    {
        int count = 0;
    	for (x = -1; x <= +1; x++)          
       	   {
    	      for (y = -1; y <= +1; y++)
    		    {
    			   if (B[i + x][j + y] == 'X' && (x!=0 || y!= 0))
    			  	 count++;
    		    }
           }
      	return count;
    }
    
    //Function generation - apply the necessary conditions.
    int generation()  
    {
       int alive_neighbours=0; 
       for (i=1; i<41; i++)
       {
          for (j=1; j<41; j++)
          {
            C[i][j] = '.'; // define first all C cells dead
            alive_neighbours = count_neighbours(i,j);
            if(alive_neighbours < 2)
            {
                  C[i][j] = '.'; //make C[i][j] a dead cell
            }            
            if (alive_neighbours >= 2  &&  alive_neighbours <= 3 )
            {
                  C[i][j] = B[i][j]; //copy the alive cell B[i][j] to C[i][j]
            }           
            if(alive_neighbours > 3)
            {
                  C[i][j] = '.'; // make C[i][j] a dead cell.
            }           
            if(alive_neighbours == 3 && B[i][j]=='.')
            {
                  C[i][j] = 'X'; //make C[i][j] alive
            }          
          }printf("%1c", C[i][j]); // Print . or X of new matrix
                       if(j==40) printf("\n"); // Organize the matrix
       }
     //  return 0; 
    }
    
    //Function imprimir matriz (print the matrix after the geracao function) 
    int printmatrixB(void)  
    {                         
    printf("\nThe function printmatrixB prints the start matrix B\n");
    printf("\n");
    printf("\n");
     for(i = 0; i < 40; i++)  //Loop - i to 40
        {
             for(j = 0; j < 40; j++) //Loop - j to 40
                  {
                       printf("%1c", B[i+1][j+1]);
                       if(j==39) printf("\n");
                  }
         }          
    }
    
    int printmatrixC(void)  
    {                         
    printf("\nThe function printmatrixC prints the matrix C \n [B after the conditions applyed]\n");
    printf("\n");
    printf("\n");
     for(i = 0; i < 40; i++)  //Loop - i to 40
        {
             for(j = 0; j < 40; j++) //Loop - j to 40
                  {
                       printf("%1c", C[i+1][j+1]);
                       if(j==39) printf("\n");
                  }
         }          
    }
    
    void repeatroutine()
    {
        printf("\nPress 'n' to quit. \n Any other key to continue :\n", no);
        scanf("%c",&no); 
        if (no == 'N' || no == 'n')
          
         {       
            exit(0);
         }            
          else
          {
          for(i = 0; i < 40; i++)  //loop - i to 40
              {
                  for(j = 0; j < 40; j++) //loop - j to 40
                      {
                        B[i+1][j+1] = C[i+1][j+1];
                        C[i+1][j+1] = '.';
                      }
              }generation();   
               printmatrixB(); 
               printf("\nPress a key to see matrix C");
               getch();
               printf("\n");
               printmatrixC(); 
               repeatroutine();
          } 
          
      
    }
    
    
    int main()                                     
    {
        printf("This code only works with square matrix dimension 40:\n");        
        //This code only works with square matrix dimension 40.                       
    
        printf("\nLIFE Matrix:\n\n");       
        srand(time(NULL));  //Activate function srand
        for(i = 0; i < 40; i++)  //loop - i to 40
        {
             for(j = 0; j < 40; j++) //loop - j to 40
                  {
                       A[i][j] = rand() % 10;  // Aij value is between 0 and 9
                       printf("%1d", A[i][j]);  // Print each Aij
                       if(j==39) printf("\n"); // Organize the matrix         
                  }
                       
        }
        printf("\n");
        printf("\n");
        
        for(i = 0; i < 40; i++)  //loop - i to 40
        {
             for(j = 0; j < 40; j++) //loop - j to 40
                  {
                       B[i+1][j+1] = '.'; // define first all B cells dead 
                       if (A[i][j]<=3) B[i+1][j+1] = 'X';   //X for alive cells
                       else B[i+1][j+1] = '.'; // . for dead cells
                  }
                       
        }    
    printf("\n");
    generation();   
    printmatrixB(); 
    printf("\nPress a key to see matrix C");
    getch();
    printf("\n");
    printmatrixC(); 
    repeatroutine();
    return 0;
    }

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    You need to reduce your use of global variables it's a bit ridiculous. Also, consolidate printmatrixB and printmatrixC into one function.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    thanks for the hint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  3. Please help with my game
    By jjj93421 in forum Game Programming
    Replies: 10
    Last Post: 04-12-2004, 11:38 PM
  4. Help with a simple game
    By jjj93421 in forum Game Programming
    Replies: 1
    Last Post: 03-28-2004, 03:52 PM
  5. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM

Tags for this Thread