Mr. Czar
Mr. Czar

Reputation: 45

Problem trying to code Conway's Game of Life in C++

I'm building this program using the reni2D library found on involuntaryexercise.com I've created a grid consisting of a 2D array of rows and colums in which each square is clicked to be on and off. Now, I'm trying to make some working algorithms that adhere to the rules, but when running the program they all disappear after having the program go through the related loops. I don't see anything inherently wrong here, so I'm relying on the eyes of skilled programmers to guide me on the right path.

[UPDATE]: The program now works better, except that it doesn't follow the Game Of Life rules for some reason.

if(raGetKey(VK_RETURN))
        {                               
            //occupied cells
            for(int i=0; i<ROWS; i++){ //768/24 = 32                    
                    for(int j=0; j<COLS; j++){ //768/32 = 24                        
                        //Occupied cells
                        if(sq[i][j].isOn == true && sq[i][j].neighbors < 4)
                        {
                            //UDLR
                                if((j+1)<COLS && sq[i][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((j-1)>(-1) && sq[i][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i+1)<ROWS && sq[i+1][j].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>(-1) && sq[i-1][j].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }

                                if(sq[i][j].neighbors >= 4)
                                    break;
                            //Diagonal
                                if((i+1)<ROWS && (j+1)<COLS  && sq[i+1][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>(-1) && (j-1)> (-1) && sq[i-1][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>(-1) && (j+1)<COLS  && sq[i-1][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i+1)<ROWS && (j-1)> (-1) && sq[i+1][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The occupied sq[%d][%d] gains a neighbor\n", i, j);
                                }
                        }

                        //Empty cells
                        if(sq[i][j].isOn == false && sq[i][j].neighbors < 4)
                        {
                        //UDLR
                            /*if((i+1)<ROWS && (j+1)<COLS && (i-1)>0 && (j-1)>0)
                            {*/
                                if((j+1)<COLS && sq[i][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((j-1)>0    && sq[i][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i+1)<ROWS && sq[i+1][j].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>-1   && sq[i-1][j].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }

                                if(sq[i][j].neighbors >= 4)
                                    break;
                        //Diagonal
                                if((i+1)<ROWS && (j+1)<COLS  && sq[i+1][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>(-1) && (j-1)> (-1) && sq[i-1][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i-1)>(-1) && (j+1)<COLS  && sq[i-1][j+1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                                if((i+1)<ROWS && (j-1)> (-1) && sq[i+1][j-1].isOn == true){
                                    sq[i][j].neighbors += 1;
                                    printf("The empty sq[%d][%d] gains a neighbor\n", i, j);
                                }
                            //}
                        }
                    }
                }
                for(int i=0; i<ROWS; i++) //768/24 = 32
                {                   
                    for(int j=0; j<COLS; j++) //768/32 = 24
                    { 

                        if(sq[i][j].isOn == true)
                        {
                            if(sq[i][j].neighbors <= 1){ 
                                sq[i][j].isOn = false;
                                sq[i][j].neighbors = 0;
                                printf("sq[%d][%d] is ON and has less than 1 neighbors ... Died from lonliness\n", i, j);
                            }
                            if(sq[i][j].neighbors >= 4){
                                sq[i][j].isOn = false;
                                sq[i][j].neighbors = 0;
                                printf("sq[%d][%d] is ON and has neighbors is greater than or equal to 4... Died from overpopulation\n", i, j);
                            }
                            if(sq[i][j].neighbors > 1 && sq[i][j].neighbors < 4){ 
                                sq[i][j].isOn = true;
                                printf("sq[%d][%d] is ON has 2 or 3 neighbors... Lives\n", i, j);
                                sq[i][j].neighbors = 0;
                            }
                        }
                        if(/*sq[i][j].isOn == false && */sq[i][j].neighbors == 3){
                                printf("sq[%d][%d] is OFF has 3 neighbors... Reproduce\n", i, j);                       
                                sq[i][j].isOn = true;
                                sq[i][j].neighbors = 0;
                        }
                    }
                }
        }

Upvotes: 0

Views: 824

Answers (2)

user507787
user507787

Reputation: 126

It's quite clear what went wrong here (hint: your cells die because your program thinks their neighbourhoods are overpopulated when in fact, they aren't). The more interesting question is: Why did it go wrong? In my opinion, it is because you use an identifier called 'neighbors' for something which doesn't count neighbours at all but rather does so partially and mangled with some 'optimizations' that you built into your code.

(Read on if you want to know the exact cause:

You're not resetting 'neighbors' if the cell lives, but then add to 'neighbors' again when you go through the next iteration of the loop.)

Upvotes: 1

Nate Kohl
Nate Kohl

Reputation: 35914

There might be several problems here, but at least one of them could be misplaced curly braces: did you mean to have your 3rd and 4th for-loops inside your 1st for-loop?

As an aside, many programmers like to use text editors that have automatic indenting, which can help catch these types of bugs.

Upvotes: 1

Related Questions