I made first board, but when I use function next_generation, it prints wrong result. For example, since first board is
01000
00100
11100
00000
00000,
next one should be
00000
10100
01100
01000
00000 .
But my program prints
00000
00100
01100
00000

  1. Here is my code.

    include <stdio.h>
    #define WIDTH 10
    #define HEIGHT 10
    int lifegame[WIDTH][HEIGHT];
    int  x;
    int  y;
    void initialize(){
       for (x=1;x<WIDTH;x++) for (y=1;y<HEIGHT;y++){
          lifegame[x][y]=0;
       }
    }
    void next_generation()
    {
       for(x=1;x<WIDTH;x++)
       {
          for(y=1;y<HEIGHT;y++)
          {int count=0;
             if(lifegame[x-1][y-1]==1){
             count++;}
             if(lifegame[x-1][y]==1){
             count++;}
             if(lifegame[x-1][y+1]==1 ){
             count++;}
             if(lifegame[x][y-1]==1 ){
             count++;}
             if(lifegame[x][y+1]==1){
             count++;}
             if(lifegame[x+1][y-1]==1){
             count++;}
             if(lifegame[x+1][y]==1){
             count++;}
             if(lifegame[x+1][y+1]==1){
             count++;}
    
             if(lifegame[x][y]==0 && count==3){
                lifegame[x][y]=1;
             }
             if(lifegame[x][y]==1 && (count<2 || count>3)){
                lifegame[x][y]=0;
             }
             if(lifegame[x][y]==1 && (count==2 || count ==3)){
                lifegame[x][y]=1;
             }
          }
       }
    }
    void start(){
       initialize();
       for(x=0;x<WIDTH;x++){
          lifegame[x][0]=8;
       }
    
       lifegame[1][2]=1;
       lifegame[2][3]=1;
       lifegame[3][1]=1;
       lifegame[3][2]=1;
       lifegame[3][3]=1;
       printf("\n");
    }
    
    void express(){
       for(x=1; x<WIDTH; x++)
       {
          for(y=0; y<HEIGHT; y++)
          {
             if(lifegame[x][y]==0)
             printf(" |");
             if(lifegame[x][y]==1)
             printf("*|");
             if(lifegame[x][y]==8)
             printf("|");
    
          }
          printf("\n");
       }
    }
    
    int main(){
    
        start();
        next_generation();
       express();
    }

Dani AI

Generated

Short answer: the wrong result came from updating cells in-place while you were still counting neighbours, plus a boundary-index bug. was correct — compute the entire next generation from an unchanged current board, then replace or swap it. Also avoid letting x+1/y+1 reach WIDTH/HEIGHT (that is undefined behaviour and will scramble results).

A practical, robust approach:

  • Keep two arrays (current and next).
  • Loop rows and columns from 0 to H-1 / 0 to W-1.
  • Count neighbours with a small nested loop over offsets (-1..1) and check bounds before reading.
  • Write results into next only. After the double loop, copy or swap buffers.

Example (concise) implementation of the update step:

#define W 10
#define H 10
unsigned char cur[H][W], nxt[H][W];

void step(void) {
    for (int r = 0; r < H; ++r) {
        for (int c = 0; c < W; ++c) {
            int n = 0;
            for (int dr = -1; dr <= 1; ++dr)
                for (int dc = -1; dc <= 1; ++dc) {
                    if (dr == 0 && dc == 0) continue;
                    int rr = r + dr, cc = c + dc;
                    if (rr >= 0 && rr < H && cc >= 0 && cc < W) n += cur[rr][cc];
                }
            nxt[r][c] = cur[r][c] ? (n == 2 || n == 3) : (n == 3);
        }
    }
    memcpy(cur, nxt, sizeof cur); /* or swap pointers to avoid the copy */
}

Extra notes: remove global loop counters (x, y) and avoid magic values like 8 for borders unless you explicitly initialize every border cell. Keep printing and indexing consistent (row vs column order). Test with simple patterns (blinker, glider) and with edge cells to confirm no out-of-bounds access. This makes the code predictable, easy to debug, and fast to maintain — exactly the fix suggested, and the approach that keeps the behaviour stable for ’s tests.

Recommended Answers

All 5 Replies

For the game of life you have to check the number of adjacent cells etc over the whole board, and then update the cells as appropriate.

What you are doing is to check each cell and immediatley update it, so that changes the result for all the cells adjacent to it.

I understand what you mean.. but how can I maintain the number of cell and change them at once ?

You can't do it with one array.
You will need two arrays, one for the current state and one for the new state - so you check the number of adjacent cells in the current state array, then set the new values in the new state array.
After that you can copy the new state back into the current state, or keep the same arrays but simply swap over the references for which is current and which is next.

commented: "Always two there are; no more, no less." +0

THANK YOU!!! I finished my code well.

Great!
In that case please mark this "solved". If you have any new questions you can start another topic.
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.