All right guys, it's time for the final project of today.
And the goal is to be able to write some code here in Reeborg's world,
to be able to get our the little robot
to be able to navigate to the goal and go through a maze
no matter what the maze look like. So if you go back to Reeborgs world,
if you already got a link open..
if you don't, then simply click on the direct link in the course resources
and it should take you here.
And we've got the maze selected in the dropdown, which is right here.
Here, we have a maze. And even though the maze doesn't change,
the robot does. The direction that the robot is going to face is completely
random, the position of the robot is also random.
So you can see every time I restart the game,
it will face a different direction. So in this case, it's facing up
But the next time it might be facing down, in this case.
And it's also always going to start at a random location in the maze.
If you click on the world info,
it will tell you that the secret to getting the robot to complete the maze is to
get it to follow along the right edge of the maze.
If it starts anywhere in the maze and it just continues following along
the right wall,
it should eventually get to the final destination.
For example, like this. Now in this case,
the robot is not making a beeline straight to the exit as if it could see what
we can see, but it is following a strategy or rather what you would call in
programming, an algorithm.
And the algorithm here is to simply follow along the right edge of the wall.
And if the right side is clear,
then continue going right until it's no longer clear at which point it should
just go straight. And if you can't go straight and you can't go right,
then the last option is to turn left.
It's almost like the robot is getting a set of instructions in terms of
hierarchy. The most important telling it to turn right if it can,
if it can't turn right then go straight ahead. And finally,
if it can't turn right or go straight ahead,
then to turn left as a last resort.
So you're going to need to use a lot of the functions that you saw previously
such as move, turn_left, and testing
whether if the front is clear or the right is clear,
or there's a wall in the front or a wall in the right. And whether
if the robot is the goal. Think about how you can use a while loop
and also the conditional
if, elif, else statements to achieve this goal and get our robot
to be able to go to the final destination
no matter where it starts out and what direction it's facing.
Pause the video now
and try to use what you've learned to complete this final project.
[inaudible]
All right. So did you manage to complete this challenge? If not,
let's work through the solution together. The first thing we need to do,
which we've always pretty much needed completing each of these challenges, is we
need a way to turn right. So I'm not gonna type it all out again.
This should be already pretty clear; turn_right
is just turning left three times. Now,
the next thing we need to do is we need to tell the robot to do things and to
check for things until it reaches the goal.
We know that we have this test called at_goal.
So we can use a while loop to get the robot to continue working until it reaches
the goal. To define that, we could say while not at goal,
so basically keep repeating the instructions inside this while loop until at_goal
becomes true and not true is false.
So once this condition becomes false, then our while loop stops.
So what we were told in the world info is we should follow along the right edge.
We should test to see if the right side is clear, and if it is,
then we should turn right and go straight.
So that basically means moving right.
Let's use an if statement to test
if right_is_clear and in this instance,
so in this if block, we're going to say well turn_right
using our prebuilt function up here.
And then once you're facing the right then move forwards.
This should achieve a right move, so from here to here
for example.
Now let's test this and see what happens. In this case, the right is clear
so it moved right The right is not clear
so it doesn't know what else to do
and it's now stuck in what we would call an infinite loop.
And it will never stop until the browser actually forces it to by crashing.
So what we have to do instead is we have to provide an alternative condition;
when the right is not clear, what should it do?
So we're going to provide that condition by using an elif statement.
And what we want to say is if the right is not clear,
well, then maybe you should check if the front is clear.
And if that is the case, then simply just move forwards.
Now, this would work unless we're in the situation that we're in right now
where we're boxed in
and the only side that's clear is actually the remaining one, which is the left.
That's why we have to put in the last line of code
which is going to be an else statement. So in all other conditions, um,
when the right is not clear and the front is not clear,
so once both of these have been checked and they're false,
then actually just turn left.
So now
you can see that when our robot is able to go right,
it will try to follow along the right wall.
But when it can't go right, it will go forwards instead.
And when it can't go right or go forward, then it's gonna turn left
and hopefully the next time it checks the loop,
the front will be clear and it will be able to move forwards.
So notice how our robot is somewhat haphazardly navigating through this maze,
but it is following the algorithm that we specified,
which is to follow along the right-handed wall until you reach the goal.
And that's what these few lines of code does using our while statements, our if,
elif, else statements and also our turn_right function.
Now, while everything seems to work all right,
there are select cases where the robot might start out in a position and face a
particular direction that gets the robot into an endless loop.
So here's an example.
Let's say the robot starts off at this particular position facing down.
If we go ahead and let the robot run the code,
you can see that firstly, there's over a thousand steps in the code.
And what happens is because the robot doesn't have a wall on it's right,
it's going to keep looping through the circle because after all we're telling it
if your right side is clear,
turn right and move. And the right is always clear when it's inside this
boundary box. So depending on where the robot starts out,
it might get itself into what we call it an infinite loop,
because you can see the code just goes again and again,
through this while statement, and the robot's never going to be able to escape
from this position.
So this is one of the things that happens to your programs.
Sometimes there are edge cases which you might not have thought of
but it's only through testing the code do you discover them.
Now this particular issue I would say is more of an intermediate debugging
problem.
So if you're somebody who's already really comfortable with Python and you want
to try out debugging this particular problem,
then I've included some problem worlds for you to test your code against.
Now,
if you are somebody who is still getting to grips with Python,
I would say this is not the right time point to tackle this challenge,
and I would go ahead and make a note to yourself that once you've completed day
15 and you're firmly within the intermediate territory of Python to come back to
this problem and to continue to debug this particular edge case.
Now, if you are a beginner, I recommend to skip the rest of the video
but make a bookmark and make a note here so that you know to come back. Now,
if you are more of an intermediate programmer,
then let's go ahead and debug this.
So what you can do with Reeborg's world is you can actually create a file to load
up a particular map and robot position.
So what I've done is I've created a bunch of problem worlds for you to test your
code against that we know if you use a normal code,
it's definitely going to get into an infinite loop.
So what you need to do is head to the course resources for this lesson
and then download the zip file
which contains three problem worlds for you to test your code against.
And then in Reeborgs world, you're going to click on additional options,
open world from file and you're going to select that folder
which hopefully you've unzipped, and go through each of these problem worlds
testing your code in all of the cases. Now, when you open this up,
you can close off this box and the robot will start at a particular problem
position, facing a direction that is going to cause it to go into an
infinite loop. And you can know this ahead of time
because when you actually go to start the robot and you hit play, when you
see the number of steps here over a thousand,
it usually means that you're probably in a situation where it's going to turn into
an infinite loop.
So write your code here and then test it to make sure that the step count goes
down below 1000 and your robot doesn't get stuck in this endless circle. Now,
because the different problem worlds can have different solutions,
but what you're aiming for is one solution code that solves all of the problem
worlds,
I want you to test your code against all three of these problem worlds I've
created for you.
So here's the time, if you want to tackle this challenge, to go ahead and pause
the video and see if you can debug this pretty hard problem.
It took me a while. So I think it's going to take you a while as well.
But you might be smarter. It might take less time. So good luck,
pause the video and we'll go through the solution together a little bit later
on.
All right. So first let's try and figure out what the problem is.
The problem here is that every time when the robot's right side is clear,
our code tells it to turn to that direction, move forwards,
and then check to see if it's at the goal. So it's not, and again
the right side is clear so it turns right and moves, turns right and moves,
turns right and moves. So basically in any of these four positions,
depending on the way it's facing,
there might not be a wall on the right side of the robot.
So that is the key to solving this bug.
We have to get our robot to a starting position where it has a wall on the right
side of the robot.
Now I've been thinking about various ways of doing this and the shortest most
succinct way that's also most understandable, I think, is this.
So if we create another while loop before this while loop runs,
so remember the code goes from top to bottom,
it's going to hit this while loop first.
And we check while the front is clear,
well then the robot is not next to a wall, right? Such as in this case.
Well, if that is the case, we're going to tell our robot to just move forwards.
So this means that our robot is going to seek out a wall. So in this case,
for example,
it's going to go forwards until it reaches a wall.
So it's going to loop through that loop twice and it moves twice until it hits a
wall in front. Now this is the point which we want,
because we want to make sure that now that it's hit a wall in front,
that it's definitely got a wall on the right side of the robot. So what do we do?
Well, once it's hit a wall, so it's exited this while loop,
we're going to tell it to turn left.
So this loop makes sure that the robot has a wall in front of it.
And once it's found a wall in front, we tell it to turn left
to turn that wall on to the right side of the robot.
So now you can see we're going through this while loop. First front is clear,
so it's going to move.
And then front is still clear after that one move
so it's going to move again until it's hit a wall in front. Now,
at this point,
it exits the while loop and it goes to the next line of instruction
which tells it to turn left. Now that the robot has turned left
it now has a wall on the right side and it enters the next loop.
So in this loop, if the right side is clear, turn right and move.
If the front is clear, just simply move forward, otherwise turn left.
So as long as the robot started in a position where there is a wall on the right
side of it, right next to it, it's never going to get into that infinite loop with
this code. So this is one solution to this problem.
Now there's lots of other solutions,
but you gotta make sure that your solution works in every single one of these
test cases.
So we can go ahead and open up problem world 2 where the robot starts at a
different position, facing a different position,
and we want to make sure that still we don't have an infinite loop. In this case,
it doesn't. So that's great. And finally, let's test the last one,
which is a problem world 3,
and now we can run our program and you can see it's also less than a thousand
steps. So, it works.
So this is a bit of advanced debugging because we're now testing our
code against various terrible scenarios where the robot starts off at a position
where it's really dangerous and can get into these infinite loops. Now,
if you didn't manage to get the solution,
don't despair and don't be too hard on yourself. This is a really,
really hard one.
And it took me a long time to find a solution that I think is readable and
explainable and sort of makes sense.
Now, it might be that you should put in a note in your calendar to come and
revisit this issue in a week
once you've had some time to think about it, and to really digest and understand
the solution, then write the code yourself.
Because you would have forgotten exactly what the code was,
but you will still remember how it works. So if that is a case,
this is what you should do. If you solved it by yourself, then congratulations.
This is really, truly quite a difficult task.