MineSweeper reveal neighbors help needed java

Adam Jakiela picture Adam Jakiela · Feb 16, 2012 · Viewed 9k times · Source

I am currently working on a minesweeper program and i need a little help on revealing neighbors in it. Currently what my program can do for reveals is the following

The 1 would be the button that was selected and the lines are what i need to be filled in. Currently the buttons of around the button selected are what i can get to fill in.

I can post code if necessary.

Thanks for the help in advance. enter image description here

1 is a mine, 4 is a flagged spot on the array

public int findneighbors(int row, int col) {
      int count = 0;
    if (board[row][col] == 2)
        try {
            if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
                count ++;
        }

            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
        if (board[row + 1][col + 1] == 1 || board[row + 1][col + 1] == 4)
            count ++;
            }
        catch( ArrayIndexOutOfBoundsException e)
        {
        }
        try {
        if (board[row + 1][col - 1] == 1 || board[row + 1][col - 1] == 4)
            count ++;
            }
        catch( ArrayIndexOutOfBoundsException e)
        {
        }
        try {
            if (board[row - 1][col - 1] == 1 || board[row - 1][col - 1] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
            if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
            if (board[row + 1][col] == 1 || board[row + 1][col] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
            if (board[row - 1][col] == 1 || board[row - 1][col] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
            if (board[row][col - 1] == 1 || board[row][col - 1] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }
        try {
            if (board[row - 1][col + 1] == 1 || board[row - 1][col + 1] == 4)
                count ++;
                }
            catch( ArrayIndexOutOfBoundsException e)
            {
            }

    return count;
  }
public int buttonFloodFill(int r, int c)
{
    int loopCount = 0;
    int rowCount = 1;
    int colCount = 1;
    while (loopCount < 1)
    {
        try {
    if (g.getFloodValue(r,c + colCount) == true) {
        board[r][c + colCount].setText(Integer.toString(g.findneighbors(r,c + colCount)));
        board[r][c + colCount].setEnabled(false);
    }
        }
    catch( ArrayIndexOutOfBoundsException e)
    {
    }
    try {
    if (g.getFloodValue(r,c - colCount) == true) {
        board[r][c - colCount].setText(Integer.toString(g.findneighbors(r,c - colCount)));
        board[r][c - colCount].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r + rowCount,c + colCount) == true) {
        board[r + rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c + colCount)));
        board[r + rowCount][c + colCount].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r + rowCount,c - colCount) == true) {
        board[r + rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c - colCount)));
        board[r + rowCount][c - colCount].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r - rowCount,c - colCount) == true) {
        board[r - rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c - colCount)));
        board[r - rowCount][c - colCount].setEnabled(false);
        }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r - rowCount,c + colCount) == true) {
        board[r - rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c + colCount)));
        board[r - rowCount][c + colCount].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r - rowCount,c) == true) {
        board[r - rowCount][c].setText(Integer.toString(g.findneighbors(r - rowCount,c)));
        board[r - rowCount][c].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    try {
    if (g.getFloodValue(r + rowCount,c) == true) {
        board[r + rowCount][c].setText(Integer.toString(g.findneighbors(r+ rowCount,c)));
        board[r + rowCount][c].setEnabled(false);
    }
    }
catch( ArrayIndexOutOfBoundsException e)
{
}
    rowCount ++;
    colCount ++;
    loopCount ++;

    }
    return 0;
}

Answer

aib picture aib · Feb 17, 2012

While I haven't read your code, it looks like you need to learn some more basic techniques such as loops and refactoring using small helper functions. I can see that you're not interested in exception handling, which is fine for now for a program of this scale, but there are more visually-pleasing (and readability-increasing) solutions, such as merging the consecutive try-catch blocks into one or simply declaring the function to possibly throw.

As for your question, recursion is the answer. You cannot keep checking neighbors and neighbors-of-neighbors and their neighbors and so on. You need to come up with a repeating pattern. Flood fill is the actual answer, but you need to familiarize yourself with recursion and learn to identify problems it might solve.