public void getPossiblePositions( Position position, Node<Position> parent, int dist, int heightLimit, int jumpLimit) { if (dist <= 0) { return; } List<Position> possiblePositions = new ArrayList<Position>(); if (position.getX() > 0) { possiblePositions.addAll( getPossiblePositionsAt(position.getX() - 1, position.getZ(), heightLimit)); } if (position.getX() < getLength()) { possiblePositions.addAll( getPossiblePositionsAt(position.getX() + 1, position.getZ(), heightLimit)); } if (position.getZ() > 0) { possiblePositions.addAll( getPossiblePositionsAt(position.getX(), position.getZ() - 1, heightLimit)); } if (position.getZ() < getDepth()) { possiblePositions.addAll( getPossiblePositionsAt(position.getX(), position.getZ() + 1, heightLimit)); } for (Position possiblePosition : possiblePositions) { if (position.getY() == possiblePosition.getY()) { Node<Position> child = parent.addChild(possiblePosition, 1); if (child != null) { getPossiblePositions(possiblePosition, child, dist - 1, heightLimit, jumpLimit); } } else if (Math.abs(position.getY() - possiblePosition.getY()) <= jumpLimit) { Node<Position> child = parent.addChild(possiblePosition, 1 + jumpLimit); if (child != null) { getPossiblePositions( possiblePosition, child, dist - (1 + jumpLimit), heightLimit, jumpLimit); } } } }