地雷周围的扫雷器工作时间只有一半

minesweeper surrounding mines works half the time

我正在Java中编写扫雷克隆,我在计算周围炸弹数量的部分遇到了一些麻烦。出于某种原因,一些单元格检测到额外的炸弹,或者根本没有炸弹(甚至奇怪的是,一些单元格工作正常)。有人可以帮忙吗?谢谢!

注意:key[][]是一个记录炸弹位置的int[][]数组,记为9。

0代表空space

int count 表示每个单元格周围的 8 个单元格中的炸弹数量(这就是为什么我也有 8 个 try-catch loops 来说明边界单元格)

ps:抱歉格式错误

编辑:我知道我的问题出在哪里(我打了两次电话)。关于如何使此代码更简单、更高效,有什么建议吗?

private void numberSet() {

    int count = 0;
    for (int i = 0; i < key.length; i++) {
        for (int a = 0; a < key[0].length; a++) {

            if (key[i][a] == 0) {

                try {
                    if (key[i + 1][a] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][a] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i][a - 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i + 1][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i + 1][a - 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][i - 1] == 9) {
                        count++;

                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                key[i][a] = count;
            }
            count = 0;
        }
    }

}

看起来你在误用 continue 语句,它会将执行移回 for 循环,因此不会完成各种检查以增加 count.

catch 语句中,您什么都不应该做。

if(key[i-1][i-1] == 9){

这看起来与你所有其他的不同,它们有 i 和 a。这个用了我两次。也许这是你的问题?

我会认真考虑完全放弃你这样做的方式。当我刚开始写代码的时候,我也做了一个扫雷游戏,我做了你正在做的事情。经过几次修改后,我能够将所有内容放入几个循环中。我试图在我的代码中进行足够多的评论,以告诉您每一行的作用。此代码未经测试,因此说实话,它可能不会立即运行。但是,我仔细看了几次,似乎对我有用。我建议您采用此代码并尝试使其工作。试一试。

//For every Cell on the board
for (int i = 0; i < key.length; i++){
    for (int a = 0; a < key[0].length; a++){

        //If it Is not a mine
        if (key[i][a] == 0){ 
            int count = 0;

            //Position of cells around key[i][a] relative to key[i][a]
            for (int x = -1; x < 2; x++){  
                for (int y = -1; y < 2; y++){ 

                    //Storing x and y test points
                    int testX = i + x;//testX = i-1, i, and i+1
                    int testY = i + y;//testY = i-1, y, and y+1

                    //If the testX and testY values are within the range of the array
                    if ((testX >= 0 && testX < key[0].length) && testY >= 0 && testY < key.length){

                        //If there is a mine
                        if (key[testX][testY] == 9){
                            count++;
                        }
                    }
                }
            }

            key[i][a] = count;

            //count = 0; This is redundant. Line 7 count gets set to 0.
        }
    }
}