1 00:00:00,070 --> 00:00:03,220 All right guys, it's time for the final hurdle 2 00:00:03,250 --> 00:00:07,060 before we get to our goal post. In this lesson, 3 00:00:07,090 --> 00:00:11,200 I want you to switch over to the final hurdle challenge, which is hurdle 4. 4 00:00:11,770 --> 00:00:15,250 And in this particular version of the hurdle, 5 00:00:15,730 --> 00:00:20,730 the wall that the robot has to jump over to now has a variable height. 6 00:00:21,280 --> 00:00:24,550 So the height of each of these hurdles is completely random, 7 00:00:24,610 --> 00:00:27,190 the position of the hurdles are random 8 00:00:27,550 --> 00:00:31,240 and also the number of hurdles is also going to be random. 9 00:00:31,600 --> 00:00:35,050 You can see that every single time that this regenerates, 10 00:00:35,770 --> 00:00:40,270 it comes up with a completely different set of hurdle profiles. 11 00:00:40,780 --> 00:00:45,780 And now we can't just jump because we don't know what the height will be each 12 00:00:47,590 --> 00:00:48,423 time. 13 00:00:48,520 --> 00:00:53,520 So we have to use what we've learned about while loops to modify this jump 14 00:00:54,190 --> 00:00:59,190 function and see if we can get the jump to jump as high as necessary 15 00:01:00,520 --> 00:01:04,930 depending on the height of these walls. Just as a reminder, 16 00:01:04,930 --> 00:01:06,580 in order to complete this challenge, 17 00:01:06,580 --> 00:01:11,580 you should take a look at the Reeborg's keyboard window and go to this tab called 18 00:01:12,070 --> 00:01:17,050 conditions and see that you can check if the front of the robot is clear, 19 00:01:17,380 --> 00:01:21,280 if it's right is clear, if there's a wall on the front, 20 00:01:21,280 --> 00:01:24,940 if there's a wall on the right and various other things. 21 00:01:25,630 --> 00:01:29,950 The ones that are probably going to be the most useful is this top row here. 22 00:01:30,280 --> 00:01:32,950 So I want you to take a look at these conditions, 23 00:01:33,130 --> 00:01:35,290 see how they're spelt and how you can use them, 24 00:01:35,740 --> 00:01:40,270 and then try to use them and test them out using your Reeborg robot. 25 00:01:40,840 --> 00:01:42,130 Now, the goal is of course 26 00:01:42,130 --> 00:01:46,000 to reach this final flag going over various hurdles 27 00:01:46,030 --> 00:01:50,860 using this dotted line as the path, which is going to be different each time. 28 00:01:51,250 --> 00:01:56,170 Just as a head up, this is going to require you to modify this jump function. 29 00:01:56,560 --> 00:02:00,610 Have a serious think about it because it's not easy 30 00:02:01,030 --> 00:02:06,030 and you're going to need to use more while loops to achieve this end outcome. And 31 00:02:06,550 --> 00:02:11,440 try to see if you can solve this challenge in the least lines of code possible. 32 00:02:11,800 --> 00:02:14,950 So I managed to do it in as little as 24 lines of code, 33 00:02:15,220 --> 00:02:17,590 so see if you can aim to achieve that. 34 00:02:18,190 --> 00:02:20,980 Pause the video now and give this challenge a go. 35 00:02:22,310 --> 00:02:23,143 Right? 36 00:02:25,680 --> 00:02:26,100 All right. 37 00:02:26,100 --> 00:02:31,100 So the key difference between this particular hurdle and the previous version is 38 00:02:31,650 --> 00:02:36,650 that the height of the wall is now random. Instead of just jumping over one 39 00:02:37,530 --> 00:02:40,860 square, where we turn left, move, turn right, move, turn right move, 40 00:02:41,220 --> 00:02:44,700 we have to now see if the right side has a wall, 41 00:02:44,940 --> 00:02:48,270 or if it's completely clear. Now, in this case, 42 00:02:48,300 --> 00:02:52,950 what I want to happen is I want to turn my robot to the left first of all, 43 00:02:53,460 --> 00:02:57,990 and then I want to check if there is a wall on the right. 44 00:02:58,380 --> 00:03:02,620 So I can use this condition wall on right to achieve this. 45 00:03:03,550 --> 00:03:04,720 And if that is true, 46 00:03:04,750 --> 00:03:09,550 then I want to continue moving forward until there is no longer a wall on the 47 00:03:09,550 --> 00:03:12,850 right, in which case I'm going to turn right turn right 48 00:03:13,000 --> 00:03:17,290 and then start going down. So let's modify this jump function. 49 00:03:17,740 --> 00:03:21,730 We definitely need the robot turn left, but after it turns left, 50 00:03:21,760 --> 00:03:26,760 we're going to use a while loop to say that while there is a wall on the right 51 00:03:28,360 --> 00:03:32,080 then in this case, we're going to move forwards. 52 00:03:32,890 --> 00:03:35,800 So we're going to go up in this direction basically 53 00:03:35,800 --> 00:03:37,570 because we've turned left already. 54 00:03:38,260 --> 00:03:41,500 Let's test this out and let's run this code. 55 00:03:41,800 --> 00:03:43,450 You can see in this version of events, 56 00:03:43,510 --> 00:03:48,100 my robot is going to keep going until the very end of the screen. 57 00:03:48,580 --> 00:03:49,780 This is not what we want. 58 00:03:50,200 --> 00:03:54,970 So what we want to do is when this is no longer true, 59 00:03:55,180 --> 00:03:58,330 it's going to skip to line 10. So at this point, 60 00:03:58,360 --> 00:04:00,880 there is no longer a wall on the right, 61 00:04:01,150 --> 00:04:05,530 because otherwise it would have continued looping and calling this line 9 62 00:04:05,800 --> 00:04:09,880 where it gets it to move forward. When there's no longer a wall on the right, 63 00:04:10,030 --> 00:04:12,190 then we want to do what we did previously 64 00:04:12,190 --> 00:04:15,550 which is to get our robot to turn right move, 65 00:04:15,850 --> 00:04:19,990 and then turn right. Now at this point, we have to move down the wall. 66 00:04:20,350 --> 00:04:24,430 And what we're going to check is whether if the front is clear, 67 00:04:24,460 --> 00:04:29,440 because it's only when we reach the very bottom here, when the front is blocked, 68 00:04:29,650 --> 00:04:34,650 do we actually turn left again and continue back to our while loop here to move 69 00:04:35,830 --> 00:04:40,450 forward. To do this we're going to add yet another while loop. 70 00:04:41,410 --> 00:04:45,490 And in this case, we're going to use the front_is_clear condition. 71 00:04:45,910 --> 00:04:48,070 While the front is clear, 72 00:04:48,310 --> 00:04:51,430 we're going to get the robot to continue moving forward. 73 00:04:51,760 --> 00:04:56,440 So once it's turned to face this direction, and there's no wall in front, 74 00:04:56,440 --> 00:04:59,530 it's going to keep going until this is no longer true. 75 00:04:59,950 --> 00:05:02,590 And that is the end of our jump. 76 00:05:02,860 --> 00:05:07,860 So now we've basically modified this jump function to take into account all of 77 00:05:08,080 --> 00:05:12,250 these new things such as while there's a wall on the right 78 00:05:12,250 --> 00:05:16,540 then continue moving upwards, while the front is clear when it's coming down 79 00:05:16,570 --> 00:05:20,980 then continue moving downwards. And then at the very end, 80 00:05:21,010 --> 00:05:26,010 we're going to get our robot to turn left so that we're ready to go and check if 81 00:05:28,240 --> 00:05:30,340 we're at the goal. And if we're not at the goal, 82 00:05:30,370 --> 00:05:32,380 then we're going to check if there's a wall in front, 83 00:05:32,740 --> 00:05:34,750 if there is a wall in front that we're going to jump, 84 00:05:34,930 --> 00:05:39,610 otherwise we're going to continue moving forwards or rather to the right to get 85 00:05:39,610 --> 00:05:42,700 to the goal. So now this is the final code. 86 00:05:43,330 --> 00:05:46,540 And depending on the number of spaces you've added in here, 87 00:05:46,600 --> 00:05:51,600 obviously it's a little bit easier to read if you actually have it like this. 88 00:05:52,090 --> 00:05:56,410 But depending on how you've spaced things out in your code, 89 00:05:56,530 --> 00:06:00,860 you should end up with anywhere between say 20 to 24 lines of code. 90 00:06:01,880 --> 00:06:05,720 So I want you to be able to see all of these lines of code on the right when we 91 00:06:05,720 --> 00:06:06,553 run this code. 92 00:06:06,770 --> 00:06:10,970 And I've actually got a typo in here that it's going to tell me. 93 00:06:11,180 --> 00:06:12,830 So it's going to say invalid syntax 94 00:06:12,860 --> 00:06:17,270 and the problem is just because I forgot a colon here. If you get that 95 00:06:17,270 --> 00:06:18,860 pop-up invalid syntax 96 00:06:18,890 --> 00:06:23,890 be sure to check if you have any problems in here and it can get a little bit 97 00:06:24,410 --> 00:06:28,310 complicated if you leave your code for a long period of time before you test it. 98 00:06:28,340 --> 00:06:31,430 So test your code after every little iteration 99 00:06:31,700 --> 00:06:34,700 and then when you actually get something like an invalid syntax, 100 00:06:34,940 --> 00:06:36,530 you'll be able to catch it more easily. 101 00:06:36,740 --> 00:06:41,740 So now let's go ahead and regenerate our map and let's try it again. 102 00:06:42,620 --> 00:06:46,730 So when we're going to make a jump and then we're gonna turn right, 103 00:06:46,880 --> 00:06:50,390 and we're going to keep going down until we hit the wall and we turned left, 104 00:06:50,780 --> 00:06:53,660 and now we're not out the goal, and there's still a wall in front. 105 00:06:53,660 --> 00:06:58,130 So we make another jump and we go down this much, much larger wall. 106 00:06:58,640 --> 00:07:03,170 And then we continue this process of using several while loops, 107 00:07:03,710 --> 00:07:06,590 checking if there's a wall on the right, checking if the front is clear, 108 00:07:06,830 --> 00:07:07,250 checking 109 00:07:07,250 --> 00:07:12,250 if there's a wall in the front in order to achieve our goal and get to our final 110 00:07:12,440 --> 00:07:16,880 destination. Now for your code to successfully complete this challenge, 111 00:07:17,270 --> 00:07:22,100 the line that your robot draws should follow the line that started here, 112 00:07:22,130 --> 00:07:23,540 just like what you see here. 113 00:07:24,050 --> 00:07:27,920 So instead of going just over the top and getting to the final goal, 114 00:07:28,010 --> 00:07:30,770 it should follow each of these steps and finally 115 00:07:30,770 --> 00:07:34,820 you should get that green pop-up where it says it's at the right position. 116 00:07:35,570 --> 00:07:37,910 So did you manage to complete this challenge? 117 00:07:38,240 --> 00:07:43,240 If not, be sure to review the lesson on while loops and just have a play around 118 00:07:43,370 --> 00:07:44,120 with while loops. 119 00:07:44,120 --> 00:07:48,800 Try some of the different conditions and see what happens when you run the code 120 00:07:48,800 --> 00:07:53,480 and see how the robot behaves so that you get a good grasp of how these things 121 00:07:53,480 --> 00:07:54,500 actually work. 122 00:07:55,130 --> 00:08:00,110 Now on the next lesson is our final project and we're going to get our robot to 123 00:08:00,110 --> 00:08:04,220 be able to navigate a maze all by itself using what we've learned 124 00:08:04,250 --> 00:08:08,810 including functions as well as while loops, as well as using good indentation. 125 00:08:09,170 --> 00:08:12,290 So for all of that and more, I'll see you on the next lesson.