简体   繁体   English

这个while循环导致我的程序挂起

[英]This while loop causes my program to hang

I have a function that is causing my program to hang.我有一个 function 导致我的程序挂起。 I have commented out the function and everything else runs just fine.我已经注释掉了 function,其他一切都运行良好。 The program gets to where the loop should end and it just waits for input.程序到达循环应该结束的地方,它只是等待输入。 The isBomb() function is simply a getter and returns a true/false value. isBomb() function 只是一个 getter 并返回一个真/假值。 The function is part of a Minesweeper game. function 是扫雷游戏的一部分。 I am trying to figure out a way to figure out how many bombs are adjacent to the chosen cell.我正在尝试找出一种方法来确定与所选单元格相邻的炸弹数量。 I could post the whole program, but it is around 250-350 lines.我可以发布整个程序,但它大约是 250-350 行。 The makeNum method is a simple getter that sets the cell number equal to the value of the parameter. makeNum 方法是一个简单的 getter,它将单元格编号设置为等于参数的值。

void mazeDisplay::countBombAdj(int row, int col) {
    int counter = 0;
/*  for (int x = row - 1; x < row + 1; x++) {
        while ((x > - 1) && (x < 4)) {
            for (int y = col - 1; y < col + 1; y++) {
                while ((-1 < y) && (y < 4)) {
                    if (mazeCells[x][y].isBomb() == true)
                        counter += 1;
                }
            }
        }
    }*/

    mazeCells[row][col].makeNum(counter);
}

It's your line:这是你的行:

while ((x > - 1) && (x < 4))

x dosn't change and there aren't any break 's in that loop so the loop is infinite. x不会改变,并且该循环中没有任何break ,因此循环是无限的。

Similarly for:同样适用于:

while ((-1 < y) && (y < 4)) 

It looks like, as others have commented, that you what if statements, not (infinite) while loops:正如其他人所评论的那样,您看起来像if语句,而不是(无限) while循环:

void mazeDisplay::countBombAdj(int row, int col) {
    int counter = 0;
    for (int x = row - 1; x < row + 1; x++) {
        if ((x > - 1) && (x < 4)) {
            for (int y = col - 1; y < col + 1; y++) {
                if ((-1 < y) && (y < 4)) {
                    if (mazeCells[x][y].isBomb() == true)
                        counter += 1;
                }
            }
        }
    }

    mazeCells[row][col].makeNum(counter);
}

Once you've entered one of your while-loops, the condition will never become false and you're stuck in it forever.一旦你进入了你的一个while循环,这个条件就永远不会变成假的,你会永远被困在其中。

Also, checking those conditions with if instead would lead you to only scan a three-by-three square, which is a very boring minesweeper game.此外,使用if检查这些条件会导致您只扫描一个 3×3 的方格,这是一款非常无聊的扫雷游戏。
You need to check if you're inside the board, not within that small square.您需要检查您是否在棋盘内,而不是在那个小方块内。

I would loop over the appropriate range instead of skipping invalid cells:我会在适当的范围内循环,而不是跳过无效的单元格:

for (int x = std::max(0, row-1); x < std::min(number_of_rows, row+2); x++)
{
    for (int y = std::max(0, col-1); y < std::min(number_of_cols, col+2); y++)
    {
        if (mazeCells[x][y].isBomb())
            counter += 1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM