void nextStep() {
	int x = getX();
	int y = getY();

	if ((x + 1 < getWorldWidth()) && (y > 0)) {
		x++;
		y--;
	} else if (diag + 1 < getWorldHeight()) {
		diag++;
		y = diag;
		x = 0;
	} else {
		diag++;
		x = diag - (getWorldWidth() - 1);
		y = diag - x;
	}

	setPos(x, y);
}
void followShortestPath() {
	while (! isOverBaggle()) {

		int x = getX();
		int y = getY();

		int topValue = 9999;
		int bottomValue = 9999;
		int leftValue = 9999;
		int rightValue = 9999;

		if (! hasTopWall(x, y))
			topValue = getIndication(x, (y + getWorldHeight() - 1) % getWorldHeight());

		if (! hasBottomWall(x, y))
			bottomValue = getIndication(x, (y+1) % getWorldHeight());

		if (! hasLeftWall(x, y))
			leftValue = getIndication((x +getWorldWidth() - 1) % getWorldWidth(), y);

		if (! hasRightWall(x, y))
			rightValue = getIndication((x + 1) % getWorldWidth(), y);

		if (topValue <= bottomValue && topValue <= leftValue && topValue <= rightValue)
			setDirection(NORTH);
		else if (rightValue <= topValue && rightValue <= bottomValue && rightValue <= leftValue)
			setDirection(EAST);
		else if (leftValue <= rightValue && leftValue <= bottomValue && leftValue <= topValue)
			setDirection(WEST);
		else if (bottomValue <= topValue && bottomValue <= rightValue && bottomValue <= leftValue)
			setDirection(SOUTH);

		forward(1);
	}
	/* END SOLUTION */
}
void evaluatePaths() {
	// looking for labyrinth exit

	int x,y;
	for (x = 0; x < getWorldWidth(); x++){
		for (y = 0; y < getWorldHeight(); y++){
			if (hasBaggle(x,y)){
				setIndication(x, y, 0);
			}
		}
	}

	int changed = 1;
	while (changed) {
		changed = 0;
		for (x = 0; x < getWorldWidth(); x++) {
			for (y = 0; y < getWorldHeight(); y++) {
				int indication = getIndication(x, y);
				if (indication != 9999) {
					if (! hasBottomWall(x,y))
						changed |= setValueIfLess(x, (y + 1) % getWorldHeight(), indication + 1);

					if (! hasRightWall(x,y))
						changed |= setValueIfLess((x + 1) % getWorldWidth(), y, indication + 1);

					if (! hasTopWall(x,y))
						changed |= setValueIfLess(x, (y+getWorldHeight() - 1) % getWorldHeight(), indication + 1);

					if (! hasLeftWall(x,y))
						changed |= setValueIfLess((x +getWorldWidth() - 1) % getWorldWidth(), y, indication + 1);

				}
			}
		}
	}
}
void nextStep() {
	int x=getX();
	int y=getY();

	if (y < getWorldHeight()-1) {
		y++;
	} else {
		y = 0;
		if (x < getWorldWidth()-1) {
			x++;
		} else {
			x = 0;
		}
	}
	setPos(x,y);
}
// tools functions
int hasRightWall(int x, int y) {
	return hasLeftWall((x + 1) % getWorldWidth(), y);
}
int endingPosition() {
	return (getX() == getWorldWidth()-1) && (getY() == getWorldHeight()-1);
	/* END SOLUTION */
}