// 四个角落的雷表示
if (m == 0 && n == 0) {
bomb[m][n + 1].hmBomb++;
bomb[m + 1][n + 1].hmBomb++;
bomb[m + 1][n].hmBomb++;
}
if (m == 0 && n == NUMLEVEL-1) {
bomb[m][n - 1].hmBomb++;
bomb[m + 1][n - 1].hmBomb++;
bomb[m + 1][n].hmBomb++;
}
if (m == NUMLEVEL-1 && n == 0) {
bomb[m][n + 1].hmBomb++;
bomb[m - 1][n + 1].hmBomb++;
bomb[m - 1][n].hmBomb++;
}
if (m == NUMLEVEL-1 && n == NUMLEVEL-1) {
bomb[m][n - 1].hmBomb++;
bomb[m - 1][n - 1].hmBomb++;
bomb[m - 1][n].hmBomb++;
}
// 四条边的雷
if (m == 0 && n != 0 && n != NUMLEVEL-1) {
bomb[m][n + 1].hmBomb++;
bomb[m][n - 1].hmBomb++;
bomb[m + 1][n + 1].hmBomb++;
bomb[m + 1][n - 1].hmBomb++;
bomb[m + 1][n].hmBomb++;
}
if (m == NUMLEVEL-1 && n != 0 && n != NUMLEVEL-1) {
bomb[m][n + 1].hmBomb++;
bomb[m][n - 1].hmBomb++;
bomb[m - 1][n + 1].hmBomb++;
bomb[m - 1][n - 1].hmBomb++;
bomb[m - 1][n].hmBomb++;
}
if (n == NUMLEVEL-1 && m != 0 && m != NUMLEVEL-1) {
bomb[m + 1][n].hmBomb++;
bomb[m - 1][n].hmBomb++;
bomb[m + 1][n - 1].hmBomb++;
bomb[m - 1][n - 1].hmBomb++;
bomb[m][n - 1].hmBomb++;
}
if (n == 0 && m != 0 && m != NUMLEVEL-1) {
bomb[m + 1][n].hmBomb++;
bomb[m - 1][n].hmBomb++;
bomb[m + 1][n + 1].hmBomb++;
bomb[m - 1][n + 1].hmBomb++;
bomb[m][n + 1].hmBomb++;
}
//中间区域的雷
if (m != 0 && m != NUMLEVEL-1 && n != 0 && n != NUMLEVEL-1) {
//bomb[m][n].hmBomb++;
bomb[m][n + 1].hmBomb++;
bomb[m][n-1].hmBomb++;
bomb[m+1][n].hmBomb++;
bomb[m-1][n].hmBomb++;
bomb[m+1][n+1].hmBomb++;
bomb[m-1][n-1].hmBomb++;
bomb[m+1][n-1].hmBomb++;
bomb[m-1][n+1].hmBomb++;
}

解决方案 »

  1.   

    每个按钮都有一个属性.  hmBomb 代表 他周围有几个雷..前面判断的是..  他是雷的话...周围所有的 数字就加1.. 分了3种情况.    四个角  四个边.  还有中间.. 但是最后的结果 不对..
      

  2.   

    呵呵。我以前有做过。。好就没翻出来了。。怀念以前的岁月啊
    我以前写的代码是这样的。。感觉还可以优化一下。。不过。具体到每句代码是什么意思就不知道了。。只知道大概是那个意思,因为刚学这个的时候很少写注释
        private void openNull(int i, int j) {
            btns[i][j].setVisible(false);
            lbls[i][j].setVisible(true);
            isOpen[i][j] = true;
            //如果左上角有格子并且是空格 i-1  j-1
            if (j != 0 && i != 0 && intArray[i - 1][j - 1] == 0) {
                if (!isOpen[i - 1][j - 1]) {
                    openNull(i - 1, j - 1);
                }
            }
            //如果左边还有格子。并且左边是空格 i  j-1
            if (j != 0 && intArray[i][j - 1] == 0) {
                if (!isOpen[i][j - 1]) {
                    openNull(i, j - 1);
                }
            }
            //如果上面有格子并且是空格  i-1  j
            if (i != 0 && intArray[i - 1][j] == 0) {
                if (!isOpen[i - 1][j]) {
                    openNull(i - 1, j);
                }
            }
            //如果右上角有格子并且是空格 i+1  j-1
            if (i + 1 < intArray.length &&
                j != 0 && intArray[i + 1][j - 1] == 0) {
                if (!isOpen[i + 1][j - 1]) {
                    openNull(i + 1, j - 1);
                }
            }
            //如果右边有格子并且是空格 i j+1
            if (j + 1 < intArray[i].length && intArray[i][j + 1] == 0) {
                if (!isOpen[i][j + 1]) {
                    openNull(i, j + 1);
                }
            }
            //如果右下角有格子并且是空格 i+1  j+1
            if (i + 1 < intArray.length && j + 1 < intArray[i].length &&
                intArray[i + 1][j + 1] == 0) {
                if (!isOpen[i + 1][j + 1]) {
                    openNull(i + 1, j + 1);
                }
            }
            //如果下面有格子并且是空格  i+1  j
            if (i + 1 < intArray.length && intArray[i + 1][j] == 0) {
                if (!isOpen[i + 1][j]) {
                    openNull(i + 1, j);
                }
            }
            //如果左下角有格子并且是空格 i-1 j+1
            if (i != 0 && j + 1 < intArray[i].length &&
                intArray[i - 1][j + 1] == 0) {
                if (!isOpen[i - 1][j + 1]) {
                    openNull(i - 1, j + 1);
                }
            }
            this.display(i, j);
        }想要全部代码欢迎找我。
      

  3.   

    我好像发错代码了.下面这段是计算周围雷数的:
       /**
         * 从boolean数组中获取int数组。
         *
         *  -1代表地雷
         *
         *  0代表什么都没有
         *
         *  1-9表示旁边有地雷。并以数字代表旁边地雷的个数
         */
        private int[][] intArray = null;
        private int[][] boolArrayToIntArray(boolean[][] boolArray) {
            int[][] intArray = new int[boolArray.length][boolArray[0].length];
            for (int i = 0; i < boolArray.length; i++) {
                for (int j = 0; j < boolArray[i].length; j++) {
                    //如果本身不是地雷
                    if (!boolArray[i][j]) {
                        //旁边地雷的个数
                        int count = 0;
                        //如果左上角有格子并且是地雷 i-1  j-1
                        if (j != 0 && i != 0 && boolArray[i - 1][j - 1]) {
                            count++;
                        }
                        //如果左边还有格子。并且左边是地雷 i  j-1
                        if (j != 0 && boolArray[i][j - 1]) {
                            count++;
                        }
                        //如果上面有格子并且是地雷  i-1  j
                        if (i != 0 && boolArray[i - 1][j]) {
                            count++;
                        }
                        //如果右上角有格子并且是地雷 i+1  j-1
                        if (i + 1 != boolArray.length &&
                            j != 0 && boolArray[i + 1][j - 1]) {
                            count++;
                        }
                        //如果右边有格子并且是地雷 i j+1
                        if (j + 1 != boolArray[i].length && boolArray[i][j + 1]) {
                            count++;
                        }
                        //如果右下角有格子并且是地雷 i+1  j+1
                        if (i + 1 < boolArray.length && j + 1 < boolArray.length &&
                            boolArray[i + 1][j + 1]) {
                            count++;
                        }
                        //如果下面有格子并且是地雷  i+1  j
                        if (i + 1 < boolArray.length && boolArray[i + 1][j]) {
                            count++;
                        }
                        //如果左下角有格子并且是地雷 i-1 j+1
                        if (i != 0 && j + 1 < boolArray[i].length &&
                            boolArray[i - 1][j + 1]) {
                            count++;
                        }
                        intArray[i][j] = count;
                    } else {
                        //如果本身是地雷了
                        intArray[i][j] = -1;
                    }
                    System.out.print(intArray[i][j] + "  ");
                }
                System.out.println();
            }
            return intArray;
        }