BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 2c โ€” Decomposition

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 lab2c.

Leap

5 minutes

Bit starts in this world:

an empty 6x3 world

and runs this code:

from byubit import Bit


def leap(bit):
    if bit.can_move_front():
        bit.move()
    if bit.can_move_front():
        bit.move()
    if bit.can_move_front():
        bit.move()


def paint_spots(bit):
    while bit.can_move_front():
        bit.paint('blue')
        leap(bit)
    bit.paint('blue')


def fill_green(bit):
    while bit.can_move_front():
        bit.paint('green')
        bit.move()
    bit.paint('green')


@Bit.empty_world(6, 3)
def go(bit):
    fill_green(bit)
    bit.turn_left()
    bit.turn_left()
    paint_spots(bit)


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

Answer these questions to make sure you understand the code.

  1. What does leap do?
  • How would the result change if the last two ifs were changed to elif?
  1. What does paint_spots do?
  2. What does fill_green do?
  3. What will the final world look like?

Copy and paste this code into a new file called leap.py, and run it to be sure you have the right answer.

Discuss with the TA if there is anything you donโ€™t understand about functions.

Surround

15 minutes

Bit starts in a world that has some rectangular blocks. Here is world #1

a large world with rectangles made of blacks squares in the middle, all one row beneath bit, who starts on the left in the second row from the top

and world #2:

the same type of world, with different sized rectangles

Bit needs to surround each of the blocks with green squares:

each black rectangle is surrounded by green squares, except for the one square in the corner. Bit ends on the right

The same as above, but the black rectangles are different sizes

  1. This is a decomposition problem! Work with a friend to draw out your solution. See the guide on decomposition for examples of how to do this.

  2. After you draw it out, discuss the overall algorithm, in plain English, with the TA.

  3. Then come up with function names that could be used to solve the problem.

  • Tip: Surrounding a block can be done with a single while loop, using the event stream pattern.
  • When should bit stop?
  • What should be the while condition?
  • What is the event that happens along the way?
  1. Once you have a good idea of how to solve it, write code, using functions.

You can find starter code in surround.py.

Blossoms

15 minutes

Bit starts in a world that has some empty pools. Here is world #1

bit starts on the left 6 rows up from the bottom. The world is made up of black squares, the highest squares being one row beneath bit. The sections of lower black squares represent pools.

and world #2:

the same kind of world as above, but with differently sized pools

Bit needs to fill each pool with water and then plant a flower on the right side:

the empty spaces filled with blue squares, and a flower with a green stem and a red flower on the right of each pool

the second world's pools filled, with a flower on the right edge

  1. Guess what, this is another decomposition problem! :-) Work with a friend to draw out your solution.

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

  3. Be careful about filling the pool. A neat way to do it involves moving bit from left to right across the top, from one cliff edge to the other. Along the way, paint down and up.

  4. Once you have a good idea of how to solve the problem, write code, using functions.

You can find starter code in blossoms.py.

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. ๐Ÿ˜Š