Search code examples
javamatrixmultidimensional-arrayarraysminesweeper

Programming challenges Minesweeper help: How to check for a "*" in a 2D string array?


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!


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.