'How to check for neighbors in Game of Life C++?

I am working on an assignment for school and of course I receive very vague feedback on our code. The code I am working on is for Conway's Game of Life. I know I am super close. I have code that prints out the new generation but it's definitely not the correct one. It seems it is not counting the neighbors correctly - what should be identified as an alive neighbor doesn't seem to happen.

From our assignment as well (seeing examples of generations being formed) I notice the border cells do change which means I have to access them without going out of bounds. I feel I have been fruitless in my attempts to do this and I think I'm just missing something super obvious.

Please, any feedback would be amazing. I have several print lines in attempts of debugging.

void gameOfLife(vector<vector<string>> &originalGrid, vector<vector<string>> &grid, int row, int col,
                int Rows, int Cols){

    //counts # of alive neighbors
    int aliveNeighbors = 0;
    string alive = "*";

    for(int posX = row-1; posX <= row+1; posX++){
        for(int posY = col-1; posX <= col+1; posX++){

            std::cout << "I am in function - nested loop " << row << " " << col << std::endl;

            if(posX == row && posY == col){

                continue;
            }
            else if((posX >= 0 && posX < Rows) && (posY >= 0 && posY < Cols)){

                std::cout << "I am in function - nested loop - else if " << row << " " << col << std::endl;

                if(grid[posX][posY] == alive){

                    aliveNeighbors++;
                    std::cout << "alive neighbors: " << aliveNeighbors << std::endl;
                }
            }
        }
    }
    /*
        //top cell
        if(grid[row][col-1] == "*"){

            std::cout << "top cell " << row << " " << col << std::endl;
            aliveNeighbors++;
        }
        //bottom cell
        if(grid[row][col+1] == "*"){

            std::cout << "bottom cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //left cell
        if(grid[row-1][col] == "*"){

            std::cout << "left cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //right cell
         if(grid[row+1][col] == "*"){

            std::cout << "right cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //top left
        if(grid[row-1][col-1] == "*"){

            std::cout << "top left cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //top right
         if(grid[row+1][col-1] == "*"){

            std::cout << "top right cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //bottom left
         if(grid[row-1][col+1] == "*"){

            std::cout << "bottom left cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }
        //bottom right
         if(grid[row+1][col+1] == "*"){

            std::cout << "bottom right cell " << row << " " << col <<  std::endl;
            aliveNeighbors++;
        }

    */
        //test cases

        //test case 1: Any live cell with fewer than two live neighbors dies (as if by underpopulation).
        if(grid[row][col] == alive && aliveNeighbors < 2){

            originalGrid[row][col] = ".";
        }

        //test case 2: Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
        if(grid[row][col] == alive && aliveNeighbors > 3){

            originalGrid[row][col] = ".";
        }

        //test case 3: Any live cell with two or three live neighbors lives, unchanged, to the next generation.
        if(grid[row][col] == alive && (aliveNeighbors == 3 || aliveNeighbors == 2)){

            originalGrid[row][col] = grid[row][col];

        }

        //test case 4: Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
        if(grid[row][col] == "." && aliveNeighbors == 3){

            originalGrid[row][col] = alive;
        }

        //prints updated grid
        for(int i = 0; i < Rows; i++){
             for(int j = 0; j < Cols; j++){

                std::cout << originalGrid[i][j] << " ";
             }
            std::cout << std::endl;
           }

           std::cout << std::endl;


    return;
}

int main() {

    int rows, col, numOfGen;
    std::cin >> rows >> col >> numOfGen;

    string cell;

    vector<vector<string>> game;

    for(int i = 0; i < rows; i++){

        vector<string> temp;

        for(int j = 0; j < col; j++){

            std::cin >> cell;
            temp.push_back(cell);

        }
        game.push_back(temp);
    }

    vector<vector<string>> firstGen;
    firstGen.insert(firstGen.end(),game.begin(),game.end());


    if(numOfGen == 0){

        std::cout << "numOfGen == 0" << std::endl;

        for(int i = 0; i < rows; i++){
            for(int j = 0; j < col; j++){

                std::cout << game[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }


    for(int g = 0; g <= numOfGen; g++){
        for(int i = 1; i < rows; i++){
            for(int j = 1; j < col; j++){

                gameOfLife(game, firstGen, i, j, rows, col);
            }
        }

        if(g == numOfGen){

           for(int i = 0; i < rows; i++){
             for(int j = 0; j < col; j++){

                std::cout << game[i][j] << " ";
             }
            std::cout << std::endl;
           }
        }
    }



    return 0;
}


Solution 1:[1]

Looks like firstGen never gets updated, so you're just computing the first generation over and over. So your output is probably correct for a single generation, but it's the same for any number of generations. Also, check the conditions on your main driver loop: with for(int g = 0; g <= numOfGen; g++) the loop executes numOfGen+1 times.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dgould