简体   繁体   中英

Conway's game of Life in C with malloc'd arrays

I'm implementing Conway's game of life with a malloc'd array as a grid of 0s and 1s, anywhere with a 0 I print a white background space representing a "dead" cell and anywhere with 1 I print a black background space representing an "alive" cell.

I'm having some trouble with updating the cells on each cycle. My approach was to have a getState() function, which just checks if a grid entry is 0 or 1 and then returns a value correspondingly. I also have a countN() function which goes through the grid, and counts the numbers - accounting for corner, edge and centre cases. I've tested those thoroughly with several examples and they are working how I expect them to, for a given grid coordinate it always returns the correct state & number of alive neighbours.

This is what I've attempted to do (pseudocode):

void updateGrid(int **grid, int r, int c) {
    copyGrid = grid;
    for (i = 0; i < r; i++) 
        for (j = 0; j < c; j++) {
            alive->getState(grid, i, j) 
            neighbours->countN(grid, i, j)
          
            if (alive) {
                if (neighbour < 2 || neigbour > 3) 
                    copyGrid[i][j] = 0;
                else 
                    copyGrid[i][j] = 1;
            } else {
                if (neighbour == 3) 
                    copyGrid[i][j] = 1;
                else 
                    copyGrid[i][j] = 0; 
            }
        }
    grid = copyGrid; 
    printGrid(grid, r, c)
}

There's a simple calling function which just generates an initial grid from an input file, clears the screen and then calls updateGrid() and sleeps after each cycle, until a certain number of cycles. The initial grid is how I expect it to be, the neighbours and alive functions return the correct values but the grid updates incorrectly and the output is quite distorted.

As an example, using 1s and 0s:

Input:    Expected Output:     Actual Output
0 0 0     0 1 0                0 1 1 
1 1 1     0 1 0                1 0 1 
0 0 0     0 1 0                0 0 0 

Any tips would be greatly appreciated!

Your method for copying the grid copy back to grid does not work: copyGrid = grid; and grid = copyGrid; don't not do that, the first overwrites the global pointer copyGrid , which causes the updated cells to be written to the original grid, the second does nothing as both pointers already point to the same area.

You must remove the copyGrid = grid; statement. All updated cells are computed and stored into copyGrid so there is no need to copy the original cells. Once the new generation has been computed, you must copy it back to the original grid : assuming copyGrid has the same geometry as grid , an array of r pointers to arrays of c ints, you must use a loop to copy this indirect 2D array.

Another approach is to use 2 global arrays and swap the pointers after computing each new generation.

Here is a modified version:

int **copyGrid;  // assuming this pointer points to an allocated grid

void updateGrid(int **grid, int r, int c) {
    // compute the new grid value in copyGrid
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            int alive = getState(grid, i, j); 
            int neighbours = countN(grid, i, j);
            if (alive) {
                if (neighbours < 2 || neighbours > 3) 
                    copyGrid[i][j] = 0;
                else 
                    copyGrid[i][j] = 1;
            } else {
                if (neighbour == 3) 
                    copyGrid[i][j] = 1;
                else 
                    copyGrid[i][j] = 0; 
            }
        }
    }
    // copy the new grid value from copyGrid back to grid
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            grid[i][j] = copyGrid[i][j];
        }
    }
    printGrid(grid, r, c);
}

If you write copyGrid = grid your copy will point to the same memory location so you dont actually have a copy. You have to use

for(int i=0;i<r;i++)
memcpy(copyGrid[i],grid[i],sizeof(int)*c)

The reason for this is that you work with pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM