WEBVTT

0
00:00.180 --> 00:04.230
All right guys, it's time for the final project of today.

1
00:04.860 --> 00:09.860
And the goal is to be able to write some code here in Reeborg's world,

2
00:10.590 --> 00:13.320
to be able to get our the little robot

3
00:13.350 --> 00:17.730
to be able to navigate to the goal and go through a maze

4
00:18.000 --> 00:22.230
no matter what the maze look like. So if you go back to Reeborgs world,

5
00:22.230 --> 00:23.640
if you already got a link open..

6
00:23.910 --> 00:28.080
if you don't, then simply click on the direct link in the course resources

7
00:28.380 --> 00:29.760
and it should take you here.

8
00:30.180 --> 00:34.770
And we've got the maze selected in the dropdown, which is right here.

9
00:36.150 --> 00:41.130
Here, we have a maze. And even though the maze doesn't change,

10
00:41.520 --> 00:46.520
the robot does. The direction that the robot is going to face is completely

11
00:46.920 --> 00:51.390
random, the position of the robot is also random.

12
00:51.420 --> 00:54.180
So you can see every time I restart the game,

13
00:54.600 --> 00:58.800
it will face a different direction. So in this case, it's facing up

14
00:59.160 --> 01:02.730
But the next time it might be facing down, in this case.

15
01:03.210 --> 01:08.130
And it's also always going to start at a random location in the maze.

16
01:08.640 --> 01:10.380
If you click on the world info,

17
01:10.590 --> 01:15.240
it will tell you that the secret to getting the robot to complete the maze is to

18
01:15.240 --> 01:18.540
get it to follow along the right edge of the maze.

19
01:19.110 --> 01:23.520
If it starts anywhere in the maze and it just continues following along

20
01:23.520 --> 01:24.510
the right wall,

21
01:24.810 --> 01:28.770
it should eventually get to the final destination.

22
01:29.340 --> 01:33.060
For example, like this. Now in this case,

23
01:33.090 --> 01:38.090
the robot is not making a beeline straight to the exit as if it could see what

24
01:38.940 --> 01:43.890
we can see, but it is following a strategy or rather what you would call in

25
01:43.890 --> 01:45.600
programming, an algorithm.

26
01:46.020 --> 01:50.760
And the algorithm here is to simply follow along the right edge of the wall.

27
01:51.360 --> 01:52.980
And if the right side is clear,

28
01:53.010 --> 01:58.010
then continue going right until it's no longer clear at which point it should

29
01:59.010 --> 02:03.900
just go straight. And if you can't go straight and you can't go right,

30
02:04.230 --> 02:07.710
then the last option is to turn left.

31
02:08.160 --> 02:12.480
It's almost like the robot is getting a set of instructions in terms of

32
02:12.480 --> 02:17.040
hierarchy. The most important telling it to turn right if it can,

33
02:17.430 --> 02:20.520
if it can't turn right then go straight ahead. And finally,

34
02:20.520 --> 02:22.770
if it can't turn right or go straight ahead,

35
02:23.070 --> 02:25.920
then to turn left as a last resort.

36
02:26.400 --> 02:30.120
So you're going to need to use a lot of the functions that you saw previously

37
02:30.120 --> 02:33.240
such as move, turn_left, and testing

38
02:33.240 --> 02:36.270
whether if the front is clear or the right is clear,

39
02:36.660 --> 02:40.710
or there's a wall in the front or a wall in the right. And whether

40
02:40.710 --> 02:45.300
if the robot is the goal. Think about how you can use a while loop

41
02:45.660 --> 02:47.370
and also the conditional

42
02:47.400 --> 02:51.990
if, elif, else statements to achieve this goal and get our robot

43
02:52.140 --> 02:54.660
to be able to go to the final destination

44
02:54.990 --> 02:59.160
no matter where it starts out and what direction it's facing.

45
02:59.830 --> 03:00.820
Pause the video now

46
03:00.850 --> 03:03.970
and try to use what you've learned to complete this final project.

47
03:04.410 --> 03:09.410
<v 1>[inaudible]</v>

48
03:10.620 --> 03:14.700
<v 0>All right. So did you manage to complete this challenge? If not,</v>

49
03:14.910 --> 03:18.870
let's work through the solution together. The first thing we need to do,

50
03:19.050 --> 03:23.880
which we've always pretty much needed completing each of these challenges, is we

51
03:23.880 --> 03:27.180
need a way to turn right. So I'm not gonna type it all out again.

52
03:27.480 --> 03:29.640
This should be already pretty clear; turn_right

53
03:29.640 --> 03:32.280
is just turning left three times. Now,

54
03:32.280 --> 03:36.960
the next thing we need to do is we need to tell the robot to do things and to

55
03:36.960 --> 03:40.500
check for things until it reaches the goal.

56
03:40.890 --> 03:44.310
We know that we have this test called at_goal.

57
03:44.700 --> 03:49.700
So we can use a while loop to get the robot to continue working until it reaches

58
03:50.880 --> 03:55.880
the goal. To define that, we could say while not at goal,

59
03:57.270 --> 04:02.270
so basically keep repeating the instructions inside this while loop until at_goal

60
04:03.510 --> 04:07.050
becomes true and not true is false.

61
04:07.230 --> 04:11.100
So once this condition becomes false, then our while loop stops.

62
04:12.090 --> 04:17.090
So what we were told in the world info is we should follow along the right edge.

63
04:17.520 --> 04:22.520
We should test to see if the right side is clear, and if it is,

64
04:22.920 --> 04:25.650
then we should turn right and go straight.

65
04:25.830 --> 04:28.470
So that basically means moving right.

66
04:28.950 --> 04:30.870
Let's use an if statement to test

67
04:30.900 --> 04:35.900
if right_is_clear and in this instance,

68
04:36.390 --> 04:40.890
so in this if block, we're going to say well turn_right

69
04:40.920 --> 04:43.530
using our prebuilt function up here.

70
04:44.250 --> 04:49.020
And then once you're facing the right then move forwards.

71
04:49.680 --> 04:54.090
This should achieve a right move, so from here to here

72
04:54.090 --> 04:54.923
for example.

73
04:55.650 --> 05:00.030
Now let's test this and see what happens. In this case, the right is clear

74
05:00.030 --> 05:03.270
so it moved right The right is not clear

75
05:03.330 --> 05:05.160
so it doesn't know what else to do

76
05:05.160 --> 05:09.270
and it's now stuck in what we would call an infinite loop.

77
05:11.010 --> 05:16.010
And it will never stop until the browser actually forces it to by crashing.

78
05:16.860 --> 05:21.860
So what we have to do instead is we have to provide an alternative condition;

79
05:22.770 --> 05:25.860
when the right is not clear, what should it do?

80
05:26.460 --> 05:29.790
So we're going to provide that condition by using an elif statement.

81
05:30.300 --> 05:34.650
And what we want to say is if the right is not clear,

82
05:35.460 --> 05:39.150
well, then maybe you should check if the front is clear.

83
05:41.460 --> 05:45.450
And if that is the case, then simply just move forwards.

84
05:46.410 --> 05:50.970
Now, this would work unless we're in the situation that we're in right now

85
05:51.030 --> 05:52.260
where we're boxed in

86
05:52.620 --> 05:56.820
and the only side that's clear is actually the remaining one, which is the left.

87
05:58.040 --> 06:00.590
That's why we have to put in the last line of code

88
06:00.950 --> 06:05.900
which is going to be an else statement. So in all other conditions, um,

89
06:05.930 --> 06:09.170
when the right is not clear and the front is not clear,

90
06:09.380 --> 06:12.230
so once both of these have been checked and they're false,

91
06:12.590 --> 06:15.110
then actually just turn left.

92
06:16.730 --> 06:17.540
<v 2>So now</v>

93
06:17.540 --> 06:20.300
<v 0>you can see that when our robot is able to go right,</v>

94
06:20.540 --> 06:23.420
it will try to follow along the right wall.

95
06:24.110 --> 06:28.500
But when it can't go right, it will go forwards instead.

96
06:28.500 --> 06:32.540
And when it can't go right or go forward, then it's gonna turn left

97
06:32.810 --> 06:35.600
and hopefully the next time it checks the loop,

98
06:35.900 --> 06:39.380
the front will be clear and it will be able to move forwards.

99
06:40.010 --> 06:45.010
So notice how our robot is somewhat haphazardly navigating through this maze,

100
06:45.980 --> 06:49.550
but it is following the algorithm that we specified,

101
06:49.580 --> 06:54.580
which is to follow along the right-handed wall until you reach the goal.

102
06:56.180 --> 07:01.180
And that's what these few lines of code does using our while statements, our if,

103
07:01.490 --> 07:05.360
elif, else statements and also our turn_right function.

104
07:06.260 --> 07:09.050
Now, while everything seems to work all right,

105
07:09.440 --> 07:14.440
there are select cases where the robot might start out in a position and face a

106
07:16.490 --> 07:21.200
particular direction that gets the robot into an endless loop.

107
07:21.770 --> 07:23.360
So here's an example.

108
07:23.720 --> 07:28.310
Let's say the robot starts off at this particular position facing down.

109
07:28.850 --> 07:32.270
If we go ahead and let the robot run the code,

110
07:32.720 --> 07:37.250
you can see that firstly, there's over a thousand steps in the code.

111
07:37.850 --> 07:42.740
And what happens is because the robot doesn't have a wall on it's right,

112
07:43.160 --> 07:48.160
it's going to keep looping through the circle because after all we're telling it

113
07:49.520 --> 07:50.930
if your right side is clear,

114
07:50.960 --> 07:55.910
turn right and move. And the right is always clear when it's inside this

115
07:56.000 --> 08:00.560
boundary box. So depending on where the robot starts out,

116
08:00.860 --> 08:04.940
it might get itself into what we call it an infinite loop,

117
08:04.970 --> 08:08.180
because you can see the code just goes again and again,

118
08:08.240 --> 08:12.950
through this while statement, and the robot's never going to be able to escape

119
08:13.220 --> 08:14.210
from this position.

120
08:15.050 --> 08:20.050
So this is one of the things that happens to your programs.

121
08:20.540 --> 08:24.650
Sometimes there are edge cases which you might not have thought of

122
08:25.010 --> 08:28.040
but it's only through testing the code do you discover them.

123
08:28.940 --> 08:33.940
Now this particular issue I would say is more of an intermediate debugging

124
08:34.940 --> 08:35.773
problem.

125
08:36.170 --> 08:41.170
So if you're somebody who's already really comfortable with Python and you want

126
08:42.260 --> 08:46.190
to try out debugging this particular problem,

127
08:46.670 --> 08:51.670
then I've included some problem worlds for you to test your code against.

128
08:54.530 --> 08:54.800
Now,

129
08:54.800 --> 08:59.610
if you are somebody who is still getting to grips with Python,

130
08:59.640 --> 09:03.600
I would say this is not the right time point to tackle this challenge,

131
09:04.020 --> 09:09.020
and I would go ahead and make a note to yourself that once you've completed day

132
09:09.330 --> 09:14.330
15 and you're firmly within the intermediate territory of Python to come back to

133
09:16.260 --> 09:21.260
this problem and to continue to debug this particular edge case.

134
09:22.350 --> 09:26.820
Now, if you are a beginner, I recommend to skip the rest of the video

135
09:27.060 --> 09:31.680
but make a bookmark and make a note here so that you know to come back. Now,

136
09:31.710 --> 09:33.990
if you are more of an intermediate programmer,

137
09:34.350 --> 09:36.780
then let's go ahead and debug this.

138
09:37.590 --> 09:42.590
So what you can do with Reeborg's world is you can actually create a file to load

139
09:43.650 --> 09:46.530
up a particular map and robot position.

140
09:47.610 --> 09:52.410
So what I've done is I've created a bunch of problem worlds for you to test your

141
09:52.410 --> 09:56.070
code against that we know if you use a normal code,

142
09:56.100 --> 09:58.140
it's definitely going to get into an infinite loop.

143
09:58.860 --> 10:02.970
So what you need to do is head to the course resources for this lesson

144
10:03.360 --> 10:05.610
and then download the zip file

145
10:05.670 --> 10:09.720
which contains three problem worlds for you to test your code against.

146
10:10.350 --> 10:14.490
And then in Reeborgs world, you're going to click on additional options,

147
10:14.970 --> 10:19.020
open world from file and you're going to select that folder

148
10:19.020 --> 10:23.340
which hopefully you've unzipped, and go through each of these problem worlds

149
10:23.400 --> 10:27.630
testing your code in all of the cases. Now, when you open this up,

150
10:27.660 --> 10:32.490
you can close off this box and the robot will start at a particular problem

151
10:32.490 --> 10:37.490
position, facing a direction that is going to cause it to go into an

152
10:37.590 --> 10:40.380
infinite loop. And you can know this ahead of time

153
10:40.380 --> 10:45.380
because when you actually go to start the robot and you hit play, when you 

154
10:45.660 --> 10:48.150
see the number of steps here over a thousand,

155
10:48.510 --> 10:52.680
it usually means that you're probably in a situation where it's going to turn into

156
10:52.680 --> 10:53.520
an infinite loop.

157
10:54.180 --> 10:59.180
So write your code here and then test it to make sure that the step count goes

158
10:59.250 --> 11:04.250
down below 1000 and your robot doesn't get stuck in this endless circle. Now,

159
11:06.480 --> 11:10.680
because the different problem worlds can have different solutions,

160
11:10.980 --> 11:15.630
but what you're aiming for is one solution code that solves all of the problem

161
11:15.630 --> 11:16.350
worlds,

162
11:16.350 --> 11:21.240
I want you to test your code against all three of these problem worlds I've

163
11:21.240 --> 11:22.073
created for you.

164
11:23.250 --> 11:27.660
So here's the time, if you want to tackle this challenge, to go ahead and pause

165
11:27.660 --> 11:32.400
the video and see if you can debug this pretty hard problem.

166
11:32.910 --> 11:36.450
It took me a while. So I think it's going to take you a while as well.

167
11:36.690 --> 11:40.950
But you might be smarter. It might take less time. So good luck,

168
11:41.100 --> 11:45.030
pause the video and we'll go through the solution together a little bit later

169
11:45.030 --> 11:45.863
on.

170
11:51.380 --> 11:55.510
All right. So first let's try and figure out what the problem is.

171
11:55.870 --> 12:00.870
The problem here is that every time when the robot's right side is clear,

172
12:01.600 --> 12:06.430
our code tells it to turn to that direction, move forwards,

173
12:06.880 --> 12:11.860
and then check to see if it's at the goal. So it's not, and again

174
12:11.860 --> 12:15.610
the right side is clear so it turns right and moves, turns right and moves,

175
12:15.670 --> 12:20.200
turns right and moves. So basically in any of these four positions,

176
12:20.590 --> 12:22.360
depending on the way it's facing,

177
12:22.660 --> 12:26.170
there might not be a wall on the right side of the robot.

178
12:26.620 --> 12:29.860
So that is the key to solving this bug.

179
12:30.820 --> 12:35.820
We have to get our robot to a starting position where it has a wall on the right

180
12:36.760 --> 12:38.050
side of the robot.

181
12:39.520 --> 12:43.960
Now I've been thinking about various ways of doing this and the shortest most

182
12:43.960 --> 12:48.910
succinct way that's also most understandable, I think, is this.

183
12:49.510 --> 12:53.500
So if we create another while loop before this while loop runs,

184
12:53.530 --> 12:55.750
so remember the code goes from top to bottom,

185
12:55.810 --> 12:57.670
it's going to hit this while loop first.

186
12:58.390 --> 13:03.220
And we check while the front is clear,

187
13:04.030 --> 13:08.170
well then the robot is not next to a wall, right? Such as in this case.

188
13:08.740 --> 13:13.270
Well, if that is the case, we're going to tell our robot to just move forwards.

189
13:13.870 --> 13:18.580
So this means that our robot is going to seek out a wall. So in this case,

190
13:18.580 --> 13:19.413
for example,

191
13:20.080 --> 13:25.080
it's going to go forwards until it reaches a wall.

192
13:27.970 --> 13:32.740
So it's going to loop through that loop twice and it moves twice until it hits a

193
13:32.740 --> 13:35.950
wall in front. Now this is the point which we want,

194
13:36.250 --> 13:40.240
because we want to make sure that now that it's hit a wall in front,

195
13:40.690 --> 13:45.400
that it's definitely got a wall on the right side of the robot. So what do we do?

196
13:45.790 --> 13:49.330
Well, once it's hit a wall, so it's exited this while loop,

197
13:49.570 --> 13:52.000
we're going to tell it to turn left.

198
13:52.420 --> 13:56.080
So this loop makes sure that the robot has a wall in front of it.

199
13:56.590 --> 13:59.890
And once it's found a wall in front, we tell it to turn left

200
14:00.190 --> 14:03.010
to turn that wall on to the right side of the robot.

201
14:03.610 --> 14:08.350
So now you can see we're going through this while loop. First front is clear,

202
14:08.350 --> 14:10.150
so it's going to move.

203
14:12.220 --> 14:15.760
And then front is still clear after that one move

204
14:15.820 --> 14:20.770
so it's going to move again until it's hit a wall in front. Now,

205
14:20.770 --> 14:21.400
at this point,

206
14:21.400 --> 14:24.910
it exits the while loop and it goes to the next line of instruction

207
14:25.180 --> 14:29.770
which tells it to turn left. Now that the robot has turned left

208
14:29.890 --> 14:34.890
it now has a wall on the right side and it enters the next loop.

209
14:35.770 --> 14:40.420
So in this loop, if the right side is clear, turn right and move.

210
14:40.720 --> 14:45.070
If the front is clear, just simply move forward, otherwise turn left.

211
14:45.490 --> 14:50.230
So as long as the robot started in a position where there is a wall on the right

212
14:50.230 --> 14:54.440
side of it, right next to it, it's never going to get into that infinite loop with

213
14:55.130 --> 14:59.320
this code. So this is one solution to this problem.

214
14:59.800 --> 15:02.080
Now there's lots of other solutions,

215
15:02.320 --> 15:07.320
but you gotta make sure that your solution works in every single one of these

216
15:08.410 --> 15:09.280
test cases.

217
15:09.640 --> 15:13.870
So we can go ahead and open up problem world 2 where the robot starts at a

218
15:13.870 --> 15:16.240
different position, facing a different position,

219
15:16.570 --> 15:21.280
and we want to make sure that still we don't have an infinite loop. In this case,

220
15:21.340 --> 15:25.930
it doesn't. So that's great. And finally, let's test the last one,

221
15:26.170 --> 15:27.760
which is a problem world 3,

222
15:28.420 --> 15:33.420
and now we can run our program and you can see it's also less than a thousand

223
15:34.240 --> 15:36.190
steps. So, it works.

224
15:37.000 --> 15:41.980
So this is a bit of advanced debugging because we're now testing our

225
15:41.980 --> 15:46.980
code against various terrible scenarios where the robot starts off at a position

226
15:48.310 --> 15:53.260
where it's really dangerous and can get into these infinite loops. Now,

227
15:53.290 --> 15:55.600
if you didn't manage to get the solution,

228
15:55.810 --> 15:59.410
don't despair and don't be too hard on yourself. This is a really,

229
15:59.830 --> 16:00.670
really hard one.

230
16:00.670 --> 16:05.470
And it took me a long time to find a solution that I think is readable and

231
16:05.590 --> 16:08.080
explainable and sort of makes sense.

232
16:08.800 --> 16:13.800
Now, it might be that you should put in a note in your calendar to come and

233
16:14.440 --> 16:16.510
revisit this issue in a week

234
16:16.690 --> 16:20.950
once you've had some time to think about it, and to really digest and understand

235
16:20.950 --> 16:23.860
the solution, then write the code yourself.

236
16:24.460 --> 16:27.460
Because you would have forgotten exactly what the code was,

237
16:27.520 --> 16:32.470
but you will still remember how it works. So if that is a case,

238
16:32.770 --> 16:37.240
this is what you should do. If you solved it by yourself, then congratulations.

239
16:37.240 --> 16:40.450
This is really, truly quite a difficult task.