如何在生活游戏中检查邻居

发布于 2025-01-22 03:24:34 字数 5718 浏览 2 评论 0原文

我正在为学校做一次作业,当然,我会收到有关代码的非常模糊的反馈。我正在处理的代码是为了康威的生活游戏。我知道我非常亲密。我的代码可以打印出新一代,但绝对不是正确的代码。看来这没有正确计算邻居 - 应该确定为活着的邻居似乎没有发生。

从我们的分配(查看形成的世代示例)中,我注意到边框细胞确实会发生变化,这意味着我必须访问它们而不会超越界限。我觉得自己尝试这样做是徒劳的,我想我只是错过了一些显而易见的东西。

拜托,任何反馈都会很棒。 我有几条打印线尝试调试。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2025-01-29 03:24:34

看起来FirstGen永远不会更新,因此您只是一遍又一遍地计算第一代。因此,您的输出对于单一一代可能是正确的,但是对于任何几代人来说都是相同的。另外,请检查主驱动程序循环上的条件:使用for(int g = 0; g&lt; = numofgen; g ++)循环执行numofgen+1 times。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文