BYU logo Computer Science

To start this assignment, download this zip file.

Lab 2d — Variables

Preparation

5 minutes

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.front_clear():
        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.

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

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_empty():
    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. Remember the event stream pattern.

  2. Hint: you want to fill the rows from bottom to top, so start by turning Bit left and then 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? How many functions did you use to draw a box?

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

You can find starter code in lines.py.

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. Remember the event stream pattern.

  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.

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.