\$\begingroup\$
\$\endgroup\$
1
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
-
1\$\begingroup\$ Related question: Neighbors of a matrix element \$\endgroup\$200_success– 200_success2013年10月27日 08:11:33 +00:00Commented Oct 27, 2013 at 8:11
1 Answer 1
\$\begingroup\$
\$\endgroup\$
6
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
-
\$\begingroup\$ Beutiful thank you very much, Do you know any good sites/articles/books about code 'elegance'? \$\endgroup\$Babbleshack– Babbleshack2013年10月26日 21:25:14 +00:00Commented Oct 26, 2013 at 21:25
-
\$\begingroup\$ Glad I could help. No, unfortunately I don't know of any such resources. \$\endgroup\$Bernhard Barker– Bernhard Barker2013年10月26日 21:29:21 +00:00Commented Oct 26, 2013 at 21:29
-
1\$\begingroup\$ @Babbleshack Clean Code by Robert C. Martin: amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/… \$\endgroup\$Mathieu Guindon– Mathieu Guindon2013年10月27日 00:53:02 +00:00Commented 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\$200_success– 200_success2013年10月27日 08:18:58 +00:00Commented 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\$Bernhard Barker– Bernhard Barker2013年10月27日 21:33:38 +00:00Commented Oct 27, 2013 at 21:33
lang-java