All right guys, it's time for yet another coding challenge.
And this coding challenge is going to put into use what you've learned about
while loops,
while also testing some of the previous knowledge you got on functions.
So I want you to head back to Reeborg's World and change to hurdle number 3.
And take a look at the premise for this particular race.
1: Right?
0: Essentially what's happened this time is that the wall placement has become
random. So let's say we restart this game, you can see that in this case,
there's a wall here, one here, one here, one here. There's four in total.
But if we try a different version,
then you can see that there is different wall placement each time.
Not only don't we know where the wall is going to be,
we also don't know how many walls there will be.
So code such as what we did before using a for loop
telling our robot to jump six times is not going to work.
And instead, we're going to have to use the conditions
whether if front is clear or whether if there's a wall in front or whether
if we're at the goal and also their negations,
so not front is clear, not wall in front,
not at goal. See if you can use what you've learned about while loops
as well as your previous knowledge on if statements and functions to complete
this challenge. When you have written the correct code,
no matter what kind of wall situation or hurdle setup they have,
it should always be able to get to the goal.
And while I've been looking at this challenge, I know there is a very,
very simple way of bypassing this challenge,
which is simply to get the robots go up here and all the way down here,
going past all the hurdles.
But that's not the point of the challenge. In order to complete this challenge,
you must get your robot to follow the path that set out in the dotted lines
instead of bypassing it and doing something completely different
just to get to the final goal. Pause the video, have a think about this,
have a read of the world info popup here,
and then think about how you can solve this challenge. So give it a go now.
1: Right?
0: All right. So essentially, if we don't know where the walls are going to be,
then we have to test to see if there is a wall in front,
or if there is no wall in front.
And if there is a wall in front, then we're going to jump.
But if there's no wall in front, then we're just going to move forwards.
And we're going to do this and test this for as long as we're not at the goal.
So we're going to need a while loop
that's very similar to our previous while loop that I showed you in the demo. While
we're not at goal or rather while
this particular condition is not true,
then we're going to repeat and loop some lines of code.
The first thing we're going to do is we're going to use an if statement to check
whether if there is a wall in front.
So I'm going to say, if wall in front,
if this is true,
then I'm going to perform the jump function and get it to jump over the wall.
But if there is no wall in front, so namely else,
well, in this case, we're simply just going to move forwards.
And because our jump function is always going to get us to face the right way,
which is this direction here towards the goal,
then this move forward by one function is always going to take us in this
direction.
So let's take a look at this code and let's step through it step by step.
So the first thing we're going to check is are we at the goal. At_goal is going
to be false. So not false is going to be true.
So while this condition is true,
we're going to carry out all the lines of code inside.
So that means we should jump into this if statement and check
is there a wall in front? So at this point,
this should be false because there is nothing in front.
So it should bypass the if statement and it should go to the else statement.
In which case it should move forward by one step. So that's the end of one loop
and we go back to the beginning: are we at the goal yet? Nope, we are not.
But now when we run into our if statement,
this check should now come up as true,
because there is a wall right in front of our robot. So in this case,
it's going to go into the if statement and perform the jump function.
Now our jump function that we defined previously actually gets us to move
forward by one step first,
before we actually hurdle over this wall. So in this case,
if we try to do the same thing,
then it's actually going to hit our robot into a wall.
We have to modify our jump function by removing this first move function.
So instead we get it to just turn left, go up, turn right,
go right, turn right, go down and then turn left to face the right direction.
This is a pure hurdle without that extra step in the beginning.
And instead we're going to only move if there is no wall in front.
So we've changed the code.
Let's back it up and try this again with a different setup.
And in this case, you can see as my code is going through the different steps,
it's checking to see if there's a wall in front. If there is,
then it's going to jump over it. If there isn't,
it's just going to move forward by one step and then it's going to reevaluate.
So this way, once it gets to here,
it's actually going to reevaluate the while loop quite a few times
and every time it's going to be the else statement that fires.
It's going to move, move, move, move, move until it hits a wall again
and then it's going to jump over and take us to the finishing line.
Did you manage to get this correct? Did you manage to get the solution right?
If not be sure to review the previous lessons on the while loop so that you
familiarize yourself with it
and also if you got stuck on changing the jump function,
then be sure to remember that whenever you get stuck,
it's important to test your assumptions. And in this case,
you could have pressed the step-through button many times to see which line of
code is being triggered at which point,
and which is the moment where our code actually fails
so you can figure it out and fix it.
Now on the next lesson, I've got yet another code challenge for you.
So head over there and give it a go.