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

Chapter 9: Incremental development

Robot with a direction

face Josiah Wang

As you start writing more complex programs, remember to “test as you code”! Try to start applying this principle while working on your programming projects from now on.

Let us now evolve your robot project even further.

Now, other than a position, your robot will also be facing one of four possible directions at any one time: North, South, East and West.

The robot can face one of four directions at any one time

Please update main.py of your robot project to additionally read in a direction from the user (‘n’, ‘s’, ‘e’, ‘w’). For simplicity, you can assume that the user will enter only these four characters.

The program will print out what it has printed before.

Additionally, it will print out its current direction.

The robot will then move one step forward in the direction it is currently facing, and again report its current coordinates and quadrant.

The robot will move one step forward in the direction it is facing

Remember that the robot should only be able to navigate inside the N\,\times\,N grid. It should not move forward if it is at the edge on the grid. Make sure you test such cases, and make sure that the robot does not go over the edge.

And remember, test as you code! Develop your code incrementally (implement the direction bit first, make sure it works, and then implement the moving one step forward part)! Make sure your code always works! And make sure whatever you have just coded always works correctly as expected, before moving on to your next step.

Sample run #1

What is the name of the robot? Bender
What is its current row coordinate? 5
What is its current column coordinate? 2
What is its initial direction [n|s|e|w]? n
Hello. My name is Bender. My ID is 1000.
My current location is (5, 2). I am in the bottom left quadrant.
I am facing North.
Moving one step forward.
My current location is (4, 2). I am in the top left quadrant.

Sample run #2

What is the name of the robot? Bender
What is its current row coordinate? 3
What is its current column coordinate? 20
What is its initial direction [n|s|e|w]? e
Hello. My name is Bender. My ID is 1000.
My current location is (3, 9). I am in the top right quadrant.
I am facing East.
Moving one step forward.
My current location is (3, 9). I am in the top right quadrant.

Again, once you are happy, remember to add and commit your latest awesome robot project to your Git repo.

user@MACHINE:~/robot$ git add -u
user@MACHINE:~/robot$ git commit -m "Improved robot in main.py by allowing robot to also have a direction, initialised by the user. The robot will move one step forward in the direction it is facing."