BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 2d — Variables

Preparation

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

1 minute

Download the zip file for this lab, located above. This zip file has worlds that you will use for Bit. Extract the files and put them in your cs110 directory in a folder called lab2d.

Warm up

5 minutes

Bit starts in a world called color2:

Bit with one blue square

and uses this code in color.py:

from byubit import Bit


def go(bit, color):
    while bit.can_move_front():
        bit.move()
        bit.paint(color)


@Bit.worlds('color2')
def fill_a_color(bit):
    found_color = bit.get_color()
    go(bit, found_color)


if __name__ == '__main__':
    fill_a_color(Bit.new_bit)

(1) What does the final world look like?

(2) How does the color get from bit.get_color() to bit.paint(color)?

Discuss with the TA if there is anything you don’t understand about variables or how this code works.

Flowers

10 minutes

Bit needs to paint the buds on some flowers. Here is one starting world:

flower stems with a color in front

Bit finds a color (red or blue), remembers it, and then finds the next flower stem (green). Bit paints a flower bud on top of the stem:

flower stems with a color on top

There is a second starting world with different colors for the flowers:

flower stems with a color in front

  1. This is a decomposition problem that also uses variables. Work with a friend to draw out your solution using a flow chart. What is your stopping condition? What is the trigger to paint a flower?

  2. After you draw it out, discuss the steps with the TA.

  3. Once you have a good idea of how to solve it, write code with a friend, using functions and variables.

  4. After you write code, discuss your solution with the TA.

  5. The TA can show the solution code when everyone is done, which has good decomposition.

Lines

15 minutes

Bit starts in a world with some incomplete lines in each row:

incomplete lines in each row

Bit should fill in the line in each row:

all lines are complete in each row

One of the important things you need for this problem is that Bit needs to decide if it is on a colored square. Then if it is, it needs to know the color of that square. You can do this with this code:

if not bit.is_on_white():
    color = bit.get_color()
    # do something with that color
  1. This is a decomposition problem that also uses variables. Work with a friend to draw out your solution using a flow chart. What is the sequence of actions that happen for each row? When does bit know to paint a line?

  2. Hint: You want to fill the rows from bottom to top. Start by turning Bit left, fill a row, and then move and fill rows as long as the front is clear.

  3. After you draw it out, discuss the steps with the TA.

  4. Once you have a good idea of how to solve it, write code with a friend, using functions and variables.

  5. After you write code, discuss your solution with the TA. How did you use variables?

  6. The TA can show the solution code when everyone is done, which has good decomposition. The solution code does not use the event stream pattern, because it does not use if statements. How could you modify the code to trigger when bit should paint a line? (This is the difference between the lab and the homework).

You can find starter code in lines.py.

DaVinci

10 minutes

The file davinci.py starts with several handy functions for drawing things with Bit.

Implement draw to use these functions to draw whatever you want.

Rules:

  • Use the provided functions many times
  • Use the provided functions with different arguments
    • For example, go_paint(bit, 'red') and go_paint(bit, 'blue')
  • Have fun!

Tips

  • Bit can also paint yellow, orange, purple and black
    • Note that once Bit leaves a black square, that square is now blocked
  • Be sure to write your own functions to build more complex ideas from the provided building blocks
  • You are welcome to adjust the dimensions of the grid (as long as it stays large enough to draw something interesting)

And if you need inspiration, consider the lilies…

example davinci

Preview Homework

10 minutes

Discuss the next set of homework problems. Are there any difficulties you anticipate? Talk about decomposition strategies, but each student should write their own code.

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊