WEBVTT

0
00:00.760 --> 00:04.210
All right guys, it's time for yet another coding challenge.

1
00:04.240 --> 00:08.320
And this coding challenge is going to put into use what you've learned about

2
00:08.320 --> 00:09.153
while loops,

3
00:09.490 --> 00:13.540
while also testing some of the previous knowledge you got on functions.

4
00:13.990 --> 00:18.190
So I want you to head back to Reeborg's World and change to hurdle number 3.

5
00:18.790 --> 00:23.380
And take a look at the premise for this particular race.

6
00:24.680 --> 00:25.513
<v 1>Right?</v>

7
00:25.670 --> 00:30.670
<v 0>Essentially what's happened this time is that the wall placement has become</v>

8
00:31.100 --> 00:35.240
random. So let's say we restart this game, you can see that in this case,

9
00:35.240 --> 00:38.960
there's a wall here, one here, one here, one here. There's four in total.

10
00:39.410 --> 00:41.660
But if we try a different version,

11
00:41.690 --> 00:45.590
then you can see that there is different wall placement each time.

12
00:46.820 --> 00:49.310
Not only don't we know where the wall is going to be,

13
00:49.340 --> 00:52.250
we also don't know how many walls there will be.

14
00:52.370 --> 00:55.730
So code such as what we did before using a for loop

15
00:55.970 --> 00:59.870
telling our robot to jump six times is not going to work.

16
01:01.310 --> 01:04.400
And instead, we're going to have to use the conditions

17
01:04.460 --> 01:09.350
whether if front is clear or whether if there's a wall in front or whether

18
01:09.350 --> 01:12.740
if we're at the goal and also their negations,

19
01:12.770 --> 01:15.980
so not front is clear, not wall in front,

20
01:16.070 --> 01:20.960
not at goal. See if you can use what you've learned about while loops

21
01:21.080 --> 01:25.670
as well as your previous knowledge on if statements and functions to complete

22
01:25.730 --> 01:29.450
this challenge. When you have written the correct code,

23
01:29.690 --> 01:34.670
no matter what kind of wall situation or hurdle setup they have,

24
01:34.910 --> 01:37.760
it should always be able to get to the goal.

25
01:38.810 --> 01:42.530
And while I've been looking at this challenge, I know there is a very,

26
01:42.560 --> 01:45.380
very simple way of bypassing this challenge,

27
01:45.410 --> 01:49.490
which is simply to get the robots go up here and all the way down here,

28
01:49.880 --> 01:52.040
going past all the hurdles.

29
01:52.130 --> 01:56.450
But that's not the point of the challenge. In order to complete this challenge,

30
01:56.510 --> 02:00.650
you must get your robot to follow the path that set out in the dotted lines

31
02:00.950 --> 02:04.460
instead of bypassing it and doing something completely different

32
02:04.460 --> 02:09.410
just to get to the final goal. Pause the video, have a think about this,

33
02:09.620 --> 02:12.440
have a read of the world info popup here,

34
02:12.830 --> 02:16.700
and then think about how you can solve this challenge. So give it a go now.

35
02:17.170 --> 02:18.003
<v 1>Right?</v>

36
02:21.740 --> 02:26.300
<v 0>All right. So essentially, if we don't know where the walls are going to be,</v>

37
02:26.600 --> 02:30.470
then we have to test to see if there is a wall in front,

38
02:30.830 --> 02:33.680
or if there is no wall in front.

39
02:34.610 --> 02:37.970
And if there is a wall in front, then we're going to jump.

40
02:38.120 --> 02:41.570
But if there's no wall in front, then we're just going to move forwards.

41
02:42.170 --> 02:47.170
And we're going to do this and test this for as long as we're not at the goal.

42
02:47.780 --> 02:49.460
So we're going to need a while loop

43
02:49.460 --> 02:54.350
that's very similar to our previous while loop that I showed you in the demo. While

44
02:54.350 --> 02:58.820
we're not at goal or rather while

45
02:58.900 --> 03:02.890
this particular condition is not true,

46
03:03.580 --> 03:08.110
then we're going to repeat and loop some lines of code.

47
03:08.800 --> 03:13.090
The first thing we're going to do is we're going to use an if statement to check

48
03:13.150 --> 03:16.240
whether if there is a wall in front.

49
03:17.260 --> 03:20.890
So I'm going to say, if wall in front,

50
03:21.880 --> 03:23.530
if this is true,

51
03:23.980 --> 03:28.980
then I'm going to perform the jump function and get it to jump over the wall.

52
03:30.430 --> 03:34.570
But if there is no wall in front, so namely else,

53
03:35.230 --> 03:38.860
well, in this case, we're simply just going to move forwards.

54
03:39.370 --> 03:44.370
And because our jump function is always going to get us to face the right way,

55
03:44.590 --> 03:47.200
which is this direction here towards the goal,

56
03:47.620 --> 03:52.620
then this move forward by one function is always going to take us in this

57
03:53.050 --> 03:53.883
direction.

58
03:54.130 --> 03:58.390
So let's take a look at this code and let's step through it step by step.

59
03:59.170 --> 04:03.460
So the first thing we're going to check is are we at the goal. At_goal is going

60
04:03.460 --> 04:07.180
to be false. So not false is going to be true.

61
04:07.510 --> 04:09.340
So while this condition is true,

62
04:09.340 --> 04:11.800
we're going to carry out all the lines of code inside.

63
04:12.310 --> 04:16.480
So that means we should jump into this if statement and check

64
04:16.750 --> 04:19.900
is there a wall in front? So at this point,

65
04:19.930 --> 04:23.440
this should be false because there is nothing in front.

66
04:23.980 --> 04:28.980
So it should bypass the if statement and it should go to the else statement.

67
04:29.650 --> 04:34.600
In which case it should move forward by one step. So that's the end of one loop

68
04:34.720 --> 04:38.920
and we go back to the beginning: are we at the goal yet? Nope, we are not.

69
04:39.190 --> 04:41.770
But now when we run into our if statement,

70
04:42.070 --> 04:44.380
this check should now come up as true,

71
04:44.500 --> 04:48.910
because there is a wall right in front of our robot. So in this case,

72
04:48.910 --> 04:53.770
it's going to go into the if statement and perform the jump function. 

73
04:55.270 --> 04:59.560
Now our jump function that we defined previously actually gets us to move

74
04:59.560 --> 05:01.900
forward by one step first,

75
05:02.170 --> 05:06.700
before we actually hurdle over this wall. So in this case,

76
05:06.700 --> 05:08.380
if we try to do the same thing,

77
05:08.410 --> 05:11.320
then it's actually going to hit our robot into a wall.

78
05:11.770 --> 05:16.770
We have to modify our jump function by removing this first move function.

79
05:19.090 --> 05:23.410
So instead we get it to just turn left, go up, turn right,

80
05:23.470 --> 05:28.240
go right, turn right, go down and then turn left to face the right direction.

81
05:28.540 --> 05:32.710
This is a pure hurdle without that extra step in the beginning.

82
05:33.370 --> 05:37.300
And instead we're going to only move if there is no wall in front.

83
05:38.020 --> 05:39.250
So we've changed the code.

84
05:39.280 --> 05:43.030
Let's back it up and try this again with a different setup.

85
05:44.230 --> 05:47.710
And in this case, you can see as my code is going through the different steps,

86
05:47.920 --> 05:50.860
it's checking to see if there's a wall in front. If there is,

87
05:50.920 --> 05:53.350
then it's going to jump over it. If there isn't,

88
05:53.350 --> 05:57.830
it's just going to move forward by one step and then it's going to reevaluate.

89
05:58.160 --> 06:00.380
So this way, once it gets to here,

90
06:00.470 --> 06:04.460
it's actually going to reevaluate the while loop quite a few times

91
06:04.760 --> 06:07.520
and every time it's going to be the else statement that fires.

92
06:07.520 --> 06:11.450
It's going to move, move, move, move, move until it hits a wall again

93
06:11.600 --> 06:15.320
and then it's going to jump over and take us to the finishing line.

94
06:15.980 --> 06:20.090
Did you manage to get this correct? Did you manage to get the solution right?

95
06:20.510 --> 06:25.070
If not be sure to review the previous lessons on the while loop so that you

96
06:25.070 --> 06:26.600
familiarize yourself with it

97
06:27.050 --> 06:30.350
and also if you got stuck on changing the jump function,

98
06:30.680 --> 06:33.620
then be sure to remember that whenever you get stuck,

99
06:33.680 --> 06:37.700
it's important to test your assumptions. And in this case,

100
06:37.970 --> 06:42.350
you could have pressed the step-through button many times to see which line of

101
06:42.350 --> 06:43.970
code is being triggered at which point,

102
06:44.330 --> 06:47.840
and which is the moment where our code actually fails

103
06:47.870 --> 06:50.090
so you can figure it out and fix it.

104
06:51.290 --> 06:54.590
Now on the next lesson, I've got yet another code challenge for you.

105
06:54.650 --> 06:56.780
So head over there and give it a go.