2 hours ago, Euthyphro said:
phil67rpg, I told you before about writing if, if, if, if, if... Don't do it (unless you like torturing yourself).
Think of a way to use a for loop and an if loop together to simplify this. I usually use two if statements instead of one really long if statement. Like so:
// pseudo code
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(mouse.x > anchor.x + width*i && mouse.x < anchor.x + width*(i+1)) {
if(mouse.y > anchor.y + height*j && mouse.y < anchor.y + height*(j+1)) {
// actions at (i, j)
}
}
}
}
Like you said, we're in the realm of opinion at this point, but I'll also register a vote against the multiple 'if' statements. When I see that, I expect it to have logical significance, and have to parse further to realize it doesn't. (It seems like it could also lead the reader to wonder if there's an error in the code or something has accidentally been omitted.)
In any case, another fairly obvious solution would be to move the conditional work to a function. The code looks more or less like a point-in-axis-aligned-box containment test. Factoring that kind of stuff out can be advantageous anyway, and would leave you with something like this:
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(point_in_box(mouse, anchor, width, height, i, j)) {
// actions at (i, j)
}
}
}
Or some variation thereof. The code is (arguably) now more concise, readable, and logically clear.