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

Recommended Answers

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 …

Jump to Post

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 …

Jump to Post

All 5 Replies

Re: Could you please help me fix my game of life code? 80 80

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.

Re: Could you please help me fix my game of life code? 80 80

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

Re: Could you please help me fix my game of life code? 80 80

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
Re: Could you please help me fix my game of life code? 80 80

THANK YOU!!! I finished my code well.

Re: Could you please help me fix my game of life code? 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.