1

I have done all the code right and stuck with this silly thing: I cannot mange to stop the print when the previous generation is the same as the new...so when the prints pattern is the same as the previous pattern it should stop.

I need to copy the board before calling 'step' and then compare the new and copied boards, and only print if it has changed i need is to create a new variable just like i did board[], then to make a nested loop like the one in print, and inside do newboard[y][x] = board[y][x]

Please help me with this i cannot stop the print it always keep printing. please show me your syntax

void step(int board[][WIDTH], int rows) {
    int x, y;
    int neighbors[HEIGHT][WIDTH];
    for (y = 0; y < rows; y++)
        for (x = 0; x < WIDTH; x++)
            neighbors[y][x] = count_neighbors(board, rows, y, x);
    for (y = 0; y < rows; y++)
        for (x = 0; x < WIDTH; x++)
            if (board[y][x] == 1) { /* Currently alive */
                if (neighbors[y][x] < 2)
                    board[y][x] = 0; /* Death by boredom */
                else if (neighbors[y][x] > 3)
                    board[y][x] = 0; /* Death by overcrowding */
            }
            else { /* Currently empty */
                if (neighbors[y][x] == 3)
                    board[y][x] = 1;
            }
}
1
  • Maybe make step look for something changing, and return a boolean that you can use as the condition in your loop. Commented Jan 22, 2012 at 11:32

1 Answer 1

1

You just have to track changes. Rather trivial to do and far less work (execution/memory wise) than copying and comparing the whole array:

int step(int board[][WIDTH], int rows) { // now returns a bool
    int x, y; 
    int neighbors[HEIGHT][WIDTH]; 
    int changed = 0; // save changes
    for (y = 0; y < rows; y++) 
        for (x = 0; x < WIDTH; x++) 
            neighbors[y][x] = count_neighbors(board, rows, y, x); 
    for (y = 0; y < rows; y++) 
        for (x = 0; x < WIDTH; x++) 
            if (board[y][x] == 1) { /* Currently alive */ 
                if (neighbors[y][x] < 2) 
                {
                    board[y][x] = 0; /* Death by boredom */ 
                    changed = 1; // change happened
                }
                else if (neighbors[y][x] > 3) 
                {
                    board[y][x] = 0; /* Death by overcrowding */ 
                    changed = 1; // change happened
                }
            } 
            else { /* Currently empty */ 
                if (neighbors[y][x] == 3) 
                {
                    board[y][x] = 1; 
                    changed = 1; // change happened
                }
            } 
    return changed; // return the status (changed yes/no?)
} 

int main(void) {  
    int board[HEIGHT][WIDTH];  
    init(board, HEIGHT);  
    while (1) {  
        print(board, HEIGHT, WIDTH);
        if(step(board, HEIGHT) == 0) // no change
            break; // leave the loop
    }  
    return 0;  
}  

Edit: If wanted, you could as well count the actual changes (instead of just saying yes/no) and return the number of changes. Could would stay almost the same.

Sign up to request clarification or add additional context in comments.

4 Comments

errors on bool step(int board[][WIDTH], int rows) {: Error 2 error C2061: syntax error : identifier 'step' Error 4 error C2059: syntax error : 'type' Error 3 error C2059: syntax error : ';'
Ah, my mistake. Use int as return type. C doesn't know bool after all.
@Mario, sure that C has bool (or _Bool) since C99.
Possible, but the classic one hasn't and I wouldn't assume it for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.