BYU logo Computer Science

Lab 3 — Decomposition with Bit

To begin, download lab-3-files.zip. This is a zip file, so you will need to extract its contents. You should put the files inside of a folder called lab-3 in your CS 110 folder. These files contain worlds. A world is a file that contains a description of what Bit sees.

You should have a folder called lab-3/worlds/, and then a bunch of files in there.

You are going to write your code in the lab-3/ folder.

Grass and Sky

Remember the Grass and Sky problem? Start with:

from byubit import Bit
bit = Bit.new_world(5,3)

Then make the world so it looks like this:

grass and sky

Use functions for this problem!

  • Write a function that can color a row. It should take a color as an argument, so it can paint whatever color you give the function.
  • Write a function that can move back to the beginning of a row, assuming bit is at the end of a row to start

Using these functions, solve the grass and sky problem.

Smile!

Help Bit draw blue smiles at each green square. Each green space corresponds to the left eye of the smile.

The starting world is in smiles-start.txt. It looks like this:

smiles start

You can write your code in smiles.py and use this to load the starting world:

from byubit import Bit
bit = Bit.load("worlds/smiles-start.txt")

The ending world should look like this:

smiles finish

Use functions!!

What are the different abstractions that will help you solve this problem? Write a function for each. Think carefully about pre and post conditions of each function. You can do it!

Your solution should have one or more helper functions.

Testing

If your function to draw the smiles is draw_smiles(), you can test your solution with the following code:

bit.compare(Bit.load("worlds/smiles-finish.txt"))

Fix the Forest

Fix all the trees!

  • How can you decompose this problem?
  • For each function you define, what are the pre and post conditions?
  • How do the functions fit together?

The starting world looks like this:

fix forest start

The ending world looks like this:

fix forest finish

You can test your code with:

bit.compare(Bit.load("fix-forest-finish.txt"))

The Desert Shall Blossom as a Rose

This problem will especially help you get ready for the project.

Fill the holes with water, and plant flowers near the water.

The starting world looks like this:

blossom start

The ending world looks like this:

blossom finish

You can test your code with:

bit.compare(Bit.load("blossom-finish.txt"))

Tips

Remember that decomposition is the key to solving complex problems. You may want to start by filling in all the puddles. This will itself take a few different functions, maybe one to move to the edge of a puddle, another to fill in one column of a puddle, and so forth.

You could then see if you can fill in puddles without drawing any flowers:

blossom puddles

Now add functions for growing the flowers!

Lessons

What we want you to get from this lab:

  • You feel confident writing functions

  • You can take a complex problem and decompose it into a set of functions, each of which accomplish a small piece of the problem

  • You think carefully about pre-conditions and post-conditions for functions

  • You can clearly document your functions with docstrings

  • You can figure out what went wrong when something unexpected happens

  • Hopefully you had fun!

Points

TaskDescriptionPoints
Grass and skyYour solution works and has been decomposed into small functions1
Smile!Your solution works1
You have decomposed the problem into small functions1
Fix the ForestYour solution works2
You have decomposed the problem into small functions1
The Desert Shall Blossom as a RoseYour solution works2
You have decomposed the problem into small functions1
All problemsYour functions are documented1