One way to process the keys and move is with the following:
At this point dx and dy will both contain either zero, one, or negative one. In order for the input to be valid and the object to turn, one of the two offsets must be zero, one of the two offsets must not be zero, and a wall cannot be present at the offset location; so, we continue with:
At this point, we have completed the 'turn' part of the method; now, we can perform the 'move' part.
I think everything was quite clearly explained, except for the calculation of the degrees in turning in the 'setRotation' call. The second part on both sides of the expression (the dx*dx and dy*dy) changes any negative ones to ones, and will zero out that side if the offset was zero. The first part on both sides evaluates to the 90 degree increment, provided the offset is not zero.
public void move() { int dx=0, dy=0; // to hold the offsets if (Greenfoot.isKeyDown("up")) dy--; if (Greenfoot.isKeyDown("down")) dy++; if (Greenfoot.isKeyDown("left")) dx--; if (Greenfoot.isKeyDown("right")) dx++;
if (dx*dy == 0 && // one is zero if true dx+dy != 0 && // one is not zero if true (since one is zero) getOneObjectAtOffset(dx, dy, Wall.class) == null) // wall not at offset location if true setRotation(((1-dx)*(dx*dx)+(2-dy)*(dy*dy))*90); // turn appropriate degrees
move(1); // move forward if (getOneIntersectingObject(Wall.class) != null) move(-1); // if hit wall, move back }