C ++ Game of Life - функция бесконечного зацикливания - PullRequest
0 голосов
/ 29 сентября 2019

Эта функция в игровом назначении жизни должна проходить через 2d массив и проверять, сколько соседей имеет каждая ячейка.Когда я вызываю это в основном, даже не в каком-либо цикле, терминал останавливается, как в бесконечном цикле while.Может кто-нибудь сказать мне, что не так с моим кодом?Спасибо.

void Grids::simulate(int** myGrid, int rows, int columns)
{
    int neighbors = 0; //variable to store how many neighbors a cell has
    for(int r = 0; r<rows; ++r) // iterates through rows
    {
        for(int c=0; c<columns; ++c)//iterates through columns
        {
            for(int x=-1; x<2; x+2) //iterates through -1 and 1, the spaces next to the cell
            {
                for(int y=-1;y<2;y+2)
                {
                    if((r+x>=0) && (r+x<rows) && (c+y>=0) && (c+y<columns)) //prevents indexing the 2d array outside of its bounds
                    {
                        if(myGrid[r+x][c+y]==1) //checks if the surrounding cells are alive
                        {
                            ++neighbors;
                        }
                    }
                }
            }
            if(neighbors<2) //underpopulation
            {
                myGrid[r][c]=0; //dead
            }
            else if(neighbors==3)//reproduction
            {
                myGrid[r][c]=1;//alive
            }
            else if(neighbors>=4)//overpopulation
            {
                myGrid[r][c]=0;//dead
            }

        }
    }
}
...