Minesweeper, Bombcount method I have a method which checks all of its surrounding squares and returns the number of bombs around it. It uses a long list of if statements, which is pretty ugly and probably inefficient.

       public int countArround(){
    int bombNum = 0;
    //x=0 y=1 works
    if(((SmartSquare)board.getSquareAt(xLocation, yLocation+1)!=null)&amp;&amp;
        (((SmartSquare)board.getSquareAt(xLocation, yLocation+1)).getBomb())){
        bombNum++;
    }
    //x=1 y=1
    if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation+1)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation +1, yLocation+1)).getBomb()){
        bombNum++;
    }
    //x=1 y=0 works
    if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation+1, yLocation)).getBomb()){
        bombNum++;
    }
    //x=1, y=-1 works
    if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation-1)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation+1, yLocation-1)).getBomb()){
        bombNum++;
    }
    //x=0 y=-1
    if(((SmartSquare)board.getSquareAt(xLocation, yLocation-1)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation, yLocation-1)).getBomb()){
        bombNum++;
    } 
    //x=-1, y=-1 works
    if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation-1)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation-1, yLocation-1)).getBomb()){
        bombNum++;
    } 
    //x=-1, y=0 works
    if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation-1, yLocation)).getBomb()){
        bombNum++;
    }
    //x=-1 y=1 works
    if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation+1)!=null)&amp;&amp;
        ((SmartSquare)board.getSquareAt(xLocation-1, yLocation+1)).getBomb()){
        bombNum++;
    }
    return bombNum;


I was wondering how this can be cleaned up. I cannot seem to get it into an if/while/for statement.
A double-for-loop over x and y seems appropriate.

Also, it seems like a good idea to assign (SmartSquare)board.getSquareAt(...) to a variable.

int bombNum = 0;
for    (int xOffset = -1; xOffset &lt;= 1; xOffset++)
   for (int yOffset = -1; yOffset &lt;= 1; yOffset++)
   {
      if (xOffset == 0 &amp;&amp; yOffset == 0)
      {
         continue;
      }
      SmartSquare neighbour = (SmartSquare)board.getSquareAt(xLocation+xOffset, yLocation+yOffset);
      if (neighbour != null &amp;&amp; neighbour.getBomb())
      {
         bombNum++;
      }
   }
return bombNum;