Recomposing the Conway's Game code in C

This is a discussion on Recomposing the Conway's Game code in C within the C Programming forums, part of the General Programming Boards category; Hi here I have conway's game code in c, how can i mod it so that it reads and obtains ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Recomposing the Conway's Game code in C

    Hi here I have conway's game code in c, how can i mod it so that it reads and obtains initial state from an external txt file instead and then print out the corresponding new state? I really want to know how to do that thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ROWS 20
    #define COLS 20
    
    #define GETCOL(c) (c%COLS)
    #define GETROW(c) (c/COLS)
    
    #define D_LEFT(c)   ((GETCOL(c) == 0) ? (COLS-1) :  -1)
    #define D_RIGHT(c)  ((GETCOL(c) == COLS-1) ? (-COLS+1) :  1)
    #define D_TOP(c)    ((GETROW(c) == 0) ? ((ROWS-1) * COLS) : -COLS)
    #define D_BOTTOM(c) ((GETROW(c) == ROWS-1) ? (-(ROWS-1) * COLS) : COLS)
    
    
    typedef struct _cell
    {
        struct _cell *neighbour[8];
        char curr_state;
        char next_state;
    }cell;
    
    
    typedef struct 
    {
        int rows;
        int cols;
        cell* cells;
    } world;
    
    
    void evolve_cell(cell *c)
    {
        int count=0, i;
        for (i=0; i<8; i++)
        {
            if (c->neighbour[i]->curr_state) count++;
        }
        if (count == 3 || (c->curr_state && count == 2)) c->next_state = 1;
        else c->next_state = 0;
    
    }
      
       
    void update_world(world *w)
    {
        int nrcells = w->rows*w->cols, i;
        for (i=0; i<nrcells; i++)
        {
            evolve_cell(w->cells+i);
        }
        for (i=0; i<nrcells; i++)
        {
            w->cells[i].curr_state = w->cells[i].next_state;
            if (!(i%COLS)) printf("\n");
            printf("%c",w->cells[i].curr_state ? '#' : ' ');
        }
    }
    
    
    world* init_world()
    {  
        world* result = (world*) malloc(sizeof(world));
        result->rows = ROWS;
        result->cols = COLS;
        result->cells = (cell*) malloc(sizeof(cell) * COLS * ROWS);
        
        int nrcells = result->rows * result->cols, i;
        
        for (i = 0; i < nrcells; i++)
        {
            cell* c = result->cells + i;
                
            c->neighbour[0] = c+D_LEFT(i);
            c->neighbour[1] = c+D_RIGHT(i);
            c->neighbour[2] = c+D_TOP(i);
            c->neighbour[3] = c+D_BOTTOM(i);
            c->neighbour[4] = c+D_LEFT(i)   + D_TOP(i);
            c->neighbour[5] = c+D_LEFT(i)   + D_BOTTOM(i);
            c->neighbour[6] = c+D_RIGHT(i)  + D_TOP(i);
            c->neighbour[7] = c+D_RIGHT(i)  + D_BOTTOM(i);
            
            c->curr_state = rand() % 2;
        }
        return result;
    }
    
    
    int main()
    {
        srand(3);
        world* w = init_world();
        
        while (1)
        {
            system("CLS");
            update_world(w);
            getchar();
        }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you can write the part where it actually tries to open and read a file.


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

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    yes, and I figure it might be something like this:

    Code:
    int c;
    char inName[200];
    FILE *In, *Out;
    
        //get file name from user
        printf("Please enter a text file name: ");
        scanf("%s", inName);
            In = fopen(inName, "r");
            //test if file exists
            if(In == NULL)
            {
                printf("ERROR: cannot open file");
               return 1;
                abort();
            }
    But it seems quite impossible how it can read from the file cell by cell and test it should be alive or dead...!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Noya View Post
    yes, and I figure it might be something like this:

    Code:
    int c;
    char inName[200];
    FILE *In, *Out;
    
        //get file name from user
        printf("Please enter a text file name: ");
        scanf("%s", inName);
            In = fopen(inName, "r");
            //test if file exists
            if(In == NULL)
            {
                printf("ERROR: cannot open file");
               return 1;
                abort();
            }
    But it seems quite impossible how it can read from the file cell by cell and test it should be alive or dead...!
    Why do you think it is impossible?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > here I have conway's game code in c
    Lemme guess, you found it, or you were given it.

    I would suggest you put that code to one side for the moment, while you get some practice in reading files.

    > yes, and I figure it might be something like this:
    So you managed to use fopen(), carry on reading the book/manual page to learn about fgetc / fgets / fread and do some testing to see which is going to be best for you in terms of reading the file.
    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.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Why do you think it is impossible?
    Well, it actually is impossible... until he can get it to save the information first.

    Can't read an empty file...

    Of course figuring out how to save the data will also tell him how to read it...

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    3

    fopen part

    After long hours of testing I believe I have finally pulled something together...but still one of the major trouble I frequently run into during the trials are reading the characters from the target external txt file, I used '#' to represent alive cells, and '-' (for come reason my compiler has poor recognition of space character ' '). But even with '-', there still seem to be strange error momentarily, meaning it reads without error only when the dimension is within 25 by 25, which I can't figure out why.
    why?
    why!

    Code:
    void Read_in (void)
        {
            //local variables
            FILE *input_file;
            char file_name[100];
            int x, y;
            char temp;
    
    		//get filename from user
    		printf("Enter name of file: ");
    		scanf("%s", file_name);
    
    	//	strcat(Fname,".txt");
    
    		//open file
    		input_file = fopen(file_name, "r");
    		if (input_file == NULL)
    		{
    		    printf("Could not open input file\n");
    			exit(1);
    		}
    
    		//read data into array
    		for (y=0 ; y<YMAX; y++)
    		{
    		    for (x=0; x<XMAX; x++)
    			{
    			     temp = getc(input_file);
    				 if (temp == EOF)
    				 {
    				     printf("Unexpected end-of-file in %s", file_name);
    				     //printf("End character is %c at [%i][%i]", temp, y, x);
    				    
    					 return;
    				 }
    				 if (temp == '#')
    				     board [y][x] = 1;
    				 else if (temp == '-')
    				     board [y][x] = 0;
    				 else
    				 {
    				     //printf("Unknown character found in file: [%i] [%i]", y, x);
    				     printf("Unknown character found, that character is %c at [%i][%i]", temp, y, x);
    					 return;
    				 }
    			}
    			getc(input_file); //throw away newline character
    		}
    		fclose(input_file);
    	}

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This might be a bit obvious but why don't you just write 1 and 0 to the disk file?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There's not anything here that would die at the magic number of 25. Most command prompt windows come, out of the box, at 24 lines tall, so if you have a larger size you'll have to scroll to see it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. John Conway's Game Of Life
    By O Mighty Nips in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 11:30 PM
  2. Replies: 20
    Last Post: 05-24-2007, 01:06 AM
  3. 'Business code' vs 'Game code'
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-09-2007, 02:33 AM
  4. c++ game [code] need help
    By mario fortunato in forum Game Programming
    Replies: 3
    Last Post: 02-24-2006, 07:41 AM
  5. Conway's Life
    By prxom1 in forum C Programming
    Replies: 1
    Last Post: 03-27-2002, 01:13 PM

Tags for this Thread


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