I’m doing a task for uni where I have a robot that should not leave an area 0, 1, 2, 3, 4 by 0, 1, 2, 3, 4 on an x and y axis.
I want to check if adding a distance to x or y will not send the robot outside of the area before triggering the addition of the distance to x or y.
current code:
public void moveLeft(int distance) {
if (distance >= 1) {
if (distance + x < 0) {
distance = 0;
} else if (x > 0) {
x -= distance;
}
}
}
any tips or advice on how I might be able to check that adding distance to x or y won’t exceed the box before executing the method?
Just want to stop the robot from leaving the area
Chris is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3