Lesson 10
I am Your Father
Chapter 3: A variety of robots!
Make your robot leap!
At this point, you are ready to make your robot leap!
Step 3: Make your leaping robot leap forward!
You can now make your LeapingRobot
leap! Define a new method called leap_forward()
for this new subclass. The method is similar to step_forward()
, except that instead of moving one step ahead, your robot just go straight to the edge.
You robot will need to know the maximum grid size to leap to the edge. It should have this information either from the grid
attribute (if you have refactored it towards the end of Lesson 9), or by passing grid_size
as an input argument.
You may also need a report_leap()
method to announce that the robot has made a leap. This should be equivalent to its report_step()
counterpart. Mine prints out "Leaping forward to the edge."
Step 4: Make your leaping robot different from other robots!
You should then override the move_forward_to_wall()
method of the parent Robot
class (or equivalent). Instead of moving one step forward and then reporting its current location until it hits a wall, LeapingRobot
will simply jump straight to the edge (with leap_forward()
) and report its location (with report_leap()
). I’ve basically given you the solution for this step! 😆
Sample run
If done correctly, there is nothing else to update in your main code. Since your LeapingRobot
is a type of Robot
, it can do whatever a standard Robot
can do. It just move_forward_to_wall()
differently - but the main code does not even need to know. Feel the power of inheritance!
My sample run below generated two LeapingRobot
s and one standard Robot
.
Hello. My name is Astro Boy. My ID is 1001.
Hello. My name is Wall-E. My ID is 1002.
Hello. My name is Mega Man. My ID is 1003.
Astro Boy is searching for its drink.
I am currently at (4, 4), facing East.
Leaping forward to the edge.
I am currently at (4, 9), facing East.
I have a wall in front of me!
Turning 90 degrees clockwise.
I am currently at (4, 9), facing South.
Leaping forward to the edge.
I am currently at (9, 9), facing South.
I have a wall in front of me!
Turning 90 degrees clockwise.
I am currently at (9, 9), facing West.
Leaping forward to the edge.
I am currently at (9, 0), facing West.
I am drinking J2O! I am happy!
Wall-E is searching for its drink.
I am currently at (7, 2), facing West.
Leaping forward to the edge.
I am currently at (7, 0), facing West.
I have a wall in front of me!
Turning 90 degrees clockwise.
I am currently at (7, 0), facing North.
Leaping forward to the edge.
I am currently at (0, 0), facing North.
I am drinking Fanta! I am happy!
Mega Man is searching for its drink.
I am currently at (7, 7), facing South.
Moving one step forward.
I am currently at (8, 7), facing South.
Moving one step forward.
I am currently at (9, 7), facing South.
I have a wall in front of me!
Turning 90 degrees clockwise.
I am currently at (9, 7), facing West.
Moving one step forward.
I am currently at (9, 6), facing West.
Moving one step forward.
I am currently at (9, 5), facing West.
Moving one step forward.
I am currently at (9, 4), facing West.
Moving one step forward.
I am currently at (9, 3), facing West.
Moving one step forward.
I am currently at (9, 2), facing West.
Moving one step forward.
I am currently at (9, 1), facing West.
Moving one step forward.
I am currently at (9, 0), facing West.
I have a wall in front of me!
Turning 90 degrees clockwise.
I am currently at (9, 0), facing North.
Moving one step forward.
I am currently at (8, 0), facing North.
Moving one step forward.
I am currently at (7, 0), facing North.
Moving one step forward.
I am currently at (6, 0), facing North.
Moving one step forward.
I am currently at (5, 0), facing North.
Moving one step forward.
I am currently at (4, 0), facing North.
Moving one step forward.
I am currently at (3, 0), facing North.
Moving one step forward.
I am currently at (2, 0), facing North.
Moving one step forward.
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 Lucozade! I am happy!