Thread: Conways Game using Pointers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Conways Game using Pointers

    Hi,
    I am new to C programming language. I am trying to rewrite the code on Conways game of life using pointers. The current piece of code without pointers is this:
    Code:
    static void
    evolve(char arena[WIDTH][HEIGHT])
    {
    	int x, y;
    	
    	// First shift each cell to the left so its occupied status
    	// is in bit 1 rather than bit 0
    	for (x = 0; x < WIDTH; x++)
    		for (y = 0; y < HEIGHT; y++)
    		{
    			printf("\nx is %d, y is %d arena value is %d\n",x,y,arena[x][y]);
    			arena[x][y] <<= 1;
    		}
    
    	// Decide whether each cell should be occupied based on the
    	// number of previously occupied neighbours etc.
    	for (x = 0; x < WIDTH; x++)
    		for (y = 0; y < HEIGHT; y++) {
    			int neighbours = 0;
    
    			neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y - 1);
    			neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y);
    			neighbours += WAS_OCCUPIED_ANY(arena, x - 1, y + 1);
    			neighbours += WAS_OCCUPIED_ANY(arena, x, y - 1);
    			neighbours += WAS_OCCUPIED_ANY(arena, x, y + 1);
    			neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y - 1);
    			neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y);
    			neighbours += WAS_OCCUPIED_ANY(arena, x + 1, y + 1);
    			if (neighbours == 3 || (neighbours == 2 && WAS_OCCUPIED_ANY(arena,x,y)))
    				arena[x][y] |= 1;
    		}
    }
    The above function is the evolve part of the game and it works well. I am trying to use pointers to dynamically allocate and set the array using malloc.

    Can anyone help? pls!

    Thanks.
    Last edited by Salem; 03-19-2010 at 11:55 PM. Reason: Added code tags - learn to use them YOURSELF!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes. How much have you read about pointers? Can you explain what you don't understand?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    I know we have to use malloc to initialise the memory dynamically. Before evolving each step we have to create the memory and initialise it. I am not sure where to start. I am bit familiar with the concepts.

    Thanks for the tutorial link. I revised the concepts.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can use pointers to simulate arrays. I noticed that the tutorial linked earlier does not go into that, but other sources would. From what you've told me, you need more reading on that subject. So look for things detailing multidimensional array building and reallocation.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't realloc anything in Conways Game of Life, because the board or grid, stays the same size, throughout the game.

    This is an example, that may be relevant to what you want:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //we can pass this way without knowing the values of max rows and columns
    //we could change it, if we knew the sizes at this point
    
    void print(char**, int, int);
    
    int main(void) {
    	
       char **grid;          //2D arrays get two *'s
       int i, j;
       int max_rows = 3;     //these values could also come from the user
       int max_columns = 3;
    
       /* note that the sizeof, is to a *pointer* of the data type, not the data type size, itself */
       *grid = malloc(max_rows * sizeof(char*));  //getting memory for the 1st dimension
                                          
    
       /* getting memory for each row (2nd dimension), sized for the data type, not a pointer */
       for(i = 0; i < max_rows; i++)
          grid[i] = malloc(max_columns * sizeof(char));  
       	
    	for(i = 0; i < max_rows; i++){
    		for(j = 0; j < max_columns; j++){
    			grid[i][j] = 'X';
    		}
    	}
    	
    	print(grid, max_rows, max_columns);
    
       //free the 2nd dimension (each row), first
       for(i = 0; i < max_rows; i++)
          free(grid[i]);
    
       //then free the 1st dimension of the array
       free(grid);
    
       printf("\n\n\t\t\t      press enter when ready ");
       i = getchar();
       return 0;
    }
    
    void print(char **grid, int max_rows, int max_columns) {
       int i, j;	
    	
       for(i = 0; i < max_rows; i++){
          for(j = 0; j < max_columns; j++){
             printf("%c ", grid[i][j]);
          }
          printf("\n");
       }
    }

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Thank you Adak. I am working on it. If i get any doubts, will come back and ask. Thank you.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    The load function has already set the initial array.

    The code I tried to implement in evolve function is this: its giving me a wrong output....can anyone tell where my logic is wrong pls:

    Code:
    /*  Evolve a life arena by one generation.  The array arena should
     *  have length width with each entry representing a column of
     *  cells.  Columns that are empty should be NULL when the function is called
     *  and on return.  Columns that have some occupied cells should be a
     *  malloc'ed character array of length height.  Individual cells that
     *  are occupied should have their RH bit = 1 and empty cells should have 
     *  their RH bit = 0.
     */
    void
    evolve(char **arena, int width, int height)
    {
    	int x,y;
    	int i=0;
    	
    	for(x=0;x<width;x++)
    		if(arena[x]!=NULL)
    	for(y=0;y<height;y++) {
    		if(arena[x][y]!= NULL)
    			arena[x][y] <<= 1;
    		else 
    			arena[x][y] = arena[x][y] & 0;
    			//printf("\narena  = %d %d %d", arena[x][y], x,y);
    	}
    	
    	for(x=0;x<width;x++)
    	for(y=0;y<height;y++) {
    		int neighbours = 0;
    		
    		// if the column cell is occupied
    		if(arena[x]!=0) {
    		//	printf("\nx = %d y = %d", x,y);
    		//	getchar();
    			if(arena[x-1]!=0) {
    //				printf("did this enter");
    //				getchar();
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y-1);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y+1);
    //				printf("neighbours = %d", neighbours);
    			}
    			
    			if(arena[x+1]!=0) {
    //				printf("did this enter\n");
    //				getchar();
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y-1);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y+1);
    //				printf("neighbours = %d\n", neighbours);
    			}
    			
    			neighbours+= WAS_OCCUPIED_ANY(arena, x,y-1);
    			neighbours+= WAS_OCCUPIED_ANY(arena, x,y+1);
    						
    			if(neighbours==3 || neighbours==2 || IS_OCCUPIED(arena,x,y) ) {				
    				arena[x][y]|=1;
    			} else if (neighbours > 3 || neighbours < 2) { 
    				arena[x][y]=0; int check = 0;
    				for(i = 0;i < height; i++) {
    					if(!(arena[x][i]==0)) {
    						check = 1;
    					}
    					if(check == 0) {
    						free(arena[x]);
    					}
    				}
    			}
    		} else if(arena[x]==0) { // if the column cells are not occupied
    			if(arena[x-1]!=0) {
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y-1);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y+1);
    			}
    			
    			if(arena[x+1]!=0) {
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y-1);
    				neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y+1);
    			}
    			
    			if(neighbours==3 || neighbours==2) {
    				arena[x] = malloc(height);
    				for(i=0;i<height;i++)
    					arena[x][i]=0;
    				arena[x][y]|=1;
    			}
    		}
    Last edited by Salem; 03-22-2010 at 11:19 PM. Reason: [code] tags ADDED AGAIN - last chance bub - learn or leave

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NewUser View Post
    ....can anyone tell where my logic is wrong
    Yep.

    1) "Giving me a wrong output" IS NOT a sufficient description of your problem. What is wrong with the output?
    2) If you get compiler errors or warnings, or runtime errors, you need to describe those too.
    3) You posted without code tags. Do not pass GO. Do not collect $200. Fix it now.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    The cellular generation is not showing accordingly, there is a logical error. What is a code tag?
    and the macros are fine, its just the logical error in code. I dont know how to fix it.

    I tried to change to a smaller version, but it has logical error too..

    <<< HORRIBLE CODE DELETED >>>

    the above version the latest.

    Any solutions for either of the two versions plsssssssssssss?

    Thanks in advance.
    Last edited by Salem; 03-22-2010 at 11:18 PM. Reason: If you wont use code tags, then don't post code - my job isn't cleaning up YOUR MESS

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NewUser View Post
    What is a code tag?
    You used them in your earlier post.

    Code:
    Code goes here.
    << !! Posting Code? Read this First !! >>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    You used them in your earlier post.
    No, Salem added them. See the edit?

    [code]
    ... put code here ...
    [/code]


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Event driven game engine
    By abachler in forum Game Programming
    Replies: 9
    Last Post: 12-01-2009, 06:03 AM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM