Thread: Conway's Game of Life using pointers!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Conway's Game of Life using pointers!

    Hey.

    I've seen that it has been posted about on this forum before, but little information was made available, so I'll start a new thread.

    Before I start, I must note: I will be careful when posting bits of code to do with this. I know that my University and many others set this regularly, so I don't want to paste too much to give away what I have to other readers. I will paste where I feel it's necessary though.

    The exercise states that we have to complete Conway's Game of Life by implementing two functions: evolve and rotate (the main function is already given, as is a 'load' function which loads a file [given as a parameter - determines how the game 'starts']). Rotate hasn't much to do with the game itself - once called, it rotates the grid 90 degrees clockwise. Evolve is the meat of the exercise, and is the one which determines when a neighbour character appears and disappears.

    The exercise states that the grid used in our assessment needs to be an array of character pointers - thus, the headings for each function are as follows:

    char **rotate(char **arena, int *pwidth, int *pheight)
    void evolve(char **arena, int width, int height)

    Another huge part of the assessment is that, when a column of the grid is in use (has one or more characters present), it must be dynamically allocated memory. When a column is not in use, that memory must be free. (So, it's a malloc()/free() exercise mainly).

    I am currently struggling with rotate and do not know where to start with evolve. We are given an evolve function that works, but uses a simple 2-dimensional array of characters:

    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++)
                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;
            }
    }
    (A few defines to match)
    Code:
    #define IN_ARENA(x,y) ((x) >= 0 && (x) < WIDTH && (y) >= 0 && (y) < HEIGHT)
    #define WAS_OCCUPIED(a, x, y) (a[x][y] >> 1 & 1)
    #define IS_OCCUPIED(a, x, y) (a[x][y] & 1)
    #define WAS_OCCUPIED_ANY(a, x, y) (IN_ARENA(x,y) ? WAS_OCCUPIED(a, x, y) : 0)

    My few questions:

    1. I have code for 'rotate', but I get a segmentation fault upon running the program (so I've gone wrong with the memory allocation somewhere!). Is someone willing to help me via PMs? I wouldn't want to paste my code publically, as it could encourage plagiarism.
    2. Where would I start when tackling the 'evolve' function? Would I be able to cleverly alter the one we're already given to work with an array of pointers, rather than a 2D array?


    Thanks for the help.

    If it helps, here is the main file (need not be changed):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include "life.h"
    #include "load.h"
    #include "rotate.h"
    #include "evolve.h"
    
    #define MAX_CYCLES 100
    #define ROTATE    20
    
    
    /*  Display the arena in a vt100 (xterm) terminal with the given width and height.
     */
    static void
    show(char **arena, int width, int height)
    {
        int x, y;
    
        printf("\033[2J");
    
        for (x = 0; x < width; x++)
            if (arena[x] != NULL) 
                for (y = 0; y < height; y++) {
                    if (arena[x][y] & 1)
                        printf("\033[%d;%dH*", y, x);
        }
        printf("\033[H");
        fflush(stdout);
    }
    
    int main(int argc, char **argv)
    {
        char **arena;
        int width = WIDTH, height = HEIGHT;
        int cycle;
    
        if (argc != 2 && argc != 4) {
            fprintf(stderr,"usage is %s filename [ width height ]\n",argv[0]);
            exit(1);
        }
        if (argc == 4) {
            width = atoi(argv[2]);
            height = atoi(argv[3]);
            if (width < MINSIZE || width > MAXSIZE || height < MINSIZE || height > MAXSIZE) {
                fprintf(stderr, "%s: width and height must be in the range %d to %d\n",
                        argv[0], MINSIZE, MAXSIZE);
                exit(1);
            }
        }
        if ((arena = load(argv[1], width, height)) == NULL) {
            fprintf(stderr,"%s: Cannot open %s\n",argv[0],argv[1]);
            exit(1);
        }
        for (cycle = 1; cycle <= MAX_CYCLES; cycle++) {
            show(arena, width, height);
            evolve(arena, width, height);
            if (cycle % ROTATE == 0)
                arena = rotate(arena, &width, &height);
            /*  Add a 1/10 sec delay so that we can see what is happening.
             */
            usleep(100000);
        }
        int x;
        for (x = 0; x < width; x++)
            if (arena[x] != NULL)
                free(arena[x]);
        free(arena);
        return 0;
    }
    (Edit: Just read that I can't actually SEND PM's until I have posted 10 times. If somebody offers to help me before that happens, I'll be more than glad to give out my email address. I'm not a spam bot, don't worry).
    Last edited by john_member; 03-10-2013 at 09:41 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I have code for 'rotate', but I get a segmentation fault upon running the program (so I've gone wrong with the memory allocation somewhere!).
    Post your code.

    > Is someone willing to help me via PMs? I wouldn't want to paste my code publically, as it could encourage plagiarism.
    Yeah, that happens a lot (the fear of it anyway). People intent on cheating will find most answers by persistent searching, so why worry about it.
    Besides, you've always got the "Well I published first" defence.

    > Where would I start when tackling the 'evolve' function? Would I be able to cleverly alter the one we're already given to work with an array of pointers, rather than a 2D array?
    Well you could imagine your existing show(char**) function written as show(char arena[WIDTH][HEIGHT])
    Can you see the similarity at this point.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Hey Salem, thanks for the reply.

    We actually have the 'show' code for that, and I do see what you're getting at:
    Code:
    static void show(char arena[WIDTH][HEIGHT])
    {
        int x, y;
    
        printf("\033[2J");
    
        for (y = 0; y < HEIGHT; y++) {
            for (x = 0; x < WIDTH; x++)
                if (IS_OCCUPIED(arena,x,y))
                    printf("\033[%d;%dH*", y, x);
        }
        printf("\033[H");
        fflush(stdout);
    }
    Only few things have changed (parts of the for loop). I'll use this advice and have a crack at evolve later on (need to get to sleep, it's 05:12AM!) I think there would be a problem here: with 'show', you don't need to dynamically allocate memory, whereas in the new 'evolve' function I have to write, I will have to. I think that makes it more challenging than just changing a few things here and there. I have a few basic ideas which I'm yet to try - if that comes to nothing, I'll desperately ask for extensive help.

    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 a, b;
            int i = 0;
      
            for(a = 0; a < width; a++) {
            if(arena[a] != NULL) {
                for(b = 0; b < height; b++) {
                    if(arena[a][b] != NULL) { arena[a][b] <<= 1; }
                    else { arena[a][b] = arena[a][b] & 0; }
    }
    Above is the beginning of my evolve code - nothing too fancy, it just checks to see if parts of the arena are null (and if they aren't, allocate their RH bit to 1, as told to do in the comments). Assuming this is a good start, what do you think I should move on to doing? I was thinking of starting an if loop (if column x is occupied, then do something), but that's all I have at the minute.

    Can I just add: thanks for the quick and helpful reply! In reply to your "just post your rotate code", I would want to wait for just a day or two to see if I get PMs. If not, I'll post my code up for people to have a look at and comment on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conway's Game of Life
    By Anhur in forum C Programming
    Replies: 37
    Last Post: 09-27-2012, 10:54 PM
  2. Text Based Conway's game of Life in C
    By spideysagnik in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2011, 07:10 AM
  3. John Conway's Game Of Life
    By O Mighty Nips in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 11:30 PM
  4. Replies: 20
    Last Post: 05-24-2007, 01:06 AM
  5. Conway's Life
    By prxom1 in forum C Programming
    Replies: 1
    Last Post: 03-27-2002, 02:13 PM