never mind solved it
// using instance fields private int curDir; private int nxtDir; // with this code int dx = 0, dy = 0; if (Greenfoot.isKeyDown("right")) dx++; if (Greenfoot.isKeyDown("left")) dx--; if (Greenfoot.isKeyDown("down")) dy++; if (Greenfoot.isKeyDown("up")) dy--; if (dx * dy == 0 && dx + dy != 0) nxtDir = (1-dx)*(dx*dx)+(2-dy)*(dy*dy); if (canMove(nxtDir)) curDir = nxtDir; if (canMove(curDir)) moveOn(curDir); // with methods private boolean canMove(int dir) { setRotation(90*dir); move(2); boolean noObjs = getIntersectingObjects(Wall.class).isEmpty(); move(-2); setRotation(0); return noObjs; } private void moveOn(int dir) { setRotation(90*dir); move(2); setRotation(0); }
if (moves(nxtDir)) curDir = nxtDir; else moves(curDir); // with just the one method private boolean moves(int dir) { setRotation(90*dir); move(2); boolean noObjs = getIntersectingObjects(Wall.class).isEmpty(); if (!noObjs) move(-2); setRotation(0); return noObjs; }