1
00:00:00,760 --> 00:00:04,210
All right guys, it's time for yet another coding challenge.

2
00:00:04,240 --> 00:00:08,320
And this coding challenge is going to put into use what you've learned about

3
00:00:08,320 --> 00:00:09,153
while loops,

4
00:00:09,490 --> 00:00:13,540
while also testing some of the previous knowledge you got on functions.

5
00:00:13,990 --> 00:00:18,190
So I want you to head back to Reeborg's World and change to hurdle number 3.

6
00:00:18,790 --> 00:00:23,380
And take a look at the premise for this particular race.

7
00:00:24,680 --> 00:00:25,513
Right?

8
00:00:25,670 --> 00:00:30,670
Essentially what's happened this time is that the wall placement has become

9
00:00:31,100 --> 00:00:35,240
random. So let's say we restart this game, you can see that in this case,

10
00:00:35,240 --> 00:00:38,960
there's a wall here, one here, one here, one here. There's four in total.

11
00:00:39,410 --> 00:00:41,660
But if we try a different version,

12
00:00:41,690 --> 00:00:45,590
then you can see that there is different wall placement each time.

13
00:00:46,820 --> 00:00:49,310
Not only don't we know where the wall is going to be,

14
00:00:49,340 --> 00:00:52,250
we also don't know how many walls there will be.

15
00:00:52,370 --> 00:00:55,730
So code such as what we did before using a for loop

16
00:00:55,970 --> 00:00:59,870
telling our robot to jump six times is not going to work.

17
00:01:01,310 --> 00:01:04,400
And instead, we're going to have to use the conditions

18
00:01:04,459 --> 00:01:09,350
whether if front is clear or whether if there's a wall in front or whether

19
00:01:09,350 --> 00:01:12,740
if we're at the goal and also their negations,

20
00:01:12,770 --> 00:01:15,980
so not front is clear, not wall in front,

21
00:01:16,070 --> 00:01:20,960
not at goal. See if you can use what you've learned about while loops

22
00:01:21,080 --> 00:01:25,670
as well as your previous knowledge on if statements and functions to complete

23
00:01:25,730 --> 00:01:29,450
this challenge. When you have written the correct code,

24
00:01:29,690 --> 00:01:34,670
no matter what kind of wall situation or hurdle setup they have,

25
00:01:34,910 --> 00:01:37,760
it should always be able to get to the goal.

26
00:01:38,810 --> 00:01:42,530
And while I've been looking at this challenge, I know there is a very,

27
00:01:42,560 --> 00:01:45,380
very simple way of bypassing this challenge,

28
00:01:45,410 --> 00:01:49,490
which is simply to get the robots go up here and all the way down here,

29
00:01:49,880 --> 00:01:52,040
going past all the hurdles.

30
00:01:52,130 --> 00:01:56,450
But that's not the point of the challenge. In order to complete this challenge,

31
00:01:56,510 --> 00:02:00,650
you must get your robot to follow the path that set out in the dotted lines

32
00:02:00,950 --> 00:02:04,460
instead of bypassing it and doing something completely different

33
00:02:04,460 --> 00:02:09,410
just to get to the final goal. Pause the video, have a think about this,

34
00:02:09,620 --> 00:02:12,440
have a read of the world info popup here,

35
00:02:12,830 --> 00:02:16,700
and then think about how you can solve this challenge. So give it a go now.

36
00:02:17,170 --> 00:02:18,003
Right?

37
00:02:21,740 --> 00:02:26,300
All right. So essentially, if we don't know where the walls are going to be,

38
00:02:26,600 --> 00:02:30,470
then we have to test to see if there is a wall in front,

39
00:02:30,830 --> 00:02:33,680
or if there is no wall in front.

40
00:02:34,610 --> 00:02:37,970
And if there is a wall in front, then we're going to jump.

41
00:02:38,120 --> 00:02:41,570
But if there's no wall in front, then we're just going to move forwards.

42
00:02:42,170 --> 00:02:47,170
And we're going to do this and test this for as long as we're not at the goal.

43
00:02:47,780 --> 00:02:49,460
So we're going to need a while loop

44
00:02:49,460 --> 00:02:54,350
that's very similar to our previous while loop that I showed you in the demo. While

45
00:02:54,350 --> 00:02:58,820
we're not at goal or rather while

46
00:02:58,900 --> 00:03:02,890
this particular condition is not true,

47
00:03:03,580 --> 00:03:08,110
then we're going to repeat and loop some lines of code.

48
00:03:08,800 --> 00:03:13,090
The first thing we're going to do is we're going to use an if statement to check

49
00:03:13,150 --> 00:03:16,240
whether if there is a wall in front.

50
00:03:17,260 --> 00:03:20,890
So I'm going to say, if wall in front,

51
00:03:21,880 --> 00:03:23,530
if this is true,

52
00:03:23,980 --> 00:03:28,980
then I'm going to perform the jump function and get it to jump over the wall.

53
00:03:30,430 --> 00:03:34,570
But if there is no wall in front, so namely else,

54
00:03:35,230 --> 00:03:38,860
well, in this case, we're simply just going to move forwards.

55
00:03:39,370 --> 00:03:44,370
And because our jump function is always going to get us to face the right way,

56
00:03:44,590 --> 00:03:47,200
which is this direction here towards the goal,

57
00:03:47,620 --> 00:03:52,620
then this move forward by one function is always going to take us in this

58
00:03:53,050 --> 00:03:53,883
direction.

59
00:03:54,130 --> 00:03:58,390
So let's take a look at this code and let's step through it step by step.

60
00:03:59,170 --> 00:04:03,460
So the first thing we're going to check is are we at the goal. At_goal is going

61
00:04:03,460 --> 00:04:07,180
to be false. So not false is going to be true.

62
00:04:07,510 --> 00:04:09,340
So while this condition is true,

63
00:04:09,340 --> 00:04:11,800
we're going to carry out all the lines of code inside.

64
00:04:12,310 --> 00:04:16,480
So that means we should jump into this if statement and check

65
00:04:16,750 --> 00:04:19,899
is there a wall in front? So at this point,

66
00:04:19,930 --> 00:04:23,440
this should be false because there is nothing in front.

67
00:04:23,980 --> 00:04:28,980
So it should bypass the if statement and it should go to the else statement.

68
00:04:29,650 --> 00:04:34,600
In which case it should move forward by one step. So that's the end of one loop

69
00:04:34,720 --> 00:04:38,920
and we go back to the beginning: are we at the goal yet? Nope, we are not.

70
00:04:39,190 --> 00:04:41,770
But now when we run into our if statement,

71
00:04:42,070 --> 00:04:44,380
this check should now come up as true,

72
00:04:44,500 --> 00:04:48,910
because there is a wall right in front of our robot. So in this case,

73
00:04:48,910 --> 00:04:53,770
it's going to go into the if statement and perform the jump function. 

74
00:04:55,270 --> 00:04:59,560
Now our jump function that we defined previously actually gets us to move

75
00:04:59,560 --> 00:05:01,900
forward by one step first,

76
00:05:02,170 --> 00:05:06,700
before we actually hurdle over this wall. So in this case,

77
00:05:06,700 --> 00:05:08,380
if we try to do the same thing,

78
00:05:08,410 --> 00:05:11,320
then it's actually going to hit our robot into a wall.

79
00:05:11,770 --> 00:05:16,770
We have to modify our jump function by removing this first move function.

80
00:05:19,090 --> 00:05:23,410
So instead we get it to just turn left, go up, turn right,

81
00:05:23,470 --> 00:05:28,240
go right, turn right, go down and then turn left to face the right direction.

82
00:05:28,540 --> 00:05:32,710
This is a pure hurdle without that extra step in the beginning.

83
00:05:33,370 --> 00:05:37,300
And instead we're going to only move if there is no wall in front.

84
00:05:38,020 --> 00:05:39,250
So we've changed the code.

85
00:05:39,280 --> 00:05:43,030
Let's back it up and try this again with a different setup.

86
00:05:44,230 --> 00:05:47,710
And in this case, you can see as my code is going through the different steps,

87
00:05:47,920 --> 00:05:50,860
it's checking to see if there's a wall in front. If there is,

88
00:05:50,920 --> 00:05:53,350
then it's going to jump over it. If there isn't,

89
00:05:53,350 --> 00:05:57,830
it's just going to move forward by one step and then it's going to reevaluate.

90
00:05:58,160 --> 00:06:00,380
So this way, once it gets to here,

91
00:06:00,470 --> 00:06:04,460
it's actually going to reevaluate the while loop quite a few times

92
00:06:04,760 --> 00:06:07,520
and every time it's going to be the else statement that fires.

93
00:06:07,520 --> 00:06:11,450
It's going to move, move, move, move, move until it hits a wall again

94
00:06:11,600 --> 00:06:15,320
and then it's going to jump over and take us to the finishing line.

95
00:06:15,980 --> 00:06:20,090
Did you manage to get this correct? Did you manage to get the solution right?

96
00:06:20,510 --> 00:06:25,070
If not be sure to review the previous lessons on the while loop so that you

97
00:06:25,070 --> 00:06:26,600
familiarize yourself with it

98
00:06:27,050 --> 00:06:30,350
and also if you got stuck on changing the jump function,

99
00:06:30,680 --> 00:06:33,620
then be sure to remember that whenever you get stuck,

100
00:06:33,680 --> 00:06:37,700
it's important to test your assumptions. And in this case,

101
00:06:37,970 --> 00:06:42,350
you could have pressed the step-through button many times to see which line of

102
00:06:42,350 --> 00:06:43,970
code is being triggered at which point,

103
00:06:44,330 --> 00:06:47,840
and which is the moment where our code actually fails

104
00:06:47,870 --> 00:06:50,090
so you can figure it out and fix it.

105
00:06:51,290 --> 00:06:54,590
Now on the next lesson, I've got yet another code challenge for you.

106
00:06:54,650 --> 00:06:56,780
So head over there and give it a go.

