【问题标题】:2D Arrays Minesweeper Surrounding Mines围绕地雷的二维阵列扫雷艇
【发布时间】:2014-05-20 09:27:46
【问题描述】:

我仍然坚持我之前提出的问题,但我想我会发一个新帖子来清理一下(抱歉,如果这很麻烦)。

我正在努力展示一个/细胞的“周围地雷”。我需要做的是让用户清除一个单元格,如果附近有地雷,则显示有多少个,例如: 我如何能够在我的数组输出中显示围绕单元格的地雷:

替换:

 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;
        }
    }

我通过以下方式展示我的“雷区”:

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();
        }
    }

感谢您的帮助,谢谢!

【问题讨论】:

    标签: java arrays cell


    【解决方案1】:

    您可以编写一个方法来计算具有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;
    }
    

    【讨论】:

    • Tnx,伙计。你的解决方案很棒。我有同样的问题,你不知道我搜索了多少,什么也没找到。但这个解决方案和分析非常出色。 tnx 这么多。
    【解决方案2】:

    您需要发布您尝试编写的内容以计算地雷,然后我们才能对其代码发表评论...

    但是我确实看到了这个:

            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);
    

    【讨论】:

    • 我很想向您展示我的尝试,但就是这样,我对如何实际处理这个问题没有最微弱的线索......我假设我需要在 if 中进行循环通过行和列的数组,并检查它们是否被挖掘。但是然后呢?
    • 将计数器初始化为 0。从 x-1 到 x+1 和 y-1 到 y+1 循环,为您看到的每个地雷递增计数器。那是你的地雷数。注意电路板的边缘。
    猜你喜欢
    • 2020-03-06
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多