public class TeleportBug extends Bug{ public TeleportBug(int dir) { if(dir % 45 == 0) setDirection(dir/45); } public void act() { if( canMove()) move(); else { ArrayList locs = new ArrayList(); for(int r = 0; r < 10; r++) for(int c = 0; c < 10; c++) { Location loc = new Location(r, c); Location nextMove = loc.getAdjacentLocation(getDirection()); if(getGrid().isValid(nextMove)&& getGrid().get(loc)==null && getGrid().get(nextMove)==null) { locs.add(loc); } } int r = (int)(Math.random()*locs.size()); moveTo(locs.get(r)); } } }