private void gps(Position2D currentPosition, int currentLengthOfTheWay, List <Position2D> way) { if (currentPosition.Equals(targetPosition)) { if (shortestWay > currentLengthOfTheWay) { shortestWay = currentLengthOfTheWay; } return; } printMessage(currentLengthOfTheWay, ("I am at the position " + currentPosition.x + "; " + currentPosition.y + " Where can I move to? ")); // If we can move right if (map.getField(currentPosition.x + 1, currentPosition.y)) { currentLengthOfTheWay++; printMessage(currentLengthOfTheWay, "I can move right."); way.Add(new Position2D(currentPosition.x + 1, currentPosition.y)); gps(new Position2D(currentPosition.x + 1, currentPosition.y), currentLengthOfTheWay, way); } // If we can move left if (map.getField(currentPosition.x - 1, currentPosition.y)) { currentLengthOfTheWay++; printMessage(currentLengthOfTheWay, "I can move left."); gps(new Position2D(currentPosition.x - 1, currentPosition.y), currentLengthOfTheWay); } // If we can move down if (map.getField(currentPosition.x, currentPosition.y - 1)) { currentLengthOfTheWay++; printMessage(currentLengthOfTheWay, "I can move down."); gps(new Position2D(currentPosition.x, currentPosition.y - 1), currentLengthOfTheWay); } // If we can move up if (map.getField(currentPosition.x, currentPosition.y + 1)) { currentLengthOfTheWay++; printMessage(currentLengthOfTheWay, "I can move up."); gps(new Position2D(currentPosition.x, currentPosition.y + 1), currentLengthOfTheWay); } }
public Gps(Map map, Position2D startPosition, Position2D targetPosition) { this.map = map; this.startPosition = startPosition; this.targetPosition = targetPosition; }