Question

I'm trying to write a Minesweeper program for the programming challenges website and I am having trouble detecting if a certain element in the 2D array is a mine and incrementing the surrounding areas. Here is my code that checks for a mine in the corners of the array and increments the surrounding areas

  public void constructPointers(){
    int counter = 0;
    for(int i = 0; i<row;i++){
        for(int j = 0;j<colm;j++){
            if(minefield[i][j].equals("*")){
                if(i == 0 && j == 0){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;

                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j+1]);
                    val++;
                    minefield[i+1][j+1] = "" + val;
                }
                if(i == minefield.length -1 && j == 0){
                    int val = Integer.parseInt(minefield[i-1][j]);
                    val++;
                    minefield[i-1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i-1][j+1]);
                    val++;
                    minefield[i-1][j+1] = "" + val;
                }
                if(i == 0 && j == minefield[i].length - 1){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j-1]);
                    val++;
                    minefield[i][j-1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j-1]);
                    val++;
                    minefield[i+1][j-1] = "" + val;
                }
                if(i == minefield.length - 1 && j == minefield[i].length -1){
                    int val = Integer.parseInt(minefield[i-1][j-1]);
                    val++;
                    minefield[i-1][j-1] = "" + val;
                    val = Integer.parseInt(minefield[i-1][j]);
                    val++;
                    minefield[i-1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j-1]);
                    val++;
                    minefield[i][j-1] = "" + val;
                }
                if(i == 0){
                    int val = Integer.parseInt(minefield[i+1][j]);
                    val++;
                    minefield[i+1][j] = "" + val;
                    val = Integer.parseInt(minefield[i][j+1]);
                    val++;
                    minefield[i][j+1] = "" + val;
                    val = Integer.parseInt(minefield[i+1][j+1]);
                    val++;
                    minefield[i+1][j+1] = "" + val;
                }
            }

if I inputed:

*.*
...
*.*

This code will output:

* 2 *
2 4 2
* 2 *

My question is: How can I get my code to "skip" adjacent mines and increment the surrounding areas? If I inputed

*.*
*..
*.*

I want it to output:

    * 3 *
    * 5 2
    * 3 *

EDIT: All of you had great answers, but I picked the top answer because it solved alot of other problems I was having which I did not mention. Thank you all for your help!

Was it helpful?

Solution

Just offering a suggestion for your mine update loop:

// You are on a mine
if(minefield[i][j].equals("*")){

    // Now go around your current position
    for(int tempRow = i-1; tempRow <= i+1; tempRow++) {
        for(int tempCol = j-1; tempCol <= j+1; tempCol++) {
            // If a valid square and not a mine
            if(tempRow >= 0 && tempRow < row &&
                tempCol >= 0 && tempCol < colm &&
                !minefield[tempRow][tempCol].equals("*")) 
            {
                // Do your update stuff
                int val = Integer.parseInt(minefield[tempRow][tempCol]);
                val++;
                minefield[tempRow][tempCol] = "" + val;
            }
        }
    }
}

This loop can scale up to larger board sizes quite a bit easier too...

Note: I assume minefield is initialized to "0" if not a mine. Otherwise there would be problems with the first parseInt.

OTHER TIPS

You might find it easier to iterate over every cell and for each cell that's not a mine count the number of mines around the cell. In other words you calculate the count for each cell all at once. This handles your question about how to deal with adjacent mines.

if(minefield[i][j] == "*"){
    // do stuff
}

I don't get it what's the big deal, hm.

EDIT

Not correct - as said in comments:

if(minefield[i][j].equals("*")){
    // do stuff, they are equal.
}

p.s. not very constructive answer, but I left it because of constructive comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top