Ouch!
Using == true is only one of the problems with that code.
Never use == true or == false which are poor style and also error‑prone.
Not
if (b == true) ... or
if (b == false) ...
Use
if (b) ... or
if (!b) ...
The reason the bottom ifs are being called is that their conditions are fulfilled. You have poorly‑formatted code without any spacing (look
here for some suggestions) and the lines are
too long, so they are difficult to read. You also have some confusing conditions. There are instances of −1 appearing (which you should write as − 1), as well as = combined with < or >. All those things make it much harder to understand what your conditions are. Always try to use
if (b) ... rather than
if (!b) ... if possible. Simply because it is easier to read. Similarly if you can use > rather than >= and < in preference to <= your code will be easier to understand. So your −1 and your <= will cause the last if to be called even when you have a 0 because that is the condition which is actually true.
I think you should not use numbers to represent those directions. Create yourself an
enumerated type for directions
You can probably encode the movement into the enum constants which is a nice object‑oriented way to do things.