0

我正在尝试为编程挑战网站编写扫雷程序,并且无法检测 2D 数组中的某个元素是否是地雷并增加周围区域。这是我的代码,用于检查数组角落中的地雷并增加周围区域

  public void constructPointers(){
    int counter = 0;
    for(int i = 0; i<row;i++){
        for(int j = 0;j<colm;j++){
            if(minefield[i][j].equals("*")){
                if(i == 0 && j == 0){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;

                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j+1]);
                    val++;
                    minefield[i+1][j+1] = "" + val;
                }
                if(i == minefield.length -1 && j == 0){
                    int val = Integer.parseInt(minefield[i-1][j]);
                    val++;
                    minefield[i-1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i-1][j+1]);
                    val++;
                    minefield[i-1][j+1] = "" + val;
                }
                if(i == 0 && j == minefield[i].length - 1){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j-1]);
                    val++;
                    minefield[i][j-1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j-1]);
                    val++;
                    minefield[i+1][j-1] = "" + val;
                }
                if(i == minefield.length - 1 && j == minefield[i].length -1){
                    int val = Integer.parseInt(minefield[i-1][j-1]);
                    val++;
                    minefield[i-1][j-1] = "" + val;
                    val = Integer.parseInt(minefield[i-1][j]);
                    val++;
                    minefield[i-1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j-1]);
                    val++;
                    minefield[i][j-1] = "" + val;
                }
                if(i == 0){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j+1]);
                    val++;
                    minefield[i+1][j+1] = "" + val;
                }
            }

如果我输入:

*.*
...
*.*

此代码将输出:

* 2 *
2 4 2
* 2 *

我的问题是:如何让我的代码“跳过”相邻的地雷并增加周边区域?如果我输入

*.*
*..
*.*

我希望它输出:

    * 3 *
    * 5 2
    * 3 *

编辑:你们所有人都有很好的答案,但我选择了最佳答案,因为它解决了我没有提到的许多其他问题。谢谢大家的帮助!

4

3 回答 3

3

只是为您的矿山更新循环提供建议:

// You are on a mine
if(minefield[i][j].equals("*")){

    // Now go around your current position
    for(int tempRow = i-1; tempRow <= i+1; tempRow++) {
        for(int tempCol = j-1; tempCol <= j+1; tempCol++) {
            // If a valid square and not a mine
            if(tempRow >= 0 && tempRow < row &&
                tempCol >= 0 && tempCol < colm &&
                !minefield[tempRow][tempCol].equals("*")) 
            {
                // Do your update stuff
                int val = Integer.parseInt(minefield[tempRow][tempCol]);
                val++;
                minefield[tempRow][tempCol] = "" + val;
            }
        }
    }
}

这个循环也可以更容易扩展到更大的电路板尺寸......

注意:如果不是地雷,我假设雷区被初始化为“0”。否则第一个会有问题parseInt

于 2013-03-27T03:57:41.350 回答
0

您可能会发现迭代每个单元格更容易,并且对于每个不是地雷的单元格,计算单元格周围的地雷数。换句话说,您一次计算每个单元格的计数。这可以解决您关于如何处理相邻地雷的问题。

于 2013-03-27T02:55:11.967 回答
-2
if(minefield[i][j] == "*"){
    // do stuff
}

我不明白有什么大不了的,嗯。

编辑

不正确 - 正如评论中所说:

if(minefield[i][j].equals("*")){
    // do stuff, they are equal.
}

ps不是很有建设性的答案,但由于建设性的意见,我离开了它。

于 2013-03-27T02:40:55.383 回答