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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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