This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: Incremental development

Randomising the robot

face Josiah Wang

Finally, let us do one more enhancement to your robot project.

This time, we will try to avoid asking the user to give an initial position and direction for the robot.

Instead, automate this by getting your program to randomly place the robot in one of the cells.

The program should also generate a random initial direction for the robot.

You can use the random module provided by Python to generate the random initial position and direction for the robot. Try to find the most suitable function(s) to do this.

Rerun your program several times to test that it still gives the correct output.

Sample run #1

What is the name of robot? Bender
Hello. My name is Bender. My ID is 1000.
My current location is (2, 5). I am in the top right quadrant.
I am facing West.
Moving one step forward.
My current location is (2, 4). I am in the top left quadrant.

Again, remember to commit your randomised robot to your repo once you are done!

user@MACHINE:~/robot$ git add -u
user@MACHINE:~/robot$ git commit -m "Automated robot initialisation by randomising the robot's initial coordinates and direction."