All right guys, it's time for the final hurdle
before we get to our goal post. In this lesson,
I want you to switch over to the final hurdle challenge, which is hurdle 4.
And in this particular version of the hurdle,
the wall that the robot has to jump over to now has a variable height.
So the height of each of these hurdles is completely random,
the position of the hurdles are random
and also the number of hurdles is also going to be random.
You can see that every single time that this regenerates,
it comes up with a completely different set of hurdle profiles.
And now we can't just jump because we don't know what the height will be each
time.
So we have to use what we've learned about while loops to modify this jump
function and see if we can get the jump to jump as high as necessary
depending on the height of these walls. Just as a reminder,
in order to complete this challenge,
you should take a look at the Reeborg's keyboard window and go to this tab called
conditions and see that you can check if the front of the robot is clear,
if it's right is clear, if there's a wall on the front,
if there's a wall on the right and various other things.
The ones that are probably going to be the most useful is this top row here.
So I want you to take a look at these conditions,
see how they're spelt and how you can use them,
and then try to use them and test them out using your Reeborg robot.
Now, the goal is of course
to reach this final flag going over various hurdles
using this dotted line as the path, which is going to be different each time.
Just as a head up, this is going to require you to modify this jump function.
Have a serious think about it because it's not easy
and you're going to need to use more while loops to achieve this end outcome. And
try to see if you can solve this challenge in the least lines of code possible.
So I managed to do it in as little as 24 lines of code,
so see if you can aim to achieve that.
Pause the video now and give this challenge a go.
Right?
All right.
So the key difference between this particular hurdle and the previous version is
that the height of the wall is now random. Instead of just jumping over one
square, where we turn left, move, turn right, move, turn right move,
we have to now see if the right side has a wall,
or if it's completely clear. Now, in this case,
what I want to happen is I want to turn my robot to the left first of all,
and then I want to check if there is a wall on the right.
So I can use this condition wall on right to achieve this.
And if that is true,
then I want to continue moving forward until there is no longer a wall on the
right, in which case I'm going to turn right turn right
and then start going down. So let's modify this jump function.
We definitely need the robot turn left, but after it turns left,
we're going to use a while loop to say that while there is a wall on the right
then in this case, we're going to move forwards.
So we're going to go up in this direction basically
because we've turned left already.
Let's test this out and let's run this code.
You can see in this version of events,
my robot is going to keep going until the very end of the screen.
This is not what we want.
So what we want to do is when this is no longer true,
it's going to skip to line 10. So at this point,
there is no longer a wall on the right,
because otherwise it would have continued looping and calling this line 9
where it gets it to move forward. When there's no longer a wall on the right,
then we want to do what we did previously
which is to get our robot to turn right move,
and then turn right. Now at this point, we have to move down the wall.
And what we're going to check is whether if the front is clear,
because it's only when we reach the very bottom here, when the front is blocked,
do we actually turn left again and continue back to our while loop here to move
forward. To do this we're going to add yet another while loop.
And in this case, we're going to use the front_is_clear condition.
While the front is clear,
we're going to get the robot to continue moving forward.
So once it's turned to face this direction, and there's no wall in front,
it's going to keep going until this is no longer true.
And that is the end of our jump.
So now we've basically modified this jump function to take into account all of
these new things such as while there's a wall on the right
then continue moving upwards, while the front is clear when it's coming down
then continue moving downwards. And then at the very end,
we're going to get our robot to turn left so that we're ready to go and check if
we're at the goal. And if we're not at the goal,
then we're going to check if there's a wall in front,
if there is a wall in front that we're going to jump,
otherwise we're going to continue moving forwards or rather to the right to get
to the goal. So now this is the final code.
And depending on the number of spaces you've added in here,
obviously it's a little bit easier to read if you actually have it like this.
But depending on how you've spaced things out in your code,
you should end up with anywhere between say 20 to 24 lines of code.
So I want you to be able to see all of these lines of code on the right when we
run this code.
And I've actually got a typo in here that it's going to tell me.
So it's going to say invalid syntax
and the problem is just because I forgot a colon here. If you get that
pop-up invalid syntax
be sure to check if you have any problems in here and it can get a little bit
complicated if you leave your code for a long period of time before you test it.
So test your code after every little iteration
and then when you actually get something like an invalid syntax,
you'll be able to catch it more easily.
So now let's go ahead and regenerate our map and let's try it again.
So when we're going to make a jump and then we're gonna turn right,
and we're going to keep going down until we hit the wall and we turned left,
and now we're not out the goal, and there's still a wall in front.
So we make another jump and we go down this much, much larger wall.
And then we continue this process of using several while loops,
checking if there's a wall on the right, checking if the front is clear,
checking
if there's a wall in the front in order to achieve our goal and get to our final
destination. Now for your code to successfully complete this challenge,
the line that your robot draws should follow the line that started here,
just like what you see here.
So instead of going just over the top and getting to the final goal,
it should follow each of these steps and finally
you should get that green pop-up where it says it's at the right position.
So did you manage to complete this challenge?
If not, be sure to review the lesson on while loops and just have a play around
with while loops.
Try some of the different conditions and see what happens when you run the code
and see how the robot behaves so that you get a good grasp of how these things
actually work.
Now on the next lesson is our final project and we're going to get our robot to
be able to navigate a maze all by itself using what we've learned
including functions as well as while loops, as well as using good indentation.
So for all of that and more, I'll see you on the next lesson.