BYU logo Computer Science

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 in a room, with a passageway to a car

Bit paints the room blue and then leaves:

the room is blue and bit is at his car

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:

Bit in a garden with some blue 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:

all the rocks are removed

Your solution should also solve the second starting world:

Bit in a garden with some blue rocks

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 in a world with some black squares

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:

The path that Bit takes, marked with green and blue squares

There is a second world that looks like this:

Bit in a world with some black squares

The second ending world looks like this:

The path that Bit takes, marked with green and blue squares

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

ActivityPoints
paint_and_exit.py6
remove_rocks.py6
scurry.py8

==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

Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.

Intent

The intent of this assignment is that you correctly use conditions with if statements and while loops, and that you decompose complicated problems.

As a standard of decomposition, we expect that your while loops are not nested within the same function.

This is an example of a nested while loop:

while not bit.is_green():
    if bit.right_clear():
        while bit.front_clear():
            bit.move()

“Nested” means that one while loop is inside of another while loop. Deeply nested code is hard to read.

RubricPoints
Whitespace1
Naming1
Decomposition4
Intent4
Total10