2D阵列扫雷周围的地雷

时间:2014-05-20 09:27:46

标签: java arrays cell

仍然坚持上一个我问过的问题,但我想我会做一个新帖子来清理一些事情(对不起,如果这很麻烦)。

我努力展示周围的地雷'一个/细胞。我需要做的是允许用户清除一个单元格,如果附近有地雷,则显示有多少,例如: 我怎样才能在我的数组输出中显示围绕单元格的地雷:

更换:

 0  1  2  3  4
0| .  .  .  .  . 
1| .  .  .  .  . 
2| .  .  .  .  * 
3| .  .  *  .  . 
4| *  .  .  *  * 

使用:

   0  1  2  3  4
0| .  .  .  .  . 
1| .  .  .  1  1 
2| .  .  1  2  * 
3| 1  2  *  2  2 
4| *  1  2  *  * 

我用来放置地雷的代码是:

    public MineField(int w, int h, int m)
    {
        Random r = new Random();
        mineField = new State[w][h];
        surroundingMines = new int[w][h];
        initialiseMineField = new int[w][h];
        traceOn = true; //set to false before submitting
        width = w;
        height = h;
        mineCount = m;
        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                mineField[i][j] = State.COVERED;
            }
        }
        for (int k = 0; k < m; k++)
        {
            while (true)
            {
                int a = r.nextInt(w);
                int b = r.nextInt(h);
                if (mineField[a][b] != State.MINED)
                {
                    break;
                }
            }
            mineField[r.nextInt(w)][r.nextInt(h)] = State.MINED;
        }
    }

我展示了我的雷区&#39;通过:

public void displayField(boolean showTruth)
    {
        System.out.print(" ");
        for (int col = 0; col < width; col++)
        {
            System.out.print("  " + col);
        }
        System.out.println();
        for (int row = 0; row < height; row++)
        {
            System.out.print("" + row + "|");
            for (int col = 0; col < width; col++)
            {
                //TODO: You need to complete this method by printing the correct character for the current field cell
                if (mineField[row][col] == State.MINED)
                {
                    System.out.print(" " + '*' + " " );
                }
                if (mineField[row][col] == State.EXPLODED)
                {
                    System.out.print(" " + '+' + " " );
                }
                if (mineField[row][col] == State.COVERED)
                {
                    System.out.print(" " + '.' + " " );
                }
                if (mineField[row][col] == State.CLEARED)
                {
                    System.out.print(" " + ' ' + " " );
                }
                if (mineField[row][col] == State.FLAGGED)
                {
                    System.out.print(" " + 'F' + " " );
                }
                if (mineField[row][col] == State.MISFLAGGED)
                {
                    System.out.print(" " + 'F' + " " );
                }
            }
            System.out.println();
        }
    }

感谢您的帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

你需要发布你曾经尝试写过的东西来计算地雷数量,然后才能评论它的代码......

但是我确实看到了这个:

        while (true)
        {
            int a = r.nextInt(w);
            int b = r.nextInt(h);
            if (mineField[a][b] != State.MINED)
            {
                break;
            }
        }

哪个不好。如果您使用while (true),几乎肯定会出现问题。

        int a, b;
        do {
            a = r.nextInt(w);
            b = r.nextInt(h);
        } while (mineField[a][b] != State.MINED);

答案 1 :(得分:0)

在给定特定单元格的坐标的情况下,您可以编写一个计算具有State.MINED的单元格的方法。正如评论中所提到的,这可以通过两个for循环来完成,迭代遍历一个单元格的邻域。返回一些给定的邻居坐标是否“有效”的辅助方法在这里可以派上用场。所以它可能大致如下:

private int countMines(int x, int y) 
{
    int counter = 0;
    for (int dx=-1; dx<=1; dx++)
    {
        for (int dy=-1; dy<=1; dy++)
        {
            if (dx == 0 && dy == 0) 
            {
                continue; 
            }
            int nx = x+dx;
            int ny = y+dy;
            if (!isValid(nx, ny))
            {
                continue;
            }
            if (mineField[nx][ny] == State.MINED)
            {
                counter++;
            }
        }
    }
    return counter;
}

private boolean isValid(int x, int y)
{
    if (x < 0 || x >= mineField.length) return false;
    if (y < 0 || y >= mineField[x].length) return false;
    return true;
}