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


i'm trying write minesweeper program programming challenges website , having trouble detecting if element in 2d array mine , incrementing surrounding areas. here code checks mine in corners of array , increments surrounding areas

  public void constructpointers(){     int counter = 0;     for(int = 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 inputed:

*.* ... *.* 

this code output:

* 2 * 2 4 2 * 2 * 

my question is: how can code "skip" adjacent mines , increment surrounding areas? if inputed

*.* *.. *.* 

i want output:

    * 3 *     * 5 2     * 3 * 

edit: of had great answers, picked top answer because solved alot of other problems having did not mention. thank help!

just offering suggestion mine update loop:

// on mine if(minefield[i][j].equals("*")){      // go around current position     for(int temprow = i-1; temprow <= i+1; temprow++) {         for(int tempcol = j-1; tempcol <= j+1; tempcol++) {             // if valid square , not mine             if(temprow >= 0 && temprow < row &&                 tempcol >= 0 && tempcol < colm &&                 !minefield[temprow][tempcol].equals("*"))              {                 // update stuff                 int val = integer.parseint(minefield[temprow][tempcol]);                 val++;                 minefield[temprow][tempcol] = "" + val;             }         }     } } 

this loop can scale larger board sizes quite bit easier too...

note: assume minefield initialized "0" if not mine. otherwise there problems first parseint.


Comments