I have a question about logical operators. I've written some code for Pset3, Game-of-Fifteen, as part of the "MOVE" function. This code aims to determine whether "tile" is in a "legal" position to move, which is determined by its relative position with respect to "cursor" (for those unfamiliar with the problem set, the "tile" is an int in an array of size [d],[d], which is input by the user; the "cursor" is the value of 0 within the array. There are four positions in which the "tile" can be said to be in a "legal" position with respect to the "cursor" - these are set out in the logical conditions set out on each line of my code, separated by the OR statement. Here is the relevant code:
if
(
((tile_row == cursor_row-1) && (tile_column == cursor_column))
||
((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
||
((tile_row == cursor_row)) && ((tile_column == cursor_column+1))
||
((tile_row == cursor_row+1)) && ((tile_column == cursor_column))
)
{
Do something;
}
else
{
Do some other thing;
}
When I compile this code, I get the following error:
fifteen.c:286:40: error: '&&' within '||' [-Werror,-Wlogical-op-parentheses]
((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fifteen.c:286:40: note: place parentheses around the '&&' expression to silence this warning
((tile_row == cursor_row)) && ((tile_column == cursor_column-1))
(In fact I get this repeated three times, corresponding to the 2nd, 3rd, and 4th of the logical conditions).
I am able to "solve" the problem by putting in an additional pair of parentheses around the outside of the statement on each line - in the sense that the code compiles and when run, does what I expect. But I don't understand why I'm getting the error, or why this should solve it! As far as I can see, of the four conditions is complete and surrounded by appropriate parentheses, which are then separated by the OR function. So adding an additional pair of parentheses should not change the logic. Can anyone explain what's going on here? Thanks!