Game of Life with C

Discussion in 'C' started by smbee, Feb 9, 2010.

  1. smbee

    smbee New Member

    Joined:
    Feb 8, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <malloc.h>
    #include <math.h>
    #include <sys/time.h>
    
    #define ALIVE 1
    #define DEAD 0
    
    int **array = NULL;
    int **history = NULL;
    int HEIGHT;
    int WIDTH;
    
    double gettime(void) {
      struct timeval tval;
    
      gettimeofday(&tval, NULL);
    
      return( (double)tval.tv_sec + (double)tval.tv_usec/1000000.0 );
    }
    
    
    int **allocarray(int P, int Q) {
      int i;
      int *p, **a;
      p = (int *)malloc(P*Q*sizeof(int));
      a = (int **)malloc(P*sizeof(int*));
      if (p == NULL || a == NULL) 
        printf("Error allocating memory\n");
      /* for row major storage */
      for (i = 0; i < P; i++)
        a[i] = &p[i*Q];
      return a;
    }
    void initarray() {
      int i,j; 
      for (i=1; i<HEIGHT-1; i++){
        for (j=1; j<WIDTH-1; j++){
    		if(drand48()>0.5){
    			array[i][j] = 1;
    		}
    		else{
    			array[i][j] = 0;
    		}
    	}
      }
    } 
    void printTable(int **table) {
            int height, width;
    
    		for (height = 1; height < HEIGHT-1; height++) {
                    for (width = 1; width < WIDTH-1; width++) {
                            if (table[height][width] == ALIVE) {
                                    printf(" X ");
                            } else {
                                    printf(" 0 ");
                            }
                    }
                    printf("\n");
            }
            printf("\n");
    }
    
    //to clear up everything
    
    void clearTable() {
            int height, width;
            for (height = 0; height < HEIGHT; height++) {
                    for (width = 0; width < WIDTH; width++) {
                            array[height][width] = DEAD;
                    }
            }
    }
    
    int getNeighborValue(int **table, int row, int col) {
            if (table[row][col] == ALIVE) 
            { 
                    return 1;
            } else {
                    return 0;
            }
    }
    
    
    int getNeighborCount(int **table, int row, int col) {
            int neighbor = 0;
    
            neighbor += getNeighborValue(table, row - 1, col - 1);  //top left
            neighbor += getNeighborValue(table, row - 1, col);  //top
            neighbor += getNeighborValue(table, row - 1, col + 1);  //top right
            neighbor += getNeighborValue(table, row, col - 1);  //left
            neighbor += getNeighborValue(table, row, col + 1);  //right
            neighbor += getNeighborValue(table, row + 1, col - 1);  //below left
            neighbor += getNeighborValue(table, row + 1, col);   // below
            neighbor += getNeighborValue(table, row + 1, col + 1);  //below right
            
            return neighbor;
    }
    
    void calculate() {
            int **tableB = NULL;
            int neighbor, height, width;
    		tableB = allocarray(HEIGHT, WIDTH);
    
            for (height = 1; height < (HEIGHT-1); height++) {
                    for (width = 1; width < (WIDTH-1); width++) {
                            neighbor = getNeighborCount(array, height, width);
                            if (neighbor==3) {
                                    tableB[height][width] = ALIVE;
                            } else if (neighbor == 2 && array[height][width] == ALIVE) {
                                    tableB[height][width] = ALIVE;
                            } else {
                                    tableB[height][width] = DEAD;
                            }
                    }
            }
    
            for (height = 1; height < (HEIGHT-1); height++) {
                    for (width = 1; width < (WIDTH-1); width++) {
                            array[height][width] = tableB[height][width];
                    }
            }
    }
    
    int matchArray(){
            int count = 0;
            int height,width,flag=0;
            for(height = 0; height<HEIGHT; height++){
                    for(width = 0; width<WIDTH; width++){
                            if(array[height][width] != history[height][width]){
                                    count++;
                            }
                    }
            }
            if(count>0){
                    flag = 1;
            }
            return flag;
    }
    
    
    int main(int argc, char *argv[]) {
            int generation = 0;
            int i;
    double starttime, endtime, time;
    		int height,width;
    		int flag = 0;
    	if (argc!=3)
    		printf("You need to enter the size of the matrix and the number of generations in the comment line arguement\n");
    	else
    	{
    		printf("The matrix size given is:%s\n",argv[1]);
    		HEIGHT = atoi(argv[1])+2;
    		WIDTH = HEIGHT;
    		array = allocarray(HEIGHT, WIDTH);
    		history = allocarray(HEIGHT, WIDTH);
            clearTable();
            // askUser(table);
            //loadTestData(array);
    		initarray();
            //printTable(array);
            //printf("Test data as Generation 0\n");
            //printf("-----------\n");
            starttime= gettime();
    	//printf("Starttime= %f", starttime);
            for(i=0; i<(atoi(argv[2])); i++)
    	{
    				for (height = 0; height < HEIGHT; height++) {
    					for (width = 0; width < WIDTH; width++) {
    							history[height][width] = array[height][width];
    					}
    				}
                    calculate();
    				flag = matchArray();
    				//printf("The flag value is:%d\n",flag);
    				if(flag == 0){
    					break;
    				}
              		     	// printTable(array);
    				//printf("\n======================================\n");
    				//printTable(history);
                    		//printf("Generation %d\n", ++generation);
            }
    	endtime= gettime();
    	//printf("Endtime= %f", endtime);
    
    	time= (endtime-starttime);
    	printf("Time taken= %1f\n", time); 
    	} 
            return 0;
    }
    Hi,
    I have written the code but it seem theres some problem. Its showing answer with small size matrices, but not with bigger ones. I need to use lesser amt of memory. Can anyone suggest any good way to do it?
    Thanks!
    smbee
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice