已解决 - 程序不计算字段的数量

SOLVED - Program does not count numbers of a field rigth

我尝试在 python 中编写扫雷程序。计算被炸弹包围的场地的数量,我遇到了一个更糟糕的问题 - 没有错误...

我的代码是:

for i in range(len(bombs)):
    numbers[bombs[i]] = -1

    if (bombs[i][0] > 0):
        if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]-step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]+step)] = numbers[(bombs[i][0]-step, bombs[i][1]+step)] + 1

    if (bombs[i][0] < width-step-1):
        if (numbers[(bombs[i][0]+step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]+step, bombs[i][1]+0)] = numbers[(bombs[i][0]+step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]+step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]-step)] = numbers[(bombs[i][0]+step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]+step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]+step)] = numbers[(bombs[i][0]+step, bombs[i][1]+step)] + 1

    if (bombs[i][1] > 0):
        if (numbers[(bombs[i][0]+0, bombs[i][1]-step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]-step)] = numbers[(bombs[i][0]+0, bombs[i][1]-step)] + 1

    if (bombs[i][1] < heigth-step-1):
        if (numbers[(bombs[i][0]+0, bombs[i][1]+step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]+step)] = numbers[(bombs[i][0]+0, bombs[i][1]+step)] + 1

step = size of the fields

bombs = all bombs in an array

谢谢 [1]: https://i.stack.imgur.com/FOxi4.png

我认为这部分代码有问题:

            if (bombs[i][0] > 0):
                # This bomb is not in the leftmost column of the grid
        #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                    # The cell to the left is not a bomb, so add one to its count
                    numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1

                    if (bombs[i][1] > 0):
                        # This bomb is not in the top row of the grid.
                        if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                            # The cell above and to the left is not a bomb, so add one to its count
                            numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

请注意,我们只查看炸弹上方和左侧的单元格,如果它不是炸弹,则向其添加一个如果左侧的单元格也不是炸弹。这是不正确的:对于任何炸弹,我们需要检查其上方和左侧的单元格是否其左侧的单元格也是炸弹。

你想要做的是从上面的第三个 if 语句中删除一个级别的缩进:

            if (bombs[i][0] > 0):
                # This bomb is not in the leftmost column of the grid
        #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                    # The cell to the left is not a bomb, so add one to its count
                    numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1

                # This line will now be reached if there is another bomb to the left of bombs[i].
                if (bombs[i][1] > 0):
                    # This bomb is not in the top row of the grid.
                    if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                        # The cell above and to the left is not a bomb, so add one to its count
                        numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

您还可以对其他三个 if 语句进行相同的更改,这些语句处理在其他方向上对角相邻正方形的计数加一。


查看您的代码,我不确定您在 if (bombs[i][1] < heigth-step-1): 等条件下是否需要 -1。很明显,这些检查试图阻止你离开网格的边缘,但额外的 -1 是不必要的。我也有点担心step:这等于1吗?您将 step 包含在检查中是否从右侧或底部掉落,但不会从顶部或左侧掉落,因此如果 step 大于 1你可能 运行 遇到问题。 (例如,如果 step 为 3,bombs[i][0] 为 1,则 bombs[i][0] - step 将为 -2。)

此外,我可以从可读性的角度对您的代码提出一些建议吗?首先,你重复 bombs[i][0]bombs[i][1] 很多:如果你添加行 x = bombs[i][0]y = bombs[i][1] 你可以写 xy 而不是bombs[i][0]bombs[i][1] 在循环的其余部分。其次,您可以写 some_expression += 1 而不是 some_expression = some_expression + 1。进行这些更改会缩短一行,例如

numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

numbers[(x-step, y-step)] += 1