Alex Moss
Alex Moss

Reputation: 161

Minesweeper game blocks around the mines

Hi so I'm building a program to create the classical minesweeper game in Java, and have almost everything down but I cant figure out how to check the sqaures around a mine and to write in the numbers (i.e. if there are one, two, three mines next to it). I only included the method it's under, but I can post the rest of the program if necessary. What should my approach be? Thanks!

private void countAdjacentMines()
{        
            for (int i = 0; i < mineField.length; i++)
    {
        for (int j = 0; j < mineField.length; j++)
        {
            if (!(mineField[i][j].getIsMine()))
            {
                mineField[i-1][j-1];
                mineField[i-1][j];
                mineField[i-1][j+1];
                mineField[i][j-1];
                mineField[i][j+1];
                mineField[i+1][j-1];
                mineField[i+1][j];
                mineField[i+1][j+1];

                mineField[i][j].setAdjacentMines(0);
            }
        } // end for loop rows
    } // end for loop columns
} // end countAdjacentMines

Upvotes: 2

Views: 4408

Answers (6)

Sam DeHaan
Sam DeHaan

Reputation: 10325

You're on the right track. You should be keeping a counter, that represents the count of adjacent mines that return true for .getIsMine().

if (!(mineField[i][j].getIsMine()))
{
    counter = 0;
    if (i-1 >= 0) 
    { 
        if (j-1 >=0 && mineField[i-1][j-1].getIsMine()) counter++;
        if (mineField[i-1][j].getIsMine()) counter++;
        if (j+1 < mineField.length && mineField[i-1][j+1].getIsMine()) counter++;
    }

    if (j-1 >=0 && mineField[i][j-1].getIsMine()) counter++;
    if (j+1 < mineField.length && mineField[i][j+1].getIsMine()) counter++;

    if (i+1 < mineField.length)
    {
        if (j-1 >=0 && mineField[i+1][j-1].getIsMine()) counter++;
        if (mineField[i+1][j].getIsMine()) counter++;
        if (j+1 < mineField.length && mineField[i+1][j+1].getIsMine()) counter++;
    }

    mineField[i][j].setAdjacentMines(counter);
}

You also need to be checking that all of those values (i-1, j-1, i+1, j+1) don't go outside the bounds of your array (i - 1 > -1, etc)

EDIT:: I think I covered all of the checks.

Upvotes: 3

MRAB
MRAB

Reputation: 20654

Something like this:

private void countAdjacentMines()
{        
    for (int i = 0; i < mineField.length; i++)
    {
        for (int j = 0; j < mineField.length; j++)
        {
            if (!(mineField[i][j].getIsMine()))
            {
                int count = 0;

                for (int p = i - 1; p <= i + 1; p++)
                {
                    for (int q = j - 1; q <= j + 1; q++)
                    {
                        if (0 <= p && p < mineField.length && 0 <= q && q < mineField.length)
                        {
                            if (mineField[p][q].getIsMine())
                                ++count;
                        }
                    }
                }

                mineField[i][j].setAdjacentMines(count);
            }
        } // end for loop rows
    } // end for loop columns
} // end countAdjacentMines

Upvotes: 3

user837324
user837324

Reputation:

Try something like:

private static int countAdjacentMines(int x, int y) {
    int adjacentMines = 0;
    for(int i = -1; i <= 1; i++) {
        if((x + i < 0) || (x + i >= width)) {
            continue;
        }
        for(int j = -1; j <= 1; j++) {
            if((y + j < 0) || (y + j >= height)) {
                continue;
            }
            if(mineField[x + i][y + j].getIsMine()) {
                adjacentMines++;
            }
        }
    }
    return adjacentMines;
}

This should count the number of mines neighbouring a block at (x, y).

Upvotes: 0

js1568
js1568

Reputation: 7032

            int sum = 0;
            sum += mineField[i-1][j-1].getIsMine() ? 1 : 0;
            sum += mineField[i-1][j].getIsMine() ? 1 : 0;
            sum += mineField[i-1][j+1].getIsMine() ? 1 : 0;
            sum += mineField[i][j-1].getIsMine() ? 1 : 0;
            sum += mineField[i][j+1].getIsMine() ? 1 : 0;
            sum += mineField[i+1][j-1].getIsMine() ? 1 : 0;
            sum += mineField[i+1][j].getIsMine() ? 1 : 0;
            sum += mineField[i+1][j+1].getIsMine() ? 1 : 0;

            mineField[i][j].setAdjacentMines(sum);

This is count up how many are mines and use it properly. It may be more clean to create a loop to perform this calculation, since it is the same thing for each adjacent field.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

For each item in that "list", do something like:

if ((i-1) >= 0 && (j-1) >= 0 && mineField[i-1][j-1].getIsMine()) {
    numAdjacentMines++;
}

It's probably worth writing a helper function to do all of this, and then you just need to call it 8 times.

Upvotes: 3

Jon7
Jon7

Reputation: 7215

I'm not sure what you're trying to do. This statement, mineField[i-1][j-1]; and the ones like it just access data and do nothing with it.

I'm assuming that the array stores something that includes a boolean that tells you whether or not there's a mine in that space. If that's true, just make a counter and change those statements to something like if(mineField[i-1][j-1].hasMine()) counter++;. And then change the last statement to mineField[i][j].setAdjacentMines(counter);.

Upvotes: 1

Related Questions