2
\$\begingroup\$

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)&&
 (((SmartSquare)board.getSquareAt(xLocation, yLocation+1)).getBomb())){
 bombNum++;
 }
 //x=1 y=1
 if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation+1)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation +1, yLocation+1)).getBomb()){
 bombNum++;
 }
 //x=1 y=0 works
 if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation+1, yLocation)).getBomb()){
 bombNum++;
 }
 //x=1, y=-1 works
 if(((SmartSquare)board.getSquareAt(xLocation+1, yLocation-1)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation+1, yLocation-1)).getBomb()){
 bombNum++;
 }
 //x=0 y=-1
 if(((SmartSquare)board.getSquareAt(xLocation, yLocation-1)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation, yLocation-1)).getBomb()){
 bombNum++;
 } 
 //x=-1, y=-1 works
 if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation-1)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation-1, yLocation-1)).getBomb()){
 bombNum++;
 } 
 //x=-1, y=0 works
 if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation)!=null)&&
 ((SmartSquare)board.getSquareAt(xLocation-1, yLocation)).getBomb()){
 bombNum++;
 }
 //x=-1 y=1 works
 if(((SmartSquare)board.getSquareAt(xLocation-1, yLocation+1)!=null)&&
 ((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.

Simon Forsberg
59.7k9 gold badges157 silver badges311 bronze badges
asked Oct 26, 2013 at 20:48
\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

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 <= 1; xOffset++)
 for (int yOffset = -1; yOffset <= 1; yOffset++)
 {
 if (xOffset == 0 && yOffset == 0)
 {
 continue;
 }
 SmartSquare neighbour = (SmartSquare)board.getSquareAt(xLocation+xOffset, yLocation+yOffset);
 if (neighbour != null && neighbour.getBomb())
 {
 bombNum++;
 }
 }
return bombNum;
answered Oct 26, 2013 at 21:17
\$\endgroup\$
6
  • \$\begingroup\$ Beutiful thank you very much, Do you know any good sites/articles/books about code 'elegance'? \$\endgroup\$ Commented Oct 26, 2013 at 21:25
  • \$\begingroup\$ Glad I could help. No, unfortunately I don't know of any such resources. \$\endgroup\$ Commented Oct 26, 2013 at 21:29
  • 1
    \$\begingroup\$ @Babbleshack Clean Code by Robert C. Martin: amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/… \$\endgroup\$ Commented Oct 27, 2013 at 0:53
  • 3
    \$\begingroup\$ I find this non-indentation to be a sneaky way to disguise the nested for-loop. Indent the inner loop! If you really want the characters in your code to line up vertically, put extra spaces between the "for" and left parenthesis of the outer for-loop. \$\endgroup\$ Commented Oct 27, 2013 at 8:18
  • \$\begingroup\$ @200_success Edited (not that I particularly agree - I find this to be unnecessary over-indentation). I assume what I did is what you meant by the extra spaces note. \$\endgroup\$ Commented Oct 27, 2013 at 21:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.