@Vidminas & greenleaf800073
The way that people format their braces is a personal preference, there a variety of valid styles - so don't try to force your style onto others. There are lots of people who use the style you said was bad - and there is absolutely nothing wrong with it.
The important thing is that it should be consistent, most notably as long as the closing brace lines up with the beginning opening statement - that is, the for or while or if etc.
People use different styles - so one has to get used to them all.
One advantage of having the opening brace on the same line is, hopefully it will discourage a newbie from putting a semicolon there:
1 2 3 4 5 6 7 8 9 10 11 12
|
//newbies do this
if (condition);
{
//code
}
//I like this
if (condition){
//code
}
|
There has just been a whole lot of discussion about this here:
I always put braces - even of there is only 1 statement - this will save me one day when I add more code.
Sometimes a null statement in a while loop is intentional & necessary, so best practice might look like this:
1 2 3
|
while(condition){
; //null statement
}
|
A
while ago,
L B suggested that braces would be enough to signal that the null statement was intentional, I had suggested the comment. So now I am suggesting a combination of the two, to be sure, to be sure. (It's St Patrick's day 8+D )
The following is mainly directed at the OP, to present a different approach to this type of problem.
Now, one of my pet hates is conditions like this:
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
if(r==1 && i>0 && (grid[i-1][j] != 'M' & Mgrid[i-1][j]!='M' && grid[i-1][j]!='T' && grid[i-1][j]!='X')){//check whether there is monster,trap, or end of array in new location
grid[i][j]='.';
Mgrid[i-1][j]='M';//move monster to new location on temp grid
}
else if(r==2 && i<9 && (grid[i+1][j] != 'M' && Mgrid[i+1][j]!='M' && grid[i+1][j]!='T' && grid[i+1][j]!='X')){
grid[i][j]='.';
Mgrid[i+1][j]='M';
}
else if(r==3 && j<9 && (grid[i][j+1] != 'M' && Mgrid[i][j+1]!='M' && grid[i][j+1]!='T' && grid[i][j+1]!='X')){
grid[i][j]='.';
Mgrid[i][j+1]='M';
}
else if(r==4 && j>0 && (grid[i][j-1] != 'M' && Mgrid[i][j-1]!='M' && grid[i][j-1]!='T' && grid[i][j-1]!='X')){
grid[i][j]='.';
Mgrid[i][j-1]='M';
}
|
I hate them because they are not scalable and they are just plain ugly. You only have 4 things to test here - what if you had 20 or 50?
Normally things like this are best handled with switch statements, but your case is complicated by the need for an adjacency test as well.
This would be better handled by a nested for loop that checked the contents adjacent to position [Row][Col], with a switch with possibly a function call to control what happens when a favourable direction is found.
If you did this, you could move diagonally as well - without writing any extra code.
Hope all goes well.