John Conway's Game Of Life

This is a discussion on John Conway's Game Of Life within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> #define YMAX 25 #define XMAX 80 #define cell fgetc(pFile) typedef struct ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    John Conway's Game Of Life

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    
    #define YMAX 25
    #define XMAX 80
    #define cell fgetc(pFile)
    
    typedef struct cell_t_ {
    	struct cell_t_ *neighbours[8];
    	int on;
    } cell_t;
    
    typedef struct world_t_ {
    	cell_t **array;
    	int width;
    	int height;
    	void *mem;
    } world_t;
    ////////////////////
    int CopyFile()
    {
        char inName[100];
        FILE *pIn, *pOut;
        int c;
    
        //get file name from user
        printf("Please enter a text file name: ");
        scanf("%s", inName);
            pIn = fopen(inName, "r");
            //test if file exists
            if(pIn == NULL)
            {
                printf("ERROR: cannot open file");
               return 1;
                abort();
            }
            //create new file
            pOut = fopen("board.txt", "w");
    
            //copy file contents
            while((c = getc(pIn)) != EOF)
                    putc(c, pOut);
    
            //close file
            fclose(pIn);
            fclose(pOut);
            return (0);
        }
    
    void printworld(world_t *world, FILE *pOutput)
    {
    	int x, y;
    
    	for(y = 0; y < world->height; y++) {
    		for(x = 0; x < world->width; x++) {
    			fprintf(pOutput, "%c", (world->array[y][x]).on ? 254 : ' ');
    		}
    		fputc((int)'\n', pOutput);
    	}
    	fflush(pOutput);
    }
    
    void randomizeworld(world_t *world)
    {
    	int x, y, test;
        FILE *pFile;
        pFile = fopen ("board.txt", "r");
    
    	while (!feof(pFile))
        {
          if((test = (cell)) != 10)
          {
    		  (world->array[y][x]).on = 32;
              ++x;
          }
          if(x == XMAX)                       //Move X coordinate back to 0, increment Y (increasing Y ==> moving down on board)
          {
              x = 0;
              ++y;
          }
        }
    }
    
    void updateworld(world_t *world)
    {
    	int x, y, i, neighbours;
    
    	for(y = 0; y < world->height; y++) {
    		for(x = 0; x < world->width; x++, neighbours = 0) {
    			for(i = 0; i < 8; i++)
    				if((world->array[y][x].neighbours[i]) && ((world->array[y][x]).neighbours[i]->on & 1))
    					neighbours++;
    
    			if((neighbours < 2) || (neighbours > 3))
    				(world->array[y][x]).on |= 2;
    			else if(neighbours == 3)
    				(world->array[y][x]).on |= 4;
    		}
    	}
    
    	for(y = 0; y < world->height; y++) {
    		for(x = 0; x < world->width; x++) {
    			if(world->array[y][x].on & 4)
    				world->array[y][x].on = 1;
    			else if(world->array[y][x].on & 2)
    				world->array[y][x].on = 0;
    		}
    	}
    }
    
    void destroyworld(world_t *world)
    {
    	free(world->mem);
    }
    
    int createworld(world_t *world, int width, int height)
    {
    	int i, j;
    	unsigned long base   = sizeof(cell_t *) * height;
    	unsigned long rowlen = sizeof(cell_t)   * width;
    
    	if(!(world->mem = calloc(base + (rowlen * height), 1)))
    		return 0;
    
    	world->array  = world->mem;
    	world->width  = width;
    	world->height = height;
    
    	for(i = 0; i < height; i++) {
    		
    		world->array[i] = &world->mem + base + (i * rowlen);
    	}
    
    	for(i = 0; i < height; i++) {
    		for(j = 0; j < width; j++) {
    			if(j != 0) {
    				(world->array[i][j]).neighbours[3] = &(world->array[i][j - 1]);
    			}
    
    			if(i != 0) {
    				(world->array[i][j]).neighbours[1] = &(world->array[i - 1][j]);
    			}
    
    			if(j != (width - 1)) {
    				(world->array[i][j]).neighbours[4] = &(world->array[i][j + 1]);
    			}
    
    			if(i != (height - 1)) {
    				(world->array[i][j]).neighbours[6] = &(world->array[i + 1][j]);
    			}
    
    			if((i != 0) && (j != 0)) {
    				(world->array[i][j]).neighbours[0] = &(world->array[i - 1][j - 1]);
    			}
    
    			if((i != (height - 1)) && (j != (width - 1))) {
    				(world->array[i][j]).neighbours[7] = &(world->array[i + 1][j + 1]);
    			}
    
    			if((i != (height - 1)) && (j != 0)) {
    				(world->array[i][j]).neighbours[5] = &(world->array[i + 1][j - 1]);
    			}
    
    			if((i != 0) && (j != (width - 1))) {
    				(world->array[i][j]).neighbours[2] = &(world->array[i - 1][j + 1]);
    			}
    		}
    	}
    
    	return 1;
    }
    
    int main(int argc, char *argv[]) {
    	world_t gameoflife;
    
    	if(createworld(&gameoflife, 79, 24)) {
    		randomizeworld(&gameoflife);
    		do {
    			printworld(&gameoflife, stdout);
    			getchar();
    			fflush(stdin);
    			updateworld(&gameoflife);
    		}while(1);
    		destroyworld(&gameoflife);
    	}
    
    	return 0;
    }
    So here's the deal, I am having an issue with compiling this getting message while running it: "Unhandled exception at 0x00d71e1d in HW8.3.exe: 0xC0000005: Access violation writing location 0x00318ef8." I debugged it and can't find out why I am getting.

    Thanks in advance for the help.
    Last edited by O Mighty Nips; 05-14-2011 at 10:41 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > (world->array[i][j]).neighbours[1] = &(world->array[i - 1][j]);
    Your loops start at zero.
    So here you're trying to access element -1
    Does that sound like an out of bound access to you?

    > I debugged it and can't find out why I am getting this issue resolved.
    If you actually ran this in the debugger, it would have been pointing at a line of code.
    Telling us "The debugger is at this line of code, but I don't understand" would have been evidence that you has used a debugger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Sorry, I didn't mean to say getting the issue resolved; it has been a long day. It pointed to line 138:

    (world->array[i][j]).neighbours[3] = &(world->array[i][j - 1]);

    As for the line you had pointed out, I believe the line before that is: if(i != 0)
    Last edited by O Mighty Nips; 05-14-2011 at 10:47 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    OK, so you've fixed all the out of bound array subscripts in that function then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  2. Replies: 20
    Last Post: 05-24-2007, 01:06 AM
  3. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 10:36 PM
  4. game of life
    By marleyman7 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 06:11 AM
  5. Conway's Life
    By prxom1 in forum C Programming
    Replies: 1
    Last Post: 03-27-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21