This is DirtRider. DirtRider is a cleaning robot with simple programmable functions. DirtRider knows how to stay in the cleaning zone but needs instructions so it will cover the entire area. When DirtRider moves over dirt it will clean it up. Dirt keeps coming back so we need DirtRider to keep cleaning the room over and over.
In this lesson, you will learn about while loops. A while loop goes forever until some condition is met. Later you will also learn about for loops. A for loop runs a set number of times. In the example below the while loop moves forward and turns right forever. The for loop will move forward and turn right, repeated three times.
Checkpoint #1 – First while loop
Checkpoint #2 – While loop with multiple commands
Let’s update our code in the code file as below. Pay attention to punctuation and indents:
Checkpoint #3 – Clean all pink dirt.
To help you understand while loops, have a look at this diagram:
We can represent the above diagram by this code:
This is represented by the same code, except we add a while loop to repeat over and over.
2. Your goal is to send the robot on a path which cleans all the pink dirt over and over. Try to write the code which would implement the diagram below. Focus just on green and implement those commands. The other colors are just repeats of green and handled by the while loop.
Checkpoint #4 – Clean all pink and blue dirt
See if you can implement an algorithm to complete this pattern and clean all of the dirt:
Here is another pattern to try. We removed the turns from the diagram which can be implemented in different ways:
Are there other ways to do it? What would the fastest approach be?