Chapter 3: A variety of robots!

A leaping robot

face Josiah Wang

Now, we have spent the last few lessons only refactoring your robot. Hopefully it has not been too boring!

Let us spend this last lesson actually adding new features to your robot.

So far, your robot can only move forward one cell at a time until it hits a wall. Then it rotates right and moves forward (one cell at a time) until it hits another wall. The process repeats until it finds its favourite drink.

Now, let us create a faster robot that can leap forward to the wall it is facing. Instead of moving one step at a time until it hits a wall, it just jumps forward to the wall. It will then turn right, and leap forward again, until it finds its drink.

Step 1: Define your leaping robot

Create a LeapingRobot class. Since this is a special kind of robot, it should be a subclass of your normal Robot. You can choose to define this class inside robot.py or in a different file. For simplicity, I will define it in robot.py in my implementation. If you choose to do it in a separate file, you will have to import the Robot class appropriately.

Step 2: Create your leaping robot!

Although your LeapingRobot cannot leap yet, it is already a fully functioning robot. Let’s just create some LeapingRobot instances so that you can easily test your implementation later.

Your RobotFactory class is in charge of manufacturing your robot. So modify its create_robot() method (or equivalent) to create some LeapingRobot. You can just create all LeapingRobots, or even better randomly create either a LeapingRobot or a standard Robot. Because we all appreciate some diversity!

When you run your program, you should still see the same output as before.