Home > Blockchain >  Conway's game of life algoritm is not working
Conway's game of life algoritm is not working

Time:10-20

I'm trying to simulate conway's game of life. The algorithm I made is not working, but I can't figure out how.
If I have a situation like this:
|.........|
|....x....|
|....x....|
|....x....|
|.........|
a . is a dead cell, an x is an alive cell

It is expected that the vertical bar flips into a horizontal bar, but this doesn't happen. Instead it only removes the bottom one, running it again again only removes the bottom so theres 1 left.

Clearly theres something wrong with the algorithm, but I can't figure out what. I've been looking online but all problems other people have had, the solutions didn't work for me.

So what is the error in this algorithm?

This is my code:

void new_generation() {
        // Create one new generation

        // double for loop -> iterates every cell
        for (int i=0; i<worldHeight; i  ){
            for (int j=0; j<WorldWidth; j  ) {

                // find the amount of living neighbours, stored in count
                // dubbele for loop -> iterates every neighbour of the current cell
                count = 0;
                for (int y=0; y<2; y  ) {
                    for (int x=0; x<2; x  ){
                        if (i != 0 and j!= 0) { // the cell itself doesnt count
                            if (world[i y][j x]) count  ;
                        }
                    }
                }

                if (world[i][j]) { // current cell is alive
                    if (count<2 or count>3) new_world[i][j] = false;
                    else new_world[i][j] = true;
                }
                else { // current cell is dead
                    if (count==3) new_world[i][j] = true;
                    else new_world[i][j] = false;
                }   
            }
        }

        // copy every value from the newly generated world to the current
        // double foor loop -> iterates every cell
        for (int i=0; i<worldHeight; i  ){
            for (int j=0; j<WorldWidth; j  ) {
                world[i][j] = new_world[i][j]; 
            }
        }

worldHeight and worldWidth are ints denoting how big the world is.
world and new_world are 2-dimensional arrays containing booleans where true is a living cell and false is a dead cell

CodePudding user response:

You count the neighbor cells wrong

Both x and y are runing from 0 to 2 not from -1 to 2. in

for (int y=0; y<2; y  ) {//should be int y=-1; y<2; y  
    for (int x=0; x<2; x  ){//should be int x=-1; x<2; x  
        if (i != 0 and j!= 0) { // shold be x!=0 or y!=0
            if (world[i y][j x]) count  ;
        }
     }
 }

Also you have to check if world[i y][j x] is valid (coordinates are in teh 0,size range)

And the third problem is that when you want not to count in word[i][j] you check if (i!=0 and j!=0) not x!=0 or y!=0 i and j are the coordinates of the examined cell, x and y are the difference of the coordinates.

  • Related