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();
    }

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.