Skip to main content Skip to navigation

Hint 3

We have already written the methods that will move the robot North, South, East and West in EX1. This part of the task is about determining when we will use those methods.

The easiest way is to say 25% of the time I will move North, 25% of the time I will move South, 25% of the time I will move East, and 25% of the time I will move West. This is not a very clever method but it will work.

To implememt this we use random(1) to generate a random number between 0 and 1:

float r = random(1);

We then use the output of this alongside an if-else statement

if (r<0.25) {
robot.moveNorth();
} else if (r<0.5) {
robot.moveSouth();
} else if (r<0.75) {
robot.moveEast();
} else {
robot.moveWest();
}

How can you improve this?