WEBVTT

0
00:00.070 --> 00:03.220
All right guys, it's time for the final hurdle

1
00:03.250 --> 00:07.060
before we get to our goal post. In this lesson,

2
00:07.090 --> 00:11.200
I want you to switch over to the final hurdle challenge, which is hurdle 4.

3
00:11.770 --> 00:15.250
And in this particular version of the hurdle,

4
00:15.730 --> 00:20.730
the wall that the robot has to jump over to now has a variable height.

5
00:21.280 --> 00:24.550
So the height of each of these hurdles is completely random,

6
00:24.610 --> 00:27.190
the position of the hurdles are random

7
00:27.550 --> 00:31.240
and also the number of hurdles is also going to be random.

8
00:31.600 --> 00:35.050
You can see that every single time that this regenerates,

9
00:35.770 --> 00:40.270
it comes up with a completely different set of hurdle profiles.

10
00:40.780 --> 00:45.780
And now we can't just jump because we don't know what the height will be each

11
00:47.590 --> 00:48.423
time.

12
00:48.520 --> 00:53.520
So we have to use what we've learned about while loops to modify this jump

13
00:54.190 --> 00:59.190
function and see if we can get the jump to jump as high as necessary

14
01:00.520 --> 01:04.930
depending on the height of these walls. Just as a reminder,

15
01:04.930 --> 01:06.580
in order to complete this challenge,

16
01:06.580 --> 01:11.580
you should take a look at the Reeborg's keyboard window and go to this tab called

17
01:12.070 --> 01:17.050
conditions and see that you can check if the front of the robot is clear,

18
01:17.380 --> 01:21.280
if it's right is clear, if there's a wall on the front,

19
01:21.280 --> 01:24.940
if there's a wall on the right and various other things.

20
01:25.630 --> 01:29.950
The ones that are probably going to be the most useful is this top row here.

21
01:30.280 --> 01:32.950
So I want you to take a look at these conditions,

22
01:33.130 --> 01:35.290
see how they're spelt and how you can use them,

23
01:35.740 --> 01:40.270
and then try to use them and test them out using your Reeborg robot.

24
01:40.840 --> 01:42.130
Now, the goal is of course

25
01:42.130 --> 01:46.000
to reach this final flag going over various hurdles

26
01:46.030 --> 01:50.860
using this dotted line as the path, which is going to be different each time.

27
01:51.250 --> 01:56.170
Just as a head up, this is going to require you to modify this jump function.

28
01:56.560 --> 02:00.610
Have a serious think about it because it's not easy

29
02:01.030 --> 02:06.030
and you're going to need to use more while loops to achieve this end outcome. And

30
02:06.550 --> 02:11.440
try to see if you can solve this challenge in the least lines of code possible.

31
02:11.800 --> 02:14.950
So I managed to do it in as little as 24 lines of code,

32
02:15.220 --> 02:17.590
so see if you can aim to achieve that.

33
02:18.190 --> 02:20.980
Pause the video now and give this challenge a go.

34
02:22.310 --> 02:23.143
<v 1>Right?</v>

35
02:25.680 --> 02:26.100
<v 0>All right.</v>

36
02:26.100 --> 02:31.100
So the key difference between this particular hurdle and the previous version is

37
02:31.650 --> 02:36.650
that the height of the wall is now random. Instead of just jumping over one

38
02:37.530 --> 02:40.860
square, where we turn left, move, turn right, move, turn right move,

39
02:41.220 --> 02:44.700
we have to now see if the right side has a wall,

40
02:44.940 --> 02:48.270
or if it's completely clear. Now, in this case,

41
02:48.300 --> 02:52.950
what I want to happen is I want to turn my robot to the left first of all,

42
02:53.460 --> 02:57.990
and then I want to check if there is a wall on the right.

43
02:58.380 --> 03:02.620
So I can use this condition wall on right to achieve this.

44
03:03.550 --> 03:04.720
And if that is true,

45
03:04.750 --> 03:09.550
then I want to continue moving forward until there is no longer a wall on the

46
03:09.550 --> 03:12.850
right, in which case I'm going to turn right turn right

47
03:13.000 --> 03:17.290
and then start going down. So let's modify this jump function.

48
03:17.740 --> 03:21.730
We definitely need the robot turn left, but after it turns left,

49
03:21.760 --> 03:26.760
we're going to use a while loop to say that while there is a wall on the right

50
03:28.360 --> 03:32.080
then in this case, we're going to move forwards.

51
03:32.890 --> 03:35.800
So we're going to go up in this direction basically

52
03:35.800 --> 03:37.570
because we've turned left already.

53
03:38.260 --> 03:41.500
Let's test this out and let's run this code.

54
03:41.800 --> 03:43.450
You can see in this version of events,

55
03:43.510 --> 03:48.100
my robot is going to keep going until the very end of the screen.

56
03:48.580 --> 03:49.780
This is not what we want.

57
03:50.200 --> 03:54.970
So what we want to do is when this is no longer true,

58
03:55.180 --> 03:58.330
it's going to skip to line 10. So at this point,

59
03:58.360 --> 04:00.880
there is no longer a wall on the right,

60
04:01.150 --> 04:05.530
because otherwise it would have continued looping and calling this line 9

61
04:05.800 --> 04:09.880
where it gets it to move forward. When there's no longer a wall on the right,

62
04:10.030 --> 04:12.190
then we want to do what we did previously

63
04:12.190 --> 04:15.550
which is to get our robot to turn right move,

64
04:15.850 --> 04:19.990
and then turn right. Now at this point, we have to move down the wall.

65
04:20.350 --> 04:24.430
And what we're going to check is whether if the front is clear,

66
04:24.460 --> 04:29.440
because it's only when we reach the very bottom here, when the front is blocked,

67
04:29.650 --> 04:34.650
do we actually turn left again and continue back to our while loop here to move

68
04:35.830 --> 04:40.450
forward. To do this we're going to add yet another while loop.

69
04:41.410 --> 04:45.490
And in this case, we're going to use the front_is_clear condition.

70
04:45.910 --> 04:48.070
While the front is clear,

71
04:48.310 --> 04:51.430
we're going to get the robot to continue moving forward.

72
04:51.760 --> 04:56.440
So once it's turned to face this direction, and there's no wall in front,

73
04:56.440 --> 04:59.530
it's going to keep going until this is no longer true.

74
04:59.950 --> 05:02.590
And that is the end of our jump.

75
05:02.860 --> 05:07.860
So now we've basically modified this jump function to take into account all of

76
05:08.080 --> 05:12.250
these new things such as while there's a wall on the right

77
05:12.250 --> 05:16.540
then continue moving upwards, while the front is clear when it's coming down

78
05:16.570 --> 05:20.980
then continue moving downwards. And then at the very end,

79
05:21.010 --> 05:26.010
we're going to get our robot to turn left so that we're ready to go and check if

80
05:28.240 --> 05:30.340
we're at the goal. And if we're not at the goal,

81
05:30.370 --> 05:32.380
then we're going to check if there's a wall in front,

82
05:32.740 --> 05:34.750
if there is a wall in front that we're going to jump,

83
05:34.930 --> 05:39.610
otherwise we're going to continue moving forwards or rather to the right to get

84
05:39.610 --> 05:42.700
to the goal. So now this is the final code.

85
05:43.330 --> 05:46.540
And depending on the number of spaces you've added in here,

86
05:46.600 --> 05:51.600
obviously it's a little bit easier to read if you actually have it like this.

87
05:52.090 --> 05:56.410
But depending on how you've spaced things out in your code,

88
05:56.530 --> 06:00.860
you should end up with anywhere between say 20 to 24 lines of code.

89
06:01.880 --> 06:05.720
So I want you to be able to see all of these lines of code on the right when we

90
06:05.720 --> 06:06.553
run this code.

91
06:06.770 --> 06:10.970
And I've actually got a typo in here that it's going to tell me.

92
06:11.180 --> 06:12.830
So it's going to say invalid syntax

93
06:12.860 --> 06:17.270
and the problem is just because I forgot a colon here. If you get that

94
06:17.270 --> 06:18.860
pop-up invalid syntax

95
06:18.890 --> 06:23.890
be sure to check if you have any problems in here and it can get a little bit

96
06:24.410 --> 06:28.310
complicated if you leave your code for a long period of time before you test it.

97
06:28.340 --> 06:31.430
So test your code after every little iteration

98
06:31.700 --> 06:34.700
and then when you actually get something like an invalid syntax,

99
06:34.940 --> 06:36.530
you'll be able to catch it more easily.

100
06:36.740 --> 06:41.740
So now let's go ahead and regenerate our map and let's try it again.

101
06:42.620 --> 06:46.730
So when we're going to make a jump and then we're gonna turn right,

102
06:46.880 --> 06:50.390
and we're going to keep going down until we hit the wall and we turned left,

103
06:50.780 --> 06:53.660
and now we're not out the goal, and there's still a wall in front.

104
06:53.660 --> 06:58.130
So we make another jump and we go down this much, much larger wall.

105
06:58.640 --> 07:03.170
And then we continue this process of using several while loops,

106
07:03.710 --> 07:06.590
checking if there's a wall on the right, checking if the front is clear,

107
07:06.830 --> 07:07.250
checking

108
07:07.250 --> 07:12.250
if there's a wall in the front in order to achieve our goal and get to our final

109
07:12.440 --> 07:16.880
destination. Now for your code to successfully complete this challenge,

110
07:17.270 --> 07:22.100
the line that your robot draws should follow the line that started here,

111
07:22.130 --> 07:23.540
just like what you see here.

112
07:24.050 --> 07:27.920
So instead of going just over the top and getting to the final goal,

113
07:28.010 --> 07:30.770
it should follow each of these steps and finally

114
07:30.770 --> 07:34.820
you should get that green pop-up where it says it's at the right position.

115
07:35.570 --> 07:37.910
So did you manage to complete this challenge?

116
07:38.240 --> 07:43.240
If not, be sure to review the lesson on while loops and just have a play around

117
07:43.370 --> 07:44.120
with while loops.

118
07:44.120 --> 07:48.800
Try some of the different conditions and see what happens when you run the code

119
07:48.800 --> 07:53.480
and see how the robot behaves so that you get a good grasp of how these things

120
07:53.480 --> 07:54.500
actually work.

121
07:55.130 --> 08:00.110
Now on the next lesson is our final project and we're going to get our robot to

122
08:00.110 --> 08:04.220
be able to navigate a maze all by itself using what we've learned

123
08:04.250 --> 08:08.810
including functions as well as while loops, as well as using good indentation.

124
08:09.170 --> 08:12.290
So for all of that and more, I'll see you on the next lesson.