Chapter 7: Enhancing your robot

Your robot needs a drink!

face Josiah Wang

So, your robot can now move to the edge of the world, and then turn 90 degrees clockwise so that it can do better things than to stare at a blank wall. Brilliant!

Now, let us do one more major update so that your robot is able to navigate all the way to a certain grid cell. Let’s say there is a bottle of your robot’s favourite Ribena drink at the bottom right-most grid cell (i.e. (9,9)). So the task is to get your robot to automatically navigate to that cell from where the robot is initally positioned so that the robot can enjoy the drink!

You can do this with an easy extension to your existing program. The navigation algorithm could be something simple like this: go forward until the robot hits the edge. Then turn 90 degrees clockwise. Repeat until your robot reaches the (9,9) cell.

Robot navigates to its Ribena

You really only need to add two additional lines with this algorithm (and if you are a perfectionist, you may also need to add another two lines so that the robot does not need to rotate once it has reached (9,9) and found its Ribena!)

Of course, you are free to come up with your own robot navigation algorithm if you are enjoying this too much! The only rules I have for now are:

  • the robot can only move forward one grid cell at each time step (definitely no teleporting!)
  • the robot can only move forward in the direction it is currently facing (North, South, East or West)! Sorry, it cannot walk backwards, sideways, or diagonally just yet!
  • the robot can rotate 90 degrees clockwise at a time

Your output should show the robot’s status at each time step.

Sample run

What is the name of the robot? Terminator
Hello. My name is Terminator. My ID is 1000.
I am currently at (1, 1), facing West. 
Moving one step forward.
I am currently at (1, 0), facing West. 
I have a wall in front of me! 
Turning 90 degrees clockwise. 
I am currently at (1, 0), facing North. 
Moving one step forward.
I am currently at (0, 0), facing North. 
I have a wall in front of me! 
Turning 90 degrees clockwise. 
I am currently at (0, 0), facing East. 
Moving one step forward.
I am currently at (0, 1), facing East.
Moving one step forward.
I am currently at (0, 2), facing East.
Moving one step forward.
I am currently at (0, 3), facing East.
Moving one step forward.
I am currently at (0, 4), facing East.
Moving one step forward.
I am currently at (0, 5), facing East.
Moving one step forward.
I am currently at (0, 6), facing East.
Moving one step forward.
I am currently at (0, 7), facing East.
Moving one step forward.
I am currently at (0, 8), facing East.
Moving one step forward.
I am currently at (0, 9), facing East.
I have a wall in front of me! 
Turning 90 degrees clockwise.
I am currently at (0, 9), facing South.
Moving one step forward.
I am currently at (1, 9), facing South.
Moving one step forward.
I am currently at (2, 9), facing South.
Moving one step forward.
I am currently at (3, 9), facing South.
Moving one step forward.
I am currently at (4, 9), facing South.
Moving one step forward.
I am currently at (5, 9), facing South.
Moving one step forward.
I am currently at (6, 9), facing South.
Moving one step forward.
I am currently at (7, 9), facing South.
Moving one step forward.
I am currently at (8, 9), facing South.
Moving one step forward.
I am currently at (9, 9), facing South.
I am drinking Ribena! I am happy!

Have fun, and remember to test your code incrementally as you code!

Also remember to commit your updated code! You know the drill!