To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Homework 2b — Conditions
Note: You are required to use a function that returns a boolean in at least one of the following problems.
1. Paint and exit
For this problem, Bit wants to paint the room blue and then go to its green car. There is only one starting world:
Bit paints the room blue and then leaves:
The starter code is in paint_and_exit.py
.
Advice:
- Notice that this problem should be done in two steps. You don’t need to write everything in a single while loop.
- Be sure to decompose this problem into multiple functions. You probably want two additional functions (one to paint, one to exit).
- Write one function at a time and test it to be sure it works.
- For each function, think about the stopping condition and any other conditions the function needs.
- Use the event stream pattern. For example, instead of thinking of painting the room as doing four walls, think of how to do this using an event stream. Likewise, don’t think of moving to the car as two steps, but one step with an event stream.
2. Remove Rocks
For this problem, Bit is in a garden and wants to remove rocks:
- The rocks are shown in blue.
- The green blocks mark the beginning and end of the strawberry patch. Only the rocks inside the patch should be removed.
- Do not remove the strawberries, which are the red squares!
After removing the rocks, the world should look like this:
Your solution should also solve the second starting world:
The starter code is in remove_rocks.py
.
Advice:
- This problem naturally divides into three steps. You don’t need to do everything in a single while loop.
- Be sure to decompose this problem into multiple functions. You probably want three additional functions (one for each step).
- When removing rocks, use the event stream pattern.
3. Scurry
At least one of your files needs to include a function that returns a boolean. (This is to make sure you learned the material; not because this is the best way to solve the problem). Submissions that do not include a function that returns a boolean will be docked 3 points. For a reminder of how to use return, see the guide on return.
For this problem, Bit is running around in a world like this:
Bit follows these rules:
- Bit moves until the front, left, and right are all blocked.
- Bit prefers to move straight.
- If the front is blocked, Bit turns in the direction that is open.
- As Bit moves, it paints empty squares green and green squares blue.
If Bit follows these rules for the world above, the ending world will look like this:
There is a second world that looks like this:
The second ending world looks like this:
The starter code is in scurry.py
.
Advice:
- Be sure to decompose this problem into multiple functions. You probably need at least one additional function.
- Be sure you have the right stopping condition.
- Think carefully about the additional conditions you need.
Grading
Activity | Points |
---|---|
paint_and_exit.py | 6 |
remove_rocks.py | 6 |
scurry.py | 8 |
At least one of your files needs to include a function that returns a boolean. (This is to make sure you learned the material; not because this is the best way to solve the problem). Submissions that do not include a function that returns a boolean will be docked 3 points.
The returned value must be used as the condition for a while loop or if statement. For more information see the guide.
Manual grading will focus on decomposition and use of the logical operators and
and or
.