Help with Code for Game Of Life

Discussion in 'C++' started by Njod, Apr 10, 2007.

  1. Njod

    Njod New Member

    Joined:
    Apr 10, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Please read the "Before you make a query" thread, particularly with regard to #10.

    What would your institution think if they read this post? Be a big boy and do your work. Post here for help with the problems you are having. Show the pertinent work, and explain the problems and errors.

    Since the due date was yesterday, I'd also recommend a slightly earlier start on your assignments.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you don't prefer to be writing the content of the assignment do you expect anything from somebody else. Help us by not having something like this one.
     
  4. Njod

    Njod New Member

    Joined:
    Apr 10, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    oh sorry,

    I mean I did the problem, but I was having troubles getting it to work to I wanted to see what I was doing wrong..I didnt mean to be cheating or anything
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Let us know what you have done what is the issue where you are stuck and definitely we will try to help you out.
     
  6. Njod

    Njod New Member

    Joined:
    Apr 10, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I need help with

    - int playGame (int board [ROW] [COL] )
    - int buildNewBoard (int oldBoard [ROW] [COL] , int newBoard [ROW] [COL] )
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I dont see any function in the post relating to it.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I don't mean to be unduly tough on you, so let me make a few relevant points. The first is that you haven't assigned this task any sort of important priority. That's okay if it's just a game with you, but it isn't okay if you intend to enter the field and compete with professionals who attach importance to it. That importance can be expressed simply, as "Eat vs. Starve".

    It's entirely likely that this assignment just overwhelms your mind, because you haven't been really pursuing it. That's okay, it happens. On the other hand, it is time to take control, if you're serious.

    Your assignment comes very close to being a design document. That's better than a lot of people get. Most people have to write the assignment AND complete it. Gather your wits about you and think about the problem in reasonable terms, deferring the execution in code until you understand the constraints and requirements of the assignment.

    Once you have done that, it's merely a matter of translation. If you don't understand arrays and so forth, then you're going to have to bust your butt to catch up with your classes.

    When you have shown that you are serious about this pursuit, then you will attempt a solution. You'll almost surely have problems. That's the time to come here and ask for help in a rational and intelligent manner.

    If you have been raised to believe that the world owes you a free ride, then I suspect that this will not be your first encounter with disappointment.
     
  9. smbee

    smbee New Member

    Joined:
    Feb 8, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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?

    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;
    }
    
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK, post your code, and tell us what's wrong with it. Is it doing something you don't expect? Or not doing something that you do expect? We're not going to write it for you, but we will help you fix it.

    Have you tried adding some debugging "printf" (or cout if you prefer) statements to show where the code is up to and what it is doing? This can be a very helpful debugging technique; it can show what variables contain, and you can compare that with what you think they should contain. With enough detail you will be able to find where it starts going wrong.

    smbee: please don't (a) hijack threads or (b) do someone else's homework for them, whichever your post was trying to achieve. It helps nobody to have code written for them, especially when that code is broken. Look at it this way: if reading completed code samples was the best way to learn programming, that's what teachers would give you. They're not making you program just because they're sadists (they may be that as well) but because they know it's the only way to learn.
     
  11. smbee

    smbee New Member

    Joined:
    Feb 8, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0

    I just searched with the keyword 'game of life' and got this one. I didn't want to either solve it or hijack it. Just that I wanted to post it in a contextual place. Anyways I am sorry. I'll create a new thread.

    Thanks!
     

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